Python第四次学习笔记
1.函数传递任意数量的实参
传递元组
def make_pizza(*toppings):
"""打印顾客点的所有配料"""
print(toppings)
make_pizza('pepperoni')
make_pizza('mushrooms', 'green peppers', 'extra cheese')
形参名*toppings中的星号让Python创建一个名为toppings的空元组,并将收到的所有值都封装到这个元组中。
('pepperoni',) #返回一个值的元组
('mushrooms', 'green peppers', 'extra cheese')
循环遍历元组,并打印
def make_pizza(*toppings):
"""概述要制作的比萨"""
print("\nMaking a pizza with the following toppings:")
for topping in toppings:
print("- " + topping)
#调用函数
make_pizza('pepperoni')
make_pizza('mushrooms', 'green peppers', 'extra cheese')
返回结果
Making a pizza with the following toppings:
- pepperoni
Making a pizza with the following toppings:
- mushrooms
- green peppers
- extra cheese
形参和任意数量形参结合,必须把任意数量形参放最后。Python先匹配位置实参和关键字实参,再将余下的实参都收集到最后一个形参中
def make_pizza(size, *toppings):
"""概述要制作的比萨"""
print("\nMaking a " + str(size) +
"-inch pizza with the following toppings:")
for topping in toppings:
print("- " + topping)
#调用函数
make_pizza(16, 'pepperoni')
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
传递字典
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_info中的两个星号让Python创建一个名为user_info的空字典,并将收到的所有名称—值对都封装到这个字典中。
- 创建profile空字典,将名和姓加入到这个字典中。
- 遍历字典user_info中的键—值对,并将每个键—值对都加入到字典profile中,然后返回字典
#调用函数
user_profile = build_profile('albert', 'einstein', location='princeton',
field='physics')
print(user_profile)
打印结果
{'first_name': 'albert', 'last_name': 'einstein', 'location': 'princeton', 'field': 'physics'}
2.将函数储存在模块中
意义:将函数存储在被称为模块的独立文件中,再将模块导入到主程序中。通过将函数存储在独立的文件中,可隐藏程序代码的细节,将重点放在程序的高层逻辑上。
- 创建模块
新建一个名为pizza.py的文件
def make_pizza(size, *toppings):
"""概述要制作的比萨"""
print("\nMaking a " + str(size) +
"-inch pizza with the following toppings:")
for topping in toppings:
print("- " + topping)
- 导入模块
在pizza.py所在的目录中创建另一个名为making_pizzas.py的文件,这个文件导入刚创建的模块,再调用make_pizza()两次:
import pizza
pizza.make_pizza(16, 'pepperoni') #模块的名称pizza和函数名make_pizza(),并用句点分隔它们
pizza.make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')
Python读取这个文件时,代码行import pizza让Python打开文件pizza.py,并将其中的所有函数都复制到这个程序中。你看不到复制的代码,因为这个程序运行时,Python在幕后复制这些代码。
输出
Making a 16-inch pizza with the following toppings:
- pepperoni
Making a 12-inch pizza with the following toppings:
- mushrooms
- green peppers
- extra cheese
只需编写一条import语句并在其中指定模块名,就可在程序中使用该
模块中的所有函数。
- 导入特定的函数
语法:from module_name import function_name
通过用逗号分隔函数名,可根据需要从模块中导入任意数量的函数:
from module_name import function_0, function_1, function_2
上面的例子中仅导入make_pizza函数
from pizza import make_pizza
make_pizza(16, 'pepperoni') #不用句点,只用函数名称
make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')
- 用as给函数起别名
语法:
from module_name import function_name as fn
from pizza import make_pizza as mp
mp(16, 'pepperoni')
mp(12, 'mushrooms', 'green peppers', 'extra cheese')
- 用as给模块指定别名
语法:import module_name as mn
import pizza as p
p.make_pizza(16, 'pepperoni')
p.make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')
#调用函数make_pizza()时,可编写代码p.make_pizza()而不是pizza.make_pizza()
- 导入模块中的所有函数
语法:from module_name import *
from pizza import *
make_pizza(16, 'pepperoni')
make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')
import语句中的星号让Python将模块pizza中的每个函数都复制到这个程序文件中。由于导入了每个函数,可通过名称来调用每个函数,而无需使用句点表示法。
最好不要采用这种导入方法
如果模块中有函数的名称与你的项目中使用的名称相同,可能导致意想不到的结果:Python可能遇到多个名称相同的函数或变量,进而覆盖函数
最佳的做法:要么只导入你需要使用的函数,要么导入整个模块并使用句点表示法。
时间相关的模块
- 在python中表示时间的方式
1.时间戳:10位整数位和若干小数位,例如 1551153156.6358607
2.元组(struct_time): 含有9个元素的元组,例如 (tm_year=2011, tm_mon=9, tm_mday=28, tm_hour=10, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=271, tm_isdst=-1)
3.格式化字符串:格式化的时间字符串,例如 ‘2019-02-26 12:45:46’
- import time
time获取当前时间戳 使用场景:数据库存储时间一般整形
import time
curtime = time.time()
print(curtime) #1680009675.8575468
strtime = time.ctime()
print(strtime) #Tue Mar 28 21:21:54 2023
#格式化时间
t = time.strftime('%Y/%m/%d %H:%M:%S')
print(t) #2023/03/28 21:22:28
#localtime返回当前元祖格式时间
local = time.localtime()
print(local)
'''
time.struct_time(tm_year=2023, tm_mon=3, tm_mday=28,
tm_hour=21, tm_min=23, tm_sec=17, tm_wday=1,
tm_yday=87, tm_isdst=0)
'''
print(local.tm_year,'-',local.tm_mon,'-',local.tm_mday,local.tm_hour)
#2023 - 3 - 28 21
#让程序睡一会 sleep()
print('1111')
time.sleep(3)
print('2222') #程序过三秒后返回2222
- import datetime
#获取当天的时间
now = datetime.datetime.now()
print(now) #2023-03-28 21:28:03.403970
#获取当前的日期
today = datetime.date.today()
print(today) #2023-03-28
#案例.获取昨天的时间
def getYesDay():
#获取当天的时间
today = datetime.date.today()
#获取前一天的时间
oneday = datetime.timedelta(days=1)
yes = today - oneday
print(yes)
#调用函数
getYesDay() #2023-03-27
- 获取日历相关的信息:import calendar
#获取某个月的日历
# 格式:calendar.month(x,y)
#x:显示的年份
#y显示的月份
print(calendar.month(2023,3))
'''
March 2023
Mo Tu We Th Fr Sa Su
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
'''
#设置日历的第一天
calendar.setfirstweekday(calendar.SUNDAY)
print(calendar.month(2023,3))
'''
March 2023
Su Mo Tu We Th Fr Sa
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
'''
#获取某一年的日历:
# 格式:calendar.calendar(x)
#x:显示的年份
print(calendar.calendar(2023))
#判断某年是否为闰年?
print(calendar.isleap(2020)) #True
print(calendar.isleap(2023)) #False