姜小白的Python日记Day8 字符串编码转换与函数简介

字符串编码准换
import sys
print(sys.getdefaultencoding())#打印当前文件使用的编码

s = “你好”
print(s.encode(“gbk”))
print(s.encode(“utf-8”))
print(s.encode(“gb2312”))

函数初识
import time
#函数是逻辑化和过程化的一种编程方式
#函数定义
def func1():
‘’‘文档描述’’’
print(“这是func1”)
return 0
#过程定义
def func2():
‘’‘文档描述’’’
print(“这是func2”)
x = func1()
y = func2()
print(“func1的返回值%s” %x)
print(“func2的返回值%s” %y)

#函数的好处:代码重用,保持一致性,可扩展性
def logger1():
time_fuction = “%y-%m-%d %x”#定义时间显示格式年月日时分秒
time_correct = time.strftime(time_fuction)
with open(“a.txt”,“a+”,encoding=“utf-8”) as f:
f.write("%s操作日志样例\n" %time_correct)
def function1():
print(“in the function1”)
logger1()
def function2():
print(“in the function2”)
logger1()
def function3():
print(“in the function3”)
logger1()
function1()
function2()
function3()

#函数返回值作用
我们需要函数执行结果,后续程序根据此结果进行判断
如果函数定义默认参数则调用时可不传入值

def test(x,y,z):
print(x,y,z)
test(1,3,2)#位置参数与形参一一对应
test(y=2,x=5,z=7)#关键参数不能写在位置参数之前

#参数组,参数组必须在函数定义最后
def test2(*args):#只接收N个位置参数传入成元组
print(args)
def test1(x,args):
print(x,args)
test1(1,2,3,4,5,6,7,8)
test2(
[1,2,3,4,5])#会被函数转换成一个元组

def test3(**kwargs):#只接收N个关键字参数传入字典内
print(kwargs)
test3(name = “alex”, age = “8”)

def test4(name,age=26,**kwargs):
print(name)
print(age)
print(kwargs)

test4(“alex”,sex = ‘m’,age = ‘3’)#调用函数时给默认参数赋值位置可以调整

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值