zoukankan      html  css  js  c++  java
  • 注释、变量、常量、基本数据类型、输入输出、基本运算符、流程控制 02

    一、注释

    多行注释
    """
    注释1
    注释2
    注释3
    """
    
    单行注释
    # 注释的内容
    # print("hello")  # 注释的内容
    
    # ctrl+d、ctrl+?
    # print("hello1")
    # print("hello2")
    # print("hello3")
    # print("hello4")
    # print("hello5")
    # print("hello6")

    二、变量

    """
    1、什么是变量
        量:记录的事物的状态
        变:事物的状态是可以发生变化的
    
    2、为何要有变量
        为了让计算机具备人类的记忆事物状态的能力,并且状态是可以发生改变的
    
    3、如何用变量
        原则:先定义、后引用
    """
    # 一、定义变量
    # name = "egon"
    # age = 18
    # gender = "male"
    
    # 1.1 变量的定义分为三大组成部分
    # (1)变量名:是用来访问值的
    # (2)赋值符号:负责把变量值的内存地址绑定给变量名
    # (3)变量值:是我们记录的事物的状态,即数据
    
    # 1.2 变量名的命名原则/大前提:变量名的命名应该有见名知意的效果
    # 1.3 变量名的命名规范:
    # (1). 变量名只能是 字母、数字或下划线的任意组合
    #  (2) 变量名的第一个字符不能是数字
    #  (3) 关键字不能声明为变量名['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield'
    # print=3333
    # print(111)
    
    # name1="egon"
    # name2="lxx"
    # gender_of_lxx = "male"
    
    # _=3333333
    # _x=3333333
    # print(_)
    # print(_x)
    
    # 1.4 变量名的命名风格
    # (1)驼峰体
    AgeOfOldboy = 73
    
    # (2)纯小写+下划线(变量名的名推荐该风格)
    age_of_oldboy = 73
    
    # 1.5 变量值:两大特征
    # (1)id:反映的是值的内存地址
    # (2)type:变量值的类型
    x = 10
    y=x
    # print(id(x))
    
    # print(x is y) # 如果结果为True,证明id是一样的
    # print(x == y) #
    
    
    # True
    # False
    # None
    # print(type(x))
    
    # 1.6 垃圾回收机制GC:引用计数、标记/清除、分代回收
    # 详见:https://zhuanlan.zhihu.com/p/108683483
    x = 10 # 值10的引用计数为1
    y = x  # 值10的引用计数为2
    
    y = 20 # 值10的引用计数为1
    del x # 值10的引用计数为0
    
    
    
    
    # 二、引用变量
    # print(name)
    
    
    
    # 强调1:变量名放在等号的左边代表赋值操作
    # 强调2:变量名没有放在等号的左边代表取值操作
    # age = 18
    # # age
    # age = age + 1
    # print(age)
    
    # print("hello"
    
    # xxx=111
    # xxx

    三、常量

    # 规定变量名全为大写代表常量
    AGE = 18
    AGE = 19
    print(AGE)

    四、基本数据类型

    """
    基本数据类型=》变量值的类型
    """
    
    # 一:数字类型
    # 1:整型int
    # 作用:记录年龄、个数、年份、等级等
    # 定义:
    # age = 18
    # 使用:
    # res=age * 10
    # print(res)
    
    # print(age * 10)
    # print(age > 16)
    
    # 2:浮点型float
    # 作用:记录身高、体重、薪资等
    # 定义:
    # salary = 3.1
    # print(type(salary))
    
    # 使用:
    # print(salary + 1)
    # print(3.1 + 1)
    # print(3.1 > 1)
    
    # 二:字符串类型str
    # 作用: 记录名字性别国籍等描述性质的状态
    
    # 定义: 引号('',"",'''''',""" """)内包含一串字符
    # s1='abc'
    # print(type(s1))
    
    # 区别:
    # s2="""
    # aaa
    # bbb
    # ccc
    # """
    # print(type(s2))
    
    # 注意1: 引号的嵌套
    # print('my name is "egon"')
    
    # 注意2: 转义字符
    
    # print("m	y na
    me is egon")
    # print("aaaa",end='')
    # print("bbbb",end='')
    
    # 注意3:原生字符串
    # file_path="C:aaa
    ew.txt"
    # file_path="C:\aaa\new.txt"
    # file_path=r"C:aaa
    ew.txt"
    # print(file_path)
    
    # 使用:
    # name="egon"
    # print(name)
    
    # print("abc"+"defg")
    # print("abc"*10)
    # print("="*50)
    # print("hello")
    # print("="*50)
    
    # 三 列表类型list=>索引对应值,索引反映的是位置/顺序
    # 作用: 按照顺序把多个值放在一起
    # 定义: 在[]内用逗号分隔开多个任意类型的值
    
    #     0    1    2      3
    # l1 = [111,3.3,"aaa",[222,333]]
    
    # 使用:
    # print(l1[0])
    # print(l1[-1])
    # print(l1[100]) # 索引超出范围,报错
    
    # print(l1[3][1])
    
    
    # names=["张三", "李四","王五"]
    # print(names[0])
    # print(names[1])
    
    # students_info=[
    #     ["egon1",18,['read','music']],
    #     ["egon2",19,['play','music']],
    #     ["egon3",20,['movie','music']],
    # ]
    # print(students_info[1][2][1]) # 第二个学生的第二个爱好
    
    # 四 字典dict=》key对应值,称之为map类型/映射类型
    # 作用:按照key:value的方式把多个value放在一起,
    #       其中value可以是任意类型
    #       而key通常是字符串类型,是用来描述value的属性
    # 定义:{}内用逗号分隔开多key:value的值
    # d = {"k1":111,'k2':3.1,"k3":"aaa","k4":[222,3333],"k5":{"kk":4444}}
    # 使用:
    # print(d["k1"])
    # print(d["k4"][1])
    # print(d["k5"]["kk"])
    
    
    # info={"name":"egon","age":18,"gender":"male","level":10}
    # print(info["level"])
    
    # names=["xxx","yyy"]
    #
    # print(names[0])
    # print(names[1])
    
    # infos=['egon',18,'male']
    # infos[0]
    
    
    # studetns_info=[
    #     {"name":'egon1',"age":18,'gender':"male"},
    #     {"name":'egon2',"age":19,'gender':"male"},
    #     {"name":'egon3',"age":20,'gender':"male"},
    # ]
    # print(studetns_info[1]['name']
    # )
    
    
    # 五:布尔类型True与False
    # 作用:主要用于判断
    
    # 定义:如何得到布尔值
    # 1、显式的布尔值
    # 1.1、直接定义
    # x = True
    # y = False
    
    # 1.2、通过比较运算得到
    # print(10 > 3)
    # print(10 >= 3)
    # print("egon" == "egon")
    
    # 2、隐式的布尔值
    # 所有类型的变量值都具有隐式的布尔值,前提是它们得放到条件中
    # 其中0、None、空三者隐式的布尔值为False,其他都为True
    
    # if []:
    #     print('Hello')

    五、输入输出

    # 1、python3中的input会把用户输入的任意内容都存成str类型
    # print('start...')
    # name=input("请输入您的账号: ") # "123"
    # print(name == "egon")
    # print('end...',name,type(name))
    
    # age = input("your age: ")  # age="19"
    # age=int(age)
    # print("19" > 18)
    
    # 2、
    # print("a",'b','c','d',end='
    ')
    
    # res="my name is %s my age is %s" % ("egon1","19")
    # res="my name is %s my age is %d" % ("egon1",19)
    # res="my name is %s my age is %s" % ("egon1",[1,2,3])
    # print(res)

    六、基本运算符

    # 1、算数运算符
    # print(10 / 3)
    # print(10 // 3)
    # print(10 % 3)
    # print(10 ** 3)
    
    # 2、比较运算
    # 2.1 关于相等性的比较所有数据类型可以混用
    # print("egon" == 10)
    # print("egon" != 10)
    # print([111,222] == [111,222])
    # print([111,222] == [222,111])
    
    # 2.2 > >= < <=主要用于数字类型
    # print(10 > 10)
    # print(10 >= 10)
    
    
    # 了解:**
    "abcdef"
    # "abz"
    # print("abcdef" > "abz" )
    # print(len("abcdef") > len("abz"))
    
    # l1=['abc',123,3.1]
    # l2=["az","aaa"]
    # l2=["abc","aaa"]
    # print(l1 > l2)
    
    # 3、赋值运算符
    # age = 18
    
    # 3.1 增量赋值
    # age += 1  # age = age + 1
    # print(age)
    
    # x = 10
    # x %= 3  # x = x % 3
    # print(x)
    
    # 3.2 链式赋值
    # x = 10
    # y=x
    # z=y
    
    # z = y = x = 10
    # print(id(x))
    # print(id(y))
    # print(id(z))
    
    # 3.3 交叉赋值
    # x = 10
    # y = 20
    
    # temp=y
    # y=x
    # x=temp
    # x,y=y,x
    # print(x)
    # print(y)
    
    # 3.4 解压赋值
    salaries=[11,22,33,44,55,66]
    # mon0=salaries[0]
    # mon1=salaries[1]
    # mon2=salaries[2]
    # mon3=salaries[3]
    # mon4=salaries[4]
    # mon5=salaries[5]
    # 强调:变量名的个数与值应该一一对应
    # mon0,mon1,mon2,mon3,mon4,mon5=salaries
    # mon0,mon1,mon2,mon3,mon4,mon5,mon6=salaries # 多一个不行
    # mon0,mon1,mon2,mon3,mon4=salaries # 少一个不行
    # print(mon0)
    # print(mon1)
    # print(mon2)
    # print(mon3)
    # print(mon4)
    # print(mon5)
    
    # salaries=[11,22,33,44,55,66]
    # mon0,mon1,*_=salaries
    # print(mon0)
    # print(mon1)
    # print(_)
    
    
    # mon0,mon1,*_,mon_last=salaries
    # print(mon_last)
    
    # *_,x,y=salaries
    # print(x,y)
    
    # x,y,z={'k1':111,'k2':222,'k3':3333}
    # print(x,y,z)
    
    # x,y,z="hel"
    # print(x,y,z)
    
    # 4、逻辑运算符
    # (1) not:对紧跟其后的那个条件的结果取反
    # print(not True)  # False
    # print(not 10 > 3)  # False
    # print(not 0) # True
    
    # (2) and:用来连接左右两个条件,左右两个条件的结果都为True时,and的最终结果才为True
    # print(True and 10 > 3)
    # (3) or:用来连接左右两个条件,左右两个条件的结果但凡有一个True时,or的最终结果就为True
    # print(10 < 3 or 10 == 10)
    
    # ps:偷懒原则=》短路运算
    # 条件1 and 条件2 and 条件3
    # 条件1 or 条件2 or 条件3
    
    # (4)优先级:not > and > or
    # res1 = 3>4 and 4>3 or not 1==3 and 'x' == 'x' or 3 >3
    # print(res1)
    # res2 = (3>4 and 4>3) or (not 1==3 and 'x' == 'x') or 3 >3
    # print(res2)
    #
    # res3 = 3 < 4 and (4>3 or not (1==3 and 'x' == 'x'))
    # print(res3)
    
    # 了解
    # print(1 and "abc" and 333)
    
    # print(False and True or True)
    # print( 0 and 2 or 1)

    七、流程控制之if判断

    """
    案例:
    接收用户输入的用户名
    接收用户输入的密码
    判断 输入的用户名 等于 正确的用户名 并且 输入的密码 等于 正确的密码:
        告诉用户登录成功
    否则:
        告诉用户账号名或密码输入错误
    
    if判断的完整的语法
    if 条件1:
        代码1
        代码2
        代码3
        ...
    elif 条件2:
        代码1
        代码2
        代码3
        ...
    elif 条件3:
        代码1
        代码2
        代码3
        ...
    ......
    else:
        代码1
        代码2
        代码3
        ...
    """
    # 案例:
    # 如果:成绩>=90:
    #     优秀
    # 如果 成绩>=80且<90:
    #     良好
    # 如果 成绩>=70且<80:
    #     普通
    # 其他情况:
    #     很差
    
    # print('start....')
    # score = input('请输入你的成绩:')
    # score = int(score)
    # if score >= 90:
    #     print('优秀')
    # elif score >= 80:
    #     print('良好')
    # elif score >= 70:
    #     print('普通')
    # else:
    #     print("很差")
    #
    # print('end....')
    
    # 语法1:
    """
    if 条件1:
        代码1
        代码2
        代码3
        ...
    """
    # age = 19
    # height = 1.9
    # gender = "female"
    # is_beautiful = True
    #
    # if 18 < age < 26 and 1.6 < height < 1.8 and gender == "female" and is_beautiful:
    #     print("开始表白。。。。")
    #
    # print('其他代码。。。')
    
    
    
    # 语法2:
    """
    if 条件1:
        代码1
        代码2
        代码3
        ...
    else:
        代码1
        代码2
        代码3
        ...
    """
    # age = 19
    # height = 1.9
    # gender = "female"
    # is_beautiful = True
    #
    # if 18 < age < 26 and 1.6 < height < 1.8 and gender == "female" and is_beautiful:
    #     print("开始表白。。。。")
    # else:
    #     print("你是个好人。。。")
    #
    # print('其他代码。。。')
    #
    
    # 语法3:
    """
    if 条件1:
        代码1
        代码2
        代码3
        ...
    elif 条件2:
        代码1
        代码2
        代码3
        ...
    """
    
    
    # 语法4:if判断是可以嵌套的
    
    # 登录功能实现
    # db_name = "egon"
    # db_pwd = "123"
    #
    # inp_name = input("please input your name: ")
    # inp_pwd = input("please input your password: ")
    #
    # if inp_name == db_name and inp_pwd == db_pwd:
    #     print("用户登录成功")
    # else:
    #     print("用户账号名或密码输入错误")

    八、流程控制之while循环

    """
    while 条件:
        代码1
        代码2
        代码3
        ...
    
    """
    # 1、基本使用
    # print('start...')
    # count = 0
    # while count < 5:
    #     print(count)
    #     count+=1
    # print('end...')
    #
    
    """
    start...
    0
    1
    2
    3
    4
    end...
    """
    
    # 2、死循环
    # while 1:
    #     name=input(">>: ")
    #     print(name)
    
    # 3、用户认证功能案例
    # db_name = "egon"
    # db_pwd = "123"
    #
    # while True:
    #     inp_name = input("please input your name: ")
    #     inp_pwd = input("please input your password: ")
    #
    #     if inp_name == db_name and inp_pwd == db_pwd:
    #         print("用户登录成功")
    #     else:
    #         print("用户账号名或密码输入错误")
    
    # 4、结束while循环
    # (1)把条件改成False
    # db_name = "egon"
    # db_pwd = "123"
    #
    # tag = True
    # while tag:
    #     inp_name = input("please input your name: ")
    #     inp_pwd = input("please input your password: ")
    #
    #     if inp_name == db_name and inp_pwd == db_pwd:
    #         print("用户登录成功")
    #         tag = False
    #     else:
    #         print("用户账号名或密码输入错误")
    #
    #     print('=============>')
    # (2) break会直接结束本层循环
    # db_name = "egon"
    # db_pwd = "123"
    #
    # while True:
    #     inp_name = input("please input your name: ")
    #     inp_pwd = input("please input your password: ")
    #
    #     if inp_name == db_name and inp_pwd == db_pwd:
    #         print("用户登录成功")
    #         break
    #     else:
    #         print("用户账号名或密码输入错误")
    #
    #     print('=============>')
    
    
    # while True:
    #     while True:
    #         while True:
    #             break
    #         break
    #     break
    
    # tag = True
    # while tag:
    #     while tag:
    #         while tag:
    #             tag = False
    
    # 5、while循环的嵌套
    # (1) break结束嵌套多层的while循环
    
    # db_name = "egon"
    # db_pwd = "123"
    #
    # while True:
    #     inp_name = input("please input your name: ")
    #     inp_pwd = input("please input your password: ")
    #
    #     if inp_name == db_name and inp_pwd == db_pwd:
    #         print("用户登录成功")
    #         while True:
    #             print("""
    #             0 退出
    #             1 提现
    #             2 转账
    #             """)
    #             choice = input("请输入您的命令编号 ")
    #             if choice == "0":
    #                 break
    #             elif choice == "1":
    #                 print('===========>提现功能<============')
    #             elif choice == "2":
    #                 print('===========>转账功能<============')
    #             else:
    #                 print('===========>非法指令<============')
    #
    #         break
    #     else:
    #         print("用户账号名或密码输入错误")
    
    # (2) tag = False
    # db_name = "egon"
    # db_pwd = "123"
    #
    # tag = True
    # while tag:
    #     inp_name = input("please input your name: ")
    #     inp_pwd = input("please input your password: ")
    #
    #     if inp_name == db_name and inp_pwd == db_pwd:
    #         print("用户登录成功")
    #         while tag:
    #             print("""
    #             0 退出
    #             1 提现
    #             2 转账
    #             """)
    #             choice = input("请输入您的命令编号 ")
    #             if choice == "0":
    #                 tag = False
    #             elif choice == "1":
    #                 print('===========>提现功能<============')
    #             elif choice == "2":
    #                 print('===========>转账功能<============')
    #             else:
    #                 print('===========>非法指令<============')
    #     else:
    #         print("用户账号名或密码输入错误")
    
    
    # 6、while+continue:continue会终止本次循环,直接进入下一次
    # count = 0
    # while count < 5:
    #     if count == 3:
    #         # break
    #
    #         count+=1
    #         continue # 强调:与continue同一级别的后续代码永远都不会运行
    #         # count+=1
    #     print(count)
    #     count+=1
    
    
    
    # db_name = "egon"
    # db_pwd = "123"
    #
    # while True:
    #     inp_name = input("please input your name: ")
    #     inp_pwd = input("please input your password: ")
    #
    #     if inp_name == db_name and inp_pwd == db_pwd:
    #         print("用户登录成功")
    #         break
    #     else:
    #         print("用户账号名或密码输入错误")
    #         # continue # 此处不加continue也会进入下一次,不要画蛇添足
    
    
    # 7、while+else
    # 如果while循环不是被break干掉的,那么while的结束都算正常死亡
    
    count = 0
    while count < 5:
        if count == 3:
            # count+=1
            # continue
    
            break
        print(count)
        count+=1
    else:
        print("else会在while循环正常死亡之后运行")
  • 相关阅读:
    事务,视图,索引
    SQL 编辑
    相关子查询
    4
    3
    2
    1
    BaseEditor
    EDCheckPrefabRef
    UIUseImgWindow
  • 原文地址:https://www.cnblogs.com/2722127842qq-123/p/13448882.html
Copyright © 2011-2022 走看看