python character encoding conversion concept + tuple

Original link: https://www.jianshu.com/

Character encoding conversion

Here Insert Picture Description


#coding:gbk                 //此处必声明 文件编码(看右下角编码格式)

#用来得到python默认编码
import sys
print(sys.getdefaultencoding())

#python本身所有数据类型默认Unicode (与文件编码无关)
s="你好"

#encode得到的其他编码是byte类型 decode得到的Unicode是str类型
print(s.encode("utf-8").decode("utf-8").encode("gb2312").decode("gb2312"))

@@@@@@@@@@@@@@@@@@@@@@@@
Summary: Everything coding can be decoded to Unicode and vice versa biggest Unicode encoding can be converted to other forms of

ASCII==>GB2312 >GB18030> GBK (Chinese common coding) encoding the evolution of Chinese
ASCII English accounted for an 8 byte no Chinese
Unicode Unicode all characters are 2 bytes 16
= >>>>
upgrade to a variable length coding UTF-8 all English characters accounted for in accordance with the ASCII code byte Chinese characters, 3 bytes

python 3.0 default Unicode format

======================================
function operates
1. Code reuse
2. consistency
3 scalability

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:857662006 
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
#返回数=0个 返回none
def fun1():
    print(1)

#返回数=1个 返回这个数
def fun2():
    print(2)
    return 0

#返回数>1个 返回元组组合
def fun3():
    print(3)
    return 1,[0,1,3,6],{"sa":"bi"}


def num(x,y=2):
    print(x,y)
num(1,2)        #与形参列表一一对应
num(y=1,x=2)    #位置参数都标出 与顺序无关
num(2,y=3)      #关键参数只能放于位置参数后
num(2)          #默认参数非必传 但也可给 并覆盖

def test(x,*args):            #参数组 形参以*开头 只能接受位置参数 不能接受关键参数
    print(x)                    #取出首位  接受多个参数其他位变为元组
    print(args)
test("23",23,1,4353,["32",234],{"s":2})
test(*[1,32,43,2])


#**kwargs
def test2(**kwargs):            #接受关键字参数变为字典形式
    print(kwargs)
test2(name='cf',age=20,sex="man")
test2(**{'name':'al','age':'10','sex':'f'})

to sum up:

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:857662006 
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
def test3(name,age=18,*args,**kwargs):      #按形参顺序 *args(反元组)位于一般形参后 **kwargs位于最后
    print(name)
    print(age)
    print(args)
    print(kwargs)
test3('cf',12,"s",sex="nan")

Output:

cf
12
('s',)        #位置参数变为元组
{'sex': 'nan'}  #关键字参数变为字典

Guess you like

Origin blog.csdn.net/sinat_38682860/article/details/100560223