8-1
def display_message():
print(
'Function in python')
deisplay_message()
8-2
def favorite_book(title):
print(
'One of my favorite books is '+title)
favorite_book(
'A Brief History of Time')
8-3
def make_shirt(size, word):
print(
'Your shirt is size '+size+
' with "'+word+
'" ')
make_shirt(
'S',
'You bet')
make_shirt(size=
'S', word=
'Bite me')
8-4
def make_shirt(size, word='I love Python'):
print(
'Your shirt is size '+size+
' with "'+word+
'" ')
make_shirt(
'L')
make_shirt(
'M')
make_shirt(
'S',
'I hate Python')
8-5
def describe_city(city='Reykjavik', country='Iceland'):
print(city+
' is in '+country)
describe_city()
describe_city(
'York',
'U.K.')
describe_city(
'New York',
'U.S.')
8-6
def city_country(city, country):
return city+
', '+country
print(city_country(
'H.K.',
'P.R.C.'))
print(city_country(
'York',
'U.K.'))
print(city_country(
'New York',
'U.S.'))
8-7
def make_album(singer, album, count=0):
if count:
return {singer: album,
'count': count}
return {singer: album}
print(make_album(
'Pheobe',
'Smelly Cat'))
print(make_album(
'A',
'B'))
print(make_album(
'C',
'D'))
print(make_album(
'E',
'F',
10))
8-8
def make_album(singer, album, count=0):
if count:
return {singer: album,
'count': count}
return {singer: album}
while True:
singer = input(
'Singer: ')
if singer==
'quit':
break
album = input(
'Album: ')
if album==
'quit':
break
print(make_album(singer, album))
8-9
def show_magicians(magicians):
for magician
in magicians:
print(magician)
magicians = [
'A',
'B',
'C']
show_magicians(magicians)
8-10
def show_magicians(magicians):
for magician
in magicians:
print(magician)
def make_great(magicians):
for i
in range(
0, len(magicians)):
magicians[i] =
'The Great '+magicians[i]
magicians = [
'A',
'B',
'C']
make_great(magicians)
show_magicians(magicians)
8-11
def show_magicians(magicians):
for magician
in magicians:
print(magician)
def make_great(magicians):
for i
in range(
0, len(magicians)):
magicians[i] =
'The Great '+magicians[i]
return magicians
magicians = [
'A',
'B',
'C']
changed = make_great(magicians[:])
show_magicians(magicians)
show_magicians(changed)
8-12
def sandwich(*materials):
print(
'You sandwich inludes: ', end=
'')
for material
in materials:
print(material, end=
' ')
print()
sandwich(
'A',
'B',
'C')
sandwich(
'A',
'B')
sandwich(
'A')