《Python编程从入门到实践》第五章_if语句

超、凢脫俗 2022-05-15 05:22 295阅读 0赞
  1. print("5 if语句")
  2. print("5.1 一个简单示例")
  3. cars = ['audi', 'bmw', 'subaru', 'toyota']
  4. for car in cars:
  5. if car == 'bmw':
  6. print(car.upper())
  7. else:
  8. print(car.title())
  9. print("5.2.1检查是否相等")
  10. car = "bmw"
  11. car == "bmw"
  12. if car == "bmw":
  13. print("true")
  14. else:
  15. print("false")
  16. print("5.2.2 检查是否相等不考虑大小写")
  17. car = 'Audi'
  18. # 字符串检查时是区分大小写的
  19. print(car == 'audi')
  20. print(car.lower() == 'audi')
  21. print("检查是否不等")
  22. car = "bmw"
  23. print(car != "bmw")
  24. print(car != "audi")
  25. print("比较数字")
  26. age = 18
  27. print(age == 18)
  28. print(age != 19)
  29. print(age < 21)
  30. print(age > 21)
  31. print(age <= 21)
  32. print(age >= 21)
  33. print("5.2.5检查多个条件")
  34. print("and 多个条都成立则为True,否则为False")
  35. age = 19
  36. print(age > 18 and age > 17 and age > 16)
  37. print(age > 20 and age > 17 and age < 20)
  38. print("or 多个条件成立一个则为True,全部不成立则为False")
  39. age = 19
  40. print(age > 18 or age > 20 or age > 100)
  41. print(age > 100 or age > 20 or age > 200)
  42. print(" in 要判断特定的值是否在已包含的列表中")
  43. name = ['Frank', 'Alex', 'Bob']
  44. print('Frank' in name)
  45. print('aa' in name)
  46. print("not in 要判定特定的值不在包含的列表中")
  47. print('Frank' not in name)
  48. print('May' not in name)
  49. print("布尔表达式")
  50. print("5.3 if语句")
  51. print("5.3.1 简单的if语句")
  52. print("这是最长情况下的if结构了最短的情况下,仅仅只是用 if 就行了")
  53. # 选举
  54. age = 19
  55. if age >= 18:
  56. print("You are old enough to vote!")
  57. print("5.3.2两种选择情况下的,if-else")
  58. # 选举
  59. age = 17
  60. if age >= 18:
  61. print("You are old enough to vote!")
  62. else:
  63. print("Sorry,you are too young to vote!")
  64. print("5.3.3多种情况下if-elif-else,elif可以有多个")
  65. # 游乐园卖票
  66. age = 12
  67. if age < 4:
  68. print("You admission cost is $0.")
  69. elif age < 18:
  70. print("Your admission cost is $5.")
  71. else:
  72. print("Your admission cost is $10.")
  73. print("让代码更简洁")
  74. age = 12
  75. if age < 4:
  76. price = 0
  77. elif age < 18:
  78. price = 5
  79. else:
  80. price = 10
  81. print("Your admission cost is $" + str(price) + ",")
  82. # python并不要求if-elif结构后面必须有else代码块,else是一条包罗万象的语句,只要不满足就会执行else下的语句,
  83. # 可能会引起无效甚至恶意的数据,如果知道最终要测试的条件,应该考虑是用elif代替else!
  84. # 不管if-n个elif-else,只要满足一个,就只会执行判断为True下面的条件,不会执行其他的。
  85. # python将在列表至少包含一个元素的时候返回True,并在列表为空时返回False!
  86. print("5.3.4使用多个elif代码块")
  87. age = 12
  88. if age < 4:
  89. price = 0
  90. elif age < 18:
  91. price = 5
  92. elif age < 65:
  93. price = 10
  94. else:
  95. price = 5
  96. print("Your admission cost is $" + str(price) + ",")
  97. print("5.3.5省略else代码块")
  98. age = 12
  99. if age < 4:
  100. price = 0
  101. elif age < 18:
  102. price = 5
  103. elif age < 65:
  104. price = 10
  105. elif age >= 65:
  106. price = 5
  107. print("Your admission cost is $" + str(price) + ",")
  108. print("5.3.6测试多个条件")
  109. requested_toppings = ['mushrooms', 'extra cheese']
  110. if 'mushrooms' in requested_toppings:
  111. print("Adding mushrooms")
  112. if 'aaa0' in requested_toppings:
  113. print("Adding aaa0")
  114. if 'extra cheese' in requested_toppings:
  115. print("Adding extra cheese")
  116. print("Finshed making your pizza!")
  117. requested_toppings = ['mushrooms', 'extra cheese']
  118. if 'mushrooms' in requested_toppings:
  119. print("Adding mushrooms")
  120. elif 'aaa0' in requested_toppings:
  121. print("Adding aaa0")
  122. elif 'extra cheese' in requested_toppings:
  123. print("Adding extra cheese")
  124. print("Finshed making your pizza!")
  125. print("5.4使用if语句处理列表")
  126. requested_toppings = ['mushrooms', 'extra cheese']
  127. for requested_topping in requested_toppings:
  128. print("Adding " + requested_topping + ".")
  129. print("Finshed making your pizza")
  130. requested_toppings = ['mushrooms', 'extra cheese']
  131. for requested_topping in requested_toppings:
  132. if requested_topping == 'extra cheese':
  133. print("Sorry,we are out of " + requested_topping + ",")
  134. else:
  135. print("Adding " + requested_topping + ".")
  136. print("Finished making your pizza")
  137. print("5.4.2确定列表不是空的")
  138. requested_toppings = []
  139. if requested_toppings:
  140. for requested_topping in requested_toppings:
  141. print("Adding " + requested_topping + ".")
  142. print("Finished making your pizza")
  143. else:
  144. print("Are you sure you want a plain pizza")
  145. print("5.4.3使用多个列表")
  146. available_toppings = ['mushrooms', 'olives', 'green peppers', 'pineapple', 'extra cheese']
  147. requested_toppings = ['mushrooms', 'extra cheese', 'aaaa']
  148. for requested_topping in requested_toppings:
  149. if requested_topping in available_toppings:
  150. print("Adding " + requested_topping + ".")
  151. else:
  152. print("Sorry,we are out of " + requested_topping + ",")
  153. print("Finished making your pizza")
  154. 输出:
  155. C:\Anaconda3\python.exe H:/python/venv/text
  156. 5 if语句
  157. 5.1 一个简单示例
  158. Audi
  159. BMW
  160. Subaru
  161. Toyota
  162. 5.2.1检查是否相等
  163. true
  164. 5.2.2 检查是否相等不考虑大小写
  165. False
  166. True
  167. 检查是否不等
  168. False
  169. True
  170. 比较数字
  171. True
  172. True
  173. True
  174. False
  175. True
  176. False
  177. 5.2.5检查多个条件
  178. and 多个条都成立则为True,否则为False
  179. True
  180. False
  181. or 多个条件成立一个则为True,全部不成立则为False
  182. True
  183. False
  184. in 要判断特定的值是否在已包含的列表中
  185. True
  186. False
  187. not in 要判定特定的值不在包含的列表中
  188. False
  189. True
  190. 布尔表达式
  191. 5.3 if语句
  192. 5.3.1 简单的if语句
  193. 这是最长情况下的if结构了最短的情况下,仅仅只是用 if 就行了
  194. You are old enough to vote!
  195. 5.3.2两种选择情况下的,if-else
  196. Sorry,you are too young to vote!
  197. 5.3.3多种情况下if-elif-else,elif可以有多个
  198. Your admission cost is $5.
  199. 让代码更简洁
  200. Your admission cost is $5,
  201. 5.3.4使用多个elif代码块
  202. Your admission cost is $5,
  203. 5.3.5省略else代码块
  204. Your admission cost is $5,
  205. 5.3.6测试多个条件
  206. Adding mushrooms
  207. Adding extra cheese
  208. Finshed making your pizza!
  209. Adding mushrooms
  210. Finshed making your pizza!
  211. 5.4使用if语句处理列表
  212. Adding mushrooms.
  213. Adding extra cheese.
  214. Finshed making your pizza
  215. Adding mushrooms.
  216. Sorry,we are out of extra cheese,
  217. Finished making your pizza
  218. 5.4.2确定列表不是空的
  219. Are you sure you want a plain pizza
  220. 5.4.3使用多个列表
  221. Adding mushrooms.
  222. Adding extra cheese.
  223. Sorry,we are out of aaaa,
  224. Finished making your pizza
  225. Process finished with exit code 0

发表评论

表情:
评论列表 (有 0 条评论,295人围观)

还没有评论,来说两句吧...

相关阅读