1.开发环境搭建
2.数据类型
3.变量
4.运算符
5.输入输出
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
print()函数本身用于输入value数据到标准输出【控制台界面】
sep参数控制分隔符号~
end参数用于控制输出后的显示结果是否需要换行,默认换行
file参数~默认指定了stdout标准输出
6.程序结构
7.组合数据类型
8.辅助函数
9.函数操作
def函数名称(函数中代码执行需要的参数列表):
函数中执行的代码块
[retuen 返回值]
文档中:如果在操作语言说明中出现说明中出现了方括号代表这一部分内容是可选的
函数名称:
def test():
函数中的代码
test() 调用执行test函数,执行test函数中的代码
test2 = test 将函数test赋值给变量test2,并不会执行函数test
test2() 执行test2函数~ test2中存放的是test-> 执行test()函数
函数参数的定义:(用一些示例进行说明)
位置参数:
例一:
def changfang(chang,kuang):
area=chang*kuang
zhouchang=2*(chang+kuang)
return area,zhouchang
diji=0
b=3
while diji<=b:
ch=int(input("请输入长方形的长:"))
ku= int(input("请输入长方形的宽:"))
a=changfang(ch,ku)
diji+=1
print("第",diji,"个""长方形的面积和周长:"+str(a))
例二:
def yuan(R):
area=3.14**R
zhouchang = 2*3.14*R
return area,zhouchang
r=yuan(3)
def test(a, b, c, d):
print(.....)
操作过程中,调用执行时~实际参数按照形式参数的前后顺序[位置]依次赋值
test(1,2,3,4)
默认值参数:
例:
def changfang(chang,kuang=6):
area = chang * kuang
zhouchang=2*(chang+kuang)
return area,zhouchang
ch=int(input("请输入长方形的长:"))
a=changfang(ch)
print("长方形的面积和周长:"+str(a))
注:可对宽进行赋值,如果不赋值将使用默认值
def test(a, b, c, d=100):
print(.....)
操作过程中,函数声明中~某个形式参数可以直接赋值~该形式参数就是默认值参数;默认值参数可以在调用时不传递数据~使用默认数据进行运算。
test(1,2,3) d自动赋值默认100运算
test(1,2,3,4) d赋值4进行运算
注意:默认值参数一般放在最后面
可变参数:
例:
可变参数:可以接收0~n个数据
def shu(*smg):
sum=0
for x in smg:
sum=sum+x
print(sum)
shu(55,99,999)
def test(a, *args): args: arguments 参数
print(.....)
操作过程中,可以通过符号*,声明某个可以接受多个数据的可变参数~ 可变参数可以接受0~n个数据。
test(1,2,3)
a赋值1,args赋值(2,3)
test(1,2,3,4)
a赋值1, args赋值(2,3,4)
可变参数可以将接受到的多个参数~自动封装成元组进行操作。自动封装过程~某些资料:自动封箱/自动装箱
注意:可变参数一般放在最后面
关键字参数:
例:
def shu(name,**things):
print("参加party的人员:",name)
print("参加party需要的物资",things)
shu('李白',wine='82年的拉菲')
def test(a, **kwargs): kwargs : keyword arguments 关键字参数
print(.....)
操作过程中,可以通过符号**,声明某个可以接受多个key-value键值对数据,并且自动装箱成字典接受到函数中进行处理
test(1, name="jerry", age=12)
a赋值1,kwargs赋值{"name": "jerry", "age": 12}
注意:关键字参数一般放在最后面
万能参数:
例:
def cpy(*args,**kwargs):
print("出生年份:",args)
print("姓名:资产",kwargs)
a=[1990,10,20]
b={"小明":6000000}
cpy(*a,**b)
def test(*args, **kwargs):
pass
*args可以接受任意多个的单个数据
**kwargs可以接受任意多个的key-value键值对数据
按照函数调用规范,单个数据放在前面,key-value数据放在后面。所以上面的参数列表:万能参数,可以接受任意数据当成函数的参数进行处理。
强制关键字参数:
def party(name, *, wine, food):
print(name)
print(wine)
print(food)
party('老刘', wine='二锅头', food='牛排')
party('老刘', food='牛排', wine='二锅头')
def test(*, name, age):
pass
声明在符号*后面的所有参数,都是强制关键字参数~
理由?为什么要用它?
特点:一旦声明~函数中定义了的参数名称,必须在调用的时候使用它们作为key值
test(name="tom", age=100) right
test(x="jerry", y=10) error
10.字符串str
s = "我叫{}, 今年{}".format("tom", 30)
s = "我叫{name}, 今年{age}".format(name="tom",age=30)
s = "我叫{name}, 今年{age}".format(age=30, name="tom")
s = "我叫%s, 今年%d" % ("jerry", 30)
11.文件IO
1.将程序中的数据,写入到文件中
file=open('./data/1.1.txt','w',encoding='utf-8')
message='hello 世界'
file.write(message)
file.close()
2.将文件中的数据,读取到程序中
按照只读的方式打开文件
file=open(file='./data/1.1.txt',mode='r',encoding='utf-8')
##从文件中读取数据,展示到控制台
info=file.read()
print(info)
file.close()
3.文本文件的追加
file=open('./data/1.1.txt','w',encoding='utf-8')
message='人说,林深时见鹿,海蓝时见鲸,夜深时见你。'
file.write(message)
message1='\n但是,林深时雾起,海蓝时浪涌,夜神时梦续.\n'
file.write(message1)
message3='\n你可知:鹿踏雾而来,鲸随浪而起,你未曾转身,怎知我已到来..\n'
file.write(message3)
file.close()
4.二进制文件的操作
file=open(file='D:/图片/world.jpg',mode='rb')
# print(file.read())
file2=open(file='./data/a.jpg',mode='wb')
file2.write(file.read())
file=open(file='D:/图片/am.jpg',mode='rb')
file2=open(file='./data/aini.jpg',mode='wb')
file2.write(file.read())
5.文件的快捷操作:with语法
with open('D:/图片/鬼刀.jpg','rb')as file1:
with open('./data/'+file1.name[file1.name.rfind('/'):], 'wb')as file2:
file2.write(file1.read())
import json
users={'admin':{'username':'admin','password':'123'}}
with open('./data/3.0','w')as file:
json.dump(users,file)
with open('./data/3.0','r')as file:
users=json.load(file)
print(users,type(users))
import shelve
users={'admin':{'username':'admin','password':'123'}}
file=shelve.open('./data/3.1')
file['users']=users
print(file['users'], type(file['users']))
import marshal
s="字符串"
i=19
f=3.1415
d={'username':'admin','password':'123'}
x=[s , i , f , d]
# with open('./data/4.0','wb')as file:
# marshal.dump(len(x),file)
# for x1 in x:
# marshal.dump(x1,file)
with open('./data/4.0','rb')as file:
n=marshal.load(file)
for b in range(n):
a=marshal.load(file)
print(a)