Python全栈(第一部分)day2

昨日内容回顾

  1. 编译型:一次性将全部代码编译成二进制文件
    代表语言:
    C,C++

    • 优点:执行效率高

    • 缺点:开发速度慢,不能跨平台

  2. 解释型:当程序运行时,从上至下一行一行的解释成二进制

    • 优点:开发速度快,效率高,可以跨平台

    • 缺点:运行效率低

  3. python2x 和 python3x 区别:

    • 宏观上:python2x源码,重复率高,不规范,python 崇尚简单,优美,创建了python3,规范化

    • 默认编码:在python2首行添加:#-*-encoding:utf-8-*-,解决中文报错问题

  4. 变量:

    • 由数字,字母,下划线任意组合,且不能以数字开头

    • 具有可描述性

    • 不能用python中的关键字

    • 不能用中文

  5. 常量
    约定俗成,不可更改,全部是大写字母

  6. 注释

    • 单行注释用#

    • 多行注释用''' '''/ """ """

  7. 用户交互
    input,数据类型全部是str

  8. 数据类型

    • int: + - / * % **

    • str: 加引号的就是str, 可以相加,可以与数字相乘

    • bool: True, False

  9. if语句

     if 条件:
         结果
    
     if 条件:
         结果
     else:
         结果
    
     if 条件:
         结果
     elif 条件:
         结果
     elif 条件:
         结果
     else:
         结果
    
     if 条件:
         if 条件:结果
             if...
         else:结果
    
  10. while语句

    while 条件:
        结果
    
  11. 终止循环

    • 改变条件
    • break
    • continue: 结束本次循环,继续下一次循环

作业讲解

  1. while循环输入1,2,3,4,5,6,8,9,10

     count = 1
     while(count<=10):
         count += 1
     if(count==7):
         print(' ')
     else:
         print(count)
    
  2. 输出1-100内所有奇数

     count = 1
     while count<=100 :
         if count%2==1 :
             print(count)
         count += 1
    
  3. 输出1-100内所有偶数

     count = 1
     while count<=100:
         if count%2==0:
             print(count)
         count += 1
    
  4. 求1-2+3-4+...+99的值

     count = 1
     sum = 0
     while count<100:
         if count%2==1:
             sum = sum + count
         else:
             sum = sum - count
         count += 1
     print('1-2+3...+99='+str(sum))
    
  5. 用户登陆(三次机会尝试)

     count = 1
     while True:
         if count>3:
             print('已错误登陆3次')
             break
         count += 1
         name = input('请输入用户名:')
         passwd = input('请输入密码:')
         if name == 'xkzhai' and passwd == '1234':
             print('登陆成功')
             break
         else:
             print('用户名或密码错误,请重新输入!')
             continue
    

格式化输出

%占位符,s字符串,d数字
%% 单纯地显示百分号

    '''用户交互,格式化输出'''
    name = input('请输入姓名:')
    age = input('请输入年龄:')
    msg = 'My name is %s, I am %s years old' %(name,age)
    print(msg)

    '''%d接受数据'''
    name = input('请输入姓名:')
    age = input('请输入年龄:')
    job = input('请输入工作:')
    hobbie = input('你的爱好:')
    msg = '''------------ info of %s -----------
    Name  : %s
    Age   : %d
    job   : %s
    Hobbie: %s
    ------------- end -----------------''' %(name,name,int(age),job,hobbie)
    print(msg)

    '''单纯显示百分号,转义'''
    name = input('请输入姓名:')
    age = input('请输入年龄:')
    msg = "我叫%s,今年%s 学习进度为3%%s" %(name,age)
    print(msg)

while else

当while循环被break打断,就不会执行else结果

    count = 0
    while count <= 5:
        count += 1
        if count ==3:brerak
        print("Loop",count)
    else:
        print("循环正常执行结束")
    print("-----out of while loop ----")

编码初始

电报,电脑的传输,存储都是01010101

0000110 晚
1010100 上
0010100 喝
0010111 点
0000001 儿

000010 1010100 0010100 0010111 0000001

最早的'密码本'

  1. ascii
    涵盖了英文字母大小写,特殊字符,数字。
    01000001 A
    01000010 B
    01000011 C
    ascii 只能表示256种可能,太少

  2. 存储单位换算
    1bit 8bit = 1bytes
    1byte 1024byte = 1KB
    1KB 1024kb = 1MB
    1MB 1024MB = 1GB
    1GB 1024GB = 1TB

  3. 万国码 unicode

    • 起初:
      1个字节可以表示所有的英文,特殊字符,数字等等
      2个字节,16位表示一个中文,不够,unicode一个中文用4个字节,32位
      你 00000000 00000000 00000000 00001000

    • Unicode 升级 utf-8 utf-16 utf-32
      utf-8 一个字符最少用8位去表示:
      1). 英文用8位 一个字节
      2). 欧洲文字用16位去表示 两个字节
      3). 中文用24位去表示 三个字节
      utf-16 一个字符最少用16位去表示

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

运算符

  1. 数学运算符

    • + 加
    • - 减
    • * 乘
    • / 除
    • // 整除
    • ** 幂
  2. 逻辑运算符

    • and
    • or
    • not

    优先级:() > not > and > or

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

     #非零数转成True,零转成False
     print(bool(2)) # True
     print(bool(0)) # False
     print(bool(-1)) # True
    
  4. bool --> int

     #True转成1,False转成0
     print(int(True)) # 1
     print(int(False)) # 0
    
  5. 数字逻辑运算

     '''x or y, x为True,则返回x,否则返回y'''
     print(1 or 2) # 1
     print(3 or 1) # 3
     print(0 or 2) # 2
     print(0 or 100) # 100
     print(2 or 100 or 3 or 4) # 2
    
     '''x and y,与or正好相反'''
     print(1 and 2) # 2
     print(0 and 2) # 0
    
     '''混合运算'''
     print(0 or 4 and 3 or 2) # 3
    
  6. 数字布尔混合逻辑运算

     print(2 or 1<3) # 2
     print(3>1 or 2 and 2) # True
     print(1>2 and 3 or 4 and 3<2) #False
原文地址:https://www.cnblogs.com/SweetZxl/p/9540840.html