## python第二课 ### 课程内容 1、条件判断 2、循环 3、函数 4、类 ### 条件判断 ```python #伪代码表示 if condition: do something else: do something ``` #### 应用题:小明买水果,合计金额为32.5元,水果店搞活动,满30打九折,求小明的实际花费? ```python total_cost = 32.5 if total_cost >30: discount = 0.9 else: discount = 1 total_cost *= discount print('小明的实际花费为: {}元'.format(total_cost)) ``` 小明的实际花费为: 29.25元 #### 应用题:如果购买水果超过30元,打九折,超过50元,打八折,求小明的实际花费? ```python total_cost = 62.5 is_vip = True if total_cost > 50: if is_vip: discount = 0.8 else: discount = 1 elif total_cost >30: discount = 0.9 else: discount = 1 total_cost *= discount print('小明的实际花费为: {}元'.format(total_cost)) ``` 小明的实际花费为: 50.0元 ### 重点 1、条件判断可以任意组合 第一层意思:elif可以有0到任意多个,else可有可无 第二层意思:条件判断可以进行嵌套 2、着重看一下condition ```python bool(''),bool({}),bool([]) ``` (False, False, False) ```python condition = '' if condition: print('True') else: print('False') ``` False #### 从理解的角度来讲,一个值被当做布尔值,概念上更像是有与没有的区别。 and or not ### 布尔型变量做运算 ```python a = True b = False print('a and b is {}'.format(a and b)) print('a or b is {}'.format(a or b)) ``` a and b is False a or b is True ### 非布尔型变量做and or not 运算 ```python a = 'hello world' b = [] print(bool(b)) print('a and b is {}'.format(a and b)) print('a and b is {}'.format(a or b)) ``` False a and b is [] a and b is hello world ```python #非布尔型变量 and 运算 a = [1,2,3] b = 10 print(a and b) #非布尔型变量 or 运算 a = 'ni hao' b = {'apple': 100} print(a or b) #非布尔型变量 not 运算,永远返回True或者False print (not b) ``` 10 ni hao False ### 条件判断的近亲 - 断言 ```python #伪代码 if not condition: crash program #它的意思是说:我断言它肯定是这样的,如果不是这样,那我就崩溃。 ``` ```python age = 19 assert age == 18,'他竟然不是18岁' ``` --------------------------------------------------------------------------- AssertionError Traceback (most recent call last) <ipython-input-22-b5515fdbbb07> in <module>() 1 age = 19 ----> 2 assert age == 18,'他竟然不是18岁' AssertionError: 他竟然不是18岁 ### 循环 for 循环 - 遍历循环 while循环 - 条件循环 ```python costs = [3,4,12,23,43,100] for cost in costs: print('消费 {} 元'.format(str(cost).center(10))) ``` 消费 3 元 消费 4 元 消费 12 元 消费 23 元 消费 43 元 消费 100 元 ### 生成一个长度为20的随机列表 ```python import random random_numbers = [] while len(random_numbers) < 20: random_numbers.append(random.randint(1,10)) print(random_numbers,len(random_numbers)) ``` [3, 4, 2, 3, 1, 1, 5, 10, 3, 7, 10, 7, 6, 8, 6, 5, 6, 3, 9, 4] 20 #### 编程建议:只要能使用For循环,就不要使用while循环。 ```python random_numbers = [] for i in range(20): random_numbers.append(random.randint(1,10)) print(random_numbers,len(random_numbers)) ``` [9, 5, 3, 6, 1, 3, 4, 9, 8, 10, 2, 7, 10, 4, 7, 5, 7, 1, 6, 7] 20 #### 什么时候必须用while循环:当循环的条件跟数量没有关系时,只能用while #### 题目:往空列表中添加随机数,直到添加的随机数为9,则终止 ```python random_numbers = [] while (9 not in random_numbers): random_numbers.append(random.randint(1,10)) print(random_numbers,len(random_numbers)) ``` [6, 1, 10, 4, 4, 4, 3, 6, 9] 9 ### 重点:只有一个元素的列表 #### 问题:a = [1,2,3] , b = 1, c = (b in a),猜测一下c是什么类型,它是不是一个元祖呢? ```python # 死循环演示 import time number = 0 while True: time.sleep(1) number += 1 print('hello world, {}'.format(number),end = '\r') ``` hello world, 54 --------------------------------------------------------------------------- KeyboardInterrupt Traceback (most recent call last) <ipython-input-36-ef3ff4317da5> in <module>() 2 number = 0 3 while True: ----> 4 time.sleep(1) 5 number += 1 6 print('hello world, {}'.format(number),end = '\r') KeyboardInterrupt: ```python a = [] b = () type(a),type(b) ``` (list, tuple) ```python a = [1] b = (1) type(a),type(b),b ``` (list, int, 1) ```python a = [1] b = (1,) type(a),type(b),b,len(b) ``` (list, tuple, (1,), 1) ```python a = [1,2,3] b = 1 c = (b in a) type(a),type(b),type(c),c ``` (list, int, bool, True) #### 上题中c不是一个元祖,它是一个布尔型变量 ```python random_numbers ``` [6, 1, 10, 4, 4, 4, 3, 6, 9] #### continue 跳过 ```python for number in random_numbers: if number % 2 == 0: print('{} is 偶数'.format(number)) else: # print('{} is 奇数'.format(number)) continue print('没有跳过') ``` 6 is 偶数 没有跳过 10 is 偶数 没有跳过 4 is 偶数 没有跳过 4 is 偶数 没有跳过 4 is 偶数 没有跳过 6 is 偶数 没有跳过 #### break 跳出循环 ```python for number in random_numbers: if number % 2 == 0: print('{} is 偶数'.format(number)) else: break print('没有结束') ``` 6 is 偶数 没有结束 #### 循环中的else:如果在循环过程中没有碰到break语句,就会执行else里面的代码 ```python random_numbers = [4,2,4] for number in random_numbers: if number % 2 == 0: print('{} is 偶数'.format(number)) else: break print('没有结束') else: print('all') ``` 4 is 偶数 没有结束 2 is 偶数 没有结束 4 is 偶数 没有结束 all #### for循环可以构建推导式 #### 所谓推导式,就是从一个数据序列构建另一个数据序列的方法 ```python random_numbers = list(range(10)) random_numbers ``` [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] ```python new_numbers = [] for number in random_numbers: new_numbers.append(number*10) new_numbers ``` [0, 10, 20, 30, 40, 50, 60, 70, 80, 90] #### 列表推导式 ```python #for循环构建的推导式 new_numbers = [number*10 for number in random_numbers] new_numbers ``` [0, 10, 20, 30, 40, 50, 60, 70, 80, 90] #### 字典推导式 ```python dict_numbers = {number:'A' for number in random_numbers} dict_numbers ``` {0: 'A', 1: 'A', 2: 'A', 3: 'A', 4: 'A', 5: 'A', 6: 'A', 7: 'A', 8: 'A', 9: 'A'} ```python tuple_numbers = (number*10 for number in random_numbers) tuple_numbers ``` <generator object <genexpr> at 0x0000021E4B4F6E08> #### 生成器 ```python #运行第一次和第二次的数据是不一样的,第二次为空列表,生成器只能生成一次 tuple(tuple_numbers) ``` ()