There are majorly 3 types of variables in Python:
String: Containing alpha numeric and character values.
In this tutorial we shall try to understand all of them in detail:
In Python you can assign values to a variable using an equal to ' = ' sign.
Numbers:
In the following code are assigning a value 150000 to annual_salary variable, while no_of_month is assigned the value equal to 12.
annual_salary = 150000;
no_of_month = 12;
You can print the variables to see the values stored in them using print( ) function:
print(annual_salary);
print(no_of_month)
![](https://static.wixstatic.com/media/99d03c_4d1a7041b5f74afaa9ae4144086be435~mv2.jpg/v1/fill/w_335,h_116,al_c,q_80,enc_auto/99d03c_4d1a7041b5f74afaa9ae4144086be435~mv2.jpg)
You can also use these variables to do mathematical computations.
annual_salary/no_of_month
![](https://static.wixstatic.com/media/99d03c_a080dc68a93541408dacd5aa44efffae~mv2.jpg/v1/fill/w_425,h_85,al_c,q_80,enc_auto/99d03c_a080dc68a93541408dacd5aa44efffae~mv2.jpg)
It is also feasible to save the values of these computations in a variable (So that we can use the computations later on easily).
For instance, we are saving the above calculation in a new variable called monthly_salary.
monthly_salary = annual_salary/no_of_month
print(monthly_salary)
![](https://static.wixstatic.com/media/99d03c_14497ecfce5a48d88faa895a1dcd5bd7~mv2.jpg/v1/fill/w_529,h_102,al_c,q_80,enc_auto/99d03c_14497ecfce5a48d88faa895a1dcd5bd7~mv2.jpg)
type( ) function
We can check the variable type using type( ) function.
We can see that no_of_month is an integer while monthly_salary is a float.
type(no_of_month)
type(monthly_salary)
![](https://static.wixstatic.com/media/99d03c_f41cf32a7d924ca0bc7fa28f8d24229e~mv2.jpg/v1/fill/w_344,h_179,al_c,q_80,enc_auto/99d03c_f41cf32a7d924ca0bc7fa28f8d24229e~mv2.jpg)
Multiple assignment:
We can assign multiple values to variables in a single statement.
For eg. Below code assigns the value 2 to 'a' and 3 to 'b'.
a,b = 2,3
![](https://static.wixstatic.com/media/99d03c_b4592f7d66e0421fb41a41b890138970~mv2.jpg/v1/fill/w_277,h_266,al_c,q_80,enc_auto/99d03c_b4592f7d66e0421fb41a41b890138970~mv2.jpg)
Strings:
String variables can comprise of letters, numbers and special characters eclosed in quotes.
Here we are creating our string:
my_string = "This is my first Python string variable";
print(my_string)
![](https://static.wixstatic.com/media/99d03c_a98c84ef007b42caace8d41b5d135a92~mv2.jpg/v1/fill/w_642,h_133,al_c,q_80,enc_auto/99d03c_a98c84ef007b42caace8d41b5d135a92~mv2.jpg)
type(my_string)
![](https://static.wixstatic.com/media/99d03c_811b4715aac2471cad29ee7d2f9c3b24~mv2.jpg/v1/fill/w_271,h_78,al_c,q_80,enc_auto/99d03c_811b4715aac2471cad29ee7d2f9c3b24~mv2.jpg)
Python indexing starts from 0 thus following code will return the first character in the string:
print(my_string[0])
![](https://static.wixstatic.com/media/99d03c_179fb7b9b1bb416ba7bf038ef6df630a~mv2.jpg/v1/fill/w_350,h_90,al_c,q_80,enc_auto/99d03c_179fb7b9b1bb416ba7bf038ef6df630a~mv2.jpg)
Writing [0:3] in front of the string name would provide first 3 characters (i.e. index 0 till index 2, index 3 is excluded)
print(my_string[0:3])
![](https://static.wixstatic.com/media/99d03c_9c59b6ab1f4f4fd7bb6c4bad306f857b~mv2.jpg/v1/fill/w_324,h_81,al_c,q_80,enc_auto/99d03c_9c59b6ab1f4f4fd7bb6c4bad306f857b~mv2.jpg)
Following code prints the entire string starting from 3rd index (i.e. 4th character)
print(my_string[3:])
![](https://static.wixstatic.com/media/99d03c_80a1bb4e014a418b81b852e7f6953418~mv2.jpg/v1/fill/w_479,h_92,al_c,q_80,enc_auto/99d03c_80a1bb4e014a418b81b852e7f6953418~mv2.jpg)
Writing -1 as the index tells Python to retrieve the last character from our string.
print(my_string[-1])
![](https://static.wixstatic.com/media/99d03c_09b8782d566b434ab1524af19fac2bf1~mv2.jpg/v1/fill/w_363,h_87,al_c,q_80,enc_auto/99d03c_09b8782d566b434ab1524af19fac2bf1~mv2.jpg)
Multiplication with strings:
If we write my_string * 2 then the string will be repeated twice!
print(my_string * 2)
![](https://static.wixstatic.com/media/99d03c_8382de37c3684f98805e4d428b3c2195~mv2.jpg/v1/fill/w_882,h_92,al_c,q_80,enc_auto/99d03c_8382de37c3684f98805e4d428b3c2195~mv2.jpg)
Adding strings:
We can concatenate the strings using a '+' symbol:
print(my_string + "abcd")
![](https://static.wixstatic.com/media/99d03c_46dca982ad5c47d2b8e5d2aed3e66445~mv2.jpg/v1/fill/w_537,h_85,al_c,q_80,enc_auto/99d03c_46dca982ad5c47d2b8e5d2aed3e66445~mv2.jpg)
Boolean:
Here we have calculated a Boolean variable x which takes the value True.
x = True;
type(x)
![](https://static.wixstatic.com/media/99d03c_18b361e95e78492db45813f29b8ac6e3~mv2.jpg/v1/fill/w_346,h_120,al_c,q_80,enc_auto/99d03c_18b361e95e78492db45813f29b8ac6e3~mv2.jpg)
Note that in Python while mentioning True and False : T or F is in upper case and rest of the letters in lower case!
Python won't treat TRUE as a Boolean True or FALSE as Boolean False.
I'm new python I could easily grasp this code. Thanks for the article Ekta.