PYTHON EXP 01
PYTHON EXP 01
1.Create an output dictionary which contains only the odd numbers that are present in the
input list as keys{} and their cubes as there values.
Program:
v = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
d = {}
for i in v:
if i % 2 != 0:
d[i] = i**3
print(d)
Output:
{1: 1, 3: 27, 5: 125, 7: 343, 9: 729}
2. To take dictionary from the keyboard and print the sum of values.
Program:
v = input("Enter a dictionary (in the format {'key': value}): ")
d = eval(v)
total = sum(d.values())
print("Sum of the values:", total)
Output:
Enter a dictionary (in the format {'key': value}): {'A':10, 'B':50, 'C':30}
Sum of the values: 90
3.Find the number of occurence of each letters present in the given string.
Program:
string = input("Enter a string: ")
count = {}
for letter in string:
if letter in count:
count[letter] += 1
else:
count[letter] = 1
for letter, count in count.items():
print(f"'{letter}': {count}")
Output:
Enter a string: varun gurav
'v': 2
'a': 2
'r': 2
'u': 2
'n': 1
Program:
input_string = input("Enter a string: ")
vowel_count = 0
vowels = "aeiouAEIOU"
for letter in input_string:
if letter in vowels:
vowel_count += 1
print(f"Number of vowels in the string: {vowel_count}")
Output:
Enter a string: Varun Gurav
Number of vowels in the string: 4
Program:
m = input("Enter your name:")
print(f"Hii, Good Morning {m}. Hope you have a Wonderful day.")
Output:
Enter your name:Varun
Hii, Good Morning Varun. Hope you have a Wonderful day.
6.Accept students name and marks from the keyboard and create a dictionary. also
display students marks by taking student name as input
Program:
s = input("Enter student name and marks: ")
v = eval(s)
m =input("Enter the name of the student to get the marks: ")
for x in v:
x=v.get(m)
print(f"The marks of student are {x}")
Output:
Enter student name and marks(in the format {'key': value}): {'Varun':78, 'Karan':76, 'Pratik':87}
Enter the name of the student to get the marks: karan
The marks of student are 76
SETS
Program:
list1 =[10,20,40,10,50,20,60,40,30]
m=set(list1)
print(list(m))
Output:
[40, 10, 50, 20, 60, 30]
Program:
word = input("Enter a word: ")
m = list(word)
vowels=[]
for x in m:
if x == "a" or x== "e" or x== "i" or x=="o" or x=="u" or x=="A" or x=="E" or x=="I" or x=="O" or x=="U":
vowels.append(x)
print(vowels)
Output:
Enter a word: varun
['a', 'u']
3.write a program to carry out the following operations on the given set.
Program:
s ={10, 2, -3, 4, 5, 88}
count =len(s)
print(count)
Output:
6
b. Maximum element in s
Program:
max_num = 0
for x in s:
if x > max_num:
max_num = x
else:
break
print(f"The maximum number is {max_num}")
Output:
The maximum number is 88
c. Minimum element in s
Program:
min_num = float('inf')
for x in s:
if x < min_num:
min_num = x
else:
break
print(f"The minimum number is {min_num}")
Output:
The minimum number is 2
Program:
for x in s:
sum =sum+x
print(f"The sum of element is {sum}")
Output:
The sum of element is 106
Program:
sorted_set =[]
while s:
min_value = min(s)
sorted_set.append(min_value)
s.remove(min_value)
print(f"The sorted set is {(sorted_set)}")
Output:
The sorted set is [-3, 2, 4, 5, 10, 88]
Output:
No
Program:
x = -3
if x in s:
print("yes its an element")
else:
print("No")
Output:
yes its an element
Flow Control
1.Conditional statement
Program:
num1 = int(input("Enter a number: "))
num2 = int(input("Enter a number: "))
num3 = int(input("Enter a number: "))
biggest_num = 0
Program:
num = int(input("Enter a number: "))
if 0<= num <= 100:
print("YES! The number is between 1 to 100")
else:
print("NO! The number is not between 1 to 100")
Output:
Enter a number: 103
NO! The number is not between 1 to 100
Enter a number: 2
YES! The number is between 1 to 100
2.Iterative statements.
1. program to print characters present in given string
Program:
input_string = input("Enter a string: ")
for char in input_string:
print(char)
Output:
Enter a string: HII
H
I
I
Program:
input_string = input("Enter a string: ")
for index, char in enumerate(input_string):
print(f"Index {index}: {char}")
Output:
Enter a string: VARUN
Index 0: V
Index 1: A
Index 2: R
Index 3: U
Index 4: N
Program:
list =[2,10,20,40,50,70,56]
sum =0
for x in list:
sum=sum+x
print(f"The sum of the numbers present in list is {sum}")
Output:
The sum of the numbers present in list is 248
Program:
n = int(input("Enter a number: "))
sum =0
i=1
while i<=n:
sum+=i
i+=1
print(f"The sum of the first n number is {sum} ")
Output:
Enter a number: 23
The sum of the first n number is 276