Professional Documents
Culture Documents
Dictionary and Tuples
Dictionary and Tuples
Dictionary and Tuples
collection of
Ad
all mutable values are
unordered values which
he types
and tuples are not. compound types. Lists and are accessed by keys.
g s
which
dictionaries are mutable, while
move
tors which move
characters the cursor
Any without printing visible characters
clear() method removes all items
from the called whitespace.
are called
ihe
The get() meti method returns a Value tor the dictionary. whitespace
given key. If key is not
value None.
available then it returns detauit
The item() method returns a list of the
ne key)method returns a list of the dictionary's key-value pairs.
alue) method returns a list of thedictionary' s keys.
7
The
dictionary's values.
IDdate() methd adds dictionary contentsinto
function does not return anything, another dictionary's key-value pairs. This
unles are
Tuples are a type of
ot sequence like
strings. But unlike
tupies can contain elements of any type. strings, which can only contain characters,
The len() function returns the number of elements in the
tuple.
The tuple functions max() and min() find the maximum and
minimum value in a sequence of values.
SoLVED EXERCISES
1. What is a dictionary?
Ans. A Python dictionary is a group of key-value pairs. The elements in a dictionary are indexed by keys.
Keys in a dictionary are required to be unique.
2. How can you access data from a dictionary?
Ars. The simplest way to retrieve a value from a dictionary is by directly accessing it with a key. To get a
key's value, just put the key in brackets, following the name of the dictionary.
3. What does the item() method of dictionary return?
Ans. The items() method returns a list of dictionaries (key: value) pairs as tuples.
4. What is the main of
use a dictionary?
Ans. The main use of a dictionary is to look up the value associated with a particular key.
x))
for x, y in
data.items():
total = sum(y)
print ("%s 's total marks %d" % (x,
if total < 200: total))
print ("%s failed" % x)
else:
print ("%s passed)" %
10. Write a y)
program to input
information in ascendingemployee number and name for
order of their 'N'
employee number. employees and display all emp oyee's
Ans. # File
name:
#
Searching customer name with
Emp } # An
. \ MyPythonXI\PyChap15\Eorder.py respective phone number
empty dictionary
#Function to append employee
def EmployeeDict(Emp): number and name
Ch 'Y
while Ch ==
'Y' or Ch ==
"YES':
print ("Enter
ENo = input() employee no.: "
, end="")
print ("Enter name: "
, end="")
sing with Dictionaries and Tuples
WorkA
break
HAdding employee into dictionary
EmployeeDict(Emp)
EKey Emp.keys()
Elist = list(EKey) # Converts keys into a list
Flist.sort() #Employee nos. arearranged in ascending order
print()
print ("Employee list...")
print ( " " * 40)
for i in EList:
11. Write a program to enter 'N numbers in a tuple Num and find the sum, maximum and minimum
values in the tuple.
Ans. # File name: ...\\MyPythonXI\PyChap15\TSum.py
# Program to find sum, max and min in a tuple
Num tuple() #An empty tuple
print ("How many numbers you want to enter: ", end=")
N = int(input())
Ctr 1
ifN>0:
while Ctr<= N:
print ("Enter No.:", Ctr, end="
-> ")
Val =intfinput())
Num += (Val,)
Ctr+= 1
print ("Tuple is:", Num)
print ("Total =", sum(Num))
print ("Maximum =", max(Num))
print ("Minimum =", min(Num)) will randomly pick a word from a
12. Write aa where computer
word jumble game
program for if it is correct, then print an
se
e should guess a
word from the tuple,
uence (tuple). The user
as follows:
appropriate messa The tuple is given "MySql", "Fortran")
"Cobol", "C++", "Java", "Visual Studio",
Ans File dme:..("Python",
Ans. #RDS= \MyPythonXI\PyChap15Vgame.py
#Word Jumble game
import random
cre choose from
Sequence of words to "MySql", "Fortran")
d "C++", "Java", "Visual Studio",
("Python", "Cobol",
330 Sarasavatl Computer Sci
#pick one word randomly from the sequence
science n
word random.choice(WORDS)
#Create a variable to use later to see ifthe guess is correct
correct= word
correct correct.lower()
#create a jumbled version of the word
jumble =""
while word:
position random.randrange(len(word))
jumble + word[position]
word =
word[:position] word[(position+1):]
+
#start the game
print
guess guess.lower()
if guess == correct:
print()
#exit
and Tuples
y a l w i t hD
th
Dictionaries 331
= =
"O"
choice
f print ("Good-bye.")
sCore
a
ddd
4 == "1":
choice
scores.sort()
REVIEW QUESTIONS
declare a dictionary.
we can
Write the methods by which example.
with an
dictionary? Explain
L.Can you modify a
allowed)
3.Whatare the dictionary keys?
answers
dictionary?(multiple
statements
create a