第一章——第1部分——02、判断语句与循环语句

  1 import random
  2 """
  3 1、判断语句
  4 定义及作用;
  5 比较运算符、关系运算符,有哪些及作用;
  6 if...
  7 if...else...
  8 if...elif...else...
  9 
 10 2、循环语句及嵌套
 11 while循环及嵌套
 12 for循环及嵌套
 13 3、break、continue的作用
 14 break跳出本层循环
 15 continue跳出本次循环进入下次循环
 16 """
 17 
 18 # demo1  if... 满足某条件做什么事情
 19 # age = int(input("请输入您的年龄: "))
 20 age = 17
 21 if age >= 18:
 22     print("哥成年了,可以进网吧了!")
 23 
 24 # 比较运算符 >= > == =! <= <
 25 
 26 # 逻辑运算符(关系运算符) and or not
 27 
 28 # demo2  if...else...
 29 # age_new = int(input("请输入您的年龄: "))
 30 age_new = 17
 31 if age_new >= 15:
 32     print("需要买票")
 33 else:
 34     print("无需买票")
 35 
 36 # demo3 if...elif...elif...else....   注意else必须放在最后
 37 score = 96
 38 if score >=90:
 39     print("优秀")
 40 elif score >=70:
 41     print("良好")
 42 elif score >= 60:
 43     print("")
 44 else:
 45     print("差劲")
 46 
 47 # demo4 if嵌套 内外层都可以是 if...else...
 48 chePiao = True
 49 site = True
 50 
 51 # 判断是否有车票
 52 if chePiao:
 53     print("上车")
 54 
 55     # 判断是否有座位
 56     if site:
 57         print("坐下")
 58     else:
 59         print("站着吧")
 60 
 61 else:
 62     print("去买票")
 63 
 64 # demo5 复习random模块
 65 """
 66 random.random() 产生一个(0, 1)的随机小数;
 67 random.randint(a, b) 产生一个(a, b)的随机整数;
 68 random.uniform(a, b) 产生一个(a, b)的随机小数
 69 random.randrange(a, b, 2) 产生一个在其范围内的随机数
 70 random.choice([1,2,3]) 随机获取一个元素
 71 random.shuffle([1,2,3]) 用户将列表中的元素打乱
 72 random.sample([1,2,3,4,5,6,7,8], 3) 随机获取三个元素
 73 """
 74 # random.random() 产生0到1的随机小数
 75 print(random.random())
 76 
 77 # random.randint(10 ,20) 产生范围内的随机整数
 78 print(random.randint(10, 100))
 79 
 80 # random.uniform(10, 20) 产生范围内的随机小数
 81 print(random.uniform(10, 20))
 82 
 83 # random.randrange(10, 20, 2) 产生范围内的整数
 84 print(random.randrange(10, 21, 2))
 85 
 86 # random.choice("abcdef") 随机获取序列中的一个元素
 87 print(random.choice("abcd"))
 88 print(random.choice((1,2,3,4)))
 89 
 90 # random.shuffle("abcd") 打乱序列的顺序
 91 a = [1, 2, 3, 4, 5, 6]
 92 random.shuffle(a)
 93 print(a)
 94 
 95 # random.sample("abcdef", 3) 从序列中获取3个元素 返回结果均为列表
 96 print(random.sample("abcdef", 3))
 97 print(random.sample([1, 2, 3, 4, 5, 6], 3))
 98 
 99 # 循环语句 while for
100 i = 1
101 while i <= 5:
102     print("第%s打印"%i)
103     i += 1
104 print(i)
105 
106 for i in range(6):
107     print(i)
108 
109 # 循环的应用 计算1~100的累加和
110 i = 1
111 sum = 0
112 while i <= 100:
113     sum += i
114     i += 1
115 print("sum: %s"%sum)
116 
117 # 循环的应用 计算1~100的偶数累加和
118 i = 1
119 sum = 0
120 while i <= 100:
121     if i % 2 == 0:
122         sum += i
123     else:
124         pass
125     i += 1
126 print("偶数sum: %s"%sum)
127 
128 # while嵌套
129 
130 # 应用一:打印三角形
131 # 外层循环限定行 内层循环限定列
132 i = 1
133 while i <= 6:
134     j = 1
135     while j <= i:
136         print("*", end="\t")
137         j += 1
138     print("")
139     i += 1
140 
141 # 应用二 九九乘法表
142 i = 1
143 while i <= 9:
144     j = 1
145     while j <= i:
146         print("%s * %s = %s"%(i, j, i*j), end="\t")
147         j += 1
148     print("")
149     i += 1
150 
151 # for 循环的使用
152 for i in range(10):
153     print(i)
154 
155 # break—跳出本层循环 continue—跳出本次循环进入下次循环
156 for i in range(6):
157     print(i)
158     if i == 3:
159         break
160 
161 for i in range(6):
162     if i == 4:
163         continue
164     else:
165         print(i)
166 
167 i = 0
168 while i <= 6:
169     if i == 4:
170         i += 1
171         continue
172     else:
173         print(i)
174     i += 1

 

posted on 2019-09-06 11:27  zl666张良  阅读(89)  评论(0)    收藏  举报