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

Term 2 - Practical Programs 2021

Uploaded by

2kkidsff
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Term 2 - Practical Programs 2021

Uploaded by

2kkidsff
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

1.

FINDING LARGEST AND SMALLEST NUMBER IN A LIST

Write a python program to find the largest and smallest number in a list.

AIM:
To write a python program to find the largest and smallest number in a list.

SOURCE CODE:

lst = []

num = int(input('How many numbers: '))

for n in range(num):

numbers = int(input('Enter number '))

lst.append(numbers)

print("Maximum element in the list is :", max(lst), "\nMinimum element in the list is :",
min(lst))

OUTPUT:
How many numbers: 5

Enter number :45

Enter number :15

Enter number :96

Enter number :75

Enter number :38

Maximum element in the list is : 96

Minimum element in the list is : 15

Result:

Thus the python program has been executed and output verified successfully.

2. DISPLAYING EVEN AND ODD ELEMENTS IN A LIST

Write a python program to display the even and odd elements in a list into two different lists.

AIM:

To write a python program to display the even and odd elements in a list into two different lists.
SOURCE CODE:

a=[]

n=int(input("Enter number of elements:"))

for i in range(1,n+1):

b=int(input("Enter element:"))

a.append(b)

even=[]

odd=[]

for j in a:

if(j%2==0):

even.append(j)

else:

odd.append(j)

print("The even list",even)

print("The odd list",odd)

Result:

Thus the python program has been executed and output verified successfully.
Output

Enter number of elements:5

Enter element:67

Enter element:43

Enter element:44

Enter element:22

Enter element:455

The even list [44, 22]

The odd list [67, 43, 455]


3. SEARCHING AN ELEMENT IN A LIST

Write a program to check if a number is present in the list or not. If the number is present, print the
position of the number. Print an appropriate message if the number is not present in the list.

AIM:

To search whether a given number is present in a list or not.

SOURCE CODE:

lst = eval(input("Enter first list :-"))

num = int(input("Enter the number which you want to search :-"))

if num in lst :

print(lst.index( num ) )

else :

print("Number not found")

Result:

Thus the python program has been executed and output verified successfully.

4. PROGRAM USING TUPLES

Write a python program to create a list of Tuples with the first element as the number and
second element as the square of the number.

AIM:

To write a python program to create a list of Tuples with the first element as the number and
second element as the square of the number.

SOURCE CODE:

l_range=int(input("Enter the lower range:"))

u_range=int(input("Enter the upper range:"))

a=[(x,x**2) for x in range(l_range,u_range+1)]

print(a)

Result:

Thus the python program has been executed and output verified successfully.
Output

Enter the lower range:1

Enter the upper range:4

[(1, 1), (2, 4), (3, 9), (4, 16)]

5 . CREATING A NESTED TUPLE

Write a program to create a nested tuple to store roll number, name and marks of students.

AIM:

To write a program to create a nested tuple to store roll number, name and marks of students.

SOURCE CODE:

tup= ()

while True :

roll = int(input("Enter a roll number :- "))

name = input("Enter name :-")

mark = input("Enter marks :-")

tup += ( (roll,name,mark ),)

user = input("Do you want to quit enter yes =")

if user == "yes":

print(tup)

break

Result:

Thus the python program has been executed and output verified successfully.

6. SEARCHING A KEY IN A DICTIONARY

Write a python program to check if a given key exists in a Dictionary or not.

AIM:

To write a python program to check if a given key exists in a Dictionary or not .

SOURCE CODE:

d={'A':1,'B':2,'C':3}

key=input("Enter key to check:")


if key in d.keys():

print("Key is present and value of the key is:", d[key])

else:

print("Key isn't present!")

Result:

Thus the python program has been executed and output verified successfully.
Output:

Case 1:

Enter key to check: A

Key is present and value of the key is:1

Case 2:

Enter key to check: F

Key isn't present!

7. COUNT THE FREQUENCY OF WORDS USING DICTIONARY

Write a python program to count the frequency of words appearing in a string using a
Dictionary.

AIM:

To write a python program to count the frequency of words appearing in a string using a Dictionary.

SOURCE CODE:

test_string=input("Enter string:")

l=[ ]

l=test_string.split()

wordfreq=[l.count(p) for p in l]

print(dict(zip(l,wordfreq)))

Result:

Thus the python program has been executed and output verified successfully.
Output:

Enter string : orange banana apple apple orange pineapple

{'orange': 2, 'pineapple': 1, 'banana': 1, 'apple': 2}


8. FILTERING STUDENTS NAME USING DICTIONARY

Write a python program to create a dictionary with the roll number, name and marks of n students
in a class and display the names of students who have scored marks above 75.
AIM:

To write a python program to create a dictionary with the roll number, name and marks of n
students in a class and display the names of students who have marks above 75.

SOURCE CODE:
n = int(input("Enter number of students: "))
result = {}
for i in range(n):
print("Enter Details of student No.", i+1)
rno = int(input("Roll No: "))
name = input("Name: ")
marks = int(input("Marks: "))
result[rno] = [name, marks]
print(result)
for student in result:
if result[student][1] > 75:
print(result[student][0],"got marks more than 75")
Result:

Thus the python program has been executed and output verified successfully.

OUTPUT:
Enter number of students: 5
Enter Details of student No. 1
Roll No: 1
Name: Alex
Marks: 90
Enter Details of student No. 2
Roll No: 2
Name: Raj
Marks: 73
Enter Details of student No. 3
Roll No: 3
Name: Nesiga
Marks: 78
Enter Details of student No. 4
Roll No: 4
Name: Arun
Marks: 95
Enter Details of student No. 5
Roll No: 5
Name: Banu
Marks: 66
{1: ['Alex', 90], 2: ['Raj', 73], 3: ['Nesiga', 78], 4: ['Arun', 95], 5: ['Banu', 66]}
Alex got marks more than 75
Nesiga got marks more than 75
Arun got marks more than 75

9. PROGRAM USING MATH MODULE

Write a python program to find the roots of a quadratic equation using math module.

AIM:

To write a python program to find the roots of a quadratic equation using math module.

SOURCE CODE:

import math
a=float(input(“Enter value of a=”))
b=float(input(“Enter value of b=”))
c=float(input(“Enter value of c=”))
r1=(-b+math.sqrt(b*b-4*a*c))/(2*a)
r2=(-b-math.sqrt(b*b-4*a*c))/(2*a)
print(“root1=”, r1)
print(“root1=”, r2)

Output

Enter value of a=2


Enter value of b=6
Enter value of c=3
root1= -0.6339745962155614
root1= -2.3660254037844384

Result:

Thus the python program has been executed and output verified successfully.
10. PROGRAM USING RANDOM & STATISTICS MODULE

Write a program that generates six random numbers in a sequence created with (start, stop, step).
Then print the mean, median and mode of the generated numbers.

AIM:

To write a python program that generates six random numbers and print the mean,
median and mode of the generated numbers.

SOURCE CODE:
import random

import statistics

start = int(input("Enter Start :-"))

stop = int(input("Enter stop :- "))

step = int(input("Enter step :- "))

a = random.randrange(start,stop,step)

b = random.randrange(start,stop,step)

c = random.randrange(start,stop,step)

d = random.randrange(start,stop,step)

e = random.randrange(start,stop,step)

f = random.randrange(start,stop,step)

print( "Numbers are =",a,b,c,d,e,f )

print("Mean = ", statistics.mean( ( a,b,c,d,e,f ) ) )

print("Mode = ", statistics.mode( ( a,b,c,d,e,f ) ) )

print("Median =", statistics.median( ( a,b,c,d,e,f ) ) )

Result:

Thus the python program has been executed and output verified successfully.

You might also like