Python Numbers
Integers, floating point numbers and complex numbers fall under Python numbers category.
They are defined as int, float and complex classes in Python.
We can use the type() function to know which class a variable or a value belongs to.
Similarly, the isinstance() function is used to check if an object belongs to a particular
class.
In [1]: a = 5
print(a, "is of type", type(a))
print(id(a))
a = 2.0
print(a, "is of type", type(a))
print(id(a))
a = 1+2j
print(a, "is complex number?", isinstance(1+2j,complex))
print(id(a))
# Integers can be of any length, it is only limited by the memory available.
# A floating-point number is accurate up to 15 decimal places.
# Integer and floating points are separated by decimal points.
# 1 is an integer, 1.0 is a floating-point number.
# Complex numbers are written in the form, x + yj, where x is the real part and y is the i
maginary part.
In [2]: a=12345698745612301456
b=0.1456987456213025469
c="1-2j"
d=complex(c)
In [3]: a , b, d
5 is of type <class 'int'>
140725548917264
2.0 is of type <class 'float'>
2727302580656
(1+2j) is complex number? True
2725156847856
Out[3]: (12345698745612301456, 0.14569874562130256, (1-2j))
Python Collections (Arrays)
-There are four collection data types in the Python programming language:
-List is a collection which is ordered and changeable. Allows duplicate members.
-Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
-Set is a collection which is unordered and unindexed. No duplicate members.
-Dictionary is a collection which is unordered and changeable. No duplicate members.
Python List
* Lists are used to store multiple items in a single variable.
* List is an ordered sequence of items.
* It is one of the most used datatype in Python and is very flexible.
* All the items in a list do not need to be of the same type.
* Declaring a list is pretty straight forward. Items separated by commas are enclosed with
in brackets [ ].
* We can use the slicing operator [ ] to extract an item or a range of items from a list.
* The index starts from 0 in Python.
* Negative indexing
* Python allows negative indexing for its sequences.
* The index of -1 refers to the last item, -2 to the second last item and so on.
In [4]: a = [5,10,15,20,25,30,35,40]
print("a[2] = ", a[2])
print("a[0:3] = ", a[0:3])
print("a[5:] = ", a[5:])
In [6]: a[::-1]
In [7]: a=[1,2,3,5,6,9,8,7,4,5,6,1,2,3]
In [8]: a.count(1)
a[2] = 15
a[0:3] = [5, 10, 15]
a[5:] = [30, 35, 40]
Out[6]: [40, 35, 30, 25, 20, 15, 10, 5]
Out[8]: 2
In [9]: a.sort()
In [10]: a
In [11]: a.sort(reverse= True)
In [12]: a
In [13]: a=['a','b','c',1,2,3,[1,2],(1,2),"raj",1.25,2.56,1+2j]
In [18]: a=['a','b','c','kone','raj',"apple"]
In [19]: a.sort()
In [20]: a
In [23]: a=[1.2,3.4,0.12,0.123]
In [24]: a.sort()
In [25]: a
In [26]: print(dir(list))
In [65]: a=['a','b','c',1,2,3,[1,2],(1,2),"raj",1.25,2.56,1+2j]
In [66]: a.remove(1.25)
In [67]: a
In [68]: a.index([1,2])
In [69]: a.insert(1,'kone')
In [70]: a
Out[10]: [1, 1, 2, 2, 3, 3, 4, 5, 5, 6, 6, 7, 8, 9]
Out[12]: [9, 8, 7, 6, 6, 5, 5, 4, 3, 3, 2, 2, 1, 1]
Out[20]: ['a', 'apple', 'b', 'c', 'kone', 'raj']
Out[25]: [0.12, 0.123, 1.2, 3.4]
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '_
_doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt_
_', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__',
'__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__redu
ce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__s
izeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'exten
d', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
Out[67]: ['a', 'b', 'c', 1, 2, 3, [1, 2], (1, 2), 'raj', 2.56, (1+2j)]
Out[68]: 6
Out[70]: ['a', 'kone', 'b', 'c', 1, 2, 3, [1, 2], (1, 2), 'raj', 2.56, (1+2j)]
In [71]: a.pop()
In [72]: a
In [73]: a.append("hi")
In [74]: a
In [75]: b=a.copy()
In [76]: b
In [77]: a.extend("123")
In [78]: print(a)
In [79]: a.extend([1,2,3])
In [80]: print(a)
In [81]: a.append([4,5,6])
In [82]: print(a)
In [83]: b=iter(a)
In [84]: print(next(b))
In [86]: print(b.__next__())
In [91]: a=[1,2,3,4,5,6,7,'a']
b=['b','c','d']
c=[1.0,2.1,3.2]
In [92]: list1=a+b
list1
Out[71]: (1+2j)
Out[72]: ['a', 'kone', 'b', 'c', 1, 2, 3, [1, 2], (1, 2), 'raj', 2.56]
Out[74]: ['a', 'kone', 'b', 'c', 1, 2, 3, [1, 2], (1, 2), 'raj', 2.56, 'hi']
Out[76]: ['a', 'kone', 'b', 'c', 1, 2, 3, [1, 2], (1, 2), 'raj', 2.56, 'hi']
['a', 'kone', 'b', 'c', 1, 2, 3, [1, 2], (1, 2), 'raj', 2.56, 'hi', '1', '2', '3']
['a', 'kone', 'b', 'c', 1, 2, 3, [1, 2], (1, 2), 'raj', 2.56, 'hi', '1', '2', '3',
1, 2, 3]
['a', 'kone', 'b', 'c', 1, 2, 3, [1, 2], (1, 2), 'raj', 2.56, 'hi', '1', '2', '3',
1, 2, 3, [4, 5, 6]]
a
kone
Out[92]: [1, 2, 3, 4, 5, 6, 7, 'a', 'b', 'c', 'd']
In [93]: list1.extend(c)
list1
In [94]: print(list1)
In [95]: list1[0]="raj"
list1
In [96]: list1.insert(0,"kone")
list1
In [101]: list2=[x for x in list1]
list2
In [103]: # Separate only numerics from list
list3=[]
for x in list2:
if not str(x).isalpha():
list3.append(x)
else:
continue
print(list3)
In [104]: string="rajesh"
print(list(string))
Python Tuple
Tuple is an ordered sequence of items same as a list.
The only difference is that tuples are immutable. Tuples once created cannot be modified.
Tuples are used to write-protect data and are usually faster than lists as they cannot cha
nge dynamically.
It is defined within parentheses () where items are separated by commas.
In [53]: print(dir(tuple))
Out[93]: [1, 2, 3, 4, 5, 6, 7, 'a', 'b', 'c', 'd', 1.0, 2.1, 3.2]
[1, 2, 3, 4, 5, 6, 7, 'a', 'b', 'c', 'd', 1.0, 2.1, 3.2]
Out[95]: ['raj', 2, 3, 4, 5, 6, 7, 'a', 'b', 'c', 'd', 1.0, 2.1, 3.2]
Out[96]: ['kone', 'raj', 2, 3, 4, 5, 6, 7, 'a', 'b', 'c', 'd', 1.0, 2.1, 3.2]
Out[101]: ['kone', 'raj', 2, 3, 4, 5, 6, 7, 'a', 'b', 'c', 'd', 1.0, 2.1, 3.2]
[2, 3, 4, 5, 6, 7, 1.0, 2.1, 3.2]
['r', 'a', 'j', 'e', 's', 'h']
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq_
_', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__
gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__',
'__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'in
dex']
In [54]: print(tuple.__doc__)
In [55]: a=(1,2,3)
In [56]: a[1]=3
In [61]: print(dir(list))
In [62]: a
In [ ]:
Built-in immutable sequence.
If no argument is given, the constructor returns an empty tuple.
If iterable is specified the tuple is initialized from iterable's items.
If the argument is a tuple, the return value is the same object.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-56-d46f8bf7ffd1> in <module>
----> 1 a[1]=3
TypeError: 'tuple' object does not support item assignment
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '_
_doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt_
_', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__',
'__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__redu
ce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__s
izeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'exten
d', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
Out[62]: (1, 2, 3)

Data types

  • 1.
    Python Numbers Integers, floatingpoint numbers and complex numbers fall under Python numbers category. They are defined as int, float and complex classes in Python. We can use the type() function to know which class a variable or a value belongs to. Similarly, the isinstance() function is used to check if an object belongs to a particular class. In [1]: a = 5 print(a, "is of type", type(a)) print(id(a)) a = 2.0 print(a, "is of type", type(a)) print(id(a)) a = 1+2j print(a, "is complex number?", isinstance(1+2j,complex)) print(id(a)) # Integers can be of any length, it is only limited by the memory available. # A floating-point number is accurate up to 15 decimal places. # Integer and floating points are separated by decimal points. # 1 is an integer, 1.0 is a floating-point number. # Complex numbers are written in the form, x + yj, where x is the real part and y is the i maginary part. In [2]: a=12345698745612301456 b=0.1456987456213025469 c="1-2j" d=complex(c) In [3]: a , b, d 5 is of type <class 'int'> 140725548917264 2.0 is of type <class 'float'> 2727302580656 (1+2j) is complex number? True 2725156847856 Out[3]: (12345698745612301456, 0.14569874562130256, (1-2j))
  • 2.
    Python Collections (Arrays) -Thereare four collection data types in the Python programming language: -List is a collection which is ordered and changeable. Allows duplicate members. -Tuple is a collection which is ordered and unchangeable. Allows duplicate members. -Set is a collection which is unordered and unindexed. No duplicate members. -Dictionary is a collection which is unordered and changeable. No duplicate members. Python List * Lists are used to store multiple items in a single variable. * List is an ordered sequence of items. * It is one of the most used datatype in Python and is very flexible. * All the items in a list do not need to be of the same type. * Declaring a list is pretty straight forward. Items separated by commas are enclosed with in brackets [ ]. * We can use the slicing operator [ ] to extract an item or a range of items from a list. * The index starts from 0 in Python. * Negative indexing * Python allows negative indexing for its sequences. * The index of -1 refers to the last item, -2 to the second last item and so on. In [4]: a = [5,10,15,20,25,30,35,40] print("a[2] = ", a[2]) print("a[0:3] = ", a[0:3]) print("a[5:] = ", a[5:]) In [6]: a[::-1] In [7]: a=[1,2,3,5,6,9,8,7,4,5,6,1,2,3] In [8]: a.count(1) a[2] = 15 a[0:3] = [5, 10, 15] a[5:] = [30, 35, 40] Out[6]: [40, 35, 30, 25, 20, 15, 10, 5] Out[8]: 2
  • 3.
    In [9]: a.sort() In[10]: a In [11]: a.sort(reverse= True) In [12]: a In [13]: a=['a','b','c',1,2,3,[1,2],(1,2),"raj",1.25,2.56,1+2j] In [18]: a=['a','b','c','kone','raj',"apple"] In [19]: a.sort() In [20]: a In [23]: a=[1.2,3.4,0.12,0.123] In [24]: a.sort() In [25]: a In [26]: print(dir(list)) In [65]: a=['a','b','c',1,2,3,[1,2],(1,2),"raj",1.25,2.56,1+2j] In [66]: a.remove(1.25) In [67]: a In [68]: a.index([1,2]) In [69]: a.insert(1,'kone') In [70]: a Out[10]: [1, 1, 2, 2, 3, 3, 4, 5, 5, 6, 6, 7, 8, 9] Out[12]: [9, 8, 7, 6, 6, 5, 5, 4, 3, 3, 2, 2, 1, 1] Out[20]: ['a', 'apple', 'b', 'c', 'kone', 'raj'] Out[25]: [0.12, 0.123, 1.2, 3.4] ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '_ _doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt_ _', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__redu ce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__s izeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'exten d', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] Out[67]: ['a', 'b', 'c', 1, 2, 3, [1, 2], (1, 2), 'raj', 2.56, (1+2j)] Out[68]: 6 Out[70]: ['a', 'kone', 'b', 'c', 1, 2, 3, [1, 2], (1, 2), 'raj', 2.56, (1+2j)]
  • 4.
    In [71]: a.pop() In[72]: a In [73]: a.append("hi") In [74]: a In [75]: b=a.copy() In [76]: b In [77]: a.extend("123") In [78]: print(a) In [79]: a.extend([1,2,3]) In [80]: print(a) In [81]: a.append([4,5,6]) In [82]: print(a) In [83]: b=iter(a) In [84]: print(next(b)) In [86]: print(b.__next__()) In [91]: a=[1,2,3,4,5,6,7,'a'] b=['b','c','d'] c=[1.0,2.1,3.2] In [92]: list1=a+b list1 Out[71]: (1+2j) Out[72]: ['a', 'kone', 'b', 'c', 1, 2, 3, [1, 2], (1, 2), 'raj', 2.56] Out[74]: ['a', 'kone', 'b', 'c', 1, 2, 3, [1, 2], (1, 2), 'raj', 2.56, 'hi'] Out[76]: ['a', 'kone', 'b', 'c', 1, 2, 3, [1, 2], (1, 2), 'raj', 2.56, 'hi'] ['a', 'kone', 'b', 'c', 1, 2, 3, [1, 2], (1, 2), 'raj', 2.56, 'hi', '1', '2', '3'] ['a', 'kone', 'b', 'c', 1, 2, 3, [1, 2], (1, 2), 'raj', 2.56, 'hi', '1', '2', '3', 1, 2, 3] ['a', 'kone', 'b', 'c', 1, 2, 3, [1, 2], (1, 2), 'raj', 2.56, 'hi', '1', '2', '3', 1, 2, 3, [4, 5, 6]] a kone Out[92]: [1, 2, 3, 4, 5, 6, 7, 'a', 'b', 'c', 'd']
  • 5.
    In [93]: list1.extend(c) list1 In[94]: print(list1) In [95]: list1[0]="raj" list1 In [96]: list1.insert(0,"kone") list1 In [101]: list2=[x for x in list1] list2 In [103]: # Separate only numerics from list list3=[] for x in list2: if not str(x).isalpha(): list3.append(x) else: continue print(list3) In [104]: string="rajesh" print(list(string)) Python Tuple Tuple is an ordered sequence of items same as a list. The only difference is that tuples are immutable. Tuples once created cannot be modified. Tuples are used to write-protect data and are usually faster than lists as they cannot cha nge dynamically. It is defined within parentheses () where items are separated by commas. In [53]: print(dir(tuple)) Out[93]: [1, 2, 3, 4, 5, 6, 7, 'a', 'b', 'c', 'd', 1.0, 2.1, 3.2] [1, 2, 3, 4, 5, 6, 7, 'a', 'b', 'c', 'd', 1.0, 2.1, 3.2] Out[95]: ['raj', 2, 3, 4, 5, 6, 7, 'a', 'b', 'c', 'd', 1.0, 2.1, 3.2] Out[96]: ['kone', 'raj', 2, 3, 4, 5, 6, 7, 'a', 'b', 'c', 'd', 1.0, 2.1, 3.2] Out[101]: ['kone', 'raj', 2, 3, 4, 5, 6, 7, 'a', 'b', 'c', 'd', 1.0, 2.1, 3.2] [2, 3, 4, 5, 6, 7, 1.0, 2.1, 3.2] ['r', 'a', 'j', 'e', 's', 'h'] ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq_ _', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__ gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'in dex']
  • 6.
    In [54]: print(tuple.__doc__) In[55]: a=(1,2,3) In [56]: a[1]=3 In [61]: print(dir(list)) In [62]: a In [ ]: Built-in immutable sequence. If no argument is given, the constructor returns an empty tuple. If iterable is specified the tuple is initialized from iterable's items. If the argument is a tuple, the return value is the same object. --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-56-d46f8bf7ffd1> in <module> ----> 1 a[1]=3 TypeError: 'tuple' object does not support item assignment ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '_ _doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt_ _', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__redu ce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__s izeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'exten d', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] Out[62]: (1, 2, 3)