自学python

                                    第六天

1.实现utf-8,unicode和gbk的转换

with open("yesterday2","r",encoding="utf-8") as f:
    for line in f:
        print(line)


'''
        utf-8->unicode(decode)
        unicode->utf-8(encode)
        gbk->unicode(decode)
        unicode->gbk(encode)        
    

'''
s = "你好"
print(s.encode("gbk"))
print(s.encode("utf-8"))
print(s.encode("utf-8").decode("utf-8"))
2.函数
def func1():
    print('in the func1')
    return 0#定义了一个函数

def func2():
    print('in the func2')#定义了一个过程

x = func1()
y = func2()#过程是没有返回值的函数

print('from func1 return is %s' %x)
print('from func2 return is %s' %y)

import time
def logger():
    time_format = '%Y-%m-%d %X'
    time_current = time.strftime(time_format)
    with open('a.text','a+') as f:
        f.write('%s end action\n'%time_current)

def test1():
    print('test1 starting action')
    logger()

def test2():
    print('test2 starting action')
    logger()

def test3():
    print('test3 starting action')
    logger()


test1()
test2()
test3()

'''
函数三大优势:
1.代码重用
2.保持一致性
3.可拓展性
'''

def test1():
    print("in the test1")

def test2():
    print("in the test2")
    return 0

def test3():
    print("in the test3")
    return 1,'hello',['alex','wuxiubo'],{'name':'alex'}

x = test1()
y = test2()
z = test3()

print(x)
print(y)
print(z)

def test(x,y):
    print(x)
    print(y)

#(y = 2,x = 1)#与形参顺序无关
#test(1,2)#与形参一一对应


'''
参数
1.形参和实参
2.位置参数和关键字(标准调用:实参和形参位置一一对应;关键字调用:位置无需固定)
3.默认参数
4.参数组

'''

def test(x,y=2):
    print(x)
    print(y)

test(1,3)

#默认参数特点:调用函数的时候,默认参数非必须传递
#用途:1.默认值

def test(*args):#参数不固定的情况下
    print(args)

test(1,2,3,4,5,6,7)
test(*[1,2,3,4,5,6,7])#  args = tuple([1,2,3,4,5,6,7])变成元组

def test1(y,*args1):
    print(y)
    print(*args1)

test(1,2,3,4,56,76,7,4,3)
#  *接收元组

#  **kwargs:把n个关键字参数,转换成字典的方式
def test2(**kwargs):
    print(kwargs)
    print(kwargs['name'])
    print(kwargs['age'])


test2(name = 'alex',age = 8,sex = 'f')

test2(**{'name':'alex','age':8})#26



阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页