02、day02_while循环,运算符,格式化输出,编码的初识
While循环
-
基本结构
while 条件: 循环体 -
初识循环
# 模拟循环播放列表歌曲 while True: print("我们不一样") print("左手指月") print("Shap of my heart") print("我想我是海") -
循环原理
![]()
-
打断?如何终止循环
- 改变条件
flge = True while flge: print("我们不一样") print("左手指月") flge = False print("Shap of my heart") print("我想我是海")将flge变成false也会执行完这次循环再停止,这只是变量的重新赋值而已,跟break有本质区别
练习①:输出1-100所有的数字
flag = 1 while flag <= 100: print(flag) flag = flag + 1练习②:计算1 + 2 + 3 + 4........+100的和
a = 1 sum = 0 while a <= 100: sum = sum + a a = a + 1 print(sum) -
# 打印输出1-100的偶数 count = 1 while count <= 100: if count % 2 == 0: print(count) count = count + 1break:循环中遇到break直接退出循环
while True: print("我们不一样") print("左手指月") print("Shap of my heart") break print("我想我是海") -
continue:退出本次循环继续下一次循环
当做while循环的底
while True: print(111) print(222) continue print(333)这里只会输出111,222,continue作为循环的底,不执行333
-
while else :while循环如果被break打断,则不会执行else里的语句
counte = 1 while counte < 5: print(counte) if counte == 2: break counte = counte + 1 else: print(666)
-
while循环用在哪?
需要重复之前的动作,比如输入用户名密码,可以错了重填(3次)
count = 1 while count <= 3: username = input("请输入用户名:") password = input("请输入密码:") code = 'qwer' your_code = input("请输入验证码:") if your_code == code: if username == 'xxx' and password == '123': print("登录成功") else: print("用户名或密码错误") else: print("验证码错误") count = count + 1
格式化输出
%占位符 s --> str , d, i, r
# 格式化输出
# %s表示占位符,
name = input('请输入你的姓名:')
age = input('请输入你的年龄:')
job = input('请输入你的工作:')
hobby = input('请输入你的爱好:')
msg = """
-------------------- info of %s --------------------
Name : %s
Age : %s
Job : %s
Hobbie : %s
-------------------- end --------------------
""" % (name,name,age,job,hobby)
print(msg)
# 在格式化输出中,如果只想表示一个%,而不是作为占位符使用,
msg = '我叫%s,今年%s,python的学习进度为1%%' % ('Murphy', 34)
print(msg)
- 当你遇到这样的需求:字符串中,想要某些位置变成动态可传入的,首先要考虑到格式化输出。
运算符
-
算术运算
+-*/
% 取余
** 幂次方
// 取整除,去商的部分
-
比较运算
==
!=
<, >
<=, >=
-
逻辑运算符
and
or
not
在没有()的情况下,优先级:not > and > or
# 在没有()的情况下,优先级:not > and > or,同一优先级从左至右依次计算 # 情况1:两边都是比较运算符 print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1) # True# 在没有()的情况下,优先级:not > and > or,同一优先级从左至右依次计算 # 情况1:两边都是比较运算符 print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1) # True # 情况2:两边都是整数 # x or y,x为真,值就是x,x为假,值是y???? print(1 or 2) print(3 or 2) print(4 or 2) print(-1 or 2) print(0 or 2)- 这里理解了好久,1和2的问题,1 or 2输出的就是1,1 and 2输出的就是2,why?百度一下去:
- (1)在纯and语句中,如果每一个表达式都不是假的话,那么返回最后一个,因为需要一直匹配直到最后一个。如果有一个是假,那么返回假
(2)在纯or语句中,只要有一个表达式不是假的话,那么就返回这个表达式的值。只有所有都是假,才返回假
(3)在or和and语句比较难表达,总而言之,碰到and就往后匹配,碰到or如果or左边的为真,那么就返回or左边的那个值,如果or左边为假,继续匹配or右边的参数。
————————————————
版权声明:本文为CSDN博主「TOMOCAT」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/TOMOCAT/article/details/85871579 - int --> bool,非零即True,0 是False
- 再者bool --> int True为1,False为0
编码初识
编码相当于密码本
最早的密码本:
ASCII码:只包含:英文字母,数字,特殊字符。
0000 0001 : a
0000 0101 : b
8bit (位)== 1byte(字节)
'hello123': 8byte
gbk: 英文字母,数字,特殊字符和中文。国标
一个英文字母: 0000 0001 : a 1字节
一个中文 中: 0000 0001 0100 0001 : 中 2字节
Unicode: 万国码:把世界上所有的文字都记录到这个密码本。
起初一个字符用2个字节表示:
0000 0001 0000 0011: a 2字节
0000 0001 0100 0001 : 中 2字节
后来为了涵盖全部文字:
0000 0001 0000 0011 0000 0001 0000 0011: a 4字节
0000 0001 0100 0001 0000 0001 0000 0011: 中 4字节
浪费空间,浪费资源。
Utf-8:升级:最少用8bit1个字节表示一个字符。
0000 0011 a 1字节
0000 0011 0000 0011 欧洲 2个字节
0000 0011 0000 0011 0000 0011 中: 3个字节
重点:
'中国12he' : GBK: 8个字节
'中国12he' : UTF-8: 10个字节
8bit = 1byte
1024byte = 1KB
1024KB = 1MB
1024MB = 1GB
1024GB = 1TB
1024TB = 1PB
1024TB = 1EB
1024EB = 1ZB
1024ZB = 1YB
1024YB = 1NB
1024NB = 1DB
比如:7.6MB ----> 7.6 * 1024 * 1024 * 8 bits
8bit (位)= 1byte(字节) 即1个字节=8个位


浙公网安备 33010602011771号