Basics of Python Programming Language
September 7, 2022
1 Basics of Python Programming Language
[7]: print("Pavan")
Pavan
[8]: print(10)
10
[9]: print(name)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [9], in <cell line: 1>()
----> 1 print(name)
NameError: name 'name' is not defined
[10]: print(pavan)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [10], in <cell line: 1>()
----> 1 print(pavan)
NameError: name 'pavan' is not defined
[11]: print('pavan')
pavan
[12]: print(12.2)
12.2
1
1.1 Data types in Python
• int
• float
• str
• bool
• list
• tuple
• set
• dict
1.1.1 int
• Used to represent integers
• integers - positive, negative, 0
[14]: a = 10
print(a)
print(type(a))
10
<class 'int'>
[15]: a = -10
print(a)
print(type(a))
-10
<class 'int'>
[16]: a = 0
print(a)
print(type(a))
0
<class 'int'>
1.1.2 float
• any number with point value
• +ve, -ve, 0
[17]: f = 10.2
print(f)
print(type(f))
10.2
<class 'float'>
[18]: f = -10.2
print(f)
2
print(type(f))
-10.2
<class 'float'>
[20]: f = 0.0
print(f)
print(type(f))
0.0
<class 'float'>
1.1.3 str
• everything put within single quotes or double quotes or triple
quotes will be treated as a string.
[22]: name = "Pavan"
print(name)
print(type(name))
Pavan
<class 'str'>
[23]: name = 'Pavan'
print(name)
print(type(name))
Pavan
<class 'str'>
[24]: name = '''Pavan'''
print(name)
print(type(name))
Pavan
<class 'str'>
[27]: a = 10
b = 20
print(a+b)
30
[28]: a = '10'
b = '20'
print(a+b)
1020
3
1.1.4 bool
• Boolean
• True, False
[29]: 10 > 20
[29]: False
[30]: 2 > 1
[30]: True
[31]: 7 <= 7
[31]: True
1.1.5 List
• List is an ordered collection of elements or items
• List items can be value that belongs python data type
• List elements are enclosed using square braces
• There are two types of lists in Python
• Homogeneous list, Heterogeneous list
• Homogeneous list -> A list that contains elements of same type
– Ex: [10, 20, 30], [10.2, 20.2, 30.2], [‘this’, ‘is’, ‘python’]
• Heterogeneous list -> A list that contains elements of different types
– Ex: [10, 2.2, ‘hello world’, True]
• List elements can be accessed using indexes
• List index starts with 0
[1]: lst = [10,20,30]
print(lst)
print(type(lst))
[10, 20, 30]
<class 'list'>
[2]: pavan = [10,20,30]
print(pavan)
print(type(pavan))
[10, 20, 30]
<class 'list'>
[3]: a = [10, 20, 30]
#index 0 1 2
print(a)
[10, 20, 30]
4
[4]: # Accessing list elements
lst = [10, 20, 30]
print(lst[1])
20
[5]: x = [12, 19, 71, -14, 6, 8, 13, -76]
#in 0 1 2 3 4 5 6 7
print(x[2] + x[3] - x[4] + x[6]) #64
# 71 - 14 - 6 + 13
64
[6]: my_list = [10, 12.2, 'hello', 67.2, '123', True, 795]
print(my_list[2])
print(type(my_list[2]))
print(my_list[4])
print(type(my_list[4]))
print(my_list[5])
print(type(my_list[5]))
hello
<class 'str'>
123
<class 'str'>
True
<class 'bool'>
1.1.6 tuple()
• Is also an ordered collection of elements
• Elements are enclosed within round braces (parentheses)
• tuple elements can also be accessed using indexes.
[7]: x = (10, 20, 30)
#in 0 1 2
print(x)
print(type(x))
(10, 20, 30)
<class 'tuple'>
[8]: x = (10, 20, 30)
#in 0 1 2
print(x[0])
10
1.1.7 set
• set is an unordered collection of unique elements
5
• set elements are enclosed using curly/flower braces
• set will not hold duplicates
• set elements cannot be accessed using indexes
[9]: l = [10, 20, 30, 40, 10]
print(l)
print(type(l))
[10, 20, 30, 40, 10]
<class 'list'>
[11]: s = {10, 20, 30, 40, 10, 10, 10, 10}
print(s)
print(type(s))
{40, 10, 20, 30}
<class 'set'>
[12]: fruits = {'apple', 'orange', 'apple', 'mango', 'kiwi', 'kiwi', 'mango'}
print(fruits)
{'apple', 'orange', 'kiwi', 'mango'}
[13]: s1 = {10, 20, 30}
s2 = {30, 40, 50, 10}
print(s1.intersection(s2))
{10, 30}
[14]: my_set = {'a', 'a', 'a', 'a', 'a', 'a', 'a'}
print(my_set)
{'a'}
1.1.8 dictionary
• dictionaries are used to store elements that are in the forms of pairs
• a dictionary element should contain a key and a value
• Ex:
• word: definition
• actor: no.of films
• actor: best picture
• batsmen: no. of runs
• bowler: no. of wickets
• author: best seller
• dictionary elements are enclosed using curly braces
• Using key we can get the value
[16]: d = {'chiru': 'khaidi',
'balayya': 'legend',
'jrntr': 'aadi',
6
'prabhas': 'baahubali',
'sampu': 'kobbarimatta'}
print(d)
print(type(d))
{'chiru': 'khaidi', 'balayya': 'legend', 'jrntr': 'aadi', 'prabhas':
'baahubali', 'sampu': 'kobbarimatta'}
<class 'dict'>
[19]: d = {'chiru': 'khaidi',
'balayya': 'legend',
'jrntr': 'aadi',
'prabhas': 'baahubali',
'sampu': 'kobbarimatta'}
print(d['sampu'])
print(d['jrntr'])
kobbarimatta
aadi
[26]: d = {'chiru': ['khaidi', 'aacharya', 154],
'balayya': ['legend', 'veerabhadra', 101],
'jrntr': ['aadi', 'rabhasa', 29],
'prabhas': ['baahubali', 'radhe shyam', 24],
'sampu': ['kobbarimatta', 'cauliflower', 5]}
print(d['jrntr'])
print(d['chiru'][2])
print(d['balayya'][1])
print(d['prabhas'][0])
['aadi', 'rabhasa', 29]
154
veerabhadra
baahubali
[ ]: