Python学习 day02

一、格式化输出

c/java:"xxxxx" + name + "xxxxxxx" + age

python:“xxxxxx%sxxxxxx%d” % (name. age)

在python中也可以用与c/java中类似的方法,但int不能直接与字符串相加,需做str()转换

一般情况,python中输出按照以上方式输出,%称为占位符,只有s%(字符串)和d%(数字)

ps:若想输出%,则需用%%表示。

二、编码

unicode  -- 万国码  在ASCII码基础上发明

在unicode基础上升级,有了:

utf-8  一个字符最少用8位去表示,英文用8位  一个字节

         欧洲文字用16位去表示                两个字节

         中文用24 位去表示                   三个字节

utf-16   一个字符最少用16位去表示

gbk是中国人自己基于ASCII码发明的

gbk与utf-*不能直接转换,需经过unicode

三、运算符

数学运算符:+,-,*,/,%,**(幂),//(地板除)

逻辑运算符:and,or,not

位运算符:&,|

比较运算符:略

赋值运算符:略

注意:Python中的“/”不同于java中的除,得到的结果是包含小数位的结果,“//”等同于java中的“/”

python中的逻辑运算符:

python中逻辑运算符两边可以直接连接数字,not(数字)得到的是对应的not(布尔型)

and和or的规律:

and 后面非0返回后面的数,后面为0返回前面的数,or相反,如下

7 and 9 = 9

9 and 7 = 7

0 and 6 = 0

6 and 0 = 0

7 or 9 = 7

0 or 6 = 6

若True/False通过逻辑运算符于数字相连,先按短路方式判断,然后按数字的逻辑运算方式运算,如下:

True and 0 = True

False and 0 = False

True and 2 = 2

False and 2 = False

0 and True = 0

0 and False = 0

2 and True = True

2 and False = False

True or 0 = True

False or 0 = 0

True or 2 = True

False or 2 = 2

……

猜你喜欢

转载自www.cnblogs.com/misutang/p/9427448.html