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

ujjwal cs

The document outlines various Python programming exercises, each with a specific aim, source code, output, and conclusion indicating successful execution. The exercises cover topics such as string manipulation, list operations, and pattern generation. Each section provides a clear example of input and expected output for the corresponding code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

ujjwal cs

The document outlines various Python programming exercises, each with a specific aim, source code, output, and conclusion indicating successful execution. The exercises cover topics such as string manipulation, list operations, and pattern generation. Each section provides a clear example of input and expected output for the corresponding code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 59

1.Aim: Write a python code to input a word.

Display each
letter of word one by one.

Source code:
Str=input(“Enter a word :”)
p=len(str)
for i in range(0,p):
print(str[i])
Output: C
A
T

Conclusion: Program was executed successfully.


2.Aim: Write a python code to input a string and display all
the vowels present in it .

Source code:
vowels= ’AEIOUaeiou’
a=input(“Enter a word :”)
p=len(a)
print(“Vowels in the string are :”)
for i in range(0,p):
if a[i] in vowels:
print(a[i])
Output:
Enter a word: Computer
Vowels in the string are :
o
u
Conclusion: Program was executed successfully.
3.Aim: Write a python code to input a string. Replace the
letter ‘E’ or ‘e’ with ‘*’ in the given string. Display the new
string.

Date:1-11-2024

Source code:
str=input(“Enter a string :”)
print(“The new string :”)
p=len(str)
for i in range(0,p):
if(str[i]==’E’ or str[i]==’e’):
print(‘*’,end=” “)
else:
print(str[i],end=” “)
Output:
Enter a string: English Literature
The new string:
*nglish Lit*ratur*

Conclusion: Program was executed successfully.


4.Aim: Write a python code to input a string and change the
case of each letter (uppercase to lowercase and vice-versa).
Display the new string.
Source Code:
str1= ‘ ’
str=input(“Enter a string”)
p=len(str)
for i in range(0,p):
chr=str[i]
if(chr>=’a’ and chr<=’z’):
chr1=chr.upper()
str1=str1+chr1
elif(chr>=’A’ and chr<=’Z’):
chr1=chr.lower()
str1=str1+chr1
else:
str1=str1+chr
print(“The new string :”,str1)
Output:
Enter a string: Welcome To python LanGuAGe
The new string: wELcOME to PYTHON LANgUagE
Conclusion: Program was executed successfully.
5.Aim: Write a python code to input a string and display:
(i) the number of lowercase letters.
(ii) the number of uppercase letters.
(iii) the number of digits.
(iv) the number of special characters.
Date:1-11-2024
Source Code:
lc=up=d=sp=0
str=input(“Enter a string : “)
p=len(str)
for i in range(0,p):
chr=str[i]
if(chr>=’a’ and chr<=’z’):
lc=lc+1
elif(chr>=’A’ and chr<=’Z’):
up=up+1
elif(chr>=’0’ and chr<=’9’):
d=d+1
else:
sp=sp+1
print(“The number of lowercase letters :”,lc)
print(“The number of uppercase letters :”,up)
print(“The number of digits :”,d)
print(“The number of special characters :”,sp)
Output:
Enter a string : New Delhi : 110005
The number of lowercase letters : 6
The number of uppercase letters : 2
The number of digits : 6
The number of special characters : 4
Conclusion: Program was executed successfully.
6.Aim: Write a python code to input a string and check whether it is a
palindrome string or not.(A word is said to be a palindrome, if it
reads the same after reversing the letters)
Source Code:
wd=input(“Enter a word in uppercase : ”)
if(wd[: :-1]==wd):
print(“Reversed Word :”,wd[: :-1])
print(“Original Word :”,wd)
print(“A palindrome Word”)
else:
print(“Reversed Word :”,wd[: :-1])
print(“Original Word :”,wd)
print(“A palindrome Word”)
Output:
Enter a word in uppercase : PYTHON
Reversed Word : NOHTYP
Original Word : PYTHON
Not a palindrome Word
>>>>>>>>>>>>>>>
Enter a word in uppercase : PYTHON
Reversed Word : NOHTYP
Original Word : PYTHON
A palindrome Word
Conclusion: Program was executed successfully.
7.Aim: Write a python code to input a word in uppercase and
display the same in piglatin form.

Source Code:
vowels=”AEIOU”
wd=input(“Enter a word in uppercase : “)
p=len(wd)
k=0
For i in range(0,p):
If(wd[i]in vowels);
k=i
Break
print(“Piglatin from of “,wd,’”:”)
print(wd[k:p+1]+wd[0:k]+’AY’)

Output:
Enter a word in uppercase :TROUBLE
Piglatin form of TROUBLE :
OUBLETRAY

Conclusion: Program was executed successfully.


8.Aim: Write a python code to input a string, convert it into
uppercase and display the string in alphabetical order of its
letters.

Source Code :
wd=input(“Enter a word in uppercase : “)
print(“Dictionary Order :”)
wd=wd.upper()
p=len(wd)
for i in range(65,91):
for j in range(0,p):
ch=wd[j]
if(ch==ch(i)or ch==chr(i+32)):
print(ch, end=”)

Output:
Enter a word in uppercase : COMPUTER
Dictionary Order :
CEMOPRTU

Conclusion: Program was executed successfully.


9.Aim: Write a python code to input a sentence in the
lowercase. Convert the first letter of each word of the
sentence in uppercase and display the new sentence.

Source Code :
str1=’ ‘,a=0
str=input(“Enter a sentence in lowercase : “)
print(“The sentence after converting each letter of the word
in uppercase :”)
str=’ ‘+str
p=len(str)
while(a<p):
ch=str[a]
if(ch==’ ‘):
ch1=str[a+1]
str1=str1+’ ‘+ch1.upper( )
a=a+2
else:
str1=str1+ch
a=a+1
print(str1)
Output:
Enter a sentence in lowercase : we are learning python
language
The sentence after converting each letter of the word in
uppercase :
We Are Learning Python Language
Conclusion: Program was executed successfully.
10.Aim: Write a python code to input a sentence. Enter a
word separately to find the frequency of the given word in
the sentence. Finally, display the frequency of the word.

Source Code :
str=input(“Enter a sentence :”)
wd=input(“Enter a word to be searched in sentence : “)
p=len(wd)
ps=len(wd)
st=c=0
end=p
while True:
pt=str.find(wd,st,end)
if(pt!=-1):
c=c+1
st=pt+ps
else:
break
print(“Frequency of word”, wd, ‘=’,c)

Output:
Enter a sentence: Bring all the papers which are kept on the
table.
Enter a word to search: the
Frequency of word 'the' = 2

Conclusion: Program was executed successfully.


11. Aim: Write a Python program to display the given pattern.
Date: 31-01-2025
Pattern:
A
AB
ABC
ABCD
ABCDE

Source Code:
# A program to display the given pattern
print("The Pattern:")
for i in range(1, 6):
for j in range(65, 65 + i): # ASCII value of 'A' is 65
print(chr(j), end="")
print()

Output:
The Pattern:
A
AB
ABC
ABCD
ABCDE

Conclusion: Program was executed successful


12.Aim: Write a Python program to display the given word
pattern.

Date: 31-01-2025

Pattern :
P
PY
PYT
PYTH
PYTHO
PYTHON
Source Code:
word = input("Enter a word: ")
print("The Pattern:")
for i in range(1, len(word) + 1):
print(word[:i])

Output:
Enter a word: PYTHON
The Pattern:
P
PY
PYT
PYTH
PYTHO
PYTHON

Conclusion: Program was executed successfully.


13.Aim: Write a Python program to accept a set of integer
numbers (including negative and positive numbers) in a list.
Find and display the following:
(a) Sum of negative numbers
(b) Sum of positive even numbers
(c) Sum of positive odd numbers

Date: 01-02-2025

Source Code:
L1 = eval(input("Enter list elements: "))
print("The list of integers:", L1)
sn = sevn = sodd = 0
for a in L1:
if a < 0:
sn += a
elif a % 2 == 0:
sevn += a
else:
sodd += a
print("Sum of negative numbers:", sn)
print("Sum of positive even numbers:", sevn)
print("Sum of positive odd numbers:", sodd)
Output:
Enter list elements: [-11, 10, 15, 3, -14, 12, 23]
The list of integers: [-11, 10, 15, 3, -14, 12, 23]
Sum of negative numbers: -25
Sum of positive even numbers: 22
Sum of positive odd numbers: 41

Conclusion: Program was executed successfully.


14.Aim: Write a Python program to accept ten integer numbers in a
list. Find and display the sum of the first five integers and the product
of the next five integers.
Source Code:

L1 = eval(input("Enter list elements: "))


print("The list of integers:", L1)
sum_part = sum(L1[:5])
prod_part = 1
for num in L1[5:]:
prod_part *= num
print("Sum of first five integers:", sum_part)
print("Product of next five integers:", prod_part)

Output:

Enter list elements: [2, 4, 6, 8, 10, 3, 5, 7, 9, 11]


The list of integers: [2, 4, 6, 8, 10, 3, 5, 7, 9, 11]
Sum of first five integers: 30
Product of next five integers: 103

Conclusion: Program was executed successfully.


15.Aim: Write a Python program to enter a set of numbers in
a list. Find and display the maximum and minimum numbers
among them.

Date: 01-02-2025

Source Code:
L1 = eval(input("Enter list elements: "))
print("The list of integers:", L1)
print("Maximum of the numbers:", max(L1))
print("Minimum of the numbers:", min(L1))

Output:

Enter list elements: [10, 31, 45, 62, 87, 99, 18, 54, 38]
The list of integers: [10, 31, 45, 62, 87, 99, 18, 54, 38]
Maximum of the numbers: 99
Minimum of the numbers: 10

Conclusion: Program was executed successfully.


16.Aim: Write a Python program to input integer numbers in
a list and display the numbers that are Buzz numbers.[Hint: A
Buzz number is divisible by 7 or ends with the digit 7].

Date: 01-02-2025

Source Code:
L1 = eval(input("Enter integer numbers: "))
print("The buzz numbers are:")
for a in L1:
if a % 7 == 0 or str(a).endswith("7"):
print(a)

Output:
Enter integer numbers: [37, 49, 35, 10, 14, 67]
The buzz numbers are:
37
49
35

Conclusion: Program was executed successfully.


17.Aim: Write a Python code to accept ten integer numbers
in a list. Find and display the sum of the first five integers and
the product of the next five integers.

Date: 01-02-2025

Source Code:
L1 = eval(input("Enter list elements: "))
print("The list of integers:", L1)
sum_first_five = sum(L1[:5])
product_next_five = 1
for num in L1[5:]:
product_next_five *= num
print("Sum of first five integers:", sum_first_five)
print("Product of next five integers:", product_next_five)

Output:
Enter list elements: [8, 12, 15, 10, 20, 17, 2, 3, 4, 5]
The list of integers: [8, 12, 15, 10, 20, 17, 2, 3, 4, 5]
Sum of first five integers: 65
Product of next five integers: 2040
Conclusion: Program was executed successfully.
18.Aim: Write a Python code to enter a set of numbers in a
list. Find and display the maximum and minimum numbers
among them.

Date: 01-02-2025

Source Code:
L1 = eval(input("Enter list elements: "))
print("The list of integers:", L1)
max_num = max(L1)
min_num = min(L1)
print("Maximum of the numbers:", max_num)
print("Minimum of the numbers:", min_num)

Output:
Enter list elements: [10, 31, 45, 62, 87, 99, 18, 54, 38]
The list of integers: [10, 31, 45, 62, 87, 99, 18, 54, 38]
Maximum of the numbers: 99
Minimum of the numbers: 10

Conclusion: Program was executed successfully.


19.Aim: Write a Python code to input integer numbers in a
list. Display those numbers which are Buzz numbers.

Date: 01-02-2025

Source Code:
L1 = eval(input("Enter integer numbers: "))
print("The buzz numbers are:")
for a in L1:
if a % 7 == 0 or str(a).endswith('7'):
print(a)

Output:
Enter integer numbers: [37, 49, 35, 10, 47]
The buzz numbers are:
37
49
35

Conclusion: Program was executed successfully.


20.Aim: Write a Python code to accept the integer numbers
in two lists (say, L1 and L2). Find and display the frequency of
each element of list L1 in L2.

Date: 01-02-2025

Source Code:
L1 = eval(input("Enter the numbers in first list: "))
L2 = eval(input("Enter the numbers in second list: "))
for a in L1:
print("Frequency of", a, "in the second list is", L2.count(a))

Output:
Enter the numbers in first list: [12, 18, 15]
Enter the numbers in second list: [12, 18, 15, 15, 12, 15, 18,
20, 10, 18, 15]
Frequency of 12 in the second list is 2
Frequency of 18 in the second list is 3
Frequency of 15 in the second list is 4

Conclusion: Program was executed successfully.


21.Aim: Write a Python code to accept an integer number in
a list and display the largest prime number in the list.

Date: 01-02-2025

Source Code:
def is_prime(n):
if n == 1:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True
L1 = eval(input("Enter integer numbers:"))
max_prime = -1
for num in L1:
if is_prime(num) and num > max_prime:
max_prime = num
if max_prime == -1:
print("No prime numbers found")
else:
print("Largest prime number is:", max_prime)
Output:
Enter integer numbers: [12, 15, 7, 11, 17, 19, 23]
Largest prime number is: 23
Conclusion: Program was executed successfully.
22.Aim: Write a Python code to accept a string and then print
the number of vowels, consonants, digits, and spaces in the
string.

Date: 01-02-2025

Source Code:
str1 = input("Enter a string: ")
vowels = "AEIOUaeiou"
digits = "0123456789"
vowel_count = consonant_count = digit_count = space_count
=0
for char in str1:
if char in vowels:
vowel_count += 1
elif char.isalpha():
consonant_count += 1
elif char in digits:
digit_count += 1
elif char == " ":
space_count += 1

print("Vowels:", vowel_count)
print("Consonants:", consonant_count)
print("Digits:", digit_count)
print("Spaces:", space_count)
Output:
Enter a string: Hello World 123
Vowels: 3
Consonants: 7
Digits: 3
Spaces: 2
Conclusion: Program was executed successfully.
23.Aim: Write a Python code to accept a list of integers.
Count how many prime numbers are there in the list.

Source Code:
def is_prime(n):
if n == 1:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True
L1 = eval(input("Enter integer numbers:"))
prime_count = 0
for num in L1:
if is_prime(num):
prime_count += 1
print("Number of prime numbers in the list:", prime_count)

Output:
Enter integer numbers: [5, 10, 11, 18, 13, 19]
Number of prime numbers in the list: 4
Conclusion: Program was executed successfully.
24.Aim: Write a Python code to accept a string and convert all
vowels to uppercase and all consonants to lowercase.

Date: 01-02-2025

Source Code:
str1 = input("Enter a string: ")
vowels = "AEIOUaeiou"
result = ""
for char in str1:
if char in vowels:
result += char.upper()
else:
result += char.lower()
print("Modified string:", result)

Output:
Enter a string: Hello World
Modified string: hEllO wOrld
Conclusion: Program was executed successfully.
25.Aim: Write a Python code to accept a number and print
whether it is a perfect number or not.
Hint: A perfect number is a number that is equal to the sum
of its proper divisors (excluding the number itself).

Source Code:
def is_perfect(n):
sum_of_divisors = 0
for i in range(1, n):
if n % i == 0:
sum_of_divisors += i
return sum_of_divisors == n
num = int(input("Enter a number:"))
if is_perfect(num):
print(num, "is a perfect number.")
else:
print(num, "is not a perfect number.")

Output:
Enter a number: 28
28 is a perfect number.
Conclusion: Program was executed successfully.
26.Aim: Write a Python code to accept a list of numbers and
display the second smallest number in the list.

Source Code:
def second_smallest(numbers):
unique_numbers = list(set(numbers)) # Remove duplicates
unique_numbers.sort() # Sort the numbers
return unique_numbers[1] if len(unique_numbers) > 1 else
None
numbers = eval(input("Enter a list of numbers: "))
result = second_smallest(numbers)
if result is not None:
print("The second smallest number is:", result)
else:
print("There is no second smallest number.")

Output:
Enter a list of numbers: [4, 7, 2, 5, 9, 3, 2]
The second smallest number is: 3
Conclusion: Program was executed successfully.
27.Aim: Write a Python code to check whether a number is
an Armstrong number.
Hint: An Armstrong number is a number that is equal to the
sum of its digits raised to the power of the number of digits.

Source Code:
def is_armstrong(num):
digits = str(num)
power = len(digits)
sum_of_powers = sum(int(digit) ** power for digit in digits)
return sum_of_powers == num
num = int(input("Enter a number:"))
if is_armstrong(num):
print(num, "is an Armstrong number.")
else:
print(num, "is not an Armstrong number.")

Output:
Enter a number: 153
153 is an Armstrong number.
Conclusion: Program was executed successfully.
28.Aim: Write a Python code to accept a list of strings and
sort them in ascending order based on their length.

Source Code:
str_list = input("Enter a list of strings: ").split(", ")
sorted_list = sorted(str_list, key=len)
print("Strings sorted by length:", sorted_list)

Output:
Enter a list of strings: apple, banana, pear, cherry, kiwi
Strings sorted by length: ['kiwi', 'pear', 'apple', 'cherry',
'banana']
Conclusion: Program was executed successfully.

75.Aim: Write a Python code to accept two lists and return a


list with common elements between the two.
Date: 01-02-2025
Source Code:
list1 = eval(input("Enter the first list: "))
list2 = eval(input("Enter the second list: "))
common_elements = list(set(list1) & set(list2))
print("Common elements:", common_elements)
Output:
Enter the first list: [1, 2, 3, 4, 5]
Enter the second list: [4, 5, 6, 7]
Common elements: [4, 5]
Conclusion: Program was executed successfully.
29.Aim: Write a Python code to reverse a string without using
the built-in reverse function.

Date: 01-02-2025

Source Code:
str1 = input("Enter a string: ")
reversed_str = ""
for char in str1:
reversed_str = char + reversed_str
print("Reversed string:", reversed_str)

Output:
Enter a string: Hello
Reversed string: olleH

Conclusion: Program was executed successfully.


30.Aim: Write a Python code to find the intersection of two
sets.

Date: 01-02-2025

Source Code:
set1 = eval(input("Enter the first set: "))
set2 = eval(input("Enter the second set: "))
intersection = set1 & set2
print("The intersection of the sets is:", intersection)
Output:
Enter the first set: {1, 2, 3, 4}
Enter the second set: {3, 4, 5, 6}
The intersection of the sets is: {3, 4}

Conclusion: Program was executed successfully.


31. Aim: Write a Python code to accept a set of integers in a
tuple. Find and display the following:
(a) Sum of integers available at even indices.
(b) Sum of integers available at odd indices.
Source Code:
tpl = eval(input("Enter a set of integers enclosed within (): "))
p = len(tpl)
sum1 = 0
sum2 = 0
for i in range(p):
if i % 2 == 0:
sum1 = sum1 + tpl[i]
else:
sum2 = sum2 + tpl[i]
print("Sum of the elements at even indices:", sum1)
print("Sum of the elements at odd indices:", sum2)
Output:
Enter a set of integers enclosed within (): (10, 20, 30, 40, 50)
Sum of the elements at even indices: 90
Sum of the elements at odd indices: 60

Conclusion: Program was executed successfully.


32.Aim: Write a Python code to accept ten integer numbers
in a tuple. Find and display the maximum of the even
numbers and the minimum of the odd numbers.

Date: 06-02-2025

Source Code:
tpl = eval(input("Enter a set of integers enclosed within (): "))
p = len(tpl)
max_even = -99999
min_odd = 99999
for i in range(p):
if tpl[i] % 2 == 0 and tpl[i] > max_even:
max_even = tpl[i]
if tpl[i] % 2 != 0 and tpl[i] < min_odd:
min_odd = tpl[i]

print("Maximum of even numbers:", max_even)


print("Minimum of odd numbers:", min_odd)

Output:
Enter a set of integers enclosed within (): (24, 11, 45, 56, 92,
88, 17, 27)
Maximum of even numbers: 92
Minimum of odd numbers: 11

Conclusion: Program was executed successfully.


33.Aim: Write a Python code to accept the marks secured in
Physics, Chemistry, and Mathematics of the students of a
class in three different tuples, respectively. Find and display
the following:
(i) Number of students getting 80% and above in aggregate.
(ii) Number of students getting 35% and below in aggregate.

Date: 06-02-2025

Source Code:
phy = eval(input("Enter marks in physics: "))
chem = eval(input("Enter marks in chemistry: "))
maths = eval(input("Enter marks in maths: "))
p = len(phy)
c1 = 0
c2 = 0
for i in range(p):
avg = (phy[i] + chem[i] + maths[i]) / 3
if avg >= 80:
c1 = c1 + 1
if avg <= 35:
c2 = c2 + 1
print("Number of students getting 80% and above:", c1)
print("Number of students getting 35% and below:", c2)

Output:
Enter marks in physics: (45, 36, 55, 42, 96, 89)
Enter marks in chemistry: (31, 88, 62, 28, 87, 86)
Enter marks in maths: (30, 97, 58, 30, 90, 89)
Number of students getting 80% and above: 3
Number of students getting 35% and below: 1

Conclusion: Program was executed successfully.


34.Aim: Write a Python code to accept a set of ten integers in
a tuple and swap the first five values with the last five values.

Date: 06-02-2025

Source Code:
tpl = eval(input("Enter a set of ten integers enclosed within ():
"))
tpl = tpl[5:] + tpl[:5]
print("Tuple after swapping:", tpl)

Output:
Enter a set of ten integers enclosed within (): (1, 2, 3, 4, 5, 6,
7, 8, 9, 10)
Tuple after swapping: (6, 7, 8, 9, 10, 1, 2, 3, 4, 5)

Conclusion: Program was executed successfully.


35.Aim: Write a Python code to accept a set of ten integers in
a tuple. Rotate the elements of the tuple in a circular manner
towards the right by two places.

Date: 06-02-2025

Source Code:
tpl = eval(input("Enter a set of ten integers enclosed within ():
"))
tpl = tpl[-2:] + tpl[:-2]
print("Tuple after right rotation by 2 places:", tpl)

Output:
Enter a set of ten integers enclosed within (): (10, 20, 30, 40,
50, 60, 70, 80, 90, 100)
Tuple after right rotation by 2 places: (90, 100, 10, 20, 30, 40,
50, 60, 70, 80)

Conclusion: Program was executed successfully.


36.Aim: Write a Python code to accept a set of ten integers in
a tuple and display them in reverse order.

Date: 06-02-2025

Source Code:
tpl = eval(input("Enter a set of ten integers enclosed within ():
"))
tpl = tpl[::-1]
print("Tuple in reverse order:", tpl)

Output:
Enter a set of ten integers enclosed within (): (5, 10, 15, 20,
25, 30, 35, 40, 45, 50)
Tuple in reverse order: (50, 45, 40, 35, 30, 25, 20, 15, 10, 5)

Conclusion: Program was executed successfully.


37.Aim: Write a Python code to accept a set of ten integers in
a tuple and sort them in ascending order.

Date: 06-02-2025

Source Code:
tpl = eval(input("Enter a set of ten integers enclosed within ():
"))
tpl = tuple(sorted(tpl))
print("Tuple in ascending order:", tpl)

Output:
Enter a set of ten integers enclosed within (): (45, 10, 90, 60,
30, 15, 70, 80, 25, 50)
Tuple in ascending order: (10, 15, 25, 30, 45, 50, 60, 70, 80,
90)
Conclusion: Program was executed successfully.
38.Aim: Write a Python code to accept a tuple of integers and
a number. Find and display the number of times the given
number is available in the tuple.

Date: 06-02-2025

Source Code:
tpl = eval(input("Enter a tuple of integers enclosed within ():
"))
num = int(input("Enter the number to count: "))
count = tpl.count(num)
print("The number", num, "occurs", count, "times in the
tuple.")

Output:
Enter a tuple of integers enclosed within (): (5, 10, 15, 10, 20,
10, 30, 10, 40, 50)
Enter the number to count: 10
The number 10 occurs 4 times in the tuple.

Conclusion: Program was executed successfully.


39.Aim: Write a Python code to accept a tuple of integers and
two numbers, num1 and num2. Find and display whether
num1 is available before num2 in the tuple.

Date: 06-02-2025

Source Code:
tpl = eval(input("Enter a tuple of integers enclosed within ():
"))
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
if tpl.index(num1) < tpl.index(num2):
print(num1, "occurs before", num2, "in the tuple.")
else:
print(num1, "does not occur before", num2, "in the tuple.")
Output:
Enter a tuple of integers enclosed within (): (5, 10, 15, 20, 25,
30, 35, 40, 45, 50)
Enter the first number: 15
Enter the second number: 40
15 occurs before 40 in the tuple.

Conclusion: Program was executed successfully.


40.Aim: Write a Python code to accept a tuple of integers and
replace all occurrences of a given number with another
number.

Date: 06-02-2025

Source Code:
tpl = eval(input("Enter a tuple of integers enclosed within ():
"))
old = int(input("Enter the number to replace:"))
new = int(input("Enter the new number: "))
tpl = tuple(new if x == old else x for x in tpl)
print("Tuple after replacement:", tpl)

Output:
Enter a tuple of integers enclosed within (): (10, 20, 30, 10,
40, 50, 10, 60, 10, 70)
Enter the number to replace: 10
Enter the new number: 99
Tuple after replacement: (99, 20, 30, 99, 40, 50, 99, 60, 99,
70)

Conclusion: Program was executed successfully.


41. Aim: Write a Python code to accept the name of a student
and create a dictionary by assigning marks secured by
him/her in English, Maths and Science subjects. Display the
name of the student along with the total marks and average
marks
Source Code:
name = input("Enter name of the student: ")
marks = {"Eng": 65, "Maths": 82, "Science": 75}
sum = 0
for key in marks:
sum = sum + marks[key]
avg = sum / 3
print("Name of the student:", name)
print("Total marks:", sum)
print("Average marks:", avg)

Output:
Enter name of the student: Prakash
Name of the student: Prakash
Total marks: 222
Average marks: 74.0

Conclusion: Program was executed successfully.


42. Aim: Write a Python code to create a dictionary
containing four products with the names of the products and
their sales as values. Display the dictionary and the product
with the highest sale.

Date: 06-02-2025

Source Code:
n = int(input("Enter number of products: "))
Prod_Sale = {}
for a in range(n):
key = input("Enter Product's name: ")
value = int(input("Enter Sale: "))
Prod_Sale[key] = value
print("The dictionary")
print(Prod_Sale)
max = 0
for b in Prod_Sale:
if Prod_Sale[b] > max:
max = Prod_Sale[b]
prod = b
print(prod, "has the highest sale as:", max)
Output:
Enter number of products: 3
Enter Product's name: Product1
Enter Sale: 5000
Enter Product's name: Product2
Enter Sale: 3000
Enter Product's name: Product3
Enter Sale: 8000
The dictionary
{'Product1': 5000, 'Product2': 3000, 'Product3': 8000}
Product3 has the highest sale as: 8000

Conclusion: Program was executed successfully.


43.Aim: Write a Python code to create a dictionary containing
the names of the students as keys and the marks secured by
them in computer science as the values. Display the names
and marks of the students who have secured 80 and above.
Also display the number of students securing 80 marks and
above.

Date: 06-02-2025

Source Code:
n = int(input("Enter number of students: "))
count = 0
Name_Marks = {}
for a in range(n):
key = input("Enter name: ")
value = int(input("Enter marks: "))
Name_Marks[key] = value
print("The dictionary")
print(Name_Marks)
print("Students getting marks 80 and above:")
for b in Name_Marks:
if Name_Marks[b] >= 80:
print("Name:", b, "Marks:", Name_Marks[b])
count = count + 1
print("Number of students getting marks 80 and above:",
count)

Output:
Enter number of students: 3
Enter name: Sidharth
Enter marks: 85
Enter name: Samas
Enter marks: 60
Enter name: Prakash
Enter marks: 82
The dictionary
{'Sidharth': 85, 'Samas': 60, 'Prakash': 82}
Students getting marks 80 and above:
Name: Sidharth Marks: 85
Name: Prakash Marks: 82
Number of students getting marks 80 and above: 2
Conclusion: Program was executed successfully.
44.Aim: Write a Python code to assign three dictionaries,
each containing name, age, and salary of an employee. The
company has decided to increase the salary by 20% for the
employees whose age is 55 years and above. Display the
original and updated dictionaries.

Date: 06-02-2025
Source Code:
emp1 = {"Name": "Piyush", "Age": 48, "Salary": 35000.0}
emp2 = {"Name": "Dinesh", "Age": 56, "Salary": 42000.0}
emp3 = {"Name": "Gaurav", "Age": 58, "Salary": 45000.0}
employee = {"E1": emp1, "E2": emp2, "E3": emp3}
print("Original Dictionary")
print(emp1)
print(emp2)
print(emp3)
print("Updated Dictionary")
for x in employee:
if employee[x]["Age"] >= 55:
employee[x]["Salary"] = employee[x]["Salary"] +
employee[x]["Salary"] * 20 / 100
print(emp1)
print(emp2)
print(emp3)

Output:
Original Dictionary
{'Name': 'Piyush', 'Age': 48, 'Salary': 35000.0}
{'Name': 'Dinesh', 'Age': 56, 'Salary': 42000.0}
{'Name': 'Gaurav', 'Age': 58, 'Salary': 45000.0}
Updated Dictionary
{'Name': 'Piyush', 'Age': 48, 'Salary': 35000.0}
{'Name': 'Dinesh', 'Age': 56, 'Salary': 50400.0}
{'Name': 'Gaurav', 'Age': 58, 'Salary': 54000.0}

Conclusion: Program was executed successfully.


45.Aim: Write a Python code to assign two dictionaries, one
containing states' names and the other containing the
corresponding capitals' names. Now, enter a state name and
search for it in the dictionary of states' names. If found, then
display the state name along with its capital name otherwise,
display "State name not found".

Date: 06-02-2025

Source Code:
states = {1: "Uttar Pradesh", 2: "Madhya Pradesh", 3:
"Himachal Pradesh", 4: "Rajasthan"}
caps = {1: "Lucknow", 2: "Bhopal", 3: "Shimla", 4: "Jaipur"}
flag = False
print("Dictionary containing states' names:")
print(states)
print("Dictionary containing capitals' names:")
print(caps)
stat = input("Enter state's name to search: ")
for x in states:
if states[x] == stat:
print("State name:", states[x], "Capital:", caps[x])
flag = True
break
if not flag:
print("State name not found")
Output:
Dictionary containing states' names:
{1: 'Uttar Pradesh', 2: 'Madhya Pradesh', 3: 'Himachal
Pradesh', 4: 'Rajasthan'}
Dictionary containing capitals' names:
{1: 'Lucknow', 2: 'Bhopal', 3: 'Shimla', 4: 'Jaipur'}
Enter state's name to search: Rajasthan
State name: Rajasthan Capital: Jaipur

Conclusion: Program was executed successfully.

You might also like