top of page

Variables

 

Variables can be defined as named memory locations, which are used in computer programs to store values. As the name implies, the value stored in the memory location referenced by the variable name is changeable.

We need not to declare explicitly variable in Python. When we assign any value to the variable that variable is declared automatically.

The assignment is done using the equal (=) operator.

a = 5
b = 8.4
c = "Bhuvana"

a, b, c = 5, 8.4, "Bhuvana"

a = b = c = "Bhuvana"

Python Data Types

There are different types of python data types. Some built-in python data types are:

Python Data Type – Numeric

Python numeric data type is used to hold numeric values like

int – holds signed integers of non-limited length.
long- holds long integers(exists in Python 2.x, deprecated in Python 3.x).
float- holds floating precision numbers and it’s accurate upto 15 decimal places.
complex- holds complex numbers.

#create a variable with integer value.
a=100
print("The type of variable having value", a, " is ", type(a))

#create a variable with float value.
b=10.2345
print("The type of variable having value", b, " is ", type(b))

#create a variable with complex value.
c=100+3j
print("The type of variable having value", c, " is ", type(c))

('The type of variable having value', 100, ' is ', <type 'int'>)                                                                                                                
('The type of variable having value', 10.2345, ' is ', <type 'float'>)                                                                                                     
('The type of variable having value', (100+3j), ' is ', <type 'complex'>)

Python Data Type – String

String is a sequence of characters. Python supports unicode characters. Generally strings are represented by either single or double quotes.


a = "string in a double quote"
b= 'string in a single quote'
print(a)
print(b)

# using ',' to concate the two or several strings
print(a,"concated with",b)

#using '+' to concate the two or several strings
print(a+" concated with "+b)

string in a double quote                                                                                                                                                        
string in a single quote                                                                                                                                                        
('string in a double quote', 'concated with', 'string in a single quote')                                                                                             
string in a double quote concated with string in a single quote

   

Python Data Type – List

List is a versatile data type exclusive in Python. List in Python is it can simultaneously hold different type of data. Formally list a ordered sequence of some data written using square brackets([]) and commas(,).

#list of having only integers


a= [1,2,3,4,5,6]
print(a)

#list of having only strings
b=["hello","john","reese"]
print(b)

#list of having both integers and strings
c= ["hey","you",1,2,3,"go"]
print(c)

#index are 0 based. this will print a single character
print(c[1]) #this will print "you" in list c

[1, 2, 3, 4, 5, 6]                                                                                                                                                              
['hello', 'john', 'reese']                                                                                                                                                      
['hey', 'you', 1, 2, 3, 'go']                                                                                                                                                   
you   

Python Data Type – Tuple

Tuple is another data type which is a sequence of data similar to list. But it is immutable. That means data in a tuple is write protected. Data in a tuple is written using parenthesis and commas.

#tuple having only integer type of data.
a=(1,2,3,4)
print(a) #prints the whole tuple

#tuple having multiple type of data.
b=("hello", 1,2,3,"go")
print(b) #prints the whole tuple

#index of tuples are also 0 based.

print(b[4]) #this prints a single element in a tuple, in this case "go"

(1, 2, 3, 4)                                                                                                                                                                    
('hello', 1, 2, 3, 'go')                                                                                                                                                        
go  

Dictionary

Python Dictionary is an unordered sequence of data of key-value pair form. It is similar to the hash table type. Dictionaries are written within curly braces in the form key:value. It is very useful to retrieve data in an optimized way among large amount of data.

#a sample dictionary variable

a = {1:"first name",2:"last name", "age":33}

#print value having key=1
print(a[1])
#print value having key=2
print(a[2])
#print value having key="age"


print(a["age"])

first name                                                                                                                                                                      
last name                                                                                                                                                                       
33 

© 2015 Thirumurugan

bottom of page