ujjwal cs
ujjwal cs
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
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*
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
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
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
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
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
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
Output:
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
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
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
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
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
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.
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
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}
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]
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
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
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)
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)
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)
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.
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.
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)
Output:
Enter name of the student: Prakash
Name of the student: Prakash
Total marks: 222
Average marks: 74.0
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
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}
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