while循环,格式化输出,常用运算符,编码
一、while循环
1.基本循环
while 条件:
# 循环体
# 如果条件为真,那么循环则执行
# 如果条件为假,那么循环不执行
while True:
print('I love python!')
条件为真,则一直循环,成为死循环,改变条件,当条件为假时停止循环
flage = True
str_num = input('请输入要比较的数字:')
print('进入循环')
while flage:
if '3' > str_num:
print('执行循环')
else:
print('终止循环')
flage = False
print('退出循环')
2.使用while计数
count = 0
while True:
count = count + 1
print(count)
此时,代码会循环执行
count = 0
while count < 100:
count = count + 1
print(count)
此时,打印到100会自动停止
3.break关键字
除了改变条件停止循环,还可以使用break关键字来停止循环
num = 1
while num < 6:
print(num)
num = num + 1
break
print('结束了')
当程序执行到break的时候就结束了.break就是结束当前这个while循环的 break以下的代码都不执行,最后的num值为2,但没有打印
4.continue关键字
continue用于结束当前循环,继续下次循环
num = 1
while num <6:
print(num)
num+=1
continue
print("end")
break是终止循环,continue是跳出本次循环,继续下次循环
5.while else 使用
# 循环一
while True:
if 3 > 2:
print('你好')
break
else:
print('不好')
# 循环二
while True:
if 3 > 2:
print('你好')
print('不好')
循环一打印一个你好之后,碰到break就停止了
循环二是死循环,会一直打印你好
循环一将3>2改成3<2这个条件就不成立,然后执行了else里打印了不好
6.while else 练习
首先让用户输入序号选择格式如下:
0.退出
1.开始登录
如果用户选择序号0 就提示用户退出成功
如果用户选择序号1就让用户输入用户名密码然后进行判断,正确就终止循环,错误重新输入
# print('请选择数字0或1')
# num = int((input('请输入选择的数字:')))
# while num == 1:
# name = input('请输入用户名:')
# password = input('请输入密码:')
# if name == 'python' and password == '123':
# break
# else:
# print('用户名或密码错误,请重新输入!')
# else:
# print('退出成功')
二、格式化输出
name = input('请输入姓名:')
age = input('请输入年龄:')
job = input('请输入职业:')
hobby = input('请输入爱好:')
a = '------------ info of Alex Li ----------'
b = 'Name:'
c = 'Age:'
d = 'Job:'
e = 'Hobby:'
f = '------------- end ----------------'
print(a+'\n'+b+name+'\n'+c+age+'\n'+d+job+'\n'+e+hobby+'\n'+f)
# 运行结果
------------ info of Alex Li ----------
Name:meet
Age:18
Job:it
Hobby:3
------------- end ----------------
1.1 %s — 字符串类型
name = input('请输入姓名:')
age = input('请输入年龄:')
job = input('请输入职业:')
hobby = input('请输入爱好:')
msg = '''
------------ info of Alex Li ----------
Name : %s
Age : %s
job : %s
Hobbie: %s
------------- end ----------------
'''
print(msg%(name,age,job,hobby))
% 是占位符,s 代表字符串类型
1.2 %d|%i — 数字类型
name = input('>>>')
s1 = '1234%d'%int(name)
s2 = '1234%i'%int(name)
print(s1)
print(s2)
结果:
>>>89
123489
123489
# %d和%i这种格式化只能用数字来填补占位
1.3 %% — 转义
num = input('>>>')
s= '目前学习进度:%s%%'%num
print(s)
结果:
>>>80
目前学习进度:80%
# 如果我们字符串中想要显示单独的%就需要用来个%%来转义,不然程序会认为那是一个占位
while else 练习
print('请选择数字0或1')
num = int((input('请输入选择的数字:')))
while num == 1:
name = input('请输入用户名:')
password = input('请输入密码:')
if name == 'python' and password == '123':
break
else:
print('用户名或密码错误,请重新输入!')
else:
print('退出成功')
三、常用运算符
除了加减乘除外,计算机还可以进行多种运算,例如:算数运算、比较运算、逻辑运算、赋值运算、成员运算
1.算术运算
a = 10, b = 20 (ctrl + T表格)
| 描述 | 运算符 | 实例 |
|---|---|---|
| + | 加 | a+b=30 |
| - | 减 | a-b=-10 |
| * | 乘 | a*b=300 |
| / | 除 | b/a=2 |
| % | 取模,余数 | b%a=0 |
| ** | 取幂 | a**3=1000 |
| // | 取商的整数部分 | 9//2=4 |
2.比较运算
a = 10, b = 20
| 运算符 | 描述 | 实例 |
|---|---|---|
| == | 等于,比较是否相等 | a==b,False |
| != | 不等于 | a != b,True |
| > | 大于 | a > b,False |
| < | 小于 | a < b,True |
| >= | 大于等于 | a >= b,False |
| <= | 小于等于 | a <= b,True |
3.赋值运算
a = 10, b = 20
| 运算符 | 描述 | 实例 |
|---|---|---|
| = | 赋值运算符 | c=a+b |
| += | 加法赋值运算符 | c+=a即c=c+a |
| -+ | 减法赋值运算符 | c-=a即c=c-a |
| *= | 乘法赋值运算符 | c=a即c=ca |
| /= | 除法赋值运算符 | c/=a即c=c/a |
| %= | 取模赋值运算符 | c%=a即c=c%a |
| **= | 幂赋值运算符 | c **a =即c = c ** a |
| //= | 取整除赋值运算符 | c//=a即c=c//a |
4.逻辑运算
| 运算符 | 描述 | 实例 |
|---|---|---|
| and | 与,a为False, a and b 为False,否则返回它的计算值 | (a and b) 返回False |
| or | 或 ,a为True, a or b 为True,否则返回它的计算值 | (a or b) 返回True |
| not | 非, a为True, not a 为False | not(a and b) 返回True |
在没有()的情况下not 优先级高于 and,and优先级高于or,即优先级关系为( )>not>and>or,同一优先级从左往右计算。
判断下列逻辑语句的True,False。
x or y , x为真,值就是x,x为假,值是y;
x and y, x为真,值是y,x为假,值是x。
3>4 or 4<3 and 1==1 F
1 < 2 and 3 < 4 or 1>2 T
2 > 1 and 3 < 4 or 4 > 5 and 2 < 1 T
not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 F
8 or 4 8
0 and 3 0
0 or 4 and 3 or 7 or 9 and 6 3
面试会考##
and(与) or(或) not(非)
and : 条件1 and 条件2 两个条件都为真时才是真
print(3 > 2 and 4 < 8)
True and True
or : 条件1 or 条件2 两个条件只要有一个为真就是真
print(3 > 2 or 5 < 1)
True or False
and 运算都为真的时候选择and后边的内容
and 运算都为假的时候选择and前边的内
or 运算都为真的时候选择or前面的内容
or 运算都为假的时候选择or后面的内容
逻辑运算符的优先级: () > not > and > or 从左向右执行
5.成员运算
in not in :
判断子元素是否在原字符串(字典,列表,集合)中:
例如:
#print('喜欢' in 'dkfljadklf喜欢hfjdkas')
#print('a' in 'bcvd')
#print('y' not in 'ofkjdslaf')
四. 编码初识
ascii码:一个英文占一个字节,用8位表示,不支持中文
gbk: 国标,一个英文用一个字节,一个中文用两个字节
Unicode:万国码,中文英文都用四个字节
utf-8:可变长的编码,英文一个字节,欧洲两个字节,亚洲三个字节
单位转换:
1字节 = 8位
1byte = 8bit
1024byte = 1KB
1024KB = 1MB
1024MB = 1GB
1024GB = 1TB
常用到TB就够用了
浙公网安备 33010602011771号