WORLD COLLEGE OF TECHNOLOGY AND MANAGEMENT
COMPUTER SCIENCE AND DESIGN
PRACTICAL FILE
SESSION:-2022
SUBMITTED BY:- SUBMITTED TO:-
NAME: - CHANDAN GIRI Mrs. SENGEETA
COURSE: - B.TECH 3rd sem. ASSISTANT. PROFESSOR
(WCTM)
BRANCH: - CSD
SUBJECT: - PYTHON FILE
ROLL NUMBER:-9038985
INDEX
S.NO. Name of Program Page no. Signature
1. . 1
2. 3
3. . 5
4. . 9
5. . 11
6. . 13
7. . 15
8. 18
.
9. 21
.
10. 25
11. 29
12. 31
13. 34
Chandan Giri (CSD)3rd sem
1. Write a Program to Print “Hello World” in Python. (Single Line)
Code: print("Hello World")
Output:
2. Write a Program to Print “Twinkle Twinkle” poem in Python. (Multiple Lines)
Code:print('''Twinkle Twinkle little star How I wonder what you are up above the
world so high like a diamond in the sky''')
Output:
Chandan Giri (CSD)3rd sem
3. Write a Program to play audio in Python.
Code: from IPython.display import Audio
Play=r'C:\Program file\Dell\chand\play.mp3'
Audio(data=Play,autoplay=False)
Output: audio is playing
4. Write a Python Program to print the contents of a directory using OS module.
Code: import os
print (os.listdir())
Output:-
Chandan Giri (CSD)3rd sem
5. Label the program written in Problem 4 with Comments.
'''Write a Python Program to print the contents of a directory using OS module. Code
Done By:______'''
importos print (os.listdir())
# ***** Thanks for Reading *****
6. Variables and Data Types in Python.
a=20 #integer
b=5.5 #floating point
c=”name” #string
d=True #boolean
Chandan Giri (CSD)3rd sem
7. Arithmatic Operation In Python
Code: a=50 ,b=10
print("The value of a+b is:",(a+b))
print("The value of a-b is:",(a-b))
print("The value of a*b is:",(a*b))
print("The value of a/b is:",(a/b))
8. WAP to convert a string into number and vice versa
Code: a="44557667"
a=int(a)
print (type(a))
print(a+5)
Chandan Giri (CSD)3rd sem
9. WAP to use input function.
Code: a=input("Enter your Name")
print(type(a))
Output:
10.WAP to add two Numbers
Code: a=30
b=15
print("The sum of a and b is:", a+b)
Output:
Chandan Giri (CSD)3rd sem
11. WAP to find remainder when a number is divided by Z
Code: a=45
b=15
print("The remainder when a is divided by b is:", a%b)
Output:
12.Check the type of a variable assigned using input function and convert its type
into int.
Code: a=input("Enter a number here")
print(type(a))
int(a)
Output: -
Chandan Giri (CSD)3rd sem
13. Use Comparison operator to find out whether a given variable ‘a’ is greater than
‘b’ or not. Take a=34 and b=80
Code: a=34
b=80
print(a>b)
print(a)
Output: -
Chandan Giri (CSD)3rd sem
14. Write a Python program to find average of two numbers entered by the user by
using input function.
Code: a=input("Enter first number")
b=input("Enter second number")
a=int(a)
b=int(b)
avg=(a+b)/2
print("Average of two numbers is:",avg)
Output: Enter first number25
Enter second number35
Average of two numbers is: 30.0
Chandan Giri (CSD)3rd sem
15. Write a Python program to calculate square of a number entered by the user.
Code: a=input("Enter a number")
a=int(a)
square=a*a
print("Square of given number is:",square)
Output:-
16.Concatenation of string
Code: greetings="Good Morning,"
name="Himanshu pal"
print (type(name))
c=greetings+name
print(c)
Output:-
Chandan Giri (CSD)3rd sem
17.Print string slices
Code: name="himanshupalgood "
print(name[1])
print(name[0:3])
print(name[0: ])
print(name[ :4])
print(name[1:8:2])
print(name[1::2])
print(name[1::3])
print(name[-4:-2])
print(name[-11:])
Output:
Chandan Giri (CSD)3rd sem
18.Write a Python Program to display a user entered name followed by Good
Afternoon using input() function.
Code: name=input("Enter your name \n")
print("Good afternoon,"+name)
Output:
Chandan Giri (CSD)3rd sem
19.Write a program to fill in a letter template given below with name and date.
Letter = ‘‘‘ Dear You are selected ’’’
Code: letter='''Dear , Greetings from WCTM. I am happy to tell you that You are
selected! Have a great day! Thanks and regrds!
Date: ''' name=input("Enter your name\n")
date=input("Enter Date\n")
letter=letter.replace("|Name|",name)
letter=letter.replace("|Date|",date)
print(letter)
Output: Enter your name Himanshu pal
Enter data 12/09/2022
Chandan Giri (CSD)3rd sem
20.Write a program to detect double spaces in a string.
Code: st="This is a string with double spaces"
doubleSpaces = st.find(" ")
print(doubleSpaces)
Output: 4
21.Write a program to replace double spaces with single space in a string.
Code: st="This is a string with double spaces"
st = st.replace(" "," ")
print(st)
Chandan Giri (CSD)3rd sem
Output:-
22.WAP to find the greatest of four numbers entered by the user.
Code: num1=int(input("Enter number1:"))
num2=int(input("Enter number2:"))
num3=int(input("Enter number3:"))
num4=int(input("Enter number4:"))
if(num1>num2):
f1=num1
else:
f1=num2
if(num3>num4):
f2=num3
else:
f2=num4
if(f1>f2):
print(str(f1)+"is greater")
else:
print(str(f2)+"is greater")
Output:
Chandan Giri (CSD)3rd sem
23.WAP to find out whether a student is pass or fail if it require total 40% and at
least 35% in each subject to pass. Assume 3 subjects and take marks as an input
from user.
Code: sub1=int(input("Enter first subject marks: \n"))
sub2=int(input("Enter second subject marks: \n"))
sub3=int(input("Enter third subject marks: \n"))
if (sub1<35 or sub2<35 or sub3<35):
print("Student is fail because he has score less than 35% in particular subject")
elif ((sub1+sub2+sub3)/3<40):
print("Student is fail because total is less than 40%") else:
print("Student has passed the exam")
Output:
Chandan Giri (CSD)3rd sem
24.WAP which find out whether a given name is present in a list or not.
Code: names = ["Kasid", "Pushkar", "radhey", "Himanshu", "Rishi", "shipra"]
name = input("Enter the name to check\n")
if name in names:
print("Your name is present in the list") else:
print("Your name is not present in the list")
Output:-
Chandan Giri (CSD)3rd sem
25. A spam content is defind as a text containing following keyword:"make a lot of
money", "buy now", "subscribe this", "click this". WAP to detect these spams
Code: text = input("Enter the text\n")
if("make a lot of money" in text):
spam = True
elif("buy now" in text):
spam = True
elif("click this" in text):
spam = True
elif("subscribe this" in text):
spam = True
else:
spam = False
if(spam):
print("This text is spam")
else:
print("This text is not spam")
Output:
Chandan Giri (CSD)3rd sem
26. WAP to calculate grade of a student from his marks from the following scheme:
90-100 Ex
80-90 A
70-80 B
60-70 C
50-60 D
<50 Fail'''
Code: marks = int(input("Enter Your Marks\n"))
if marks>=90:
grade = "Ex"
elif marks>=80:
grade = "A"
elif marks>=70:
grade = "B"
elif marks>=60:
grade = "C"
elif marks>=50:
grade = "D"
else:
grade = "Fail"
print("Your grade is " + grade)
Output:
Chandan Giri (CSD)3rd sem
27.WAP to print table of a given number using for loop
Code: num = int(input("Enter the number"))
for i in range(1, 11):
print(f"{num}X{i}={num*i}")
Output:-
Chandan Giri (CSD)3rd sem
28.WAP to greet all the persons names stored in a list 1 and which start with R
Code:1=("Radhey", "Rishi", , "Kasid", "Pushkar", "Rohit")
for name in l1:
ifname.startswith("R"):
print("Hello " + name)
Output:
Chandan Giri (CSD)3rd sem
29.WAP to find whether a given number is prime or not
Coding:-
num = int(input("Enter the number: "))
prime = True
for i in range(2, num):
if(num%i == 0):
prime = False
break
if prime:
print("This number is Prime")
else:
print("This number is not Prime")
Output:
Chandan Giri (CSD)3rd sem
30.WAP to calculate factorial of a given number
Coding: num = int(input("Enter the number: "))
factorial = 1
for i in range(1, num+1):
factorial = factorial * i
print(f"The factorial of {num} is {factorial}")
Output:
31.WAP to print following star pattern
*
***
*****
Chandan Giri (CSD)3rd sem
Chandan Giri (CSD)3rd sem
32.‘’’WAP to print following star pattern
*
**
***
****'''
33.WAP to create a list and access any element using index and change the value at
particular index
a=[3,5,6,8,5,43,56]
print(a)
print(a[3])
a[3]=12
print(a)
print(a[3])
a=[23,5.5,"sam","c",324]
print(a)
Chandan Giri (CSD)3rd sem
34.WAP for list slicing
a=[23,5.5,"sam","c",324]
print(a[0:])
print(a[:4])
print(a[0:3])
print(a[2:4])
print(a[-3:4])
35.WAP for different functions of list
a=[4,45,6,9,78,5,98]
print(a)
a.sort()
print(a)
a.reverse()
print(a)
a.append(100)
print(a)
a.insert(2,67)
print(a)
a.pop(2)
Chandan Giri (CSD)3rd sem
36.WAP to store seven fruits in a list entered by the user
Coding:-f1 = input("Enter Fruit Number 1: ")
f2 = input("Enter Fruit Number 2: ")
f3 = input("Enter Fruit Number 3: ")
f4 = input("Enter Fruit Number 4: ")
f5 = input("Enter Fruit Number 5: ")
f6 = input("Enter Fruit Number 6: ")
f7 = input("Enter Fruit Number 7: ")
myFruitList = [f1, f2, f3, f4, f5, f6, f7]
print(myFruitList)
Output:-
Chandan Giri (CSD)3rd sem
37.WAP to accept marks of six students and display them in a sorted manner
Coding: - m1 = int(input("Enter Marks for Student Number 1: "))
m2 = int(input("Enter Marks for Student Number 2: "))
m3 = int(input("Enter Marks for Student Number 3: "))
m4 = int(input("Enter Marks for Student Number 4: "))
m5 = int(input("Enter Marks for Student Number 5: "))
m6 = int(input("Enter Marks for Student Number 6: "))
myList = [m1, m2, m3, m4, m5, m6]
myList.sort()
print(myList)
Output:-
Chandan Giri (CSD)3rd sem
38.WAP to create a tuple and execute its methods
Coding :-t = (1, 2, 4, 5, 1)
print(t)
t1 = (1) # Tuple with Single element
print(t1)
# Printing the elements of a tuple
print(t[0])
# Cannot update the values of a tuple
#t[0] = 34
# throws an error
#Tuple methods
print(t.count(1))
print(t.index(5))
Output:-
Chandan Giri (CSD)3rd sem
39.WAP to sum a list with four numbers
Codig:-a = [2, 4, 56, 7]
#print(a[0] + a[1] + a[2] + a[3])
print(sum(a))
Output:-
40.WAP to count the number of two's in following tuple
Coding:- a=(4,6,2,4,2,4,6,2,5,7)
a.count(2)
Output:-
Chandan Giri (CSD)3rd sem
41.WAP to create dictionary and use its methods
Coding:-mydict={
"fast":"in a quick manner",
"sam":"programmer",
"marks":[34,56,74], 1: 2
}print(mydict)
print(mydict['fast'])
print(mydict['sam'])
print(mydict['marks'])
print(mydict[1])
mydict['marks']=[34,79,25]
print(mydict['marks'])
#dictionary methods
#print the keys of dictionaries
print(list(mydict.keys()))#with typecast
print(list(mydict.values())) #with typecast
print(mydict.keys())#without typecast
print(mydict.values()#without typecast
print(mydict.items())
#update dictionary
updatedict={"friend":'abdul'}
mydict.update(updatedict)
print(mydict)
print(mydict.get("marks"))
print(mydict["marks"]) #same output as above
print(mydict.get("mathmarks")) #will return none #
print(mydict["mathmarks"]) #will throw an error
Chandan Giri (CSD)3rd sem
42.WAP to create a dictionary of hindi words with values as their english
translation
Coding:-myDict = {
"Pankha": "Fan",
"darwaja": "door",
"khidki": "window"
}
print("Options are ", myDict.keys())
a = input("Enter the Hindi Word\n")
# print("The meaning of your word is:", myDict[a])
# Below line will not throw an error if the key is not present in the dictionary
print("The meaning of your word is:", myDict.get(a))
Output:-
Chandan Giri (CSD)3rd sem
43.WAP to input eight numbers from the user and display all the unique numbers
by using sets
Coding:-num1 = int(input("Enter number 1\n"))
num2 = int(input("Enter number 2\n"))
num3 = int(input("Enter number 3\n"))
num4 = int(input("Enter number 4\n"))
num5 = int(input("Enter number 5\n"))
num6 = int(input("Enter number 6\n"))
num7 = int(input("Enter number 7\n"))
num8 = int(input("Enter number 8\n"))
s = {num1, num2, num3, num4, num5, num6, num7, num8} print(s)
Output:-
Chandan Giri (CSD)3rd sem
44.create an empty dictionary, allow four friends to enter their favourite
language as values and use keys as their names. Assume that the names are
unique.
Coding :-favLang = {}
a = input("Enter your favorite language Kasid\n")
b = input("Enter your favorite language Pushkar\n")
c = input("Enter your favorite language Rishi\n")
d = input("Enter your favorite language Himanshu\n")
favLang['kasid'] = a
favLang['pushkar'] = b
favLang['rishi'] = c
favLang['himanshu'] = d
print(favLang)
Output:-
Chandan Giri (CSD)3rd sem
45.WAP to calculate percentage of marks by using functions.
Coding:-def percent(marks):
p = ((marks[0] + marks[1] + marks[2]+ marks[3])/400 )*100
return p
marks1 = [45, 78, 86, 77]
percentage1 = percent(marks1)
marks2 = [75, 98, 88, 78]
percentage2 = percent(marks2)
print(percentage1, percentage2)
Output:-
Chandan Giri (CSD)3rd sem
46.WAP to greet someone.
Coding:-def greet(name):
print("Good Day, "+ name)
defmySum(num1, num2):
return num1 + num2
greet("Sam")
s = mySum(6, 32)
print(s)
Output:-
Chandan Giri (CSD)3rd sem
47.WAP for factorial of a number by using recursion
Coding:-deffactorial_iter(n):
product = 1
for i in range(n):
product = product * (i+1)
return product
#def factorial_recursive(n):
# if n == 1 or n == 0:
# return 1
# return n * factorial_recursive(n-1)
f = factorial_iter(5)
#f = factorial_recursive(5)
print(f)
Output: -
Chandan Giri (CSD)3rd sem
48.WAP to find maximum of three numbers using function
Coding:-def maximum(num1, num2, num3):
a = maximum(13, 55, 2)
print("The value of the maximum is " +str(a))
Output:-
49. WAP to convert temperature from celcius to farneheit
Coding:-deffarh(cel):
return (cel *(9/5)) + 32
c=0
f = farh(c)
print("Fahreheit Temperature is " + str(f))
Output:-
Chandan Giri (CSD)3rd sem
50.WAP to read data from a file by using open() function
Coding:-f = open(r"C:\Users\asus\Desktop\AIDS.txt",'r') # by default the mode is r
data = f.read()
#data = f.read(5) # reads first 5 characters from the file
print(data)
f.close()
Output:-
51.WAP to write data to a file
Coding:-f = open(r"C:\Users\asus\Desktop\AIDS3rd.txt",'w')
data = f.write("Please add this content to file")
#data = f.read(5) # reads first 5 characters from the file
print(data)
f.close()
Output:-
Chandan Giri (CSD)3rd sem
52. # WAP to append data to a file
Coding:-f = open(r"C:\Users\asus\Desktop\AIDS3rd.txt",'a')
data = f.write("Please add this content to file")
print(data)
f.close()
Output:-
53.WAP to read the text from a given file poem.txt and find whether it contain
the word 'World'
Coding:-f = open(r"C:\Users\asus\Desktop\poem.txt")
t = f.read()
if 'Twinkle' in t:
print("Twinkle is present")
else:
print("Twinkle is not present")
f.close()
Output:-
Chandan Giri (CSD)3rd sem
54.WAP to generate multiplication tables from 2 to 20 and write it to the
different files
Coding:-for i in range(2, 21):
with open(f"F:\\Python\\AIDS 3rd\\UNIT-
II\\table\\Multiplication_table_of_{i}.txt", 'w') as f:
for j in range(1, 11):
f.write(f"{i}X{j}={i*j}"'\n')
Output:-
55.WAP to find and replace a word in file
Coding:-with open(r"C:\Users\asus\Desktop\AIDS.txt",'r') as f:
content = f.read()
content = content.replace("programming", "$%^@$^#")
with open(r"C:\Users\asus\Desktop\AIDS.txt", "w") as f:
f.write(content)
Output:-
Chandan Giri (CSD)3rd sem
56.WAP to mine a file and find out whether it contain “what” and find out the
line number
Coding:-content = True
i=1
with open(r"C:\Users\asus\Desktop\poem.txt") as f:
while content:
content = f.readline()
if 'what' in content.lower():
print(content)
print(f"Yes what is present on line number {i}")
i+=1
Output:-
57.WAP to make a copy of the text file
Coding:-with open(r"C:\Users\asus\Desktop\poem.txt") as f:
content = f.read()
with open(r"C:\Users\asus\Desktop\poem1.txt", 'w') as f:
f.write(content)
Output:-
Chandan Giri (CSD)3rd sem
58.WAP to check similarity of two files
Coding:- file1 = r"C:\Users\Dell\Desktop\AIDS.txt"
file2 = r"C:\Users\Dell\Desktop\WCTM.txt"
with open(file1) as f:
f1 = f.read()
with open(file2) as f:
f2= f.read()
if f1 == f2:
print("Files are identical")
else:
print("Files are not identical")
Output:-
Chandan Giri (CSD)3rd sem
59.WAP to wipe out the content of a file
Coding:-filename = r"C:\Users\asus\Desktop\poem.txt"
with open(filename, "w") as f:
f.write("")
Output:-
60.WAP to rename a file
Coding:-import os
oldname = r"C:\Users\asus\Desktop\poem.txt"
newname = r"C:\Users\asus\Desktop\poem2.txt"
with open(oldname) as f:
content = f.read()
with open(newname, "w") as f:
f.write(content)
os.remove(oldname)
Output:-
Chandan Giri (CSD)3rd sem