PYTHON PROGRAM
TUPLES AND DICTIONARY
Q1- WAP to create a tuple by accepting elements from the user using while loop.
Q2-WAP to create a program using for loop.
Q3-WAP to store ‘n’ numbers of subject in a tuple.
Q4-WAP to input n number from the user,store these numbers in a tuple and print the
maximum, minimum, number along with the sum and mean of all elements from this
tuple.
Q5- WAP to input any two tuples and swap their value.
Q6- WAP to input total streams and sections in class 11 and display it on screen.
Q7. Write a program to check if a number is present in the list or not. If the number is present, print
the position of the number. Print an appropriate message if the number is not present in the list.
Q8- Write a program to count the number of times a character appears in a given string.
Q9- Write a program to enter names of employees and their salaries as input and store them in a
dictionary. Here n is to input by the user.
Q10 WAP to Repeatedly ask the user to enter a team name and how many games the team has
won and how many they lost. Store this information in a dictionary where the keys are the team
names and the values are lists of the form [wins, losses].
(a) Using the dictionary created above, allow the user to enter a team name and print out the team's
winning percentage.
(b) Using the dictionary, create a list whose entries are the number of wins of each team.
(c) Using the dictionary, create a list of all those teams that have winning records.
1.
t=tuple()
n=int(input("Enter number of elements :"))
i=1
while (i<=n):
a=input("Enter number")
t=t+(a,)
i=i+1
print("tuple Created as:")
print(t)
OUTPUT
2.
t=tuple()
n=int(input("Enter any number"))
for i in range (n):
a=input("Enter Number: ")
t=t+(a,)
print ("output is")
print(t)
OUTPUT
3.
t=tuple()
n=int(input("how ,any subjects you want to add: "))
print ("Enter all subjects one after another:")
for i in range(n):
a=input("Enter subject:")
t+=(a,)
print("output is")
print(t)
OUTPUT
4.
numbers=tuple()
n=int(input("How many numbers you want to enter?:"))
for i in range(0,n):
num=int(input())
numbers=numbers+(num,)
print('\nThe numbers in the tuples are:')
print(numbers)
print("\nThe max number is:")
print(max(numbers))
print("The min number is:")
print(min(numbers))
print("The sum of tuple elements :")
print(sum(numbers))
mean=sum(numbers)/len(numbers)
print("the mean of values in tuples is:")
print(mean)
OUTPUT
5.
t1=tuple()
n=int(input("Total no of value in first tuple:"))
for i in range(n):
a=input("Enter elements:")
t1=t1+(a,)
t2=tuple()
m=int(input("Total no of values in second tuples:"))
for i in range(m):
a=input("Enter elements:")
t2=t2+(a,)
print("First tuple:")
print(t1)
print("Second tuple")
print(t2)
t1,t2=t2,t1
print("After swapping:")
print("First tuple:")
print(t1)
print("Second tuple")
print(t2)
OUTPUT
6.
classxi=dict()
n=int(input("Enter total number of sections in xi class:"))
i=1
while i<=n:
a=input("Enter section:")
b=input("Enterstream name:")
classxi[a]=b
i=i+1
print("Class",'\t',"Section",'\t',"Stream name")
for i in classxi:
print("XI",'\t',i,'\t',classxi[i])
OUTPUT
7.
list1 = []
print("How many numbers do you want to enter in the list: ")
maximum = int(input())
print("Enter a list of numbers: ")
for i in range(0,maximum):
n = int(input())
list1.append(n)
num = int(input("Enter the number to be searched: "))
position = -1
for i in range (0, len (list1)):
if list1[i] == num:
position = i+1 #save the position of number
if position == -1 :
print("Number",num,"is not present in the list")
else:
print("Number",num,"is present at",position + 1, "position")
Q8-
st = input("Enter a string: ")
dic = {}
for ch in st:
if ch in dic:
dic[ch] += 1
else:
dic[ch] = 1
for key in dic:
print(key,':',dic[key])
OUTPUT
Q9-
num = int(input("Enter the number of employees whose data to bestored: "))
count = 1
employee = dict()
for count in range (num):
name = input("Enter the name of the Employee: ")
salary = int(input("Enter the salary: "))
employee[name] = salary
print("\n\nEMPLOYEE_NAME\tSALARY")
for k in employee:
print(k,'\t\t',employee[k])
OUTPUT
Q10-
d = {}
ans = "y"
while ans == "y" or ans == "Y" :
name = input("Enter Team name: ")
w = int(input("Enter number of wins: "))
l = int(input("Enter number of losses: "))
d[name] = [w, l]
ans = input("Do you want to enter more team names? (y/n): ")
team = input("Enter team name for winning percentage: ")
if team not in d:
print("Team not found", team)
else:
wp = d[team][0] / sum(d[team]) * 100
print("Winning percentage of", team, "is", wp)
w_team = []
for i in d.values():
w_team.append(i[0])
print("Number of wins of each team", w_team)
w_rec = []
for i in d:
if d[i][0] > 0:
w_rec.append(i)
print("Teams having winning records are:", w_rec)