Python
Python
Introduction
to Python
(CCC634)
Dr. Shankar Kumar Ghosh
Dept. of Computer Science
Engineering, SNIoE
• Midterm: 25%
• Assignment: 25%
• End term: 50%
• Numbers
• Strings
• Dictionary
• Tuple
• List
• Modules
• Decision statements
• System utilities: Allow access to the lowest level (i.e., has access
the same set of functions as operating systems).
• Internet Programming: Reading an Email/Socket programming etc.
• Database programming: SQL support.
101=25*4+1
101 // 4 = 25 [Floor division operator]
101 % 4 = 1 [Modulo operator]
101/25=4.04
# use of float
i=3.5
j=1.2
print(i%j) // (2*1.2+1.1) % works on floats!!
#use of complex
a=1+2j
b=3*(1+2j)
c=a*b
print(a)
print(b)
print(c) [-9+12j]
print(a.real)
print(a.imag) It is possible to obtain real and imaginary part!!
print(a.conjugate()) [1-2j]
#use of bool
x=True
print(a)
y=3>4 On evaluation of condition, it is replaced by True or False!!
print(x)
print(y)
print(abs(-25))
print(pow(2,4))
import math
x=1.5357
print(math.pi)
print(math.sqrt(x))
Output:
==== RESTART: D:/Sharkar Ghosh/Desktop/Semester2022JantoJun/Coding/math1.py ====
3.141592653589793
1.2392336341465238
Other functions: factorial, fabs, log, exp, trunc, floor, ceil [explore yourself!!]
import random
import datetime
random.seed(datetime.time())
print(random.random())
print(random.random())
print(random.randint(10,100))
Output:
<class 'set'>
<class 'list'>
<class 'dict'>
<class 'str'>
<class 'complex'>
Hoopla
He said,'Let Us Python'.
C:\temp\newfile
C:\\temp\\newfile
What is this life if full of care... We have no time to spend and stare
What is this life if full of care...We have no time to spend and stare
HooplaHooplaHoopla
UtopiaToday!!
p
A Gentle Intro. to Python: Dr. Shankar K. Ghosh
String: Indexing
01 2 3 4 28
• String[start : end]
• The returned string contains all of the characters starting
from start up until but not including end.
• If only start is specified, the slice continues until the end
of the string.
• The only end is specified, the slice starts from 0 up to but
not including the end.
D:\Sharkar Ghosh\Desktop\Semester2022JantoJun\Coding>python
string2.py
I
returned
s
bag of groceries
groceries
I returned a bag of
input = open('myfile.txt')
for line in input.readlines():
print(line)
input.close()
D:\Sharkar Ghosh\Desktop\Semester2022JantoJun\Coding>python
array1.py
10
20
30
40
50
for x in array1:
print(x)
D:\Sharkar Ghosh\Desktop\Semester2022JantoJun\Coding>python
array1.py
10
60
20
30
40
50
for x in array1:
print(x)
D:\Sharkar Ghosh\Desktop\Semester2022JantoJun\Coding>python
array1.py
10
20
30
50
for x in array1:
print(x)
Day 1: 11 12 5 2
Day 2: 15 6 10
Day 3: 10 8 12 5
Day 4: 12 15 8 6
Representation:
0 1 2
T = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]
0 1 2 3
Accession the éléments:
from array import *
T = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]
print(T[0]) // prints the first array
print(T[1][2]) // prints the 2nd element of the 1st array
D:\Sharkar Ghosh\Desktop\Semester2022JantoJun\Coding>python
2darr.py
11 12 5 7
15 6 10
11 9
12 15 8 6
Example:
D:\Sharkar Ghosh\Desktop\Semester2022JantoJun\Coding>python list2.py
[25, 26, 25, 27, 26, 28, 25, 33, 44, 55]
On assigning one list to another, both refer to the same list. Changing
one changes the other. This assignment is known as shadow
copy/aliasing.
lst1=[10,20,30,40,50]
lst2=lst1
print(lst1)
print(lst2)
lst1[0]=100
print(lst1[0], lst2[0]) # prints 100 100
A Gentle Intro. to Python: Dr. Shankar K. Ghosh
Searching methods for Lists
lst=[10,2,0,50,4]
lst.reverse()
print(lst)
lst.sort()
print(lst)
a=[1, 3,5,7,9]
b=[2,4,6,8,10]
c=[a, b]
print(c[0][0], c[1][2]) # prints 1 and 6
D:\Sharkar Ghosh\Desktop\Semester2022JantoJun\Coding>python
prac1.py
['Anil', 'Amol', 'Anuj', 'Aditya', 'Avi', 'Alka']
['Anil', 'Amol', 'Anuj', 'Aditya', 'Avi', 'Alka', 'Zulu']
['Anil', 'Amol', 'Anuj', 'Aditya', 'Alka', 'Zulu']
['AnilKumar', 'Amol', 'Anuj', 'Aditya', 'Alka', 'Zulu']
['Aditya', 'Alka', 'Amol', 'AnilKumar', 'Anuj', 'Zulu']
['Zulu', 'Anuj', 'AnilKumar', 'Amol', 'Alka', 'Aditya']
S=input(‘prompt’)
split() function will split the entered fullname with space as a delimeter. The
split values will then be assigned to fname, mname, Iname.
A Gentle Intro. to Python: Dr. Shankar K. Ghosh
input() function
D:\Sharkar Ghosh\Desktop\Semester2022JantoJun\Coding>python
circle.py
Enter radius, length and breadth: 2 3 4
12.56
14
• Nested tuple
a=(1, 3, 5, 7, 9)
b=(2, 4, 6, 8, 10)
c=(a, b)
print (c[0][0], c[1][2])
• Embedded tuple
x=(1, 2, 3, 4)
y=(10, 20, x, 30)
print(y)
Unpacking a tuple
x=(1, 2, 3, 4)
y=(10, 20, *x, 30)
print(y)