python 快速入門(二)
阿新 • • 發佈:2018-12-09
六. 字典 dict (增、刪、查、改)
字典是一系列鍵-值(key-value)對
######1.操作字典
alien = { 'color': 'green' , 'points':3,'height':12 }
#增
alien['x_position'] = 0
alien['y_position'] = 0
print(alien) #{'color': 'green', 'points': 3, 'x_position': 0, 'y_position': 0, 'height': 12}
#查
print(alien['color']) #green
#改
alien[ 'color'] = 'red'
print(alien) #{'color': 'red', 'points': 3, 'x_position': 0, 'y_position': 0, 'height': 12}
#刪
#直接刪除
del alien['points'] #red
print(alien) #{'color': 'red', 'x_position': 0, 'y_position': 0, 'height': 12}
#彈出
b = alien.pop('color')
print(b) #red
print(alien) #{'x_position': 0, 'y_position': 0, 'height': 12}
#隨機刪除一項
alien.popitem()
print(alien) #{'y_position': 0, 'height': 12}
#清空整個字典
alien.clear()
print(alien) #{}
######2.遍歷字典
alien = { 'color': 'green' , 'points':3,'height':12 }
for key,value in alien.items():
print(key)
print(value)
# color
# green
# points
# 3
# height
# 12
#單獨遍歷key
for key in alien.keys():
print(key)
# color
# points
# height
#單獨遍歷values
for values in alien.values():
print(values)
# green
# 3
# 12
七. while迴圈
a = 1
while a<=5:
print(a)
a+=1
# 1
# 2
# 3
# 4
# 5
八. 函式
1.函式的定義
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def greet_user():
"""顯示簡單的問候語"""
print("Hello!")
greet_user()
#Hello!
2.函式的傳參
def greet_user1(username):
"""顯示簡單的問候語"""
print("Hello,"+username.title()+"!")
greet_user1("neng")
#Hello,Neng!
3.實參和形參
在函式greet_user()的定義中,變數username是一個形參, 在呼叫函式greet_user(‘neng’)中,值‘neng’是一個實參。
a.實參:呼叫函式時傳遞給函式的資訊
b.形參:函式定義時,函式完成其工作所需的一項資訊。
4.位置實參和關鍵字實參
def greet_user2(username,sex):
"""顯示簡單的問候語"""
print("Hello,"+username.title()+"!"+"性別:"+sex.title())
greet_user2("neng","男")
def greet_user3(username,sex):
"""顯示簡單的問候語"""
print("Hello,"+username.title()+"!"+"性別:"+sex.title())
greet_user3(sex="男",username="neng")
#Hello,Neng!性別:男
5.預設值
def greet_user4(username,sex='男'):
"""顯示簡單的問候語"""
print("Hello,"+username.title()+"!"+"性別:"+sex.title())
greet_user4("neng")
#Hello,Neng!性別:男
6.返回值
函式可以返回任何型別的值,包括列表和字典等較複雜的資料結構
def build_person(first_name, last_name):
"""返回一個字典,其中包含有關一個人的資訊"""
person = {'first':first_name, 'last': last_name}
return person
musician = build_person('jimi','hendrix')
print(musician)
#{'last': 'hendrix', 'first': 'jimi'}
7.傳遞列表
直接將列表作為實參傳遞給函式,函式能直接訪問其內容,並且修改原列表的值
def greet_users(names):
names[1] = 'neng'
usernames = ['hannah', 'ty', 'margot']
greet_users(usernames)
print(usernames)
#['hannah', 'neng', 'margot']
禁止修改列表
def greet_users(names):
names[1] = 'neng'
usernames = ['hannah', 'ty', 'margot']
greet_users(usernames[:])
print(usernames)
#['hannah', 'ty', 'margot']
8.傳遞任意數量的實參
def make_pizza(*toppings):
"""列印顧客點的所有配料"""
print(toppings)
make_pizza('pepperoni')
make_pizza('mushrooms', 'green peppers', 'extra cheese')
#('pepperoni',)
#('mushrooms', 'green peppers', 'extra cheese')
9.將函式儲存在模組中
函式的優點之一是,使用它們可將程式碼塊與主程式分離。
建立pizza.py檔案
def make_pizza(size, *toppings):
"""概述要製作的比薩"""
print("\nMaking a " + str(size) +
"-inch pizza with the following toppings:")
for topping in toppings:
print("- " + topping)
建立making_pizzas.py檔案
import pizza
pizza.make_pizza(16, 'pepperoni')
pizza.make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')
#
#Making a 16-inch pizza with the following toppings:
#- pepperoni
#
#Making a 12-inch pizza with the following toppings:
#- mushrooms
#- green peppers
#- extra cheese