forked from pythonpeixun/article
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d58d6dc
commit 64bb147
Showing
4 changed files
with
72 additions
and
17 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# 跟黄哥学习python第五章 | ||
|
||
# 顺序结构、判断结构,循环结构 | ||
# 顺序结构、判断结构 | ||
|
||
#顺序结构 | ||
顺序结构的程序设计是最简单的,只要按照解决问题的顺序写出相应的语句就行,它的执行顺序是自上而下,依次执行。 | ||
|
@@ -137,7 +137,7 @@ instances of user-defined classes, if the class defines a __bool__() or __len__( | |
else: | ||
语句块 | ||
|
||
4、嵌套if | ||
4、if嵌套 | ||
if 布尔表达式: | ||
语句块 | ||
if 布尔表达式: | ||
|
@@ -155,34 +155,67 @@ if之后的布尔表达式俗称条件,如果它为真,如果为真,则执 | |
 | ||
|
||
两路分支判断 | ||
if 布尔表达式: | ||
语句块 | ||
else: | ||
语句块 | ||
|
||
if 布尔表达式: | ||
语句块 | ||
else: | ||
语句块 | ||
|
||
这个是if 语句后面的条件为真,则执行下面的语句块,否则执行else下面的语句块。 | ||
 | ||
|
||
多路分支判断 | ||
|
||
if 布尔表达式: | ||
语句块 | ||
elif 布尔表达式: | ||
语句块 | ||
elif 布尔表达式: | ||
语句块 | ||
........ | ||
else: | ||
语句块 | ||
if 布尔表达式: | ||
语句块 | ||
elif 布尔表达式: | ||
语句块 | ||
elif 布尔表达式: | ||
语句块 | ||
........ | ||
else: | ||
语句块 | ||
|
||
如果if 语句后面条件为假,就不执行if下面的语句块,转到elif 判断,如果有一个elif | ||
后面的条件为真,则执行下面的语句,执行完,就跳出判断结构,继续下面的语句执行。 | ||
如果if elif 语句后面的条件都为假,则执行else 下面的语句块。 | ||
 | ||
|
||
代码解释:上图中,第三行代码,input()函数为python内置函数,直接可以调用, | ||
input()函数可以传递一个参数,一般是传递字符串,提示输入信息。input作用是 | ||
从输入读一行,并以字符串的形式返回。前面加float(),是将这样的字符串("89.8") | ||
转化为浮点数。4到12行为多路分支判断结构。 | ||
|
||
待续............. | ||
|
||
if 嵌套 | ||
if 或 if-else 语句中可以是任意语句,当然也可以是if或if-else语句。 | ||
如果if 或if-esle 中再包含if 或if-else语句,则称为if 嵌套。 | ||
|
||
if 布尔表达式: | ||
语句块 | ||
if 布尔表达式: | ||
语句块 | ||
else: | ||
语句块 | ||
else: | ||
语句块 | ||
|
||
 | ||
|
||
|
||
# 判断结构实例: | ||
1、判断是不是闰年 | ||
代表年的数字(如2016)能被4整除但不能被100整除,或能被400整除,那么这个年份为闰年。 | ||
整除的意思是能除尽,没有余数,python中用% 运算符, 2016 % 4 结果为 0 | ||
将闰年的定义,翻译为python语言中的逻辑表达式,变量year代表年份 | ||
year % 4 == 0 and year % 100 != 0 or year % 400 == 0 | ||
前面说过,关系运算符优先逻辑运算符,逻辑运算符and优先or,所以可以写成上面的表达式。 | ||
为了清晰,可以写成(year % 4 == 0 and year % 100 != 0) or (year % 400 == 0) | ||
 | ||
|
||
|
||
# 习题: | ||
特别提示:只有参加[黄哥python远程视频培训班](https://github.com/pythonpeixun/article/blob/master/index.md) 或参加黄哥收费答疑群,才能得到习题和辅导。 | ||
|
||
|
||
如果你感觉黄哥的文章对你有帮助请打赏,支付宝账号:[email protected] | ||
|
@@ -192,7 +225,7 @@ else: | |
|
||
[点击黄哥python培训试看视频播放地址](https://github.com/pythonpeixun/article/blob/master/python_shiping.md) | ||
|
||
[黄哥python远程视频培训班](https://github.com/pythonpeixun/article/blob/master/index.md) | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# 跟黄哥学习python第五章 | ||
|
||
# 循环结构 | ||
|
||
待续.......... | ||
|
||
|
||
# 习题: | ||
特别提示:只有参加[黄哥python远程视频培训班](https://github.com/pythonpeixun/article/blob/master/index.md) 或参加黄哥收费答疑群,才能得到习题和辅导。 | ||
|
||
|
||
如果你感觉黄哥的文章对你有帮助请打赏,支付宝账号:[email protected] | ||
|
||
[跟黄哥学习python第六章](learn_python_follow_brother_huang_6.md) | ||
|
||
|
||
[点击黄哥python培训试看视频播放地址](https://github.com/pythonpeixun/article/blob/master/python_shiping.md) | ||
|
||
|
||
|
||
|
||
|