[go: up one dir, main page]

0% found this document useful (0 votes)
5 views7 pages

PYTHON EXP 01

The document contains various Python programs demonstrating different concepts such as dictionaries, sets, flow control, and iterative statements. Each program includes a description, code, and sample output. Topics covered include creating dictionaries from lists, counting letter occurrences, eliminating duplicates from lists, and calculating sums.

Uploaded by

Sneha Gaikwad
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)
5 views7 pages

PYTHON EXP 01

The document contains various Python programs demonstrating different concepts such as dictionaries, sets, flow control, and iterative statements. Each program includes a description, code, and sample output. Topics covered include creating dictionaries from lists, counting letter occurrences, eliminating duplicates from lists, and calculating sums.

Uploaded by

Sneha Gaikwad
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/ 7

DICTIONARY

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

4.Find no of vowels present in given string.

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

5.generate personalized greeting

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

1. write a program tp eliminate duplicate present in the list

Program:
list1 =[10,20,40,10,50,20,60,40,30]
m=set(list1)
print(list(m))

Output:
[40, 10, 50, 20, 60, 30]

2. write a program to print different vowels present in given word.

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

d. sum of all element in s

Program:
for x in s:
sum =sum+x
print(f"The sum of element is {sum}")

Output:
The sum of element is 106

e. obtain a new sorted set for s, set s remaining unchanged.

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]

f. report whether 100 is element of s


Program:
x=0
if x in s:
print("yes")
else:
print("No")

Output:
No

g. whether -3 is not an element of s

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

1. program to find biggest of given 3 numbers

Program:
num1 = int(input("Enter a number: "))
num2 = int(input("Enter a number: "))
num3 = int(input("Enter a number: "))

biggest_num = 0

if num1>=num2 and num1 >= num3:


biggest = num1
elif num2 >=num1 and num2>=num3:
biggest = num2
else:
biggest = num3
print(f"The biggest number is: {biggest}")
Output:
Enter a number: 23
Enter a number: 34
Enter a number: 56
The biggest number is: 56

2. program to check whether the given number is between 1 to 100.

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

2. print character present in string index wise.

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

3. sum of numbers present inside list.

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

4. display the sum of first n number.

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

You might also like