Codes
Codes
Ask the user to enter a list containing numbers between 1 and 12 then replace all of the entries in the list that are greater than 10
with 10
Cumulative sum of a list [a, b, c...] is defined as [a, a + b, a + b + c, ...). Write a program to input a list of number and then create
another list from this list that contains cumulative sum of numbers in list1.
DICTIONARY
Write a program to enter names of employees and their salaries as input and store them in a dictionary.
dic = { }
while True :
name = input("Enter employee name :-")
sl = int(input("Enter employee salary :-"))
dic[ name] = sl
user = input("Do you want to quit then enter yes :-")
if user == "yes" :
Break
print(dic)
Count no of character
string = input("Enter String :- ")
dic = { }
for i in string :
dic [ i ] = string.count( i )
print(dic)
Number to word
num = input ("Enter a number :- ")
dic = { "0" : "Zero" , "1":"One" , "2" : "Two" , "3" : "Three" , "4" : "Four" , "5" : "Five" , "6" : "Six" , "7" : "Seven" , "8" : "Eight" , "9" :
"Nine"}
for i in num :
print( dic[ i ], end = " ")
Write a program that lists the over lapping keys of the two dictionaries if a key of d1 is also a key of d2, the list it.
for i in lst :
a = lst.count(i)
if a > 1 :
count += a
for j in lst :
if i == j :
lst.remove( j )
if count == 0 :
print( "no values are same " )
else :
print( count,"values are same ")
If one dict is in another
dic1 = eval(input("Enter first dictionary :-"))
dic2 = eval(input("Enter second dictionary :-"))
count = 0
for i in dic1 :
for j in dic2 :
if i == j :
count += 1
if count == len(dic1):
print("Then d1 is contained in d2.")
else :
print("Then d1 is not contained in d2.")
A dictionary D1 has values in the form of lists of numbers. Write a program to create a new dictionary D2 having same keys as D1
but values as the sum of the list elements
for i in dic1 :
lst = dic1[ i ]
sum_of_num = sum( lst )
dic2 [ i ] = sum_of_num
print(dic2)
Username and pass
dic = {'Path' : "2003",
'walla' : "1809",
'Portal' : "1912",
'Express' : "2003",
'Computer' : "9834",
'Python' : "1990",
'Mysql' : "2001",
'c++' : "1996",
'java' : "1998",
'html' : "1999" }
if username in dic :
password = input("Enter password :- ")
if dic[username] == password :
print ("You are now logged into the system.")
else :
print ("Invalid password.")
else :
print ("You are not valid user.")