zoukankan      html  css  js  c++  java
  • Python入门学习笔记5:分支、循环、条件与枚举

      1 # 分支、循环、条件与枚举
      2 # 表达式(Expression)是运算符(operator)和操作数(operand)所构成的序列
      3 a = 1
      4 b = 2
      5 c = 3
      6 print(a + b * c, 1 or 2, 1 and 3, a or (b and c), (a or b) and c)  # 7 1 3 1 3    a or b and c 从左向右(and优先级高于or)
      7 print(not a or b + 2 == c)  # False     not a or b +2 == c 等价于   (not a) or ((b+2) == c)
      8 print()
      9 # 流程控制语句
     10 '''
     11 多行注释
     12 条件控制 循环控制 分支
     13 if else    for while switch
     14 '''
     15 mood = False
     16 if mood:
     17     print('go to left')
     18 else:
     19     print('go to right')
     20 
     21 a = 1
     22 b = 2
     23 if a > b:
     24     print('go to left')
     25 else:
     26     print('go to right')
     27 
     28 d = []
     29 if d:
     30     print('go to left')
     31 else:
     32     print('go to right')
     33 
     34 """
     35 模块注释:使用if else语句 模拟登录流程
     36 """
     37 account = 'admin'
     38 password = '123456'
     39 print("please input account")
     40 user_account = 'admin' #input()
     41 print("please input password")
     42 user_password = '123456' #input()
     43 
     44 if user_account == account and user_password == password:
     45     print("success")
     46 else:
     47     print("fail")
     48 #  constant常量
     49 """
     50 if else 语句
     51 
     52 pass 空语句(占位语句)保持语句的完整性
     53 
     54 """
     55 code = True
     56 if code:
     57     pass
     58 else:
     59     pass
     60 #if 可以单独使用
     61 if True:
     62     pass #空语句(占位语句)保持语句的完整性
     63 if False:
     64     pass
     65 #if 嵌套使用
     66 if True:
     67     if True:
     68         pass
     69     else:
     70         pass
     71 else:
     72     if False:
     73         pass
     74     else:
     75         pass
     76 """
     77 类表达式
     78 
     79 class biaodashi(object):
     80     pass
     81 
     82 函数表达式(静态方法)
     83 
     84 @staticmethod
     85 def funcname(parameter_list):
     86     pass
     87 
     88 """
     89 """
     90 a = x
     91 a = 1 print('apple')
     92 a = 2 print('orange')
     93 a = 3 print('banana')
     94 print('shopping')
     95 
     96 """
     97 a = 1 # int(input())
     98 if a == 1:
     99     print('apple')
    100 else:
    101     if a == 2:
    102         print('orange')
    103     else:
    104         if a == 3:
    105             print('banana')
    106         else:
    107             print('shopping')
    108 
    109 #代码改写 elif用法(简化代码、简化行数)
    110 if a == 1:
    111     print('apple')
    112 elif a == 2:
    113     print('orange')
    114 elif a == 3:
    115     print('banana')
    116 else:
    117     print('shopping')
    118 
    119 a = 1
    120 b = 0
    121 
    122 """
    123 循环语句
    124 while
    125 """
    126 counter = 1
    127 
    128 while counter <= 10:
    129     counter += 1
    130     print(counter)
    131 else:
    132     print("end")
    133 """
    134 循环语句
    135 for
    136 主要是用来遍历/循环 序列或者集合、字典
    137 for 自定义变量 in 数组列表:
    138      print(打印出 自定义变量)
    139 """
    140 fruits = ['banana','apple','mango']
    141 for fruit in fruits:
    142     print('当前水果:',fruit)
    143 """
    144 当前水果: banana
    145 当前水果: apple
    146 当前水果: mango
    147 """
    148 
    149 a = [['apple','orange','banana','grape'],(1,2,3)]
    150 for x in a:
    151     for y in x:
    152         print(y,end='')
    153 else:
    154     print('fruit is gone') # appleorangebananagrape123fruit is gone
    155 # break终止循环    continue终止本次,循环继续
    156 a = [1,2,3]
    157 for x in a:
    158     if x==2:
    159         continue#break
    160     print(x)
    161 #range循环次数  range(开始,结束,间隔<步长>)
    162 for x in range(10,0,-2):
    163     print(x,end='|')
    164 
    165 a = [1,2,3,4,5,6,7,8]
    166 for i in range(0,len(a),2):
    167     print(a[i],end='|')
    168 
    169 b = a[0:len(a):2]
    170 print(b)
    171 
    172 def printme(str):
    173     #打印任何传入的字符串
    174     print(str)
    175     return
    176 printme("我要调用用户自定义函数!")
    177 printme("我要再次调用同一函数!")
    178 
    179 import time;
    180 localtime = time.localtime(time.time())
    181 print("当前时间戳为:",localtime)
    182 print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
    183 
    184 import calendar
    185 
    186 cal = calendar.month(2020,6)
    187 print("以下输出2020年6月份的日历:")
    188 print(cal)
    189 
    190 
    191 """
    192 枚举
    193 enum模块是系统内置模块,可以直接使用import导入,但是在导入的时候,不建议使用import enum将enum模块中的所有数据都导入,一般使用的最多的就是enum模块中的Enum、IntEnum、unique这几项
    194 """
    195 
    196 # 导入枚举类
    197 from enum import Enum
    198 
    199 
    200 # 继承枚举类
    201 class color(Enum):
    202     YELLOW = 1
    203     BEOWN = 1
    204     # 注意BROWN的值和YELLOW的值相同,这是允许的,此时的BROWN相当于YELLOW的别名
    205     RED = 2
    206     GREEN = 3
    207     PINK = 4
    208 
    209 
    210 class color2(Enum):
    211     YELLOW = 1
    212     RED = 2
    213     GREEN = 3
    214     PINK = 4
    215 
    216 
    217 print(color.YELLOW)  # color.YELLOW
    218 print(type(color.YELLOW))  # <enum 'color'>
    219 
    220 print(color.YELLOW.value)  # 1
    221 print(type(color.YELLOW.value))  # <class 'int'>
    222 
    223 print(color.YELLOW == 1)  # False
    224 print(color.YELLOW.value == 1)  # True
    225 print(color.YELLOW == color.YELLOW)  # True
    226 print(color.YELLOW == color2.YELLOW)  # False
    227 print(color.YELLOW is color2.YELLOW)  # False
    228 print(color.YELLOW is color.YELLOW)  # True
    229 
    230 print(color(1))  # color.YELLOW
    231 print(type(color(1)))  # <enum 'color'>
  • 相关阅读:
    CentOS单用户模式下修改ROOT密码和grub加密
    CentOS配置SSH单向无密码访问
    物流追踪
    SpringBoot整合Redis及Redis
    小程序毫秒级倒计时(适用于拼团秒杀功能)
    foreach中的collection
    ArrayList和LinkedList的区别
    JavaScript 基础
    自用 goodsdetail
    JAVA常用处理数据
  • 原文地址:https://www.cnblogs.com/liuxiaoming123/p/13152766.html
Copyright © 2011-2022 走看看