Variables in Python

Computers always work in binary numbers 1 and 0 but humans work with decimal and words. To create a communication between humans and computers we need some medium which can translate human’s instructions into computer’s and computer’s language into human’s understandable medium. This is the point where programming languages come into play. Programming languages allow human to write computer instructions in human readable form so it can be translated into machine understandable format.

One of the most important thing in any programming language including Python are variables (data types) where human can write some instructions to computer to handle different types of data including numbers, letters, words etc. Variables are in fact locations in memory for storing these data. Variables can be any type, it can be integer type which can hold age or score, it can be string type which can contain name or email address.

Declaring a variable in Python

In Java, C++ or other programming languages, variables are defined with different keywords; for example integer variables are defined with int, character variables with char, for handling strings, there is a variable type String and for float variable you have a type float.

Declaration of variable in Python is very simple and straight forward. You can declare a variable without any keyword.

## String type variable
name = "Khan"

## Float type variable
height = 6.7

## Integer type variable
age = 35

print("My name is",name,"I am ",age,"years old and my height is ",height)

Now you have three different types of variables: name as a string, height as a float and age as an integer. Python makes the programmer’s life very easy, type of the variable depends on the value which you will assign to the variable. If you write

name = "Khan"

It will become string type variable.

age = 10

it will become integer type of variable. You can declare boolean variable as well with

myBoolean = False
myBoolean = True

One more interesting thing, you can change the type of variable when ever you want. Python has no objection. Check this example, create a variable called myBoolean with value True, now you can check its type, then assign string value to the same variable, now if you check the type of this variable, it will be string type variable instead of boolean.

myBoolean = True

myBoolean = "Khan"

Multiple variable declaration  

Python provides tons of features, multiple variable declaration and assignment is one of them. 

# multiple assignment
a = b = c = 1

Three variables a, b, c are declared and value 1 is assigned to all of them. Train does not stop here, see following example

# multiple assignment of different types 
a , b , c = "python" , 100 , 101.11  

Three different types of variables are declared and values are also assigned to them. 

Variable type casting   

Type casting is also possible in Python, you can convert from string to integer or float or vice versa. 

## String to integer
string_x = "5"
integer_x = int(string_x)
print(integer_x + 4)

## String to float
string_x = "7.9"
float_x = float(string_x)
print (float_x)

## Float to integer 
integer_x = int(float_x)
print (integer_x)

Make sure that the source value is valid and can be converted into other type, otherwise you will receive run type error.

Remove variable  

You can remove variable any time by using del command. 

## multiple assignment of different types 
a , b , c = "python" , 100 , 101.11 
del a # remove variable a
print(a) # it will through error