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

Term-II Practical Programs

The program generates a random list of 10 numbers and calculates the mean, median, and mode of the list. It imports the random and statistics modules, generates random integers from 50-100 and adds them to a list. It then uses statistics functions like mean(), median(), and mode() to calculate and print the values of the list. The output shows the generated random list and the calculated mean, median, and mode values.

Uploaded by

hacking tech
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Term-II Practical Programs

The program generates a random list of 10 numbers and calculates the mean, median, and mode of the list. It imports the random and statistics modules, generates random integers from 50-100 and adds them to a list. It then uses statistics functions like mean(), median(), and mode() to calculate and print the values of the list. The output shows the generated random list and the calculated mean, median, and mode values.

Uploaded by

hacking tech
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Program: 1 Printing the Second largest element in a list

Question: Write a python program to print the second largest element of the
given list
Aim: To write a python program to print the second largest element of the
given list.
Algorithm:
Step 1: Open the idle
Step 2: Get a list of numbers from the user
Step 3: Sort the elements in ascending order using sort( ) function
Step 4: print the second largest element using the index position [len(list)-2]
Step 5: Stop the process
Coding:
L=eval(input("Enter a list of integers(All elements should be distinct):\n"))
L.sort()
Sec_Large=L[len(L)-2]
print("The sorted list is:",L)
print("The second largest element is:",Sec_Large)
Output:
Enter a list of integers (All elements should be distinct):
[10,20,30,15,18,19]
The sorted list is: [10, 15, 18, 19, 20, 30]
The second largest element is: 20
Result:
The program is executed, and the output is verified

Program No:02 Removing the duplicate elements from a list


Question:
Write a python program to remove the duplicate elements from a list
Aim:
To write a python program to remove the duplicate elements from a list
Algorithm:
Step1: Open the idle.
Step 2: Input a list of integers.
Step 3: Traverse the elements of the list using for loop.
Step 4: Use naive method(append the first occurrence of the elements and ignore
the duplicates) to create a fresh list without duplication.
Step 5: Display the result.
Step 6: Stop the process.
Coding:
val=eval(input("Enter a list of numbers:"))
print ("The original list is : ",val)
res = []
for i in val:
if i not in res:
res.append(i)
print ("The list after removing duplicates : " , res)
Output:
Enter a list of numbers:[10,20,30,20,15,45,20]
The original list is : [10, 20, 30, 20, 15, 45, 20]
The list after removing duplicates : [10, 20, 30, 15, 45]
Result:
The program is executed, and the output is verified

Program No:03 Printing the biggest and smallest number from a list
Question:
Write a python program to print the biggest and smallest number present in a list
Aim:
To write a python program to print the biggest and smallest number present in a
list
Algorithm:
Step 1: Open the idle.
Step 2: Get a list of integers from the user.
Step 3: Sort the elements using sort( ) functions.
Step 4: Print the smallest element from the index position 0 and biggest element
from the index position len(list)-1.
Step 5: Stop the process.
Coding:
L=eval(input("Enter a list of integers(All elements should be distinct):\n"))
L.sort()
small=L[0]
big=L[len(L)-1]
print("The Smallest element is:",small)
print("The biggest element is:",big)
Output:
Enter a list of integers (All elements should be distinct):
[5,3,10,9,7,6,4]
The Smallest element is: 3
The biggest element is: 10
Result:
The program is executed, and the output is verified.

Program No:04 Linear Search


Question: Write a python program to implement linear search mechanism in a
list of integers.
Aim: To write a python program to implement linear search mechanism in a list
of integers.
Algorithm:
Step 1: open the idle
Step 2: Input a list of integers
Step 3: Get the integer to be searched
Step 4: Use for loop and match the searching element with all the elements in the
list one by one.
Step 5: If the elements are similar, copy the position and break the iteration.
Step 6: Print the index position of the element if it is found. Otherwise print a
proper message about the nonavailability of the element.
Step 7: Stop the process.
Coding:
found=0
A=eval(input("Enter a list of integers:"))
B=int(input("Enter the element to be searched:"))
for x in range(len(A)):
if B==A[x]:
found=1
pos=x
break
if found==1:
print("the element is found at the index position:",pos)
else:
print("Sorry, the element is not found....")
Output 1:
Enter a list of integers: [10,20,30,40,50]
Enter the element to be searched:50
the element is found at the position: 4
Output 2:
Enter a list of integers: [10,20,30,40,50,60]
Enter the element to be searched:70
Sorry, the element is not found....
Result:
The program is executed, and the output is verified.

Program No:05 Printing positive and Negative integers of a list


Question:
Write a python program to print the positive and negative numbers stored in it
separately
Aim :
To write a python program to print the positive and negative numbers stored in it
separately
Algorithm:
Step 1: Open the idle.
Step 2: Get a list of integers from the user.
Step 3: Traverse the elements using for loop.
Step 4: Store the element inside positive list if the elements are greater than zero.
Step 5: Store the element inside negative list if the element is less than zero.
Step 6: Display the lists.
Step 7: Stop the process.

Coding:
Li=eval(input("Enter a list of integers:"))
positive=[]
negative=[]
for x in Li:
if x!=0 and x.isdigit():
if x>0:
positive.append(x)
else:
negative.append(x)
print("Positive integers are:",positive)
print("Negative integers are:",negative)

Output:
Enter a list of integers:[1,0,-10,5,-5.8,20]
Positive integers are: [1, 5, 20]
Negative integers are: [-10, -5.8]
Result:
The program is executed and the output is verified

Program No:06 printing the sum of all the elements stored in a tuple
Question:
write a python program to print the sum of all elements stored in a tuple
Aim:
To write a python program to print the sum of all elements stored in a tuple
Algorithm:
Step 1: Open python idle
Step 2: Get a tuple of integers from the user
Step 3: Declare a variable to store the sum value of all elements
Step 4: Use for loop and traverse the elements one by one
Step 5: Add the cumulative sum of each element during each iteration
Step 6: Display the sum value
Step 7: Stop the process
Coding:
T=eval(input("Enter a tuple of numbers:"))
sum=0
for x in T:
sum+=x
print("Sum of all elements :",sum)

Output:
Enter a tuple of numbers:(10,15,20,25,30)
Sum of all elements : 100
Result:
The program is executed, and the output is verified.

Program No:07 Printing the odd and even values from a tuple
Question:
Write a python program to print the odd and even values stored in a tuple
separately
Aim:
To write a python program to print the odd and even values stored in a tuple
separately
Algorithm:
Step 1: Open python idle
Step 2: Get a tuple of numbers from the user
Step 3: Declare two lists to store the odd and values separately
Step 4: Traverse the tuple elements using for loop
Step 5: Divide the elements by 2 and check whether the remainder is zero or
not.
a) If the remainder is zero, append the element with even list
b) Otherwise append the element with odd list
Step 6: Repeat step 5 for all elements
Step 7: Display the result and stop the process.
Coding:
T=eval(input("Enter a tuple of integers except 0(zero):"))
odd=[]
even=[]
for x in T:
if x%2==0:
even.append(x)
else:
odd.append(x)
print("Odd elements are:",odd)
print("Even elements are:",even)
Output:
Enter a tuple of integers except 0(zero):(3,5,7,9,12,14,16,20)
Odd elements are: [3, 5, 7, 9]
Even elements are: [12, 14, 16, 20]
Result:
The program is executed, and the output is verified.

Program No:08
Updating a dictionary
Question:
Write a python program to update the value of a dictionary. Obtain the key
from the user to update the value.
Aim:
To write a python program to input the key and update its value stored in a
dictionary
Algorithm:
Step 1: Open python IDLE.
Step 2: Get the number pairs to be stored in a dictionary.
Step 3: Use for loop to get the pairs and store the same.
Step 4: Get the key as input and change its value.
Step 5: Change the value using its key position.
Step 6: Display the result.
Step 7: End the program.
Coding:
x=int(input("Enter the number of pairs in dictionary:"))
d={ }
for i in range(x):
key=eval(input("Enter the key:"))
value=eval(input("Enter the value:"))
d[key]=value
print("original dictionary is:",d)

k=eval(input("Enter the key whose value you want to change:"))


value2=eval(input("Enter the new value:"))
d[k]=value2
print("Updated dictionary:",d)

Output:
Enter the number of pairs in dictionary:3
Enter the key:1
Enter the value:"Chennai"
Enter the key:2
Enter the value:"Covai"
Enter the key:3
Enter the value:"Madhurai"
original dictionary is: {1: 'Chennai', 2: 'Covai', 3: 'Madhurai'}
Enter the key whose value you want to change:2
Enter the new value:"Coimbatore"
Updated dictionary: {1: 'Chennai', 2: 'Coimbatore', 3: 'Madhurai'}
Result:
The Program is executed, and the output is verified.

Program No:09
Calculating Mean, Median , Mode
Question:
Write a python program to generate a list of 10 numbers using random
function and print its mean, median, mode by importing statistics module
Aim:
To write a python program to generate a list of 10 numbers using random
function and print its mean, median, mode by importing statistics module
Algorithm:
Step 1: Open python idle
Step 2: Import the random and statistics modules
Step 3: Generate 10 random integers using random.randint( )
Step 4: Append the integers to a list
Step 5: Now, use the mean, median and mode functions to print the values
Step 6: Stop the process.
Coding:
import random
import statistics

L1=[ ]
for x in range(11):
L=random.randint(50,100)
L1.append(L)
print("The list is:",L1)
print("Mean values is:",statistics.mean(L1))
print("Median value is:",statistics.median(L1))
print("Mode value is:",statistics.mode(L1))
Output:
The list is: [56, 75, 86, 63, 94, 65, 75, 88, 98, 69, 74]
Mean values is: 76.63636363636364
Median value is: 75
Mode value is: 75
(Note: Mode will be displayed only when the random function generates the
same number at least two times)
Result:
The program is executed, and the output is verified

Program No:10
Counting the frequency of characters in a string using dictionary
Question:
Write a python program to print the number of times a character appears
in a string using dictionary
Aim:
To Write a python program to print the number of times a character appears in
a string using dictionary
Algorithm:
Step 1: open python idle
Step 2: Input a string
Step 3: Create an empty dictionary
Step 4: Count the frequency of each character iteratively using naive method.
Step 5: Display the count
Step 6: Stop the process
Coding:
test_str = input("Enter any word:")
all_freq = {}

for i in test_str:
if i in all_freq:
all_freq[i] += 1
else:
all_freq[i] = 1
print ("Count of all characters is :\n " + str(all_freq))
Output:
Enter any word: GEDEE PUBLIC SCHOOL
Count of all characters is:
{'G': 1, 'E': 3, 'D': 1, ' ': 2, 'P': 1, 'U': 1, 'B': 1, 'L': 2, 'I': 1, 'C': 2, 'S': 1, 'H': 1, 'O': 2}
Result:
The program is executed, and the output is verified.

You might also like