python基础二

while else

正常循环完毕,执行else下代码

如果被break打断,则不走else,直接跳出

格式化输出

格式化:format

python3中有%s(字符串型) %d(数字型)

python2中还有%f(浮点型)

#第一种写法
name = input("请输入名字")
age = int( input("请输入年龄"))
score = input("请输入分数")
msg = "我叫%s,今年%d,我得分数%s"%(name,age,score)
print(msg)
#第二种写法
name1 = input("请输入名字")
age1 = int(input("请输入年龄"))
score1 = input("请输入分数")
msg = "我叫%(name)s,今年%(age)d,我的分数%(score)s"%{"name" : name1,"age" : age1,"score": score1}
print(msg)

#第二种写法{}里面的内容可以改变顺序,第一种必须按照顺序写

如果想显示%,需要输入两个%,例如:

运算符

逻辑运算符

优先级:()> not > and > or

                同一优先级,从左到有

比较的是数字 X or Y 如果X为True ,结果为X 否则Y

and与or相反

0是False  非0是True

#in ,not in 用法
comment = input('请输入你的评论')
s1 = '苍老师'
if s1 in comment:
    print('有非法字符,从新输入')
else:
    print('评论成功')

编码历史

ascii:8位 1字节   表示1个字符

unicode:创建之初  16位 2字节 表示一个字符

                后期          32位 4字节 表示一个字符

由于unicode浪费资源  升级成  utf-8:

                                                    英文             8位      1字节    表示一个字符

               欧洲文字     16位     2字节   表示一个字符

               中国/亚洲     24位    3字节   表示一个字符

gbk:国标   只能中国用人自己用   16位  2字节  表示一个字符

单位转换:

1bytes = 8bit

1024bytes = 1kb

1024kb = 1mb

1024mb = 1gb

1024gb = 1tb

原文地址:https://www.cnblogs.com/pygg/p/8329619.html