8-1 messages
def display_message():
print("nice to learn the method")
display_message()
8-2 favorite books
def favorite_book(title):
print("One of my favorite books is Alice in Wonderland")
favorite_book("Harry Potter")
8-3 T-shirt
def make_shirt(size, word):
print("This T-shirt is " + str(size) + " size", end = '')
print(", and the appended word is " + '"' + word + '".')
make_shirt(175, "hello world")
make_shirt(size = 175, word = "hello world")
8-4 Big T-shirt
def make_shirt(size, word = "I love python"):
print("This T-shirt is " + str(size) + " size", end = '')
print(", and the appended word is " + '"' + word + '".')
make_shirt("big")
make_shirt("middle")
make_shirt("small", "I'll go home")
8-5 Cities
def describe_city(name, state="china"):
print(name + " is in " + state)
describe_city("Beijing", "China")
describe_city("London", "British")
describe_city("Yantai")
8-6 Name of cities
def city_country(name, state):
print('"' + name + ', ' + state + '"')
city_country("Beijing", "China")
city_country("London", "British")
city_country("New York", "America")
8-7 Albums
def make_album(song, singer, songs_number = -1):
if songs_number == -1:
dic = {"name": song, "singer": singer}
else:
dic = {"name": song, "singer": singer, "number of songs": songs_number}
return dic
print(make_album("houlai", "Liu ruoying"))
print(make_album("henaihenaini", "Liu ruoying"))
print(make_album("Bingyu", "Liu dehua", 1))
8-8 User's Album
def make_album(song, singer, songs_number = -1):
if songs_number == -1:
dic = {"name": song, "singer": singer}
else:
dic = {"name": song, "singer": singer, "number of songs": songs_number}
return dic
while True:
print('You can input "quit" to exit the program')
song = input("Please input the song name: ")
if song == "quit":
break
singer = input("Please input the singer: ")
if singer == "quit":
break
songs_number = input("Please input the number of all songs: ")
if songs_number == "quit":
break
album = make_album(song, singer, songs_number)
print(album)
8-9 magicians
def show_magicians(magicians):
print(magicians)
magicians = ["zzr", "zj", "zbq"]
show_magicians(magicians)
8-10 outstanding magicians
def show_magicians(magicians):
print(magicians)
def make_great(magicians):
for i in range(len(magicians)):
magicians[i] = "the Great " + magicians[i]
magicians = ["zzr", "zj", "zbq"]
show_magicians(magicians)
make_great(magicians)
show_magicians(magicians)
8-11 Fixed magicians
def show_magicians(magicians):
print(magicians)
def make_great(magicians):
for i in range(len(magicians)):
magicians[i] = "the Great " + magicians[i]
return magicians
magicians = ["zzr", "zj", "zbq"]
magicians2 = make_great(magicians[:])
show_magicians(magicians)
show_magicians(magicians2)
實際上,傳進函數當中的列表是原來的列表本身,但是傳遞進去的數字和字符串卻是重新生成的臨時變量,在這當中又一點需要注意的是,使用for語句的時候,前面的循環變量i是臨時生成的,而不是這個列表當中真正的i,例如:
def make_great(magicians):
for magician in magicians:
magician = "the Great " + magician
return magicians
這種寫法裏面就把magician認爲是magicians裏面的真實變量,但是其實不是。
8-12 sandwiches
def get_items(*items):
print(items)
get_items("cheese", "beef", "tomato")
get_items("cheese", "beef", "tomato", "potato")
get_items("cheese", "beef", "tomato", "potato", "milk")
8-13 brief introduction
def build_profile(first, last, **user_info):
'''創建一個字典,其中包含我們知道的有關用戶的一切'''
profile = {}
profile['first_name'] = first
profile['last_name'] = last
for key, value in user_info.items():
profile[key] = value
return profile
user_profile = build_profile('Binqi', 'Zhao', birthday = "19980506", favourite_color = "Blue")
print(user_profile)
8-14 cars
def make_car(producer, type, **others):
car = {}
car['producer'] = producer
car['type'] = type
for key, value in others.items():
car[key] = value
return car
car = make_car('subaru', 'outback', color='blue', tow_package=True)
print(car)
8-15 print the model
import func
car = func.make_car('subaru', 'outback', color='blue', tow_package=True)
print(car)
8-16 import
import func as f
car = f.make_car('subaru', 'outback', color='blue', tow_package=True)
print(car)
from func import make_car
car = make_car('subaru', 'outback', color='blue', tow_package=True)
print(car)
from func import make_car as make
car = make('subaru', 'outback', color='blue', tow_package=True)
print(car)
from func import *
car = make_car('subaru', 'outback', color='blue', tow_package=True)
print(car)