zoukankan      html  css  js  c++  java
  • day002|python基础回顾2

    00 上节课复习

    """
    1、python的特点
        python是一门解释型、强类型、动态语言
    2、安装解释器
        path环境变量
    3、运行python程序的三个步骤
        Ⅰ 先启动python解释器
        Ⅱ python解释器将py文件从硬盘读入内存
        Ⅲ 解释器开始识别语法,解释执行代码
    4、注释
        井号#(前一行或当前行)<当前行前空两格,后空一格>
        """"""或''''''
    5、变量
        原则:先定义,后引用
            只要不在等号左边都是取值
        变量名:
            前提:见名知意
            规范:字母数字加下划线
                不能以数字开头
                不能使用关键字
            风格:纯小写+下划线;驼峰体
        变量值:
            id(x)
            type(x)
        垃圾回收机制GC:
            引用计数
                x = 10
                y = x
                
                y = 30
                del x
        = 赋值
        == 值是否相等
        is id是否相等
    """
    

    01 基本数据类型

    """
    1、什么是数据类型?
        数据的种类,不同种类的数据存取机制不一样,用途也不一样
        整型int
        浮点型float
        字符串str
        列表类型list
        字典类型dict
        布尔类型bool
    2、数据为何要分类型?
        数据是事物的状态,事物的状态是分为多种多样的,对应着就应该不同类型的数据去记录
    3、如何用数据类型?
        具体什么场景用什么类型
    """
    
    # 1、整型int
    # age = 18  # age = int(18)
    # level = 10
    
    # 2、浮点型float
    # 2.1 定义
    # salary = 3.1  # salary = float(3.1)
    # print(type(salary))
    # 2.2 作用:身高体重薪资等
    
    # 3、字符串类型str
    # 3.1 定义:在' '或者" "或者""" """或''' '''中间包含一系列字符
    # msg1 = '你hao'  # msg1 = str(xxx)
    # msg2 = "你hao"
    # msg3 = """
    # 111
    # 222
    # 333
    # """
    # print(msg3)
    # print(type(msg3))
    # ps:外层用双引号,内层就要用单引号
    # print("my name is 'ccc'")
    # 3.2 作用:记录一些描述性质的内容,比如名字、国籍、一段话
    
    # 4、列表类型list
    # 4.1 定义:在[]内逗号分隔开多个任意类型的值
    #       0    1       2           3
    #      -4   -3      -2          -1
    # l = [111, 3.1, "abc123", ['xxx', 'yyy']]  # l = list(...)
    # print(type(l))
    # print(l[0])
    # print(l[-1][0])
    # 4.2 作用:存放多个不同类型的值,按顺序存取多个值
    
    # 5、字典类型dict
    # 5.1 定义:在{}内用逗号分隔开多个key:value其中value可以是任意类型,而key通常是字符串类型
    # d = {"aa":111,"bb":1.1,"cc":[1,2,3],"dd":{"x":1,"y":2}}
    # 5.2 作用:按照属性名存放多个值key:value组合
    # info = {
    #     "name": "ccc",
    #     "age": 18,
    #     "gender": "male",
    #     "annual_salary": 18,
    #     "level": 10
    # }
    # print(info)
    # print(info["age"])
    #
    # info = [
    #     {"name": "aaa", "age": 18},
    #     {"name": "bbb", "age": 19},
    #     {"name": "ccc", "age": 20}
    # ]
    # print(info[0]["name"])
    
    # 6、布尔类型bool
    # 6.1 定义:就两个值True 、 False
    # 显式的布尔值
    # 隐式的布尔值
    # 所有数据类型的值都可以作为隐式布尔值使用
    # 其中0、空、None三者的布尔值为False,其余均为True
    # print(bool(0))  # False
    # print(bool(""))  # False
    # print(bool([]))  # False
    # print(bool({}))  # False
    # print(bool(none))  # False
    # print(bool(1))  # True
    # 6.2 作用:通常作为条件使用
    # x = True
    # y = False
    # print(type(x))  # bool
    #
    # x = None
    # print(x is None)  # True
    

    02 与用户交互

    # # 接收用户输入
    # name = input("请输入用户名:")  # name = "ccc"
    # print(name)
    #
    # age = input("请输入你的年龄:")
    # age = int(age)
    # print(age, type(age))
    #
    # # 格式化输出
    # msg = "my name is %s , my age is %s
    " % ("egon", 18)
    # print(msg, end="")
    # print(msg)
    #
    # print("-"*10+'info of Egon'+"-"*10)
    # print(
    #     """
    #     Name : Egon
    #     Age  : 22
    #     Sex  : male
    #     Job  : Teacher
    #     """
    # )
    # print("-"*10+'end'+"-"*10)
    #
    
    msg = "Name : %s
     Age : %s
     Sex : %s
     Job : %s" % ("Egon", 22, "male", "Teacher")
    print("-"*6+'info of Egon'+"-"*6)
    print(msg)
    print("-"*11+'end'+"-"*11)
    

    03 运算符

    # 1、算术运算符
    # print(10 + 3)
    # print(10 + 3.1)  # int与float可统称数字类型
    # print(10 - 3)
    # print(10 * 3)
    # print(10 ** 3)
    # print(10 / 3)
    # print(10 // 3)
    # print(10 % 3)
    # 字符串、列表和字典也可以使用+和*
    # print([1, 2, 3] + [3, 4, 5])
    # print([1, 2, 3] * 3)
    
    # 2、比较运算符
    # print(10 == 10)
    # print(10 > 10)
    # print(10 <= 10)
    # print('abc' != 'ddd')
    # print(10 > 3.1)
    # print([11,22] > [11,33])
    # print("asd" > "az")  # 根据ACKⅡ大小比较
    # 同类型之间可以相互比较是否相等,大小一般用在数字型上
    # print(10 > "20")  # 报错,不能比大小
    
    # 3、赋值运算符
    # 3.1 增量赋值
    # age = 10
    # age += 2  # 相当于age = age + 2
    # print(age)
    # 3.2 链式赋值
    # x = y = z = 10
    # print(id(x))
    # print(id(y))
    # print(id(z))
    # 3.3 交叉赋值
    # x = 10
    # y = 20
    # x,y = y,x  # 相当于temp = x、x = y、y = temp
    # print(x)
    # print(y)
    # 3.4 解压赋值
    # 变量名多一个少一个都不行,必须与值一一对应,可用*_代替剩余的值
    # salary = [1.1, 2.2, 3.3, 4.4]
    # month1, month2, *_ = salary
    # print(month1, month2)  # 得到1.1 2.2
    # print(_)  # 得到[3.3, 4.4]
    #
    # dic = {'k1':111,'k2':222,'k3':333}
    # x,*_ = dic
    # print(x)
    
    # 4、逻辑运算符
    # not代表把紧跟其后的条件结果取反
    print(10 > 3)
    print(not 10 > 3)
    # and连接左右两个条件,左右两个条件必须同时成立,最终结果才为True
    print(True and 1 > 0 and "aaa" == "aaa")
    print(False and 1 < 0 and "aaa" != "aaa")
    # or连接左右两个条件,左右两个条件但凡有一个成立,最终结果都为True
    print(True or 1 > 0 or "aaa" == "aaa")
    print(False or 1 > 0 or "aaa" != "aaa")
    # 短路运算
    print(10 and 0 or '' and 0 or 'abc' or 'egon' == 'dsb' and 333 or 10 > 4)
    print((10 and 0) or ('' and 0) or 'abc' or ('egon' == 'dsb' and 333) or 10 > 4)
    # 优先级
    #   not > and > or
    res = 3 > 4 and 4 > 3 or not 1 == 3 and 'x' == 'x' or 3 > 3
    print(res)
    
    # 对于and运算符,两边的值都为真时最终结果才为真,但是只要其中有一个值为假,那么最终结果就是假:
    # 如果左边表达式的值为假,不管右边表达式的值是什么最终结果都是假,此时and会把左边表达式的值作为最终结果。
    # 如果左边表达式的值为真,那么最终值是不能确定的,and会继续计算右边表达式的值,并将右边表达式的值作为最终结果。
    #
    # 对于or运算符,情况是类似的,两边的值都为假时最终结果才为假,只要其中有一个值为真,那么最终结果就是真:
    # 如果左边表达式的值为真,不管右边表达式的值是什么最终结果都是真,此时or会把左边表达式的值作为最终结果。
    # 如果左边表达式的值为假,那么最终值是不能确定的,or会继续计算右边表达式的值,并将右边表达式的值作为最终结果。
    print(1 and 3)  # 得3
    print(1 and 3 or 4)  # 得3
    print(0 and 2 or 1 or 4)  # 得到1
    print(0 or False and 1)  # 得到False
    

    04 流程运算之if判断

    """
    1、if判断的语法
        if 条件1:
            代码1
            代码2
            代码3
        elif 条件2:
            代码1
            代码2
            代码3
        ...
        else:
            代码1
            代码2
            代码3
    2、单分支
        if 条件1:
            代码1
            代码2
            代码3
    3、双分支
        if 条件1:
            代码1
            代码2
            代码3
        else:
            代码1
            代码2
            代码3
    4、多分支
        if 条件1:
            代码1
            代码2
            代码3
        elif 条件2:
            代码1
            代码2
            代码3
        ...
        else:
            代码1
            代码2
            代码3
    """
    # age_of_girl = input('请输入年龄:')
    # height = input('请输入身高:')
    # weight = input('请输入体重:')
    # is_pretty = input('请给自己颜值打分:')
    #
    # if 18 <= int(age_of_girl) <= 22 and int(height) > 170 and int(weight) < 100 and int(is_pretty) >=80:
    #     print('Get到WeChat')
    # else:
    #     print('游泳健身了解一下?')
    
    
    # score = input('请输入你的成绩:')
    # if int(score) >= 90:
    #     print('优秀!')
    # elif int(score) >= 80:
    #     print('良好!')
    # elif int(score) >= 70:
    #     print('普通!')
    # else:
    #     print('很差!')
    
    # score = input('请输入你的成绩:')
    # score = int(score)
    # if score >= 90:
    #     print('优秀!')
    # elif score >= 80:
    #     print('良好!')
    # elif score >= 70:
    #     print('普通!')
    # else:
    #     print('很差!')
    
    # name = input('请输入用户名:')
    # password = input('请输入密码:')
    #
    # if name == 'egon' and password == '123':
    #     print('登陆成功!')
    # else:
    #     print('登陆失败!')
    

    05 流程判断之while循环

    """
    1、什么是循环?
        循环就是重复做某件事
    2、为何要有循环?
        为了让计算机像人一样重复做某件事
    3、如何用循环?
        while 条件:
            代码1
            代码2
            代码3
    """
    
    # while True:
    #     inp_name = input("username:")
    #     inp_pwd = input("password:")
    #
    #     if inp_name == "ccc" and inp_pwd == "123":
    #         print('login successful')
    #         break
    #     else:
    #         print('username or password error')
    
    # 1、基本使用
    # count = 0
    # while count < 6:
    #     print(count)
    #     count += 1
    
    # 2、结束while循环的两种方式
    # 2.1 条件改为假
    # tag = True
    # while tag:
    #     inp_name = input("username:")
    #     inp_pwd = input("password:")
    #
    #     if inp_name == "ccc" and inp_pwd == "123":
    #         print('login successful')
    #         tag = False
    #     else:
    #         print('username or password error')
    # 2.2 break干掉本层循环
    # while True:
    #     inp_name = input("username:")
    #     inp_pwd = input("password:")
    #
    #     if inp_name == "ccc" and inp_pwd == "123":
    #         print('login successful')
    #         break
    #     else:
    #         print('username or password error')
    # 区别:break会立即终止本层循环,条件假会等到下一次循环判断条件时才会生效
    
    # 3、while+continue终止本次循环,直接进入下一次
    # count = 0
    # while count < 6:
    #     if count == 3:
    #         count += 1
    #         continue
    #     print(count)
    #     count += 1
    
    # 4、while+else
    # 情况一:while循环非正常结束,此时else不执行
    # count = 0
    # while count < 6:
    #     if count == 3:
    #         break
    #     print(count)
    #     count += 1
    # else:
    #     print("="*10)
    # 情况二:while循环正常结束,执行else
    # count = 0
    # while count < 6:
    #     if count == 3:
    #         count += 1
    #         continue
    #     print(count)
    #     count += 1
    # else:
    #     print("="*10)
    
    # 5、死循环
    # 表达式永远为真的循环
    

    06 TEST

    # info = {
    #     'Name': 'Egon',
    #     'Age': 22,
    #     'Sex': 'male',
    #     'Job': 'Teacher'
    # }
    # print("---------info of Egon-----------")
    # print("Name :", info['Name'])
    # print("Age  :", info['Age'])
    # print("Sex  :", info['Sex'])
    # print("Job  :", info['Job'])
    # print("------------end-----------------")
    
    # Name = input('请输入姓名:')
    # Age = input('请输入年龄:')
    # Sex = input('请输入性别:')
    # Job = input('请输入职业:')
    # print("---------info of Egon-----------")
    # print("Name :", Name)
    # print("Age  :", Age)
    # print("Sex  :", Sex)
    # print("Job  :", Job)
    # print("------------end-----------------")
    
    # a = 10
    # b = 20
    # print(a + b)
    # print(a - b)
    # print(a * b)
    # print(a / b)
    # print(b % a)
    # print(a ** b)
    # print(a // b)
    
    # x = 10
    # y = 20
    # print(x == y)
    # print(2*x == y)
    # print(x != y)
    # print(x < y)
    # print(x > y)
    
    # a = 10
    # b = 20
    # c = a + b
    # c += a
    # c -= a
    # c *= a
    # c /= a
    # c %= a
    # c **= a
    # c //= a
    # print(c)
    
    # print(
    #     3 > 4 and 4 > 3 or 1 == 3 and 'x' == 'x' or 3 > 2
    # )
    
    # print(
    #     10 and 0
    #     or
    #     '' and 0
    #     or
    #     'abc'
    #     or
    #     'egon' =='dsb' and 333
    #     or
    #     10 > 4
    # )
    
    # 对于and运算符,两边的值都为真时最终结果才为真,但是只要其中有一个值为假,那么最终结果就是假:
    # 如果左边表达式的值为假,不管右边表达式的值是什么最终结果都是假,此时and会把左边表达式的值作为最终结果。
    # 如果左边表达式的值为真,那么最终值是不能确定的,and 会继续计算右边表达式的值,并将右边表达式的值作为最终结果。
    #
    # 对于or运算符,情况是类似的,两边的值都为假时最终结果才为假,只要其中有一个值为真,那么最终结果就是真:
    # 如果左边表达式的值为真,不管右边表达式的值是什么最终结果都是真,此时or会把左边表达式的值作为最终结果。
    # 如果左边表达式的值为假,那么最终值是不能确定的,or 会继续计算右边表达式的值,并将右边表达式的值作为最终结果。
    
    # age_of_girl = 18
    # height = 171
    # weight = 90
    # is_pretty = True
    #
    # if 22 >= age_of_girl >= 18 and height > 170 and weight < 100 and is_pretty == True:
    #     if True:
    #         print('Get到WeChat')
    #     else:
    #         print('游泳健身了解一下?')
    # else:
    #     print('买保险吗?')
    
    # age_of_girl = input('请输入年龄:')
    # height = input('请输入身高:')
    # weight = input('请输入体重:')
    # is_pretty = input('请给自己颜值打分:')
    #
    # if 22 >= int(age_of_girl) >= 18 and int(height) > 170 and int(weight) < 100 and int(is_pretty) >=80:
    #     print('Get到WeChat')
    # else:
    #     print('游泳健身了解一下?')
    
    # score = input('请输入你的成绩:')
    # if int(score) >= 90:
    #     print('优秀!')
    # elif int(score) >= 80:
    #     print('良好!')
    # elif int(score) >= 70:
    #     print('普通!')
    # else:
    #     print('很差!')
    
    # score = input('请输入你的成绩:')
    # score = int(score)
    # if score >= 90:
    #     print('优秀!')
    # elif score >= 80:
    #     print('良好!')
    # elif score >= 70:
    #     print('普通!')
    # else:
    #     print('很差!')
    
    # name = input('请输入用户名:')
    # password = input('请输入密码:')
    #
    # if name == 'egon' and password == '123':
    #     print('登陆成功!')
    # else:
    #     print('登陆失败!')
    
    # name = input('请输入用户名:')
    # password = input('请输入密码:')
    #
    # if not name == 'egon':
    #     print('用户名错误!')
    # else:
    #     if not password == '123':
    #         print('密码错误!')
    #     else:
    #         print('登陆成功!')
    
    # today = input('请输入今天是周几:')
    # if today == "Monday":
    #     print('周一好!打工人!')
    # elif today == "Tuesday":
    #     print('周二好!无产阶级工人同志!')
    # elif today == "Wednesday":
    #     print('已经周三了!打工人再坚持一下!')
    # elif today == 'Thursday':
    #     print('马上就要周末了,加油,工人同志!')
    # elif today == 'Friday':
    #     print('今天周五,工具人同志请稳住别浪!')
    # elif today == 'Saturday':
    #     print('请将工作机关机,工具人暂停服务!')
    # elif today == 'Sunday':
    #     print('请将工作机充满电,今天周日做好准备!')
    # else:
    #     print('''
    #     必须输入以下其中一种
    #     Monday
    #     Tuesday
    #     Wednesday
    #     Thursday
    #     Friday
    #     Saturday
    #     Sunday
    #     ''')
    
    # today = input('请输入今天是周几:')
    # if today in ['Monday', 'Tuesday', 'Monday', 'Thursday', 'Friday']:
    #     print('上班')
    # elif today in ['Saturday', 'Sunday']:
    #     print('休息了')
    # else:
    #     print('''
    #     必须输入以下其中一种
    #     Monday
    #     Tuesday
    #     Monday
    #     Thursday
    #     Friday
    #     Saturday
    #     Sunday
    #     ''')
    
  • 相关阅读:
    Floydtemplate 示例
    dfa hdu 2222 AC自动机示例
    oj 中的 G++ 与 C++ 的区别
    System.Diagnostics.Process.Start(Info)
    自动关闭messagebox
    获取外部程序进程的SQL语句
    php 阿拉伯数字转中文数字 方法
    最新jQuery CHM版中文帮助文档
    parent.myFrame.cols ff 子窗体取得(访问)父窗体中另一子窗体的ID
    Jquery JS 正确的比较两个数字大小的方法
  • 原文地址:https://www.cnblogs.com/caojiaxin/p/14002294.html
Copyright © 2011-2022 走看看