Professional Documents
Culture Documents
word="ATOMIC" if s[i].islower():
print(word[0:6]) l+=1
print(word[2:5]) print('No of Upper Case characters',u)
print(word[2:4]) print('No of Lower Case characters',l)
print(word[:6]) EXERCISE 4 LIST Operations
print(word[2:]) a=[10,20,30,40,50]
print(word[0:6:2]) print(a,a[3],a[-4])
print(word[::2]) a=['A','T','O','M','I','C']
print(word[-6:-1]) print(a,a[3],a[-4])
print(word[-5:-2]) a=[10,20,[30,40],50]
print(word[-6:-1:3]) print(a,a[2],a[-4])
print(word[:-3]) b=[60]
print(word[::-2]) print(a>b,a==b,a+b)
print(ord('A')) a=[10,20,30,40,50,60]
print(chr(65)) print(a,a[1:4],a[1:-2])
print(len(word)) print(a[::-2])
EXERCISE 2 String Functions a[0:2]=['ten','twenty']
a='Atomic' print(a)
b='Energy' a[1:]='987'
print(a+b) print(a)
print(a*3) EXERCISE 5 LIST Functions
print('w' in a) a=list('123456')
print('w' not in a) print(a)
word='ATOMIC ENERGY CENTRAL SCHOOL' b=list('Atomic')
s='CENTRAL' print(b)
print(word.find(s)) a=list('5+8')
print(word.find(s,15,22)) print(a)
print(word.find(s,0,22)) b=eval('5+8')
print(s.isalnum()) print(b)
print(s.isalpha()) a=(10,20,30,40,50)
print(s.isupper()) print(a)
print(s.isdigit()) b=list(a)
print(" ".isspace()) print(b)
print(s.lower()) c=b
print(s.rstrip('AL')) print(c)
print(s.lstrip('CE')) print(min(c),max(c),c.index(30))
print(s.lstrip()) c.append(60)
c='-' print(c)
print(c.join(s)) c.insert(1,15)
print(s.swapcase()) print(c)
print(word.partition('ENERGY')) d=[70,80,90]
print(word.partition(' ')) c.extend(d)
EXERCISE 3 Count No of Upper and Lowercase print(c)
characters in a string y=c.pop()
s=input('Enter String :') print("popped item",y)
u=l=0 print(c)
for i in range (0,len(s)): d.clear()
if s[i].isupper(): print(d)
S.K,Mukherjee 1
print(c.count(20)) print(d)
c.remove(40) print("Length :",len(d))
print(c) for i in d:
c.sort() print(i,"Book costs Rs.",d[i])
print(c) l=list(d.keys())
c.reverse() print(l)
print(c) t=tuple(d.values())
s=sum(c) print(t)
print(s) d['Java']=550
del(c) print(d)
text="ATOMIC ENERGY CENTRAL SCHOOL" d['HTML']=250
print(text.split()) print(d)
EXERCISE 6 TUPLE Functions del d['Oracle']
a=tuple('123456') print(d)
print(a) d.pop('C++')
b=('A','t','o','m','i','c') print(d)
print(b,b[0]) print(d.pop('Spider','Book Not found'))
a=tuple('5+8') print(json.dumps(d,indent=2))
print(a) print(d.get('python'))
b=eval('5+8') seq=d.items()
print(b) for k,v in seq:
a,b=(10,20,30,40,50),(10,20,30,40,50) print(k,v)
print(a<b) d1={"FORTRAN":300,"COBOL":275}
a,b=(10,20),(10.0,20.0) d.update(d1)
print(a==b) print(d)
print(a+b,a*2) EXERCISE 9 Dict Student information
a=(10,20,30,40,50,60,70,80,90,100) d={}
print(a) n=int(input('Enter no of students:'))
print(a[1:4],a[2:-6],a[1:7:2]) for i in range(n):
b=a[::-2] rno=int(input('Enter Rollno:'))
print(b) name=input('Enter Name :')
a=a+(110,120) mark=int(input('Enter Mark :'))
print(a) l=[name,mark]
print(len(a),max(a),min(a),a.index(50)) d[rno]=l
EXERCISE 7 : FIBONACCI SERIES USING print(d)
FUNCTION x=int(input('Enter Student Rollno :'))
def fibo(n): print(d[x])
a,b=-1,1 EXERCISE 10 Built in functions
for i in range(n): import math
c=a+b print(abs(-7))
print(c,end=” “) print(all(['test','','a']))
a=b print(any(['test','','a']))
b=c print(bin(7) , hex(15),oct(8))
n=int(input('Enter no of terms of the Fibonacci print(int('7'))
Series')) print(bool(0.5))
fibo(n) print(chr(65) , ord('A'))
EXERCISE 8 DICTIONARY Functions print(complex(3))
import json print(divmod(7,3))
d={"python":500,"C++":400,"Java":450,"Oracle":800} x=7
S.K,Mukherjee 2
print(eval('x+7')) fact_wor(a))
exec('a=2;b=3;print(a+b)') EX 14 Calculator using MODULE
print("a={a} and b={b}".format(a=3,b=4)) example.py
print(len({1,2,2,3})) def add(a, b):
print(max(2,3,4),min(2,3,4)) result = a + b
print(round(3.777,2),round(377.77,- return result
1),math.ceil(100.1),math.floor(- def sub(a, b):
45.8),math.log10(100)) result = a - b
print(set([2,2,3,1])) return result
print('Python'[slice(1,5,2)]) def mul(a, b):
print(sorted('python')) result = a * b
print(sum([3,4,5],2)) return result
EXERCISE 11 math , random modules def div(a, b):
import math result = a / b
import random return result
print(math.comb(5, 4))
print(math.factorial(6)) a.py
print(math.fmod(5,3)) import example
print(math.gcd(10,20)) print('1. Add')
print(math.trunc(12.9)) print('2. Subtract')
print(math.dist((1,2), (2,5))) print('3. Multiply')
print(math.hypot(4,5)) print('4. Divide')
print(math.degrees(3.14),math.radians(180)) a=int(input('Enter a :'))
print(math.acosh(90)) b=int(input('Enter b :'))
print(random.random(),random.random()*10) ch=int(input('Enter Choice:'))
print(random.randint(2,6)) if ch==1:
print(random.randrange(1,7,2)) print(example.add(a,b))
EXERCISE 12 local and global variables if ch==2:
def func(b): print(example.sub(a,b))
global a if ch==3:
a=a+5 print(example.mul(a,b))
print(a,b) if ch==4:
a=10 print(example.div(a,b))
func(20) EX 15 linear search
print (a) def lsearch(l,x):
EX 13 Factorial fn with/without return for i in range(len(l)):
def fact_wr(n): if l[i]==x:
f=1 print('Element found
for i in range(2,n+1): at',i+1)
f*=i break
return f else:
def fact_wor(n): print('Not found')
f=1 l=[12,45,65,-89,-62,52,79]
for i in range(2,n+1): print(l)
f*=i x=int(input('Enter the element to be searched:'))
print(f) lsearch(l,x)
EX 16 BINARY search
a=int(input('Enter a no')) def binary_search(arr, x):
print(fact_wr(a)) low = 0
S.K,Mukherjee 3
high = len(arr) - 1 import random
mid = 0 n=int(input('Enter no of elements:'))
list1=[]
while low <= high: list1=random.sample(range(1,100),n)
print('Original List',list1)
mid = (high + low) // 2 print('Sorted List:',isort(list1))
if arr[mid] < x: Ex19: area using module
low = mid + 1
elif arr[mid] > x: area.py
high = mid - 1 def circle(r):
else: return 3.14*r*r
return mid def rectangle(l,b):
return -1 return l*b
S.K,Mukherjee 5
print(c)
S.K,Mukherjee 9
cur.execute('use sample') cur.execute('select * from student')
cur.execute('show databases') for i in cur:
for i in cur: print(i)
print(i)
cur.execute('delete from student where rollno=3')
db.close() cur.execute('select * from student')
Ex 41 to 47 : for i in cur:
import mysql.connector print(i)
db=mysql.connector.connect(host='localhost',user='
root',password='mysql') cur.execute('update student set age=17 ')
cur=db.cursor() cur.execute('update student set age=18 where
gender="M"')
cur.execute('drop database if exists sample') cur.execute('select * from student')
cur.execute('create database sample') for i in cur:
cur.execute('use sample') print(i)
cur.execute('drop table if exists student')
cur.execute('''create table student (rollno int
primary key, cur.execute('commit')
sname varchar(25), address varchar(50), deptcode db.close()
varchar(3), Ex :48
gender char(1), mobile varchar(10))''') import mysql.connector
cur.execute('desc student') db=mysql.connector.connect(host='localhost',user='
for i in cur: root',password='mysql')
print(i) cur=db.cursor()
cur.execute("insert into student cur.execute('use sample')
values(1,'SKM','UCIL cur.execute('select * from student')
COLONY','CSC','M','9869071856')") for i in cur:
cur.execute("insert into student print(i)
values(2,'GARIMA','KOLKATA','BIO','M','88690716 try :
5819')") name=input('Enter student name:')
cur.execute("insert into student sql='delete from student where sname LIKE
values(3,'BABU','TATA NAGAR','ART','F',null)") '+"'"+name+"'"
cur.execute("insert into student cur.execute(sql)
values(4,'ISHA','UCIL except:
COLONY','COM','M','8764440747')") db.commit()
cur.execute("insert into student
values(5,'KUMAR','KOLKATA','CSC','M',null)") cur.execute('select * from student')
cur.execute('select * from student') for i in cur:
for i in cur: print(i)
print(i) db.close()
s='alter table student add(mark int(3), age int(2))'
cur.execute(s) OUTPUT
EX1:
s='alter table student modify address varchar(35)' ATOMIC
cur.execute(s) OMI
cur.execute('desc student') OM
for i in cur: ATOMIC
print(i) OMIC
AOI
S.K,Mukherjee 10
AOI [10, 20, 30, 40, 50]
ATOMI 10 50 2
TOM [10, 20, 30, 40, 50, 60]
AM [10, 15, 20, 30, 40, 50, 60]
ATO [10, 15, 20, 30, 40, 50, 60, 70, 80, 90]
CMT popped item 90
65 [10, 15, 20, 30, 40, 50, 60, 70, 80]
A []
6 1
EX2: [10, 15, 20, 30, 50, 60, 70, 80]
AtomicEnergy [10, 15, 20, 30, 50, 60, 70, 80]
AtomicAtomicAtomic [80, 70, 60, 50, 30, 20, 15, 10]
False 335
True ['ATOMIC', 'ENERGY', 'CENTRAL', 'SCHOOL']
14 Ex6:
-1 ('1', '2', '3', '4', '5', '6')
14 ('A', 't', 'o', 'm', 'i', 'c') A
True ('5', '+', '8')
True 13
True False
False True
True (10, 20, 10.0, 20.0) (10, 20, 10, 20)
central (10, 20, 30, 40, 50, 60, 70, 80, 90, 100)
CENTR (20, 30, 40) (30, 40) (20, 40, 60)
NTRAL (100, 80, 60, 40, 20)
CENTRAL (10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120)
C-E-N-T-R-A-L 12 120 10 4
central Ex7:
('ATOMIC ', 'ENERGY', ' CENTRAL SCHOOL') Enter no of terms of the Fibonacci Series10
('ATOMIC', ' ', 'ENERGY CENTRAL SCHOOL') 0 1 1 2 3 5 8 13 21 34
Ex3: Ex8:
Enter String :Atomic Energy Central School {'python': 500, 'C++': 400, 'Java': 450, 'Oracle': 800}
No of Upper Case characters 4 Length : 4
No of Lower Case characters 21 python Book costs Rs. 500
Ex4: C++ Book costs Rs. 400
[10, 20, 30, 40, 50] 40 20 Java Book costs Rs. 450
['A', 'T', 'O', 'M', 'I', 'C'] M O Oracle Book costs Rs. 800
[10, 20, [30, 40], 50] [30, 40] 10 ['python', 'C++', 'Java', 'Oracle']
False False [10, 20, [30, 40], 50, 60] (500, 400, 450, 800)
[10, 20, 30, 40, 50, 60] [20, 30, 40] [20, 30, 40] {'python': 500, 'C++': 400, 'Java': 550, 'Oracle': 800}
[60, 40, 20] {'python': 500, 'C++': 400, 'Java': 550, 'Oracle': 800,
['ten', 'twenty', 30, 40, 50, 60] 'HTML': 250}
['ten', '9', '8', '7'] {'python': 500, 'C++': 400, 'Java': 550, 'HTML': 250}
Ex5: {'python': 500, 'Java': 550, 'HTML': 250}
['1', '2', '3', '4', '5', '6'] Book Not found
['A', 't', 'o', 'm', 'i', 'c'] {
['5', '+', '8'] "python": 500,
13 "Java": 550,
(10, 20, 30, 40, 50) "HTML": 250
[10, 20, 30, 40, 50] }
S.K,Mukherjee 11
500 Ex12:
python 500 15 20
Java 550 15
HTML 250 Ex13:
{'python': 500, 'Java': 550, 'HTML': 250, Enter a no6
'FORTRAN': 300, 'COBOL': 275} 720
Ex9: 720
Enter no of students:2 Ex14:
Enter Rollno:101 1. Add
Enter Name :GARIMA 2. Subtract
Enter Mark :89 3. Multiply
Enter Rollno:102 4. Divide
Enter Name :MANI Enter a :1
Enter Mark :78 Enter b :56
{101: ['GARIMA', 89], 102: ['MANI', 78]} Enter Choice:3
Enter Student Rollno :102 56
['MANI', 78] Ex15:
Ex 10: [12, 45, 65, -89, -62, 52, 79]
7 Enter the element to be searched:52
False Element found at 6
True
0b111 0xf 0o10 [12, 45, 65, -89, -62, 52, 79]
7 Enter the element to be searched:888
True Not found
A 65 Ex16:
(3+0j) [-78, -44, -20, 3, 5, 12, 56, 91]
(2, 1) Enter the item to be searched56
14 Element is present at index 6
5
a=3 and b=4 [-78, -44, -20, 3, 5, 12, 56, 91]
3 Enter the item to be searched888
42 Element is not present in array
3.78 380.0 101 -46 2.0 Ex17:
{1, 2, 3} Enter no of elements:5
yh Original List
['h', 'n', 'o', 'p', 't', 'y'] [46, 40, 87, 33, 22]
14 Sorted List
Ex11: [22, 33, 40, 46, 87]
5 Ex18:
720 Enter no of elements:5
2.0 Original List [40, 41, 62, 43, 14]
10 Sorted List: [14, 40, 41, 43, 62]
12 Ex:19
3.1622776601683795 Enter radius :3.5
6.4031242374328485 Enter length :4
179.9087476710785 3.141592653589793 Enter breadth: 12
5.192925985263684 Area of the circle = 38.465
0.26506869999696603 3.2238332233222913 Area of the rectangle= 48
4
Ex:20
3
200
S.K,Mukherjee 12
8000 Want to add more record(y/n) :y
Ex:21 Enter student Roll No:102
Welcome to the world of computers Enter student Name :GARIMA
Lets learn the art of python programming Want to add more record(y/n) :n
Python with MYSQL helps you build you a [{'roll': '101', 'name': 'SKM'}, {'roll': '102', 'name':
successful career 'GARIMA'}]
Welcome Q30.
Welcome to the world of computers Enter student Name :GARIMA
Enter the mark:78
['Welcome to the world of computers\n', 'Lets Enter Date of Birth DDMMMYYYY09DEC2000
learn the art of python programming\n', 'Python Want to add more record(y/n) :y
with MYSQL helps you build you a successful Enter student Name :KAVYAN
career'] Enter the mark:89
Ex22: Enter Date of Birth DDMMMYYYY16JUN2004
Welcome to the world of computers Want to add more record(y/n) :n
Lets learn the art of python programming [{'name': 'GARIMA', 'mark': 78, 'age': {'day': '09',
Python with MYSQL helps you build you a 'month': 'DEC', 'year': '2000'}}, {'name': 'KAVYAN',
successful career 'mark': 89, 'age': {'day': '16', 'month': 'JUN', 'year':
'2004'}}]
Ex23: Ex31:
Welcome To The World Of Computers Lets Learn Enter student Name :SKM
The Art Of Python Programming Python With Enter the mark:87
Mysql Helps You Build You A Successful Career Enter Date of Birth DDMMMYYYY10JUN1989
Ex24: {'name': 'SKM', 'mark': 87, 'age': {'day': '10',
Welcome to the world of computers 'month': 'JUN', 'year': '1989'}}
Lets learn the art of python programming Want to add more record(y/n) :y
Python with MYSQL helps you build you a Enter student Name :GARIMA
successful career Total no of vowels:36 Enter the mark:78
Q25. Welcome to the world of computers Enter Date of Birth DDMMMYYYY16jun2002
Lets learn the art of python programming {'name': 'GARIMA', 'mark': 78, 'age': {'day': '16',
Python with MYSQL helps you build you a 'month': 'jun', 'year': '2002'}}
successful career Total no of Lower Case:102 Total Want to add more record(y/n) :n
no of Upper Case:8 Enter student Name :GARIMA
dict_values(['GARIMA', 78, {'day': '16', 'month':
Q26. Welcome to the world of computers!!!
'jun', 'year': '2002'}])
Lets learn the art of python 3.8 programming
Python with #MYSQL helps you build you a Ex32: No of Records: 3
successful career Total no of alphabets: 110 Total Ex33. Enter SN to be searched:2
no of digits :2 Total no of Spl Characters :28 Name : Tim Berners-Lee Subject : World Wide
Q27. Web
Enter the word to be searched :the Ex34:
2 1.PUSH
Welcome to the world of computers!!! 2.POP
Lets learn the art of python 3.8 programming 3.DISPLAY
Python with #MYSQL helps you build you a 4.EXIT
successful career Enter your choice1
Q28. [45, 27, 60, 16, 81, 41, 39, 16, 0, 40] Enter any number10
0 16 16 27 39 40 41 45 60 81
1.PUSH
Q29.
2.POP
Enter student Roll No:101
3.DISPLAY
Enter student Name :SKM
S.K,Mukherjee 13
4.EXIT >>>
Enter your choice1 Ex35:
Enter any number20
1.PUSH
1.PUSH 2.POP
2.POP 3.DISPLAY
3.DISPLAY 4.EXIT
4.EXIT Enter your choice1
Enter your choice1 Enter any number10
Enter any number30
1.PUSH
1.PUSH 2.POP
2.POP 3.DISPLAY
3.DISPLAY 4.EXIT
4.EXIT Enter your choice1
Enter your choice3 Enter any number20
['10', '20', '30']
1.PUSH
1.PUSH 2.POP
2.POP 3.DISPLAY
3.DISPLAY 4.EXIT
4.EXIT Enter your choice1
Enter your choice2 Enter any number30
Deleted element 30
1.PUSH
1.PUSH 2.POP
2.POP 3.DISPLAY
3.DISPLAY 4.EXIT
4.EXIT Enter your choice3
Enter your choice3 ['10', '20', '30']
['10', '20']
1.PUSH
1.PUSH 2.POP
2.POP 3.DISPLAY
3.DISPLAY 4.EXIT
4.EXIT Enter your choice2
Enter your choice2 Deleted element 10
Deleted element 20
1.PUSH
1.PUSH 2.POP
2.POP 3.DISPLAY
3.DISPLAY 4.EXIT
4.EXIT Enter your choice2
Enter your choice3 Deleted element 20
['10']
1.PUSH
1.PUSH 2.POP
2.POP 3.DISPLAY
3.DISPLAY 4.EXIT
4.EXIT Enter your choice3
Enter your choice4 ['30']
S.K,Mukherjee 14
Enter your choice4
1.PUSH Ex37:
2.POP
3.DISPLAY 1.Add Employee Data
4.EXIT 2.Delete Employee Data
Enter your choice4 3.View Employee Data
>>> 4.EXIT
Enter your choice1
Enter Employee No:101
Enter Employee Name:SKM
Ex36:
1.Add Employee Data 1.Add Employee Data
2.Delete Employee Data 2.Delete Employee Data
3.View Employee Data 3.View Employee Data
4.EXIT 4.EXIT
Enter your choice1 Enter your choice1
Enter Employee No:101 Enter Employee No:102
Enter Employee Name:PRATIK Enter Employee Name:GARIMA
with open('sample.txt','r') as f:
for i in f.readlines():
l=i.split()
for i in l:
print(i,end='#')
2.
with open('sample.txt','w') as f:
f.write("Welcome to the world of
computers")
with open('sample.txt','r') as f:
data=f.read()
v=lc=uc=s=0
for i in data:
if i in 'aeiouAEIOU':
v+=1
if i.islower():
S.K,Mukherjee 17