0% found this document useful (0 votes)
86 views

Delhi World Public School Bhiwani: Project On-Python

The document contains code snippets and output for 19 Python programming questions. The questions cover topics like data types, conditional statements, functions, files, lists, and CSV files. Sample inputs and outputs are provided for each code snippet to demonstrate its functionality. The questions progress from basic concepts to more advanced topics like file handling.

Uploaded by

Tushar Noonia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views

Delhi World Public School Bhiwani: Project On-Python

The document contains code snippets and output for 19 Python programming questions. The questions cover topics like data types, conditional statements, functions, files, lists, and CSV files. Sample inputs and outputs are provided for each code snippet to demonstrate its functionality. The questions progress from basic concepts to more advanced topics like file handling.

Uploaded by

Tushar Noonia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 30

Page 1 of 30

Delhi World Public School


Bhiwani

Project on-Python

Submitted to Submitted By
Mr. Sunil Tushar
Page 2 of 30
Page 3 of 30

Question 1
Code:
num1= float[input(“Enter first number: "))
num2 = float(input( "Enter second number:” ))
num3 float(input( “Enter third number:” ))

if (num1 > num2) and (num1 > num3):


largest = num1
elif (num2 > num1) and (num2 > num3):
largest= num2
else
largest= num3
print(‘The largest number is', largest)

Result:
Enter first number: 11
Enter second number: 15
Enter third number: 09
The largest number is 15.0
[Program finished]
Page 4 of 30

Question 2:
Code:
name = input("Enter Your Name --> ")
age = int(input("Enter Your Age: "))
if age >= 18:
print(name, "You Are Eligible For Voting !")
else:
print(name, "You Are Not Eligible For Voting !")

Result:
Enter Your Name --> Tushar
Enter Your Age : 16
Tushar You Are Not Eligible For Voting !
[Program finished]
Page 5 of 30

Question 3:
Code:
def multiple(m, n):
m = int(input("Enter Value For 'M': "))
n = int(input("Enter Value For 'N': "))
a = range(n, (m * n)+1, n)
print(*a)
multiple(m, n)

Result:
Enter Value For 'M' : 15
Enter Value For 'N' : 4
4 8 12 16 20 24 28 32 36 40 44 48 52 56 60
[Program finished]
Page 6 of 30

Question 4:
Code:
num = int(input("Enter a number: "))
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 31
temp // = 10
if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")

Result:
Enter a number: 123
123 is not an Armstrong number
[Program finished]
Page 7 of 30

Question 5:
Code:
weekday = int(input("Enter weekday day number (1-7):"))
if(weekday == 1)
print("Monday");
elif(weekday == 2):
print("Tuesday")
elif(weekday == 3):
print("Wednesday")
elif(weekday == 4):
print("Thursday")
elif(weekday == 5):
print("Friday")
elif(weekday == 6):
print("Saturday")
elif (weekday == 7):
print("Sunday")
else:
print("Please enter weekday number between 1-7.")

Result:
Enter weekday day number (1-7) : 6
Saturday
[Program finished]
Page 8 of 30

Question 6:
Code:
length = float(input('Please Enter the Length of a Cuboid: '))
width = float(input('Please Enter the Width of a Cuboid: '))
height = float(input('Please Enter the Height of a Cuboid: '))
# Calculating using standard formulas
SA = 2 * (length * width + length* height + width * height)
Volume = length * width * height
LSA = 2 * height * (length + width)
print("")
print("(1): For Calculating the Surface Area ")
print("(2): For Calculating the Volume ")
print("(3): For Calculating the Lateral surface area ")
choice = int(input("Enter Your Choice = "))
print("")
print("")
if choice==1:
print("The Surface Area of a Cuboid = ",SA)
elif choice==2:
print(" The Volume of a Cuboid = “,Volume)
elif choice==3:
print(" The Lateral Surface Area of a Cuboid = “,LSA)
Page 9 of 30

Result:
Please Enter the Length of a Cuboid: 12
Please Enter the Width of a Cuboid: 10
Please Enter the Height of a Cuboid: 8
(1) : For Calculating the Surface Area
(2) : For Calculating the Volume
(3) : For Calculating the Lateral surface area
Enter Your Choice = 2
The Volume of a Cuboid = 960.00
[Program finished]
Page 10 of 30

Question 7:
Code:
def string_test(s):
d={"UPPER_CASE":0,"LOWER_CASE":0}
for c in s:
if c.isupper():
d["UPPER_CASE"]+=1
elif c.islower():
d["LOWER_CASE"]+=1
else:
pass
print ("Original String :", s)
print ("No. of Upper case characters :",d["UPPER_CASE"))
print ("No. of Lower case Characters:",d["LOWER_CASE"))
the_string=input('ENTER THE DESIRED STRING HERE:')
string_test(the_string)

Result:
ENTER THE DESIRED STRING HERE : IamTUSHARandthisISMYproGRAM
Original String : IamTUSHARandthisISMYproGRAM
No. of Upper case characters : 15
No. of Lower case Characters : 12
[Program finished]
Page 11 of 30

Question 8:
Code:
c = input(“Enter String:”)
i=0
j = [] #Declaring empty List
for i in range(len(c)):
if c[i]==c[i].lower():
j.append(c[i].upper())
else:
j.append(c[i].lower())
f = ".join([str(elem) for elem in j])
print(f)

Result:
Enter String: IamTUSHARandTHISisMYprograme
iAMtusharANDthisISmyPROGRAME
[Programe Finished]
Page 12 of 30

Question 9:
Code:
article_file=open("article.txt","r+")
text=article_file.read()
count=0
space=0
digits=0
special_char=0
alphabets=0
for i in text:
if i !="":
count += 1
elif i.isalpha():
alphabets += 1
elif i.isdigit():
digits +=1
elif i.ispace():
space+=1
else:
special_char +=1
artcile_file.close()
print("Total Characters: “ count)
print(“Total Alphabets: “ alphabets)
print(“Total spaces: “ space)
print(“Total Digits: “ digits)
Page 13 of 30

print(“Total Special Characters: “ special_char)


CONTENTS OF article.txt :-
MY NAME IS TUSHAR :)

Result:
Total Characters: 20
Total Alphabets: 14
Total Spaces: 4
Total Digits: 0
Total Special Characters: 2
[Programe Finished]
Page 14 of 30

Question 10:
Code:
file name: story.txt
def countlines():
file=open("D:\story.txt.txt")
lines=file.readlines()
count=0
for word in lines:
if word[0]=="A" or word[0]=="a":
count=count+1
print("Total words starting with ‘a’ or ‘A’ are: ",count)
file.close
countlines()

CONTENTS OF story.txt:-
I am Tushar
And this is my programme

RESULT:
Total words starting with ‘a’ or ‘A’ are: 2
[Programme Finished]
Page 15 of 30

Question 11:
Code:
file name: story.txt
def countlines():
file=open("D:\story.txt.txt")
lines=file.readlines()
count=0
for word in lines:
if
word[0]=="a" or word[O]=="e" or word[0]=="i" or
word[0]=="0" or word[0]=="u" or word[0]==”A” or
word[0]==”E” or word[0]==”I” or word[0]==”O” or
or word[0]==”U”
count=count+1
print("Total lines starting with vovals are: ",count)
file.close
countlines()

CONTENTS OF story.txt:-
Amarica is a country
My name is Tushar
Earth is round

Result:
Page 16 of 30

Total lines starting with vovals are: 2

Question 12:
Code:
x = input("Enter The Word : ")
W = “”
for i in x:
w=i+w
if (x == w):
print("Yes it is palindrome")
else:
print("No it is not palindrome")

Result:
Enter The Word : malayalam
Yes it is palindrome
[Program finished]
Page 17 of 30

Question 13:
Code:
file = open("sample.txt", "r")
number_of_lines = 0
number_of_words = 0
number_of_characters = 0
for line in file:
line = line.strip()
words = line.split()
number_of_lines += 1
number_of_words += len(words)
number_of_characters += len(line)
file.close()
print("lines:", number_of_lines, "words:",
number_of_words, "characters:", number_of_characters)

CONTENTS OF sample.txt:-
My name is Tushar
Naruto is an anime

RESULT:
lines: 2
words: 8
characters: 34
Page 18 of 30

[Programme Finished]

Question 14
Code:
import pickle
import sys
dict={}
def write_in_file():
file=open("D:\\stud2.dat", "ab") #a-append,b-binary
no=int(input("ENTER NO OF STUDENTS: "))
for i in range(no):
print("Enter details of student",i+1)
dict["roll"]=int(input("Enter roll number: "))
dict["name"]=input("enter the name: ")
pickle.dump(dict,file) #dump-to write in student file
file.close().
def display();
#read from file and display
file=open("D:\\stud2.dat","rb") #r-read,b-binary
try:
while True:
stud=pickle.load(file) #write to the file
print(stud)
except EOFError:
pass
file.close()
def search():
Page 19 of 30

file=open("D:\\stud2.dat","rb") #r-read,b-binary
r=int(input("enter the roll no to search: "))
found=0
try:
while True:
data=pickle.load(file) #read from file
if data["roll"]==r:
print("The rollno=",r," record found")
print(data)
found=1
break
except EOFError:
pass
if found==0:
print("The rollno=",r," record is not found")
file.close()
while True:
print("MENU 1-Write in a file 2-display ")
print(" 3-search 4-exit")
ch=int(input("Enter your choice = "))
if ch==1:
write_in_file()
if ch==2:
display()
if ch==3:
search()
Page 20 of 30

if ch==4
print(" Thank you ")
sys.exit()

Result:
MENU
1-Write in a file
2-display
3-search
4-exit
Enter your choice = 1
ENTER NO OF STUDENTS: 2
Enter details of student 1
Enter roll number: 1350
enter the name: Tushar Noonia
Enter details of student 2
Enter roll number: 1353
enter the name: Karan Pratap
MENU
1-Write in a file
2-display
3-search
4-exit
Enter your choice = 2
{'roll': 1350, 'name': 'Tushar Noonia}
Page 21 of 30

{'roll': 1353, 'name': 'Karan Pratap'}


MENU
1-Write in a file
2-display
3-search
4-exit
Enter your choice = 3
enter the rollno to search: 1350
The rollno = 1350 record found
{'roll': 1350, 'name': 'karan partap'}
MENU
1-Write in a file
2-display
3-search
4-exit
Enter your choice =4
[Programme Finished]
Page 22 of 30

Question 15
Code:
import random
for i in range(1,11)
x = random.randint(1,101)
print(x)

Result:
77
55
34
56
78
34
56
78
99
6
[Programme Finished]
Page 23 of 30

Question 16:
STRUCTURE OF emp.dat
[empno,salary]

Code:
Import pickle
F1=open(‘emp.dat’,’ab’)
try:
while True:
e=pickle.load(f1)
if(e[0]== 2521)
e[1]+=2000
except:
f1.close()
Page 24 of 30

Question 17:
Code:
fin=open("D:\\book.txt","r")
fout=open("D:\\story.txt","w")
s=fin.readlines()
for j in s:
if 'a' in j:
fout.write(j)
fout.close
fin.close()
Page 25 of 30

Question 18:
Code:
Lst=[] #declaring empty list
number1=int(input(“enter length of list: “))
print(“enter numbers for the list”)
for i in range(0,number1)
data=int(input())
Lst.append(data)
Print(“User defined list is: “,Lst)
N=len(list)
If(Len%2 != 0)
Print(“length is odd”)
exit()
Lst1 = Lst[0,N/2]
Lst2 = Lst[N/2,N]
Lst3 = Lst2.append(lst1)
Print(“Swapped list is: “, Lst3)
Print(“Thank You”)

Result:
enter length of list: 4
enter numbers for the list
Page 26 of 30

1
2
3
4
User defined list is: [1,2,3,4]
Swapped list is: [3,4,1,2]
Thank You
[Programme Closed]
Page 27 of 30

Question 19:
Code:
import csv
fh = open("Items.csv", "w")
iwriter = csv.writer(fh)
ans = 'y’
itemrec = [['Item_Code', 'Description"', 'Price']]
print("Enter item details ")
while ans == 'y' :
icode = input("Enter Item code :")
desc = input("Enter description : ")
price = float(input("Enter price: "))
itemrec.append([icode, desc, price])
input ("Want to enter more items? (y/n)... ")
else:
iwriter.writerows(itemrec)
print("Records written successfully.")
fh.close()

Result:
Enter item details
Page 28 of 30

Enter item code: 001


Enter description: Hand sanitizer
Enter prize: 70
Want to enter more items (y/n)..... y
Enter item code: 002
Enter description: chocolate
Enter prize: 30
Want to enter more items (y/n).....n
Records written successfully
[Programme Finished]
Page 29 of 30

Question 20
Code:
tuple=(2,45,37,82,18,25)
list=list(tuple)
n=len(tuple)
for i in range(n):
for j in range(0, n-i-1):
if list[i]>list[j+1]:
list[i], list [j+1]=list[j+7], list[i]
a=list[-1]
print("Tuple: ", tuple)
print("The largest number from the tuple is: ", a)
sum=0
for a in list:
sum+=a
print("The sum of all the numbers from the tuple is: ", sum)

Result:
Tuple=(2,45,37,82,18,25)
Largest number of tuple is: 82
Sum of all the numbers in the tuple are: 209
Page 30 of 30

[Programme Finished]

You might also like