1. While循环
while 关键字 空格 条件 冒号
while 3>2:
print(1)
循环终止
1. break
终止循环
while 3>2:
print(2)
break
2. continue
终止当前循环,开始新的循环,在大循环中开始小循环
while 3>2:
print(2)
continue
print(1)
print(4)
这里只会输出 2 和 4 因为 continue 命令 导致 终止了大循环 所以不会执行 打印1 的命令
2. 字符编码
ascii 美国 256位 无中文
gbk 中国 2个字符
unicode 万国码 4个字符
utf-8 可变编码
字符可以是任何东西, 数字,汉字,英文。
\n 为代表键盘回车
%s, %d 为占位符
s为str 字符串, d=i 为整型
%() 为补位
%% 转义 代表了百分比
bit= 位
byte= 字节
3. 运算符
1. 比较运算符: >,<, =, ==(等于), !=(不等于)
2. 算法运算符: +-*/ //(整除) **(幂) %(除余)
3. 逻辑运算符: and, or, not
4. 成员运算符: in, not in
5. 赋值运算符: +=, -=, *=, /=, //=, **=, %=
print(1<2 and 0) print(False and 0) print(False or 0) print(1<2 or 0)
>>>0
>>>False
>>>0
>>>True
都为真的时候, or 选前边 and 选后边
都为假的时候, or 选后边 and 选前边
4. 格式化输出
while True:
name = input("姓名:")
age = int(input("年龄:"))
addr = input("住址:")
tel = int(input("电话:"))
msg = f'姓名:={name}, 年龄:={age}, 住址:={addr},电话:={tel}'
print(msg)
continue
运用到了while循环语句,continue代表了终止当前循环,开始新的循环,可以一直录入新的数据。
name = input("姓名:")
age = int(input("年龄:"))
addr = input("住址:")
tel = int(input("电话:"))
msg =‘姓名:%s,年龄:%d,住址:%s,电话:%d'%(name,age,addr,tel)
print(msg)
格式化输出的另外一种方式
name = input("姓名:")
age = int(input("年龄:"))
job = input("工作:")
salary = input("工资:")
info2 = '''
-------- info of {_name} --------
姓名:{_name},
年龄:{_age},
工作:{_job},
工资:{_salary}
''' .format(_name=name,
_age=age,
_job=job,
_salary=salary)#format函数里可以设置回车,可以想一想怎样做, 基于#姓名:{_name},年龄:{_age},工作:{_job},工资:{_salary}#没有回车
print(info2)
浙公网安备 33010602011771号