St Patrick's Sr. Sec.
School
PYTHON
PROGRAMMING
Submitted to…
Ghazanfer Ali Submitted by ….
Name:- Yashika Jaiswal
Class:- XI Sec:- Sci
Roll No. :- 14
1
INDEX
Serial PROGRAM PAGE
NO. NO. NO.
01. Program 1
02. Program 2
03. Program 3
04. Program 4
05. Program 5
06. Program 6
07. Program 7
08. Program 8
09. Program 9
10. Program 10
11. Program 11
12. Program 12
13. Program 13
14. Program 14
15. Program 15
16. Program 16
17. Program 17
18. Program 18
19. Program 19
20. Program 20
21. Program 21
22. Program 22
23. Program 23
24. Program 24
25. Program 25
2
# Program to print elements of a list [‘q’,’w’,’e’,’r’,’t’,’y’] in separate lines along with
element’s both indexes(positive and negative).
L=['q','w','e','r','t','t','y']
>>> length=len(L)
>>> for a in range(length) :
print("At indexes",a,"and",(a-length),"element:",L[a])
OUTPUT:
At indexes 0 and -7 element: q
At indexes 1 and -6 element: w
At indexes 2 and -5 element: e
At indexes 3 and -4 element: r
At indexes 4 and -3 element: t
At indexes 5 and -2 element: t
At indexes 6 and -1 element: y
3
# Program to count frequency of a given element in a list of numbers.
lst=eval(input("Enter list:"))
length=len(lst)
element=int(input("Enter element :"))
count = 0
for i in range (0,length):
if element == lst[i]:
count+=1
if count == 0:
print(element ,"not found in given list")
else:
print(element,"has frequency as",count,"in given list")
OUTPUT:
Enter list:[1,1,1,2,2,3,4,2,2,5,2,2,5]
Enter element :2
2 not found in given list
2 not found in given list
2 not found in given list
2 has frequency as 1 in given list
2 has frequency as 2 in given list
2 has frequency as 2 in given list
2 has frequency as 2 in given list
2 has frequency as 3 in given list
2 has frequency as 4 in given list
2 has frequency as 4 in given list
2 has frequency as 5 in given list
4
2 has frequency as 6 in given list
2 has frequency as 6 in given list.
5
# Program to find frequencies of all element of a list. Also, print the list of unique
elements in the list and duplicate the given list .
lst=eval(input("Enter list:"))
length=len(lst)
uniq=[]
dupl=[]
count=i=0
while i < length :
element = lst[i]
count =1
if element not in uniq and element not in dupl:
i+=1
for j in range (i,length):
if element == lst[j]:
count += 1
else:
dupl.append(element)
else:
i+=1
print("Original list",lst)
print("Unique element list",uniq)
print("Duplicates elements list",dupl)
OUTPUT:
Enter list:[2,3,4,5,3,6,7,3,5,2,7,1,9,2]
Original list [2, 3, 4, 5, 3, 6, 7, 3, 5, 2, 7, 1, 9, 2]
Unique element list []
Duplicates elements list [2, 3, 4, 5]
6
Original list [2, 3, 4, 5, 3, 6, 7, 3, 5, 2, 7, 1, 9, 2]
Unique element list []
Duplicates elements list [2, 3, 4, 5, 6, 7]
Original list [2, 3, 4, 5, 3, 6, 7, 3, 5, 2, 7, 1, 9, 2]
Unique element list []
Duplicates elements list [2, 3, 4, 5, 6, 7]
Original list [2, 3, 4, 5, 3, 6, 7, 3, 5, 2, 7, 1, 9, 2]
Unique element list []
Duplicates elements list [2, 3, 4, 5, 6, 7]
Original list [2, 3, 4, 5, 3, 6, 7, 3, 5, 2, 7, 1, 9, 2]
Unique element list []
Duplicates elements list [2, 3, 4, 5, 6, 7]
Original list [2, 3, 4, 5, 3, 6, 7, 3, 5, 2, 7, 1, 9, 2]
Unique element list []
Duplicates elements list [2, 3, 4, 5, 6, 7, 1, 9]
7
# Program to create a phone dictionary for all your friends and then print it .
PhoneDict = {"Madhav":1234567,"Steven":7654321,"Dilpreet"
:6734521,"Rabiya":4563217,"Murughan":3241567,"Sampree":4673215}
for name in PhoneDict:
print( name,":",PhoneDict[name])
OUTPUT:
Madhav : 1234567
Steven : 7654321
Dilpreet : 6734521
Rabiya : 4563217
Murughan : 3241567
Sampree : 4673215
8
# Program to create a dictionary containing names of competition winner students as keys
and numbers of their wins as values.
n = int(input("How many students ?"))
CompWinners = {}
for a in range(n) :
key = input ("Name of the student :")
value = int (input("Number of competitions won :"))
CompWinners[key] = value
print("The dictionary now is :")
print(CompWinners)
OUTPUT:
How many students ?5
Name of the student :Rohan
Number of competitions won :5
Name of the student :Zeba
Number of competitions won :3
Name of the student :Nihar
Number of competitions won :3
Name of the student :Yash
Number of competitions won :6
Name of the student :James
Number of competitions won :5
The dictionary now is :
{'Rohan': 5, 'Zeba': 3, 'Nihar': 3, 'Yash': 6, 'James': 5}
9
# Program to count the frequency of a list – elements using a dictionary.
import json
sentence = "This is a super idea This \ idea will change the idea will change the idea of
learning"
words = sentence.split()
d={}
for one in words :
key = one
if key not in d:
count = words.count(key)
d[key] = count
print("Counting frequencies in list \n",words)
print(json.dumps(d,indent=1))
OUTPUT:
Counting frequencies in list
['This', 'is', 'a', 'super', 'idea', 'This', '\\', 'idea', 'will', 'change', 'the', 'idea', 'will', 'change',
'the', 'idea', 'of', 'learning']
"This": 2,
"is": 1,
"a": 1,
"super": 1,
"idea": 4,
"\\": 1,
10
"will": 2,
"change": 2,
"the": 2,
"of": 1,
"learning": 1
11
# Program to calculate and roots of a quadratic equation : ax2+bx + c = 0 (a = 0)
import math
print ("For quadratic equation , ax**2 + bx + c =0, enter coefficients below")
a = int (input (" Enter a :"))
b = int (input (" Enter b :"))
c = int (input (" Enter c :"))
if a == 0:
print ("Value of :" , a ,'should not be zero')
print ("\n Aborting !!!!!!")
else:
delta = b*b-4*a*c
if delta > 0 :
root1 = (-b + math.sqrt(delta))/(2*a)
root2 = (-b - math.sqrt(delta))/(2*a)
print("Roots are REAL and UNEQUAL")
print("Root1=",root1,"Root2=",root2)
elif delta==0:
root1=-b/(2*a);
print("Roots areREAL and UNREAL ")
print ("Roots1=",root1,"Roots2=",root1)
else:
print("Roots are COMPLEX and IMAGINARY")
OUTPUT :
For quadratic equation , ax**2 + bx + c =0, enter coefficients below
Enter a :3
Enter b :5
12
Enter c :2
Roots are REAL and UNEQUAL
Root1= -0.6666666666666666 Root2= -1.0
>>>
= RESTART :
For quadratic equation , ax**2 + bx + c =0, enter coefficients below
Enter a :2
Enter b :3
Enter c :4
Roots are COMPLEX and IMAGINARY
>>>
= RESTART:
For quadratic equation , ax**2 + bx + c =0, enter coefficients below
Enter a :2
Enter b :4
Enter c :2
Roots areREAL and UNREAL
Roots1= -1.0 Roots2= -1.0
>>>
13
# Program that reads a line and prints its statistics like :
line = input ("Enter a line :")
lowercount = uppercount = 0
digitcount = alphacount = 0
for a in line :
if a.islower() :
lowercount += 1
elif a.isupper() :
uppercount += 1
elif a.isdigit() :
digitcount += 1
if a.isalpha() :
alphacount += 1
print ("Number of uppercase letters :", uppercount)
print ("Number of lowercase letters :", lowercount)
print ("Number of alphabets :", alphacount)
print ("Number of digits :", digitcount)
OUTPUT :
Enter a line :Hello 123 , ZIPPY zippy zap
Number of uppercase letters : 6
Number of lowercase letters : 12
Number of alphabet : 18
Number of digits : 3
14
# Program to find frequencies of all elements of a list . Also , print the list of unique
elements in the list and duplicate elements in the given list .
lst = eval( input("Enter list :"))
length = len(lst)
uniq = []
dupl = []
count = i = 0
while i < length :
element = lst[i]
count = 1
if element not in uniq and element not in dupl :
i += 1
for j in range (i , length ):
if element == lst[j]:
count+=1
else:
print ("Element", element,"frequency:",count)
if count == 1 :
uniq.append(element)
else :
dupl.appent(elements)
else:
i += 1
print("Original list ",lst)
print("Unique elements list " , uniq)
print("Duplicates elements list " , dupl)
OUTPUT:
15
Enter list :[2,3,4,5,3,6,7,3,5,2,7,1,9,2]
Element 2 frequency : 3
Element 3 frequency : 3
Element 4 frequency : 1
Element 5 frequency : 2
Element 6 frequency : 1
Element 7 frequency : 2
Element 1 frequency : 1
Element 9 frequency : 1
Original list [ 2,3,4,5,3,6,7,3,5,2,7,1,9,2]
Unique elements list [4,6,1,9]
Duplicates elements list [2,3,5,7]
16
# Program to find multiples of number( the divisor ) out of given 5 numbers .
print("Enter five numberss below ")
num1 = float(input("First number :"))
num2 = float(input("Second number :"))
num3 = float(input("Third number :"))
num4 = float(input("Fourth number :"))
num5 = float(input("Fifth number :"))
divisor = float(input("Enter divisor number "))
count = 0
print(" Multiple of " , divisor , "are :" )
remainder = num1 % divisor
if remainder == 0 :
print(num1 , sep = " ")
count += 1
remainder = num2 % divisor
if remainder == 0 :
print (num2 , sep = " ")
count += 1
remainder = num3 % divisor
if remainder == 0 :
print (num3 , sep = " ")
count += 1
remainder = num4 % divisor
if remainder == 0 :
print (num4 , sep = " ")
count += 1
remainder = num5 % divisor
17
if remainder == 0 :
print (num5 , sep = " ")
count += 1
print()
print(count , "multiples of " , divisor ," found")
OUTPUT :
Enter five numbers below
First number : 185
Second number : 3450
Third number : 1235
Fourth number : 1100
Fifth number : 905
Enter divisor number : 15
Multiple of 15.0 are :
3450.0
1 multiples of 15.0 found
>>>
18
# Program that input three numbers and calculate two sums as per this :
# Sum1 as the sum and of all input numbers
# Sum2 as the sum of non - duplicate numbers ; if there are duplicate numbers in input ,
ignores them .
sum1 = sum2 = 0
num1 = int(input("Enter number 1 :"))
num2 = int(input("Enter number 2 :"))
num3 = int(input("Enter number 3 :"))
sum1 = num1 + num2 + num3
if num1 == num2 :
if num3 != num1 :
sum2 += num3
else :
if num1 == num3 :
sum2 += num2
else :
if num2 == num3:
sum2 += num1
else :
sum2 += num1 + num2 + num3
print ("Numbers are ", num1, num2 ,num3)
print ("Sum of three given number is ", sum1)
print ("Sum of non - duplicate number is ", sum2)
OUTPUT :
Enter number 1 :2
Enter number 2 :3
Enter number 3 :4
19
Numbers are 2 3 4
Sum of three given number is 9
Sum of non - duplicate number is 9
>>>
== RESTART:
Enter number 1 :3
Enter number 2 :2
Enter number 3 :3
Numbers are 3 2 3
Sum of three given number is 8
Sum of non - duplicate number is 2
>>>
== RESTART:
Enter number 1 :4
Enter number 2 :4
Enter number 3 :4
Numbers are 4 4 4
Sum of three given number is 12
Sum of non - duplicate number is 0
>>>
20
# Program to display a menu for calculating area of a circle or perimeter of a circle .
radius = float(input("Enter radius of the circle : "))
print ("1. Calculate Area ")
print ("2. Calculate Perimeter ")
choice = int(input("Enter your choice (1 or 2): "))
if choice == 1 :
area = 3.14159*radius*radius
print ("Area of circle with radius ", radius,"is",area)
else :
perm = 2*3.14159*radius
print("Perimeter of circle with radius ",radius ,"is",perm)
OUTPUT :
Enter radius of the circle : 2.5
1. Calculate Area
2. Calculate Perimeter
Enter your choice (1 or 2): 1
Area of circle with radius 2.5 is 19.6349375
>>>
== RESET
Enter radius of the circle : 2.5
1. Calculate Area
2. Calculate Perimeter
Enter your choice (1 or 2): 2
Perimeter of circle with radius 2.5 is 15.70795
>>>
21
22