一、格式化输出
占位符%,字符串占位符%s,数字占位符%d
第一种
name = input('姓名') age = input('年龄') hobby = input ("爱好") msg="我叫%s,我今年%d,我喜欢%s"%(name,int(age),hobby) print(msg)
第二种
dic = {'name': '老男孩', 'age': 51, 'hobby': '吃'}
msg = '我叫%(name)s,我今年%(age)d,我喜欢%(hobby)s' % dic
print(msg)
第三种
在格式化输出中单纯的显示% 用%% 解决
name=input('输入名字')
age=input('年龄')
msg='我叫%s,今年%d,学习进度是1%%' % (name,int(age))
print(msg)
二、while else
如果循环被break打断,程序不会走else
例如:
count=1 while True: print(count) if count==3:break count+= 1 else: print('正常')
count=1
flag=True
while flag:
print(count)
if count ==3:
flag=False
count+=1
else:
print('正常'
三、in not in
判断子元素是否在原字符串(字典,列表,集合)中:
s = 'abcdefghijklmn' print('abc' in s) print('ds' in s) print('abc' in s) print('qwe' not in s)
应用于敏感词排查
comment = input('留言板') if '进行' in comment: print('用词过于随便,请您三思')
四、逻辑运算符 and or not
第一种:前后都是比较运算
优先级:()>not>and>or,同一个优先级,从左至右依次计算。
print(1 > 2 and 3 < 4 and 3 > 2 or 2 < 3) True
print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1) True
print(1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8) False
第二种:前后都是数值运算
x or y if x True,则 return x,否则 return y
x and y if x True,则return y,否则return x
int ---> bool 非0即True,0为False
bool---> int True 1 False 0
print(int(True))
print(int(False))
print(bool(100))
print(bool(0))
print(1 or 3) 1
print(1 or 3)1
print(2 or 3)2
print(0 or 3)3
print(-1 or 3)-1
print(1 and 2)2
print(0 and 2)0
第三种:混合。
print(1 > 2 or 3 and 4)4
print(2 or 2 > 3 and 4)2
print(0 or 2 > 3 and 4)False
五、编码
计算机在存储和传输的时候,用的是 01010101的二进制编码。
最早的是asiic 包含数字,英文,特殊字符。8位,8位 = 1 byte 表示一个字符。现在Python2x还在用。
万国码unicode,将所有国家的语言包含在这个密码本。
初期:16位,两个字节,表示一个字符。
升级:32位,四个字节,表示一个字符。
优点,大而全,缺点,占据太多内存,资源浪费。
utf-8。最少用8位(一个字节),表示一个字符。(Python3x的默认编码)
英文:a :00010000 用8位表示一个字符。
欧洲:00010000 00010000 16位两个字节表示一个字符。
亚洲 中 :00010000 00010000 00010000 24位,三个字节表示一个字符。
gbk:国标。(国产)
只包含:英文中文。
英文:a :00010000 8位,一个字节表示一个字符。
中文:中:00010000 00010000 16位,两个字节表示一个字符。
8 bit = 1byte
1024byte=1kb
1024kb = 1MB
1024MB = 1GB
1024GB = 1TB