googleAd

Python for Beginners - Learn Python Without IDE - 2 (Variables)

 Python beginner tutorial encourages you to learn by doing. Follow along this Python course to get started. This is Part 2 of the python full course for beginners which just gets you started by showing how to install python on your machine and write your very first program. Subsequent parts will follow shortly.

I have chosen not to use any python specific IDE (Integrated Development Environment) in this course. IDEs tend to do lots of autocomplete which hinders learning. As you get familiar with python basics, programming concepts and get comfortable writing python programs without an IDE, you will find IDEs more productive later. As a python beginner, start from the scratch. I value all feedbacks. If you liked this method of teaching or have any constructive criticism, please share that in the comments. Please subscribe to be notified of the next part of the course.

TABLE OF CONTENTS 0:00:00 Sample Programs for Mathematical operations 0:01:53 Command Prompt Tip 0:02:14 Program Data and Useful Output 0:02:43 Handling Multi-step processing and Hard-coding Elimination 0:03:08 Need for a Placeholder 0:03:39 What is a Variable? 0:03:48 Sample Programs Using Variables 0:06:35 Math operations using Variables 0:07:44 Sting concatenation using Variables 0:08:47 One last math operation Variables - Division with Decimal




Transcript:
Let us create a program that can do some mathematical operations. For that I'm going back to my folder where I was creating my Python Programs. And I will create a new program in notepad. So, I'll open notepad and I'll give a name math.py. Python programs will have an extension of dot py. So let me open this and I'll make use of the same function which is print and as mentioned before if you are calling a function, it needs to be followed by a parenthesis. The last time, we gave a string as an argument. The string was "hello world". This time we are not going to be passing a string argument. Rather, we are going to pass a numeric argument. So let us try giving a number like 3. And let us see if the print function will be able to print a number. Let's save this and exit out of here. And let us execute this program. python math.py . It was able to print out number 3. So now let's open notepad again and what if instead of printing this number, I did some math operation like an addition. Let's try it. python ... and pass the program name which is... math.py and we got our result which is 7. Going back to our program, what if I did a subtraction; say 3 minus 4. Again execute the program, I got a -1. How about division? What if I gave 16/2? Let's execute the program. If you are not very familiar with the command prompt, doing a up arrow or down arrow will bring back your previous commands that you typed. So, I'm not retyping these things. I'm just using the up arrow to bring back my previous commands. Ok, I'm going to execute this program. so we got 8.0 which is correct. So, a program consumes data, does some processing on it and creates a more useful output. In our examples, the data for our programs were hardcoded directly in the programs. However, in real world applications, the data comes from outside of the program. The data could come from an user input, or it could be coming from internet, or it could come from any external devices. Or it could very well be a file on your system. The operations performed on the data is not a simple addition of number. Data typically undergoes complex set of operations to produce the desired output. The output of the data processed in the first step is passed on to the second step and the output from the 2nd step will be passed on to the 3rd step and so on. It so appears, that the program needs some place holder to store these intermediate results from one step so that it can pass them on to the next step. To rerun our program with different set of inputs, we need to avoid hard-codings in the program. Again, we need a place holder that can source the input data from the outside world and pass on to the operational steps. This place holder is called a Variable. Variable is just a name given to a piece of memory that can be used as a temporary place holder for your data. Let us create a new program where we can make use of a variable. So, let me name this program variable.py. How do I define a variable ? It is very simple in python. Unlike other languages, where you need to first declare a variable and then make use of it later, in python you can create a variable and directly assign a value. For example, I can create a variable like welcomeString. And I can assign it to a value like "Hello World". Or I can put like a = 2 , b = 3 . All these are variables. welcomeString is a variable. and you assigned a value "Hello World". Here a is again is a variable and we assigned a value 2. b we assigned a value 3. b is a variable. Similarly you can define c and you can give a decimal value like 8.4. c is a variable again. Now that we have assigned a value to a variable, let us print them out. print ... And how do I print the value in a variable? In the argument of the print function, you just give the variable name. welcomeString. And I want to print the values, the variables a, b and c hold as well. So, let me print them up by providing the variable names within the parenthesis of the print function. print(a)... print(b) ...print(c). Ok, let's save this and execute it. python variable.py. We can see that we were able to print the value those variables were holding. Let us do something more with the variables. So far we have just printed the values of the variables. Now let us try doing some mathematical operations with these variables. In the last example, we added numbers like 3 and 4 by directly using the values but here we are going to make use of the variables that hold these values. So instead of just adding the 2 numbers, here let us add the variables like b and c. How about we assign the sum of these 2 variables to another variable say d ? d = b + c. It is not just enough that we assign the sum of b and c to d, but also to print it out. So let us print(d). So, let's see if this works. As you can see, we added the variables that contain the value 3 and 8.4 and we got the result of d which is now 11.4 (that is the sum of 8.4 and 3) . Let's go back to our program. How about some string functions? So, we know we have a variable called, welcomeString that holds the value 'Hello World'. How about we add one more variable: name and let it have the value 'Shan' . And now we want to concatenate these 2 strings. 'Hello World' and 'Shan' . How do we do that? Let us define another string: newString and let us try using the same plus operator and see if the concatenation works. welcomeSting + name and let us print this newString. Hope you are able to follow this. Let us execute this program. It was able to concatenate "Hello World" and "Shan" together. Let us go back to our program. Let us define another variable e which can be a division of b and c. b divided by c. the value of b is 3. the value of c is 8.4 . so 3 by 8.4 . Let's see what the answer is. It is 0.35. So we got our value here.

No comments:

Post a Comment

Python for Beginners | Chapter 8 | Conditions Explained

In this chapter we will learn how condition statements work in python.  If you are new in here, you may want to start from the beginning...