栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python编程从入门到实践 笔记7

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python编程从入门到实践 笔记7

# 位置实参:顺序很重要 如果实参的顺序不正确 可能会报错
def pets(animal_type,pet_name):
 显示宠物的信息 
 print( I have a animal_type)
 print( His name is pet_name)
pets( dog , xiaohei )
②.关键字实参 
#关键字实参是传递给函数的名称-值对
def pets(animal_type,pet_name):
 显示宠物的信息 
 print( I have a animal_type)
 print( His name is pet_name)
pets(animal_type dog ,pet_name xiaohei )
 ③.默认值

编写函数时 可以给每个形参指定默认值。

# 如果每一值频繁出现 可以设置为默认值
def pets(pet_name,animal_type dog ):
 显示宠物的信息 
 print( I have a animal_type)
 print( His name is pet_name)
pets(pet_name xiaohei )
3.返回值 ①.返回简单值
def get_name(first_name,last_name):
 返回整洁的名字 
 full_name first_name last_name
 return full_name.title()
name get_name( zhang , san )
print(name)
②.返回字典 
def build_person(first_name,last_name):
 返回一个字典 其中包含有关一个人的信息 
 person { first :first_name, last :last_name}
 return person
name build_person( Li , zian )
print(name)
def build_person(first_name,last_name,age ):
 返回一个字典 其中包含有关一个人的信息 
 person { first :first_name, last :last_name}
 if age:
 person[ age ] age
 return person
name build_person( Li , zian ,age 23)
print(name)
 ③.结合使用函数和while循环
def get_name(first_name,last_name):
 返回整洁的名字 
 full_name first_name last_name
 return full_name.title()
while True:
 print( nPlease tell me your name: )
 print( (enter q at any time to quit )
 f_name input( First_name )
 if f_name q :
 break
 l_name input( Last_name )
 if l_name q :
 break
name get_name(f_name,l_name)
print( n Hello name)
4.传递列表

def great_users(names):
 向列表中的每位用户都发出简单的问候 
 for name in names:
 msg Hello, name.title()
 print(msg)
usernames [ john , tom , eric ]
great_users(usernames)
②.在函数中修改列表
def print_models(unprinted_designs,completed_models):
 模拟打印每个设计 直到没有未打印的设计为止
 打印每个设计后 都将其移到列表completed_models中
 while unprinted_designs:
 current_design unprinted_designs.pop()
 #模拟根据设计制作3D打印模型的过程
 print( Printing model: current_design)
 completed_models.append(current_design)
def show_completed_models(completed_models):
 显示打印好的所有模型 
 print( nThe following models have been printed: )
 for completed_model in completed_models:
 print(completed_model)
unprinted_designs [ iphone case , robot pendant , dodecahedron ]
show_completed_models(unprinted_designs)

 ③.禁止函数修改列表
#切片表示法[:]创建列表的副本
# function_name(list_name[:])
#如果不想清空未打印的设计列表 可像下面这样调用print_models()
# print_models(unprinted_designs[:],completed_models)
④.传递任意数量的实参
# *toppings中的*让python创建一个名为toppings的空元组 并将受到的所有值都封装到这个元组中
def make_pizza(*toppings):
 打印顾客点的所有配料 
 print(toppings)
make_pizza( pepperoni )
make_pizza( mushrooms , green peppers , extra cheese )

 ⑤.使用任意数量的关键字实参
# **代表创建一个空字典
# 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( albert , einstein ,
# location princeton ,
# field physics 
# print(user_profile)

 5.将函数存储在模块中 ①.导入整个模块

使用 import

def make_pizza(size,*toppings):
 概述要制作的披萨 
 print( nMaking a str(size) -inch pizza with the following toppings: )
 for topping in toppings:
 print( - topping)
import pizza 
pizza.make_pizza(15, pepproni )
pizza.make_pizza(16, mushrooms , green peppers , extra cheese )
②.使用as给函数指定别名

起别名使用 as

import pizza as p
p.make_pizza(15, pepproni )
p.make_pizza(16, mushrooms , green peppers , extra cheese )
③.导入模块中的所有函数

from pizza import *

make_pizza(15, pepproni )
make_pizza(16, mushrooms , green peppers , extra cheese )

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/267551.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号