python基础 / DAY2(练习题)
#python基础 / DAY2(练习题)
1.判断下列逻辑语句的True,False
1)1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 T
2)not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 F
3) 1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8 and 4 > 6 or 3 < 2 F
2、求出下列逻辑语句的值:
1),8 or 3 and 4 or 2 and 0 or 9 and 7 8
2),0 or 2 and 3 and 4 or 6 and 0 or 3 4
3),5 and 9 or 10 and 2 or 3 and 5 or 4 or 5 9
3、下列结果是什么?:
1)、6 or 2 > 1 6
2)、3 or 2 > 1 3
3)、0 or 5 < 4 F
4)、5 < 4 or 3 3
5)、2 > 1 or 6 T
6)、3 and 2 > 1 T
7)、0 and 3 > 1 0
8)、2 > 1 and 3 3
9)、3 > 1 and 0 0
10)、3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2 2
4. 简述变量命名规范:
数字,字母,下划线组成 但不能为数字开头
5. 请问name = input(“>>>”) 中的name变量是什么数据类型?
str字符串
6.if条件语句的基本结构?
If 条件:
执行语句
7.while循环语句基本结构?
While 条件:
执行语句
8.写代码:计算 1 - 2 + 3 ... + 99 中除了88意外所有数的总和?
1 #第一种方法 结果为138 2 num = 0 3 sum = 0 4 while num < 99: 5 num += 1 6 if num == 88: 7 continue 8 else: 9 if num % 2 != 0: 10 sum = sum + num 11 else: 12 sum = sum - num 13 print(sum) 14 #第二种方法 结果为-50 15 num = 0 16 sum = 0 17 onoff = True 18 while num < 99: 19 num += 1 20 if num == 88: 21 continue 22 else: 23 if onoff: 24 sum += num 25 onoff = False 26 else: 27 sum -= num 28 onoff = True 29 print (sum)
#第三种方法
9.⽤户登陆(三次输错机会)且每次输错误时显示剩余错误次数(提示:使⽤字符串格式化):
1 saved_username = 'derezzed' 2 saved_password = '123456' 3 num = 1 4 sum = 3 5 warning = '''------------WARNING!!-------------------- 6 你还剩下 %s 次机会 7 ------------END-------------------------''' 8 while num <= sum: 9 username = input("please enter your username:") 10 password = input("please enter your password:") 11 count = sum - num 12 if username == saved_username and password == saved_password: 13 print("login is succed!") 14 break 15 else: 16 print(warning %count) 17 num +=1
10. 简述ascii、unicode、utf-8编码关系?
ASCII(American Standard Code for Information Interchange,美国标准信息交换代码)是基于拉丁字母的一套电脑编码系统,主要用于显示现代英语和其他西欧语言,其最多只能用 8 位来表示(一个字节),即:2**8 = 256,所以,ASCII码最多只能表示 256 个符号。
Unicode(统一码、万国码、单一码)是一种在计算机上使用的字符编码。Unicode 是为了解决传统的字符编码方案的局限而产生的,它为每种语言中的每个字符设定了统一并且唯一的二进制编码,规定虽有的字符和符号最少由 16 位来表示(2个字节),即:2 **16 = 65536,
注:此处说的的是最少2个字节,可能更多
UTF-8,是对Unicode编码的压缩和优化,他不再使用最少使用2个字节,而是将所有的字符和符号进行分类:ascii码中的内容用1个字节保存、欧洲的字符用2个字节保存,东亚的字符用3个字节保存...
11. 简述位和字节的关系:
8个二进制位(bit)= 1个byte字节
1kb = 1024byte
1mb = 1024kb
......
12.“⽼男孩”使⽤UTF-8编码占⽤⼏个字节?使⽤GBK编码占⼏个字节?
Utf-8 一个中文汉字24位=3个字节
所以老男孩 = 9个字节
GBK 一个汉子是两个字节
So 老男孩 = 6个字节
13. 制作趣味模板程序需求:等待⽤户输⼊名字、地点、爱好,根据⽤户的名字和爱好进⾏任意现实 如:敬爱可亲的xxx,最喜欢在xxx地⽅⼲Xxx
1 name = input("your name?") 2 someplace = input("your place?") 3 hobbie = input("your hobbie?") 4 5 text = ''' 6 dear %s is love to do %s in %s 7 ''' 8 print(text %(name,hobbie,someplace))
14. 等待⽤户输⼊内容,检测⽤户输⼊内容中是否包含敏感字符?如果存在敏感字符提示“存在敏感字符请重新输⼊”,并允许⽤户重新输⼊并打印。敏感字符:“⼩粉嫩”、“⼤铁锤”
1 flag = True 2 while flag: 3 user_input = input("please enter here:") 4 if user_input == "小粉嫩" or user_input == "大铁锤": 5 print("存在敏感字符,请重新输入") 6 else: 7 flag = False
15.单⾏注释以及多⾏注释?
# and ‘’’ ‘’’
16.简述你所知道的Python3和Python2的区别?
Python2 默认编码方式是ASCII码 所以默认不支持中文 得声明
并且源码复杂多余
Python3 则良好的支持了中文 采用utf-8编码方式
源码干净利落
17. 看代码书写结果:
a = 1>2 or 4<7 and 8 == 8
print(a) T
18.continue和break区别?
Continue就是到底了 直接回条件判断
Break就是跳出循环了 终止了!
19.看代码书写结果: a= 12&127 print(a)
12 = 00001100
127 = 01111111
按位与之后
00001100
转换成十进制为12
Day3默写代码:
Bit,Bytes,Kb,Mb,Gb,Tb之间的转换关系。
Unicode,utf-8,gbk,每个编码英文,中文,分别用几个字节表示。

浙公网安备 33010602011771号