python基础(运算符、编码、格式化、流程控制语句while)
一、运算符:
运算符类型:算术运算,赋值运算,比较运算,成员运算,位运算,逻辑运算等等
算数运算符:
格式:+ - * / % ** //
例子:3 + 4, 5//2
特例:
% 取余运算用法
规则:取余运算在做负数运算时,最后结果值为:被除数-除数*(被除数/除数所得结果最相近的两个值中取最小值做运算)
例子:
-10 % 3 = 2 (解析:-10 / 3 中最相近的两个结果为-3和-4,这里取最小值-4值,再用-10 - (3 * -4) = 2)
赋值运算符:
格式:= += -= *= /= ....
例子:sum += 1
比较运算符:
格式:== > < >= <= !=
例子:5 > 3
成员运算
格式:in not in
例子: 'a' in 'apple'
位运算:
格式:| & ^ ~ << >>
例子:略
逻辑运算:
格式:and or not
优先级:() > not > and > or
例子:

1 print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1) 2 print(1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 10)

1 True 2 True
规则:x or y if x is True, return x,else y
例子:

1 print(6 or 2 > 1) 2 print(3 or 2 > 1) 3 print(0 or 5 < 4) 4 print(5 < 4 or 3) 5 print(2 > 1 or 6) 6 print(3 and 2 > 1) 7 print(0 and 3 > 1) 8 print(2 > 1 and 3) 9 print(3 > 1 and 0) 10 print(3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2)

1 6 2 3 3 False 4 3 5 True 6 True 7 0 8 3 9 0 10 2
二、编码初识:
ASCII码:
早期7位为一段进行编码,但为了后期发展,拓展了一位为8位为一段进行编码
Unicode:
别名:万国码(满足全世界通用编码)
4bytes表示一个字符
utf-8:
定义:为了节约空间使用,在万国码的基础上进行编码改型
特点:
原ASCII码类型通用1bytes
欧洲文字:2bytes
中国文字:3bytes
例:
s2 = '老boy' : utf-8编码:6个字节
gbk:gbk2312
定义:只适合本国使用的编码规则
特点:
原ASCII码类型通用1bytes
中国文字:2bytes
例:
s2 = '老boy' : gbk编码:5个字节
三、位与字节的换算关系:
8bit = 1 bytes
1024bytes = 1Kb
1024Kb = 1Mb
1024Mb = 1Gb
1024Gb = 1Tb
四、格式化输出:
% 占位符 s:字符串类型 d:数字 i:数字 # r 原形毕露

1 name = input('请输入姓名:') 2 age = input('请输入年龄:') 3 job = input('请输入工作:') 4 hobby = input('请输入爱好:') 5 6 msg = ''' 7 ------------ info of %s ----------- 8 Name : %s 9 Age : %d 10 job : %s 11 Hobbie: %s 12 ------------- end ----------------- 13 ''' % (name,name,int(age),job,hobby)

1 name = input("请输入姓名:") 2 age = input("请输入年龄:") 3 sex = input("请输入性别:") 4 5 print("我的姓名是%s, 我的年龄是%s, 我的性别是%s" % (name, age, sex)) 6 print("我的姓名是{0}, 我的年龄是{1}, 我的性别是{2}".format(name, age, sex)) 7 print("我的姓名是"+ name +", 我的年龄是"+ age +", 我的性别是"+ sex)
五、循环
方式二:
格式:
while 条件:
循环体
代码:

1 # 打印1-100偶数: 2 3 count = 0 4 while True: 5 count += 1 6 if count % 2 == 0: 7 print(count) 8 if count == 100: 9 break

1 count = 0 2 while count < 100: 3 count += 2 4 print(count)

1 # 打印1-100数字: 2 3 FLAG = True 4 NUM = 0 5 6 while FLAG: 7 print(id(NUM)) 8 NUM += 1 9 print(NUM, end=' ') 10 if NUM == 100: 11 FLAG = False 12 print('\n打印完成!')
方式二:
格式:
while 条件:
循环体
continue
循环体
规则:
结束本次循环,继续下一次循环
代码:

1 while True: 2 print(111) 3 print(222) 4 continue 5 print(333)

1 111 2 222 3 111 4 222 5 ....

1 # continue用法: 2 3 count = 0 4 while count < 5: 5 count += 1 6 if count == 3: 7 continue 8 print(count)
方式三:
格式:
while 条件:
循环体
else:
语句体
代码:

1 # 满足条件执行循环体,当不满足条件时执行else语句体 2 3 count = 0 4 while count < 5: 5 count += 1 6 print(count) 7 else: 8 print(666)

1 # 跳出循环不再执行else语句体 2 3 count = 0 4 while count < 5: 5 count += 1 6 print(count) 7 if count == 3: 8 break 9 else: 10 print(666)
https://www.cnblogs.com/WiseAdministrator/