Term-II Practical Programs
Term-II Practical Programs
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: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.
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)
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.