1. 判断下列逻辑语句的True,False.
(1) 1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
True
(2) not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
False
优先级:<> > () > not > and > or
2. 求出下列逻辑语句的值。
(1) 8 or 3 and 46 or 2 and 0 or 9 and 78
(2) 0 or 2 and 3 and 4 or 6 and 0 or 34
(3) 5 and 9 or 10 and 2 or 3 and 5 or 4 or 59
3、下列结果是什么?
(1) 6 or 2 > 16
(2) 3 or 2 > 13
(3) 0 or 5 < 4False
(4) 5 < 4 or 33
(5) 2 > 1 or 6True
(6) 3 and 2 > 1True
(7) 0 and 3 > 10
(8) 2 > 1 and 33
(9) 3 > 1 and 00
(10) 3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 22
4. 简述变量命名规范
1. 数字、字母或者下划线任意组合,且不能以数字开头
2. 不能是关键字
3. 一般不要用汉字和拼音
4. 命名要有意义,不宜过长。(用下划线分隔)
5. name = input('>>>') name变量是什么数据类型?
字符串str
6. if条件语句的基本结构?
(1) if 条件:
代码块
(2) if 条件:
代码块
else:
代码块
(3) if 条件:
代码块
elif 条件:
代码块
...
else:
代码块
(4) if 条件:
代码块
if 条件:
代码块
else:
代码块
else:
代码块
7. while循环语句基本结构?
(1) while 条件:
代码块
(2) while 条件:
代码块
else:
代码块
# 遇到break时,直接跳出while循环,且不再执行else语句。
8. 写代码:计算 1 - 2 + 3 ... + 99 中除了88以外所有数的总和?
1 # 自己写的 2 i = 0 3 sum = 0 4 while i < 99: 5i += 1 6if i%2 == 1: 7 sum += i 8else: 9 if i == 88:10 continue11 sum -= i12 print(sum)13 14 # 法二15 count = 016 sum = 017 power = 018 while count < 99:19count += 120sum = sum + count*(-1)**power21power += 122 print(sum + 88)
View Code
改:计算 1 - 2 + 3 ... - 99 中除了88以外所有数的总和?(正负号规律不变)
1 # 自己写的 2 i = 0 3 sum = 0 4 n = 1 5 while i < 99: 6i += 1 7n = -n 8if i == 88: 9 n = -n # 因为n的改变在前面,所以此处需要对n进行改变10 continue11sum += i*(-1)*n12 print(sum)13 14 # 法二15 count = 016 sum = 017 power = 018 while count < 9:19count += 120if count == 8:21 continue # 因为n的改变在后面,所以只continue22sum = sum + count*(-1)**power23power += 124 print(sum)
View Code
9. 用户登陆(三次输错机会)且每次输错误时显示剩余错误次数(提示:使用字符串格式化)
1 n = 'yyh' 2 p = '123' 3 i = 3 4 while i > 0: 5name = input('输入用户名:') 6if name == n: 7 password = input('请输入密码:') 8 i -= 1 9 if password == p:10 print('''~~~恭喜你登陆成功!~~~11 ~~~~欢迎用户进入~~~~12 ~~~~用户名 : %s~~~~13 ~~~~密码 : %s~~~~14 ''' % (name, password))15 break16 else:17 if i == 0:18 a = input('三次机会已用光,是否重试?(Y/N)')19 if a == 'Y':20 i = 321 print('再给你三次机会,重新输入用户名:')22 continue23 print('再见~')24 break25 else:26 print('你还有%d次机会' % (i))27else:28 i -= 129 if i == 0:30 a = input('三次机会已用光,是否重试?(Y/N)')31 if a == 'Y':32 i = 333 print('再给你三次机会,重新输入用户名:')34 continue35 print('再见~')36 break37 else:38 print('你还有%d次机会'%(i))
View Code
10. 简述ascii、unicode、utf-8编码关系?
ASCII码:美国最初编码,1个字节,表示所有的英文,特殊字符,数字等等,只能表示256种可能。
unicode编码:万国码,为了解决全球化的文字问题而创建。一个中文用4个字节表示,太浪费(中文9万多字)
utf-8编码:一个字符最少用8位去表示,英文用8位 一个字节。欧洲文字用16位,两个字节。中文用24位,3个字节表示
GBK编码:中国人自己发明的,一个中文用2个字节表示
11. 简述位和字节的关系?
8位(bit) == 一个字节(Byte)
8bit == 1B
1024B == 1KB
1024KB == 1MB
1024MB == 1GB
1024GB == 1TB
12. “老男孩”使用UTF-8编码占用几个字节?使用GBK编码占几个字节?
“老男孩”使用UTF-8编码占用9个字节
“老男孩”使用GBK编码占用6个字节
13. 制作趣味模板程序需求:等待用户输入名字、地点、爱好,根据用户的名字和爱好进行任意实现 如:敬爱可亲的xxx,最喜欢在xxx地方干xxx
1 name = input('输入名字:')2 place = input('输入地点:')3 hobby = input('你的爱好')4 s = '敬爱可亲的%s,最喜欢在%s地方干%s'%(name,place,hobby)5 print(s)
View Code
14. 等待用户输入内容,检测用户输入内容中是否包含敏感字符?如果存在敏感字符提示“存在敏感字符请重新输入”,并允许用户重新输入并打印。敏感字符:“小粉嫩”、“大铁锤”
1 m = input('请输入内容:')2 while '小粉嫩' in m or '大铁锤' in m:3print('存在敏感字符请重新输入')4m= input('请输入内容:')5 else:6print('OK')
View Code
15. 单行注释以及多行注释?
单行注释:“ ” 或 ' '
多行注释:“”“ ”“” 或''' '''
16. 简述你所知道的Python3和Python2的区别?
Python2:源码不标准,混乱,重复代码太多,违背了Python的宗旨。默认编码方式是ASCII码,读取中文时会乱码
Python3:统一标准,去除重复代码,崇尚“优雅,明确,简单”,默认编码方式是utf-8,读取中文时不会乱码
中文报错解决方式:在文件的首行:#-*- encoding:utf-8 -*-
17. 看代码书写结果:
a = 1>2 or 4<7 and 8 == 8
print(a)True
18. continue 和 break 的区别?
continue:结束本次循环,继续下一次的循环;
break:直接跳出循环。
19. 看代码书写结果:
a = 12 and 127
print(a)127