02 While循环、格式化输出

1. while 循环
  why:大气循环, 吃饭,上课,睡觉,日复一日,歌曲列表循序环,程序中:输入用户名密码,
  whatwhile 无限循环。
  how
 1. 基本结构:

while 条件:
循环体

   4. 循环如何终止?
1. 改变条件

# 练习题: 1~ 100 所有的数字
count = 1
flag = True
while flag:
    print(count)
    count = count + 1
    if count == 101:
        flag = False
        
count = 1
while count < 101:
    print(count)
    count = count + 1
# 1 + 2 + 3 + ...... 100  的最终结果:

s = 0
count = 1
while count < 101:
    s = s + count
    count = count + 1
print(s)

2. break
4. continue

# continue : 退出本次循环,继续下一次循环
flag = True
while flag:
    print(111)
    print(222)
    flag = False
    continue
    print(333)
# while else: while 循环如果被break打断,则不执行else语句。
count = 1
while count < 5:
    print(count)
    if count == 2:
        break
    count = count + 1
else:
    print(666)
# and or not

# 1 在没有()的情况下,优先级:not > and > or,同一优先级从左至右依次计算
# 情况1:两边都是比较运算
# print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1)
# print(True or False)

# 情况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)

# print(1 and 2)

数据类型之间的转换

# str ---> int  : 只能是纯数字组成的字符串
s1 = '00100'
print(int(s1))
# int ----> str
i1 = 100
print(str(i1),type(str(i1)))

# int  ---> bool  : 非零即True ,0为False。
i = 0
print(bool(i))
# bool ---> int
print(int(True))  # 1
print(int(False))  # 0

4. 编码的初识

8bit == 1byte

gbk: 英文字母,数字,特殊字符和中文。国标
一个英文字母: 0000 0001 a
一个中文 中: 0000 0001 0100 0001 : 中

Utf-8:升级:最少用8bit1个字节表示一个字符。
0000 0011a 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

猜你喜欢

转载自www.cnblogs.com/MarlonKang/p/12430348.html