Python第八章函数笔记

1.定义函数
def + 函数名() + 冒号
2.向函数传递信息在函数定义时在()内添加
3.传递实参
(1)位置实参
最简单的关联方式是基于实参的顺序
(2)关键字实参
传递给函数的名称—值对,直接在实参中将名称和值关联起来了
如:
def pets(animal_type,animal_name): //函数定义
pets(animal_type = ‘dog’,animal_name = ‘harry’) //调用函数
(3)默认值
在函数定义时给形参指定默认值,如果调用函数中给形参提供了实参时, Python将使用指定的实参值; 否则 将使用形参的默认值。 因此, 给形参指定默认值后, 可在函数调用中省略相应的实参。
注 :Python将这个实参视为位置实参, 这个实参将关联到函数定义中的第一个形参,所以当实参传递给的不是第一个形参时,采用关键字实参
(4)返回简单值return
调用返回值的函数时, 需要提供一个变量, 用于存储返回的值。
print(变量名)
(5)让实参变成可选的
函数定义时给可选实参指定一个默认值:“空字符串”
用if判断是否有可选实参
在这里插入图片描述
(6)返回字典

def make_album(singer_name,album_name):
    grate = {'singer':singer_name,'album':album_name}
    return grate
momo = make_album('momo','fine')
mina = make_album('mina','ugly')
sana = make_album('sana','peace')
print(momo)
print(mina)
print(sana)

def make_album(singer_name,album_name,num = ' '):
    '''创建一个描述音乐专辑的字典'''
    music = {'singer':singer_name,'album':album_name}
    if num:
        full_word = singer_name + ' ' + album_name + ' ' + str(num)
    else:
        full_word = singer_name + ' ' + album_name
    return full_word.title()#函数返回
momo = make_album('momo','fine',25)
mina = make_album('mina','ugly')
sana = make_album('sana','peace')
print(momo)
print(mina)
print(sana)

字典中存储singer_name的值的键为singer

(7)函数和while结合使用

def get_formatted_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

    formatted_name = get_formatted_name(f_name,l_name)
    print("\nHello, " + formatted_name + "!")
   

(8)传递列表
将函数定义成接受一个名字列表, 并将其存储在形参names 中
(9)在函数中修改列表

def print_models(unprint_designs,completed_models):
    '''
    模拟打印每个设计,直到没有未打印的设计为止
    打印每个设计后,都将其移到列表completed_models中
    '''
    while unprint_designs:
        current_design = unprint_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)

unprint_designs = ['iphone','robot','dodecahedron']
completed_models= []

print_models(unprint_designs,completed_models)
show_completed_models(completed_models)
        

(10)禁止函数修改列表
切片表示法[:] 创建列表的副本,(9)中unprint_designs最终会变成空列表,如果想要列表不为空,则调用函数时传进unprint_designs的副本
print_models(unprint_designs[:],completed_models)

def show_magicians(magicians):
    for magician in magicians:
        print(magician)

def make_great(magicians,great_magicians):
    while magicians:
        magician = magicians.pop()
        great_magicians.append(magician + ' the Great')
    print("--------显示结果-------")
    for great_magician in great_magicians:
        print(great_magician)

magicians = ['john','cindy','sarah','mike']
great_magicians = []
show_magicians(magicians)
make_great(magicians[:],great_magicians)
print(magicians)
        

(10)传递任意数量的实参
形参为带有*的空元组

def make_pizza(*toppings):
    '''概述制作pizza的配料'''
    print(toppings)

make_pizza('eggs')
make_pizza('mushrooms','green peppers','extra cheese')

输出结果

('eggs',)
('mushrooms', 'green peppers', 'extra cheese')
def make_pizza(*toppings):
    '''概述制作pizza的配料'''
    print("\nMaking a pizza with the following toppings: ")
    for topping in toppings:
        print("-" + topping)

make_pizza('eggs')
make_pizza('mushrooms','green peppers','extra cheese')

输出结果

Making a pizza with the following toppings: 
-eggs

Making a pizza with the following toppings: 
-mushrooms
-green peppers
-extra cheese

(11)结合使用位置实参和任意数量的实参
函数接受不同类型的实参, 必须在函数定义中将接纳任意数量实参的形参放在最后
(12)任意数量的关键字形参
形参**user_info 中的两个星号让Python创建一个名为user_info 的空字典, 并将收到的所有名称—值对都封装到这个字典中

def build_profile(first,last,**user_info):
    profile ={}
    profile['first_name'] = first #将姓和名加入到字典中
    profile['last_name'] = last
    #对字典user_info遍历,并将每个键值对加入到字典profile
 	 for key,value in user_info.items():
        profile[key] = value
    return profile #返回字典

user_profile = build_profile('albert','einstein',
                           location = 'princeton',
                           field = 'phsics')
print(user_profile)
  

8-13例题

def build_profile(first,last,**information):
    #创建一个空字典
    profile = {}
    #将姓和名存到空字典profile中
    profile['first_name'] = first
    profile['last_name'] = last
    #遍历字典information中所有的键值对并存到字典profile
    for key,value in information.items():
        profile[key] = value
    #返回字典profile
    return profile

personal_info = build_profile('Kim','Jennie',
                 country = 'Korea',
                 age = 21,
                 sex = 'female'
                 )
print(personal_info)
                 

4.将函数存储在模块中
模块 是扩展名为.py的文件, 包含要导入到程序中的代码
(1)导入整个模块
import + 模块名
调用被导入的模块中的函数,可指定导入的模块的名称pizza 和函数名make_pizza() , 并用句点分隔它们
(2)导入特定函数
from + 模块名 + import + 函数
使用这种语法, 调用函数时就无需使用句点
(3)使用as给函数指定别名
from pizza import make_pizza as mp
上面的import 语句将函数make_pizza() 重命名为mp() ; 在这个程序中, 每当需要调用make_pizza() 时, 都可简写成mp()
(4)使用as给模块指定别名
import pizza as p
则pizza.make_pizza()可以用p.make_pizza()表示
(5)导入模块 的所有函数
使用星号(* ) 运算符可让Python导入模块中的所有函数
from pizza import *
import 语句中的星号让Python将模块pizza 中的每个函数都复制到这个程序文件中。 由于导入了每个函数, 可通过名称来调用每个函数, 而无需使用句点表示法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
内容介绍 项目结构: Controller层:使用Spring MVC来处理用户请求,负责将请求分发到相应的业务逻辑层,并将数据传递给视图层进行展示。Controller层通常包含控制器类,这些类通过注解如@Controller、@RequestMapping等标记,负责处理HTTP请求并返回响应。 Service层:Spring的核心部分,用于处理业务逻辑。Service层通过接口和实现类的方式,将业务逻辑与具体的实现细节分离。常见的注解有@Service和@Transactional,后者用于管理事务。 DAO层:使用MyBatis来实现数据持久化,DAO层与数据库直接交互,执行CRUD操作。MyBatis通过XML映射文件或注解的方式,将SQL语句与Java对象绑定,实现高效的数据访问。 Spring整合: Spring核心配置:包括Spring的IOC容器配置,管理Service和DAO层的Bean。配置文件通常包括applicationContext.xml或采用Java配置类。 事务管理:通过Spring的声明式事务管理,简化了事务的处理,确保数据一致性和完整性。 Spring MVC整合: 视图解析器:配置Spring MVC的视图解析器,将逻辑视图名解析为具体的JSP或其他类型的视图。 拦截器:通过配置Spring MVC的拦截器,处理请求的预处理和后处理,常用于权限验证、日志记录等功能。 MyBatis整合: 数据源配置:配置数据库连接池(如Druid或C3P0),确保应用可以高效地访问数据库。 SQL映射文件:使用MyBatis的XML文件或注解配置,将SQL语句与Java对象映射,支持复杂的查询、插入、更新和删除操作。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值