上次作业

 1 1、使用while循环输入 1 2 3 4 5 6 8 9 10
 2 方法一
 3 count = 0
 4 while count < 10:
 5     count += 1  # count = count + 1
 6     if count == 7:
 7         print(' ')
 8     else:
 9         print(count)
10 
11 方法二
12 count = 0
13 while count < 10:
14     count += 1  # count = count + 1
15     if count == 7:
16         continue
17     print(count)
18 
19 #2、输出 1-100 内的所有奇数
20 方法一:
21 count = 1
22 while count < 101:
23     print(count)
24     count += 2
25 
26 方法二:
27 count = 1
28 while count < 101:
29     if count % 2 == 1:
30         print(count)
31     count += 1
32 
33 #3、求1-2+3-4+5 ... 99的所有数的和
34 sum = 0
35 count = 1
36 while count < 100:
37     if count % 2 == 0:
38         sum = sum - count
39     else:
40         sum = sum + count
41     count += 1
42 print(sum)
43 
44 #4、用户登陆(三次机会重试)
45 #input 心中有账号,密码 while
46 i = 0
47 while i < 3:
48     username = input('请输入账号:')
49     password = int(input('请输入密码:'))
50     if username == '咸鱼哥' and password == 123:
51         print('登录成功')
52     else:
53         print('登录失败请重新登录')
54     i += 1    

1. 格式化输出

%s:str占位

%d:int占位 (digit)

想要输出3%,则再加一个%,即3%% 

%%只单纯显示%

 1 #格式化输出
 2 # % s d
 3 name = input('请输入姓名')
 4 age = input('请输入年龄')
 5 height = input('请输入身高')
 6 msg = "我叫%s,今年%s 身高 %s" %(name,age,height)
 7 print(msg)
 8 
 9 name = input('请输入姓名:')
10 age = input('请输入年龄:')
11 job = input('请输入工作:')
12 hobbie = input('你的爱好:')
13 
14 msg = '''------------ info of %s -----------
15 Name  : %s
16 Age   : %d
17 job   : %s
18 Hobbie: %s
19 ------------- end -----------------''' %(name,name,int(age),job,hobbie)
20 print(msg)

2. while else循环

while 条件:

else:

ps:被break打断不会执行else

1 count = 0
2 while count <= 5 :
3     count += 1
4     if count == 3:
5         continue
6     print("Loop",count)
7 else:
8     print("循环正常执行完啦")
9 print("-----out of while loop ------")

3. 初始编码

1. 最早的'密码本ascii 涵盖了英文字母大小写,特殊字符,数字。01010101

ascii 只能表示256种可能,太少,

2. 创办了万国码 unicode

16表示一个字符不行,32位表示一个字符。

A 01000001010000010100000101000001

B 01000010010000100100001001000010

Unicode 升级 utf-8 utf-16 utf-32

8位 = 1字节bytes

utf-8 一个字符最少用8位去表示,英文用8位 一个字节

欧洲文字用16位去表示 两个字节

中文用24 位去表示 三个字节

utf-16 一个字符最少用16位去表示

3. gbk 中国人自己发明的,一个中文用两个字节 16位去表示。

4. 换算

1bit 8bit = 1bytes

1byte 1024byte = 1KB

1KB 1024kb = 1MB

1MB 1024MB = 1GB

1GB 1024GB = 1TB

4. 逻辑运算

1. 优先级

() > not > and > or

1 #T or F
2 print(3>4 or 4<3 and 1==1)  # F
3 print(1 < 2 and 3 < 4 or 1>2)  # T
4 print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1)  # T
5 print(1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8)  # F
6 print(1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)  # F
7 print(not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6) # F

2. int与bool的转换

1. int---->bool

非0----True

0----False

2. bool---->int

True----1

False----0

3. x or/and y的返回值

1. x or y, x True, 则返回x

1 print(1 or 2)  # 1
2 print(0 or 2)  # 2

2. x and y, x True, 则返回y (与or相反)

1 print(1 and 2) #2
2 print(0 and 2) #0
3 print(2 or 1 < 3) #2
4 print(3 > 1 or 2 and 2) #True

ps:当int与bool同时存在时,按照优先级与从左向右的顺序