Python has a rich collection of datatypes and associated operators. It uses Dynamic typing system where type checking is performed at run time instead of compile time. Here the values have types and not the variables(as in C,C++….).
Datatype of an value stored in variable (a) can be verified as — type(a)
Datatypes and operators are as follows :-
1) Numbers
a) int —–> Ex. a=28
b) float
c) complex ——> Ex. c=5+6j(Real part= c.real, Imaginary part= c.imag, Absolute value= abs(c))
2) Sequence
Used to hold a bunch of elements in a sequence.
a) String
Anything within quotes is a string.
Ex.str = ‘Decoding’
print str # Prints complete string
print str[0] # Prints first character of the string
print str[2:5] # Prints characters starting from 3rd to 6th
print str[2:] # Prints string starting from 3rd character
print str * 2 # Prints string two times
print str + “TEST” # Prints concatenated string
b) List
Items enclosed in [] and separated by , constitute a list
Ex.list = [ ‘abcd’, 786 , 2.23, ‘john’, 70.2 ]
print list # Prints complete list
print list[0] # Prints first element of the list
print list[1:3] # Prints elements starting from 2nd to 4th(Slicing)
print list[2:] # Prints elements starting from 3rd element
print list[0:4:1] # Prints alternate element from 1st to 4th.
print tinylist * 2 # Prints list two times
print list + tinylist # Prints concatenated lists
c) Tuples
Items enclosed in [] are tuples. Tuples are read-only lists.That makes a difference in both of them.
Ex. can be same as lists only [] should be replaced by ().
d) Dictionary
Dictionary consist of associative arrays with key-value pairs.
Ex.tinydict = {‘name’: ‘ankz’,’code’:2628, ‘company’: ‘decoding’}
print tinydict # Prints complete dictionary
print tinydict.keys() # Prints all the keys
print tinydict.values() # Prints all the values
Try playing on these datatypes. There is lot more to work on.
Happy Decoding…!