Learning the Basics of Python: Variables

The most important thing we will learn about programming is its basics. Because It's the only thing that we will find similar in every language.

Variables

Variables are containers for storing data values. If you don't understand this let me give you an example.

x = 5 #Here x is a variable

Here you can see I have written x = 5. Here x is a variable which is storing a value of 5. In other words, we are assigning value 5 in the variable x. Now the value of x is 5. Python has no command for declaring a variable. A variable is created the moment you first assign a value to it.

So, Why this assignment of a value inside a variable is important?

Let's think of a program that we have to perform using Python. Suppose, we are performing the addition of two values 5 & 9 and we have to print it out.

print(5+9)

In this code, the addition of two values is done. Now Let's perform addition, subtraction, multiplication & division operation in the same program and print it out.

print(5+9) # addition
print(9-5) # subtraction
print(9*5) # multiplication
print(9/5) # division

Here you can see we have performed addition, subtraction, multiplication & division. But while performing these operations we are using 9 and 5 multiple times. Which is repeating and kind of not recommended for programming. Let's try the same thing with variables

x = 5
y = 9
print(x+y) # addition
print(y-x) # subtraction
print(x*y) # multiplication
print(y/x) # division

Here we have assigned the values 5 & 9 to the variables x & y. Then performed the operations. Here you can see we are not using 5 and the value 9 again & again. We just assigned it to the variable on the first go. Then we are just using the variable to do the operation. But we may ask we are using the variable multiple times, So what is the benefit of using variables?

Ok, Let's think of two big numbers like 54634563 and 56363222. Now do the operations on your own. You are going to appreciate the variables now and in the future your going to appreciate them more and find them more useful.

More on Variable

counter = 100          # Creates an integer variable
miles   = 1000.0       # Creates a floating point variable
name    = "Musa Ali"   # Creates a string variable

Here we have given some meaningful variable names. The counter is an integer variable, miles is a floating point variable and the name is a string variable. These are types of a variable. In Python, A variable is not only created the moment you first assign a value to it, but it also decides its type. If you assign an integer value its type will be an integer, if you assign a floating value its type will be float and if you assign a string value its type will be a string.

a,b,c = 1,2,"Zara Ali"

print (a)
print (b)
print (c)

We can declare multiple variables in one line. But remember the variables and the values has to be separated by ", ". Otherwise, you will see an error in your code. In the example, the type of a and b variables are integer and the type of variable c is string. We will know more about variables in future blogs.