• 老男孩python学习_day002作业


    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 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    False
    (4) 5 < 4 or 3    3
    (5) 2 > 1 or 6    True
    (6) 3 and 2 > 1    True
    (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. 简述变量命名规范
     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:
     5     i += 1
     6     if i%2 == 1:
     7         sum += i
     8     else:
     9         if i == 88:
    10             continue
    11         sum -= i
    12 print(sum)
    13 
    14 # 法二
    15 count = 0
    16 sum = 0
    17 power = 0
    18 while count < 99:
    19     count += 1
    20     sum = sum + count*(-1)**power
    21     power += 1
    22 print(sum + 88)
    View Code

    改:计算 1 - 2 + 3 ... - 99 中除了88以外所有数的总和?(正负号规律不变)

     1 # 自己写的
     2 i = 0
     3 sum = 0
     4 n = 1
     5 while i < 99:
     6     i += 1
     7     n = -n
     8     if i == 88:
     9         n = -n  # 因为n的改变在前面,所以此处需要对n进行改变
    10         continue
    11     sum += i*(-1)*n
    12 print(sum)
    13 
    14 # 法二
    15 count = 0
    16 sum = 0
    17 power = 0
    18 while count < 9:
    19     count += 1
    20     if count == 8:
    21         continue  # 因为n的改变在后面,所以只continue
    22     sum = sum + count*(-1)**power
    23     power += 1
    24 print(sum)
    View Code

    9. 用户登陆(三次输错机会)且每次输错误时显示剩余错误次数(提示:使用字符串格式化)

     1 n = 'yyh'
     2 p = '123'
     3 i = 3
     4 while i > 0:
     5     name = input('输入用户名:')
     6     if name == n:
     7         password = input('请输入密码:')
     8         i -= 1
     9         if password == p:
    10             print('''~~~恭喜你登陆成功!~~~
    11 ~~~~欢迎用户进入~~~~
    12 ~~~~用户名 : %s~~~~
    13 ~~~~密码   : %s~~~~
    14 ''' % (name, password))
    15             break
    16         else:
    17             if i == 0:
    18                 a = input('三次机会已用光,是否重试?(Y/N)')
    19                 if a == 'Y':
    20                     i = 3
    21                     print('再给你三次机会,重新输入用户名:')
    22                     continue
    23                 print('再见~')
    24                 break
    25             else:
    26                 print('你还有%d次机会' % (i))
    27     else:
    28         i -= 1
    29         if i == 0:
    30             a = input('三次机会已用光,是否重试?(Y/N)')
    31             if a == 'Y':
    32                 i = 3
    33                 print('再给你三次机会,重新输入用户名:')
    34                 continue
    35             print('再见~')
    36             break
    37         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:
    3     print('存在敏感字符请重新输入')
    4     m= input('请输入内容:')
    5 else:
    6     print('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

  • 相关阅读:
    【构建工具】《Maven实战》读书笔记
    【网络安全】Dos攻击科普文
    谈谈集合.CopyOnWriteArrayList
    谈谈集合.List
    小程序开发--移动端分辨率与rpx
    跟面向对象卯上了,看看ES6的“类”
    捋一捋js面向对象的继承问题
    Canvas的drawImage方法使用
    浏览器内核趣对话
    “茴”字有四种写法,this也是一样
  • 原文地址:https://www.cnblogs.com/lovely-yyh/p/9413075.html
Copyright © 2020-2023  润新知