1>函數(shù)input()
讓程序暫停運(yùn)行,等待用戶輸入一些程序,獲取用戶輸入,Python將其存儲(chǔ)在變量中。
記?。篜ython2.7版本獲取用戶輸入的是:raw_input()
使用input的話,會(huì)把用戶的輸入當(dāng)做Python的代碼來運(yùn)行。
2>while可以用break語句退出循環(huán)
使用while循環(huán)時(shí),當(dāng)不再運(yùn)行余下代碼時(shí),也不管前面測試結(jié)果如何時(shí),
可以直接使用break語句。
while True:
city=raw_input("please input the city which you want to visit ?")
if city=='quit':
break
else:
print ("city:"+city.title())
2>while循環(huán)可以用contiued回答循環(huán)的開頭:
要返回循環(huán)的開頭而不是像break語句那樣不再執(zhí)行余下的語句,
就使用continue語句只退出當(dāng)前的循環(huán)。
curr_number=0
while curr_number<10:
curr_number+=1
if curr_number%2==1:
continue
else:
print curr_number```
#####3>while循環(huán)
在列表中cat出現(xiàn)的次數(shù)是兩次,循環(huán)的次數(shù)也是兩次哦
并且利用循環(huán)刪除列表中指定的函數(shù)
```python
pets=['dog','cat','rabbit','small dog','cat','small cat']
print pets
while 'cat' in pets:
pets.remove('cat')
print pets
4>函數(shù)
關(guān)鍵字傳實(shí)參,準(zhǔn)確指出形參名和與之對應(yīng)的實(shí)參名。
還可以設(shè)置默認(rèn)值,當(dāng)設(shè)置形參默認(rèn)值時(shí),必須列出沒有默認(rèn)值的形參。
即分為位置實(shí)參以及關(guān)鍵字實(shí)參
def favourite_book(love_book,hate_book):
print("My love_book is:"+love_book.title())
print("My hate_book is:"+hate_book.title())
favourite_book(love_book='windy',hate_book='san zhi laohu')
favourite_book(hate_book='san zhi laohu',love_book='windy')
5>在給函數(shù)傳遞列表參數(shù)時(shí),不想修改列表可以同過將列表參數(shù)傳遞給函數(shù),
在調(diào)用列表參數(shù)時(shí)傳入的是列表的切片表示法。
function_name(list_name[:])
具體如下(####):
magicians=['lily','winney','sam','jonh']
def create_magician(magicians_list):
new_magicians=[]
"""for magician in magicians:
new_magician=magician+"the Greate !"
new_magicians.append(new_magician)
return new_magicians"""
while magicians_list:
curr_magician=magicians_list.pop()
new_magicians.append(curr_magician)
return new_magicians
def show_tow_magicians(new_magicians,old_magicians):
print ("new_magicians includes :"+str(new_magicians))
print
print("old_magicians includes :"+str(old_magicians))
##########
new_magicians=create_magician(magicians[:])
show_tow_magicians(new_magicians=new_magicians,old_magicians=magicians)```
#####6>傳遞任意數(shù)量的實(shí)參
結(jié)合使用位置實(shí)參以及任意數(shù)量的實(shí)參
使用指針*加變量名(例:\*p)接受任意數(shù)目的實(shí)參,
!*p將接受的多個(gè)實(shí)參,存儲(chǔ)在列表中
!在編寫函數(shù)前,將固定個(gè)數(shù)的型參放在前面,接受任意數(shù)目的實(shí)參放在后面。
```python
def makeing_pizza(size,*toppings):
print ("Making a "+str(size)+" pizza,-inch pizza with the following toppings:")
for topping in toppings:
print(topping)
makeing_pizza(12,'mushroom','cheese','beef')
7>使用任意數(shù)量的關(guān)鍵字實(shí)參
有時(shí)候,需要接受任意數(shù)量的實(shí)參,但是預(yù)先并不知道傳遞給函數(shù)的是什么樣的信息,
可以將函數(shù)編寫成能夠接受任意數(shù)量的鍵值對,調(diào)用語句提供了多少就接受多少。
例如:你在編寫一個(gè)網(wǎng)站,新用戶填寫而寫可選資料時(shí),你不確定用戶會(huì)填寫哪些可選信息,
也不確定會(huì)是什么樣的信息,這時(shí)就可以用*p表示接受任意數(shù)量的關(guān)鍵字實(shí)參。
!**p的兩個(gè)星號(hào)讓Python創(chuàng)建了個(gè)名為p的空字典,并且將所有接收到的鍵值對都存儲(chǔ)在這個(gè)字典中。
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
my_profile=build_profile('fung','winney',city='guangzhou',age='22')
print ("My name is :"+my_profile['first_name'].title()+" "+my_profile['last_name'])
print ("I live in "+my_profile['city'].title()+" "+"and my age is "+my_profile['age']+' .')
!??!總結(jié)6、 7這兩個(gè)知識(shí)點(diǎn),分別是*P只含有一個(gè)星號(hào),且把接收的任意個(gè)實(shí)參存儲(chǔ)在列表中,
而**P則有兩個(gè)星號(hào),且將接收的任意實(shí)參存儲(chǔ)到字典中且接收的任意實(shí)參是鍵值對。