Basics of Python Programming Language
Basics of Python Programming Language
September 7, 2022
Pavan
[8]: print(10)
10
[9]: print(name)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [9], in <cell line: 1>()
----> 1 print(name)
[10]: print(pavan)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [10], in <cell line: 1>()
----> 1 print(pavan)
[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.
Pavan
<class 'str'>
Pavan
<class 'str'>
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
4
[4]: # Accessing list elements
lst = [10, 20, 30]
print(lst[1])
20
64
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.
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
{10, 30}
{'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
6
'prabhas': 'baahubali',
'sampu': 'kobbarimatta'}
print(d)
print(type(d))
kobbarimatta
aadi
[ ]: