{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 条件判断和循环"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"+ if语句\n",
"+ for循环\n",
"+ while循环\n",
"+ 列表生产式"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## if语句"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1.1 什么是if语句"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"if语句用来判断一个条件,通过条件的真假来选择不同的执行方案,如果条件为真,则执行其后面的代码块,如果条件为假,则执行其后面的代码块"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"if语句的一般格式:\n",
"\n",
"if 条件判断:\n",
"\n",
" 条件判断为真时执行的代码块\n",
" \n",
"else:\n",
"\n",
" 条件判断为假时执行的代码块"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"+ 从最简单的if语句开始"
]
},
{
"cell_type": "code",
"execution_count": 84,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hello!\n"
]
}
],
"source": [
"number = 5 #变量number赋值\n",
"if number > 0: #number与0进行比较,作为条件判断,返回布尔值\n",
" print(\"hello!\") #条件为True的时候执行该语句"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"这段代码的意思就是:给变量number赋值了5,通过if语句对number>0进行一个条件判断,如果number大于0,则执行其随后锁紧的代码块,即打印出hello"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"要点注意:\n",
"1. 条件判断的后面必须要跟一个冒号:\n",
"2. 条件判断下面代码块的缩进是必要的,如若没有缩进则会出错"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1.2 if+else"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"情景:小明的爸爸对小明的成绩管教异常严格,只要小明的成绩没达到100分,就没零花钱,如果达到了100分,就奖励50块钱,而此次小明的语文成绩拿到了99分,那么用if+else的语句如何来描述这段场景呢?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"思路:\n",
"1. 定义一个变量grade,并把99分存入该变量中,即变量赋值\n",
"2. 我们已经知道了100分是小明的目标,是用来和小明目前的成绩进行比较的一个标准,因此条件判断就是99和100来比较,即grade和100比较\n",
"3. 比较的结果有两种,(1)没达到标准100分,这时的结果是没零花钱,因此要打印出没零花钱的信息 (2)达到了标准100分,这时打印出得到50块钱的信息"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"没钱花咯\n"
]
}
],
"source": [
"grade = 99\n",
"if grade != 100:\n",
" print(\"没钱花咯\")\n",
"else:\n",
" print(\"噢耶,拿到50块钱!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"要点:\n",
"1. 用!表示非的意思,因此!=就表示不等于\n",
"2. 依然要十分注意条件判断后面需要跟冒号:\n",
"3. else后面也需要跟冒号,并且当条件判断为假的时候,执行else后面跟随的代码块,依然要注意缩进"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1.3 if+elif+else"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"情景:后来小明的爸爸觉得这样太严格了,给儿子的压力太大,良心受到了谴责,于是他放松了条件,对小明说:“你这次数学考试能考到90到100(包括90),那么就给你50块钱;如果考到80到90(包括80但不包括90),则给你30块钱,如果是80以下的话,那么就不给你钱了”。结果小明那天重感冒发挥失常,只考到了77分,那么用if+elif+else如何来描述这段情景呢?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"思路:\n",
"1. 需要给小明的实际成绩赋给一个变量grade\n",
"2. 90-100,80-90,80以下,分别有三个区间,那么我们可以从第一个区间或者最后一个区间开始写条件判断都是可以的,就从第一个区间开始写吧,将小明的成绩来和90分进比较,假如小明的成绩大于等于90为真,说明小明的成绩在第一个区间即90以上,则50块钱到手;如果为假,则进入下一个条件判断,即判断小明的成绩是否大于等于80,如果为真,则表明此时小明的成绩在第二个区间即80-90,如果为假,则直接进入最后一个区间即80以下。"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"没钱拿T T\n"
]
}
],
"source": [
"grade = 77\n",
"if grade >= 90:\n",
" print(\"50块到手\")\n",
"elif grade >= 80:\n",
" print(\"30块到手\")\n",
"else:\n",
" print(\"没钱拿T T\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"要点:\n",
"1. elif后面一定要写第二个条件判断,并且冒号跟随\n",
"2. elif是可以多次出现的,可以写多个elif语句\n",
"3. 写多条件判断的时候,从范围最小的或者最大的一边开始进行条件判断"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1.4 if条件判断的简写"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"if number: \n",
" print(\"good\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"只要number是非零数值、非空字符串、非空列表、非空字典等,number就判断为True,否则为False"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"尝试下"
]
},
{
"cell_type": "code",
"execution_count": 91,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"出错啦\n"
]
}
],
"source": [
"a = \"\"\n",
"if a:\n",
" print(1)\n",
"else:\n",
" print(\"出错啦\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. for循环"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"2.1 什么是for循环"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"for循环是帮助我们做重复性操作的一种语句,对x中的每一个元素都执行相同的操作,x可以是列表、元组、字典、集合、字符串等。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2.2 开始写一个简单的for循环"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"情景:有一天,瞬,早季,觉,真理亚四个小伙伴成群结队去到了童年阴影之处伽椰子的家中探险,他们一起商量好,每次只准一人进去看看,看完就出来,我们用for循环来描述下这段场景,从而理解for循环的意义"
]
},
{
"cell_type": "code",
"execution_count": 92,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"瞬:哇,吓得我头皮发麻,赶紧溜!\n",
"早季:哇,吓得我头皮发麻,赶紧溜!\n",
"觉:哇,吓得我头皮发麻,赶紧溜!\n",
"真理亚:哇,吓得我头皮发麻,赶紧溜!\n"
]
}
],
"source": [
"people = [\"瞬\",\"早季\",\"觉\",\"真理亚\"]\n",
"for i in people:\n",
" print(i + \":哇,吓得我头皮发麻,赶紧溜!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"这段代码的意思是:\n",
"1. 将一个包含\"瞬\",\"早季\",\"觉\",\"真理亚\"的列表赋值给变量people\n",
"2. 我希望能打印出这四个人的相关信息,但是我懒得一个一个地打印出来,于是我用for循环来帮助我完成这一需求,因此for i in people:就表示把people中的每一个元素依次代入i变量中,并依次执行随后的缩进代码块即打印i\n",
"3. 所以当for循环第一次执行时,i=\"瞬\",print(i + \":哇,吓得我头皮发麻,赶紧溜!\")也就是print(\"瞬\"+\":哇,吓得我头皮发麻,赶紧溜!\");当for循环第二次执行时,i=\"早季\",print(i)也就是print(\"早季\",\":哇,吓得我头皮发麻,赶紧溜!\").依次类推一直执行到people中的所有元素都执行过为止"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2.3 没有缩进的代码只执行一次"
]
},
{
"cell_type": "code",
"execution_count": 97,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"瞬:哇,吓得我头皮发麻,赶紧溜!\n",
"\n",
"早季:哇,吓得我头皮发麻,赶紧溜!\n",
"\n",
"觉:哇,吓得我头皮发麻,赶紧溜!\n",
"\n",
"真理亚:哇,吓得我头皮发麻,赶紧溜!\n",
"\n",
"你们还想再来一次吗?\n"
]
}
],
"source": [
"people = [\"瞬\",\"早季\",\"觉\",\"真理亚\"]\n",
"for i in people:\n",
" print(i + \":哇,吓得我头皮发麻,赶紧溜!\\n\")\n",
" \n",
"print(\"你们还想再来一次吗?\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"要点:缩进的重要性,因此当你出错的时候你需要看看你是否在缩进上出错了"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2.4 数值计算"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"利用for循环我们也能快速地进行一些数学运算"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"情景:遥想当年高斯10岁的时候,他的数学老师在课堂上布置了一道题目,1+2+3+...+100的和是多少?高斯很快就算出是5050,当时老师和小伙伴们都惊呆了,然而其中有一位高斯的同学虽然没高斯那么聪明,但也不差,他表示不服,然后高呼一句:人生苦短,我用python。于是啪啪啪了一段代码,通过for循环他实现了他的人生目标。"
]
},
{
"cell_type": "code",
"execution_count": 100,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1+2+3+...+100 = 5050\n"
]
}
],
"source": [
"sum = 0\n",
"for i in range(101):\n",
" sum = sum + i\n",
"\n",
"print(\"1+2+3+...+100 = \" + str(sum))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"代码解读:\n",
"1. 我们定义了变量sum,并赋值0\n",
"2. range函数能快速地生成一系列数字,因此通过range(101)生成了0到100的数字,为什么101取不到,因为生成序列都是左闭右开的\n",
"3. 对于range(101)中的每一个数字,都代入到i变量中,然后执行其缩进的代码块,即sum=sum+1,求和运算\n",
"4. 第一次循环时,i=0,sum=0+0=0;第二次,i=1,sum=0+1=1,第三次,i=2,sum=1+2=3,依次累加\n",
"5. 最后打印出结果,注意的一点是sum是数值,数值和字符串不能直接相加,因此通过str()将sum转换成字符型数值"
]
},
{
"cell_type": "code",
"execution_count": 102,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"[0,\n",
" 1,\n",
" 2,\n",
" 3,\n",
" 4,\n",
" 5,\n",
" 6,\n",
" 7,\n",
" 8,\n",
" 9,\n",
" 10,\n",
" 11,\n",
" 12,\n",
" 13,\n",
" 14,\n",
" 15,\n",
" 16,\n",
" 17,\n",
" 18,\n",
" 19,\n",
" 20,\n",
" 21,\n",
" 22,\n",
" 23,\n",
" 24,\n",
" 25,\n",
" 26,\n",
" 27,\n",
" 28,\n",
" 29,\n",
" 30,\n",
" 31,\n",
" 32,\n",
" 33,\n",
" 34,\n",
" 35,\n",
" 36,\n",
" 37,\n",
" 38,\n",
" 39,\n",
" 40,\n",
" 41,\n",
" 42,\n",
" 43,\n",
" 44,\n",
" 45,\n",
" 46,\n",
" 47,\n",
" 48,\n",
" 49,\n",
" 50,\n",
" 51,\n",
" 52,\n",
" 53,\n",
" 54,\n",
" 55,\n",
" 56,\n",
" 57,\n",
" 58,\n",
" 59,\n",
" 60,\n",
" 61,\n",
" 62,\n",
" 63,\n",
" 64,\n",
" 65,\n",
" 66,\n",
" 67,\n",
" 68,\n",
" 69,\n",
" 70,\n",
" 71,\n",
" 72,\n",
" 73,\n",
" 74,\n",
" 75,\n",
" 76,\n",
" 77,\n",
" 78,\n",
" 79,\n",
" 80,\n",
" 81,\n",
" 82,\n",
" 83,\n",
" 84,\n",
" 85,\n",
" 86,\n",
" 87,\n",
" 88,\n",
" 89,\n",
" 90,\n",
" 91,\n",
" 92,\n",
" 93,\n",
" 94,\n",
" 95,\n",
" 96,\n",
" 97,\n",
" 98,\n",
" 99,\n",
" 100]"
]
},
"execution_count": 102,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(range(101))"
]
},
{
"cell_type": "code",
"execution_count": 104,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[2, 4, 6, 8]"
]
},
"execution_count": 104,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(range(2,10,2)) #range(开始值,终值,步长) 开始值和步长都是可以省略的,开始值省略代表从0开始,步长省略代表步长为1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2.5 遍历部分列表"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"在for循环中使用切片,只遍历列表的部分元素"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"情景:有一天你闲来无事,邀请了4位好友开黑,一起打游戏,大战一天一夜后,你希望对今晚打的最好的三个人说一句话,目前你得到的成员列表是,L=[\"fizz\",\"jack\",\"ray\",\"lucy\",\"bally\"] ,通过for循环该如何处理呢?"
]
},
{
"cell_type": "code",
"execution_count": 105,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"fizz you are so graet!\n",
"jack you are so graet!\n",
"ray you are so graet!\n"
]
}
],
"source": [
"L = [\"fizz\",\"jack\",\"ray\",\"lucy\",\"bally\"]\n",
"for i in L[:3]:\n",
" print(i + \" you are so graet!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2.6 遍历字典"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"+ 遍历所有键-值对"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"items()方法"
]
},
{
"cell_type": "code",
"execution_count": 107,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"名字:小明\n",
"分数:80\n",
"\n",
"名字:小红\n",
"分数:77\n",
"\n",
"名字:小张\n",
"分数:90\n"
]
}
],
"source": [
"dict_1 = {\"小明\":80,\"小红\":77,\"小张\":90} #创建字典\n",
"for name,score in dict_1.items(): #在for循环中,创建两个变量用于存储字典中的键和值,使用items()方法\n",
" print(\"\\n名字:\" + name) #打印出名字信息\n",
" print(\"分数:\" + str(score)) #打印出分数信息"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"要点:\n",
"1. 在for循环中创建两个变量,名称是任意的,建议使用易于识别的名称,这两个变量分别存储字典中的键和值\n",
"2. 通过items()方法可以返回一个键-值对列表"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"+ 遍历所有键"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"keys()方法"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"小明\n",
"小红\n",
"小张\n"
]
}
],
"source": [
"dict_1 = {\"小明\":80,\"小红\":77,\"小张\":90}\n",
"for name in dict_1.keys():\n",
" print(name)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"不写keys()也是可以的"
]
},
{
"cell_type": "code",
"execution_count": 109,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"小明\n",
"小红\n",
"小张\n"
]
}
],
"source": [
"dict_1 = {\"小明\":80,\"小红\":77,\"小张\":90}\n",
"for name in dict_1:\n",
" print(name)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"要点:\n",
"1. keys方法能返回一个列表,该列表包含了字典里所有的键\n",
"2. 不写keys()也是可以的,默认就是返回含有键的列表,但是如果写了keys()会更有助于你辨别该程序是用来干嘛的"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"+ 遍历所有值"
]
},
{
"cell_type": "code",
"execution_count": 110,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"80\n",
"77\n",
"90\n"
]
}
],
"source": [
"dict_1 = {\"小明\":80,\"小红\":77,\"小张\":90}\n",
"for score in dict_1.values():\n",
" print(score)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"按顺序遍历所有值"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"77\n",
"80\n",
"90\n"
]
}
],
"source": [
"dict_1 = {\"小明\":80,\"小红\":77,\"小张\":90}\n",
"for score in sorted(dict_1.values()):\n",
" print(score)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"要点:\n",
"如果要返回一个含有值的列表,就得在字典名称后面加上.values(),和keys()不一样的是values()不能省略"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3.while循环"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3.1 什么是while循环"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"while循环是一种当符合条件时就能让程序不断地运行的语句,直到条件不满足时才停止运行"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"+ 创建一个简单的while循环"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"情景:在国产大片《甄嬛传》中,华妃因长期吸入了欢宜香(含有大量麝香),导致不能怀孕,那么假设她连续吸了30天以上,就导致无法怀孕,请用while循环来描述这段场景"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "code",
"execution_count": 112,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"第1天吸了欢宜香,爽~美滋滋\n",
"第2天吸了欢宜香,爽~美滋滋\n",
"第3天吸了欢宜香,爽~美滋滋\n",
"第4天吸了欢宜香,爽~美滋滋\n",
"第5天吸了欢宜香,爽~美滋滋\n",
"第6天吸了欢宜香,爽~美滋滋\n",
"第7天吸了欢宜香,爽~美滋滋\n",
"第8天吸了欢宜香,爽~美滋滋\n",
"第9天吸了欢宜香,爽~美滋滋\n",
"第10天吸了欢宜香,爽~美滋滋\n",
"第11天吸了欢宜香,爽~美滋滋\n",
"第12天吸了欢宜香,爽~美滋滋\n",
"第13天吸了欢宜香,爽~美滋滋\n",
"第14天吸了欢宜香,爽~美滋滋\n",
"第15天吸了欢宜香,爽~美滋滋\n",
"第16天吸了欢宜香,爽~美滋滋\n",
"第17天吸了欢宜香,爽~美滋滋\n",
"第18天吸了欢宜香,爽~美滋滋\n",
"第19天吸了欢宜香,爽~美滋滋\n",
"第20天吸了欢宜香,爽~美滋滋\n",
"第21天吸了欢宜香,爽~美滋滋\n",
"第22天吸了欢宜香,爽~美滋滋\n",
"第23天吸了欢宜香,爽~美滋滋\n",
"第24天吸了欢宜香,爽~美滋滋\n",
"第25天吸了欢宜香,爽~美滋滋\n",
"第26天吸了欢宜香,爽~美滋滋\n",
"第27天吸了欢宜香,爽~美滋滋\n",
"第28天吸了欢宜香,爽~美滋滋\n",
"第29天吸了欢宜香,爽~美滋滋\n",
"第30天吸了欢宜香,爽~美滋滋\n",
"我居然不能怀孕了?!***\n"
]
}
],
"source": [
"count = 0\n",
"while count < 30:\n",
" count += 1\n",
" print(\"第\" + str(count) + \"天吸了欢宜香,爽~美滋滋\")\n",
"\n",
"print(\"我居然不能怀孕了?!***\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"情景:你来到了一家拉面店,老板和你说:如果你能吃够10碗,就给你免单,你一听就来劲了,卷起了衣服,开始干!请用while循环来描述这段情景"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"我已经吃了1碗拉面!\n",
"我已经吃了2碗拉面!\n",
"我已经吃了3碗拉面!\n",
"我已经吃了4碗拉面!\n",
"我已经吃了5碗拉面!\n",
"我已经吃了6碗拉面!\n",
"我已经吃了7碗拉面!\n",
"我已经吃了8碗拉面!\n",
"我已经吃了9碗拉面!\n",
"我已经吃了10碗拉面!\n",
"\n",
"欧耶,达成成就!\n"
]
}
],
"source": [
"lamian = 0\n",
"while lamian < 10:\n",
" lamian += 1\n",
" print(\"我已经吃了\" + str(lamian) + \"碗拉面!\")\n",
" \n",
"print(\"\\n欧耶,达成成就!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3.2 交互式while语句"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"+ input()"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"请输入你最喜欢的数字:1\n",
"哇,你最喜欢的数字是:1!\n"
]
}
],
"source": [
"number = input(\"请输入你最喜欢的数字:\")\n",
"print(\"哇,你最喜欢的数字是:\" + number + \"!\" )"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 判断变量是否为某种类型\n",
"isinstance(number,str)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"要点:\n",
"1. 指定让人清晰易懂的提示\n",
"2. 输入的结果会显示为字符串格式,如果需要得到输入的数值,需要加个int"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"+ while交互式语句"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"请输入你喜欢的数字,如果你不想继续输入内容就输入q来退出:1\n",
"1\n",
"请输入你喜欢的数字,如果你不想继续输入内容就输入q来退出:2\n",
"2\n",
"请输入你喜欢的数字,如果你不想继续输入内容就输入q来退出:q\n",
"q\n"
]
}
],
"source": [
"number = \"请输入你喜欢的数字,如果你不想继续输入内容就输入q来退出:\"\n",
"message = \"\"\n",
"while message != \"q\":\n",
" message = input(number)\n",
" print(message)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3.3 break语句"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"应用场景:当你需要在某个条件被满足时就直接退出整个while循环时,可以使用break语句来实现"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n"
]
}
],
"source": [
"number = 0\n",
"while number < 20:\n",
" number += 1\n",
" if number % 2 == 0:\n",
" break #遇到number能被2整除的情况即number为偶数时,就直接结束掉所有的while循环,退出循环\n",
" else:\n",
" print(number)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3.4 continue语句"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"应用场景:当你需要在某个条件被满足时直接跳过后面的步骤,再重新执行下一次循环时,可以用continue语句实现"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false,
"scrolled": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"3\n",
"5\n",
"7\n",
"9\n",
"11\n",
"13\n",
"15\n",
"17\n",
"19\n"
]
}
],
"source": [
"number = 0\n",
"while number < 20:\n",
" number += 1\n",
" if number % 2 == 0:\n",
" continue #直接忽略掉偶数部分,即当number能被2整除时,直接退出这一次的循环,继续下一轮循环,因此下轮循环即是奇数 \n",
" #当number为奇数部分时,if number % 2 == 0不成立,于是会执行print(number)的语句,即打印出该奇数\n",
" print(number)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3.5 pass语句"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"应用场景:当你在某一块内容暂时不知道写什么的时候,可以用pass占位,pass不会做任何事情,只是为了保护整个程序完整的结构"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false,
"scrolled": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"3\n",
"4\n",
"5\n",
"6\n",
"7\n",
"8\n",
"9\n",
"10\n",
"11\n",
"12\n",
"13\n",
"14\n",
"15\n",
"16\n",
"17\n",
"18\n",
"19\n",
"20\n"
]
}
],
"source": [
"number = 0\n",
"while number < 20:\n",
" number += 1\n",
" if number % 2 == 0:\n",
" pass\n",
" print(number)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4.列表生成式"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"4.1 什么是列表生成式(List Comprehensions)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"能用一行代码来生成列表list的生成式"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"场景:如果你要生成[1x2,2x2,3x2,...,10x2]这样的列表,按照一般的想法是用for循环来实现:"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]\n"
]
}
],
"source": [
"#用了4行代码实现\n",
"list1 = []\n",
"for i in range(1,11): \n",
" list1.append(i * 2)\n",
"\n",
"print(list1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"列表生成式(只需要一行代码即可实现相同功能)"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[i * 2 for i in range(1,11)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"4.2 给列表生成式加个if判断"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[12, 14, 16, 18, 20]"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[i * 2 for i in range(1,11) if i - 5 > 0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"4.3 双重循环"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"[6,\n",
" 7,\n",
" 8,\n",
" 9,\n",
" 10,\n",
" 11,\n",
" 7,\n",
" 8,\n",
" 9,\n",
" 10,\n",
" 11,\n",
" 12,\n",
" 8,\n",
" 9,\n",
" 10,\n",
" 11,\n",
" 12,\n",
" 13,\n",
" 9,\n",
" 10,\n",
" 11,\n",
" 12,\n",
" 13,\n",
" 14]"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[i + k for i in range(1,5) for k in range(5,11)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"4.4 通过字典用双变量创建列表"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"['北京工资为:8000', '上海工资为:7500', '杭州工资为:7000']"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dict_1 = {\"北京\":8000,\"上海\":7500,\"杭州\":7000}\n",
"[key + \"工资为:\" + str(value) for key,value in dict_1.items()]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 练习题"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1 创建一个列表list1,其中包含1到30的数字。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"+ (1)使用for循环打印出list1中能被3整除的数字 \n",
"+ (2)用列表生成式得到一个列表,该列表包含list1中能被3整除的数字"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[3, 6, 9, 12, 15, 18, 21, 24, 27, 30]"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list = []\n",
"for number in range(1,31):\n",
" if number % 3 ==0:\n",
" list.append(number)\n",
" \n",
"list"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[3, 6, 9, 12, 15, 18, 21, 24, 27, 30]"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[number for number in list1 if number % 3 == 0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2 创建一个变量number,number=0,当number小于10的时候,number的数值加1,当number为奇数时,打印出number,为偶数时不打印,如此不断循环,直到number大于等于10的时候结束循环\n",
"+ (1)请用while循环执行\n",
"+ (2)请用列表生成式来实现相同的结果"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"3\n",
"5\n",
"7\n",
"9\n"
]
}
],
"source": [
"number = 0\n",
"while number < 10:\n",
" number += 1\n",
" if number % 2 != 0:\n",
" print(number)"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[1, 3, 5, 7, 9]"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[number for number in range(11) if number % 2 != 0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3 创建变量age,age的值由input函数输入,写一个if-elif-else的语句,根据输入的age值来判断此人是什么阶段的人"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"+ (1)如果这个人的年龄age在18岁以上(含18),则打印出他是成年人的消息\n",
"+ (2)如果这个人的年龄age在13~18岁(含13不含18),则打印出他是青少年的消息\n",
"+ (3)如果这个人的年龄在13岁以下(不含13),则打印出他是儿童的消息"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"age = input(\"请输入你的年龄:\")\n",
"if int(age) >= 18:\n",
" print(\"你是成年人\")\n",
"elif int(age) >= 13:\n",
" print(\"你是青少年\")\n",
"else:\n",
" print(\"\")"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [default]",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {
"height": "469px",
"left": "295px",
"top": "159px",
"width": "171px"
},
"toc_section_display": true,
"toc_window_display": true
}
},
"nbformat": 4,
"nbformat_minor": 2
}
一键复制
编辑
Web IDE
原始数据
按行查看
历史
本文详细介绍了Python编程中的条件判断和循环结构,包括if语句、for循环和while循环,以及它们在实际场景中的应用。通过实例演示了如何使用条件判断进行条件分支操作,以及如何用for循环和while循环实现重复执行任务。此外,还介绍了列表生成式在简化代码生成列表方面的应用。


被折叠的 条评论
为什么被折叠?



