Skip to content

Commit dd79284

Browse files
committed
Signed-off-by: twowater <[email protected]>
1 parent f04d119 commit dd79284

File tree

6 files changed

+377
-2
lines changed

6 files changed

+377
-2
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@
1010
|Python 代码规范 | [掘金](https://juejin.im/post/593ebd7eac502e006b520a8f)[简书](http://www.jianshu.com/p/8b6c425b65a6)[CSDN](http://blog.csdn.net/Two_Water/article/details/73153945)[个人博客](http://twowater.com.cn/2017/06/13/Python%E4%BB%A3%E7%A0%81%E8%A7%84%E8%8C%83/)|
1111
|草根学Python(一)第一个Python程序|[掘金](https://juejin.im/post/594633e5ac502e006b9e1331)[简书](http://www.jianshu.com/p/0d757860c8cf)[CSDN](http://blog.csdn.net/Two_Water/article/details/73433637)[个人博客](http://twowater.com.cn/2017/06/18/%E8%8D%89%E6%A0%B9%E5%AD%A6Python-%E4%B8%80-%E7%AC%AC%E4%B8%80%E4%B8%AAPython%E7%A8%8B%E5%BA%8F/)|
1212
|草根学 Python(二)基本数据类型和变量|[掘金](https://juejin.im/post/5946b7f25c497d006bef5704)[简书](http://www.jianshu.com/p/b5388a6c2e72)[CSDN](http://blog.csdn.net/Two_Water/article/details/73478060)[个人博客](http://twowater.com.cn/2017/06/19/%E8%8D%89%E6%A0%B9%E5%AD%A6Python-%E4%BA%8C-%E5%9F%BA%E6%9C%AC%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E5%92%8C%E5%8F%98%E9%87%8F/)|
13-
|草根学 Python(三)List 和 Tuple|[掘金](https://juejin.im/post/593fdb87128fe1006a02ce92)[简书](http://www.jianshu.com/p/97c97d5a5a7c)[CSDN](http://blog.csdn.net/Two_Water/article/details/73524367)[个人博客](http://twowater.com.cn/2017/06/21/%E8%8D%89%E6%A0%B9%E5%AD%A6Python-%E4%B8%89-List-%E5%92%8C-Tuple/)|
13+
|草根学 Python(三)List 和 Tuple|[掘金](https://juejin.im/post/593fdb87128fe1006a02ce92)[简书](http://www.jianshu.com/p/97c97d5a5a7c)[CSDN](http://blog.csdn.net/Two_Water/article/details/73524367)[个人博客](http://twowater.com.cn/2017/06/21/%E8%8D%89%E6%A0%B9%E5%AD%A6Python-%E4%B8%89-List-%E5%92%8C-Tuple/)|
14+
|草根学Python(四) Dict 和 Set|[掘金](https://juejin.im/post/5947bf84ac502e5490e4a6a1)[简书](http://www.jianshu.com/p/90f5b897ce77)[CSDN](http://blog.csdn.net/two_water/article/details/73719026)[个人博客](http://twowater.com.cn/2017/06/25/%E8%8D%89%E6%A0%B9%E5%AD%A6Python-%E5%9B%9B-Dict-%E5%92%8C-Set/)|
15+
|草根学Python(五) 条件语句和循环语句|[掘金](https://juejin.im/post/594c6c52f265da6c1f75f164)[简书](http://www.jianshu.com/p/2b80009b1e8c)[CSDN](http://blog.csdn.net/Two_Water/article/details/73762517)[个人博客](http://twowater.com.cn/2017/06/27/%E8%8D%89%E6%A0%B9%E5%AD%A6Python-%E4%BA%94-%E6%9D%A1%E4%BB%B6%E8%AF%AD%E5%8F%A5%E5%92%8C%E5%BE%AA%E7%8E%AF%E8%AF%AD%E5%8F%A5/)|

SUMMARY.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,8 @@
2020
* [二、tuple(元组)](/python3/tuple.md)
2121
* [ Dict 和 Set](/python4/Preface.md)
2222
* [一、字典(Dictionary)](/python4/Dict.md)
23-
* [二、set](/python4/Set.md)
23+
* [二、set](/python4/Set.md)
24+
* [条件语句和循环语句](/python5/Preface.md)
25+
* [一、条件语句](/python5/If.md)
26+
* [二、循环语句](/python5/Cycle.md)
27+
* [三、条件语句和循环语句综合实例](/python5/Example.md)

python5/Cycle.md

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
# 二、循环语句 #
2+
3+
一般编程语言都有循环语句,循环语句允许我们执行一个语句或语句组多次。
4+
5+
循环语句的一般形式如下:
6+
7+
![python循环语句](https://user-gold-cdn.xitu.io/2017/6/23/d0761ec24b83fea3d7008c29074ba5cb)
8+
9+
Python 提供了 for 循环和 while 循环,当然还有一些控制循环的语句:
10+
11+
|循环控制语句|描述|
12+
|------|------|
13+
|break|在语句块执行过程中终止循环,并且跳出整个循环|
14+
|continue|在语句块执行过程中终止当前循环,跳出该次循环,执行下一次循环|
15+
|pass|pass 是空语句,是为了保持程序结构的完整性|
16+
17+
18+
## 1、While 循环语句 ##
19+
20+
21+
```python
22+
count = 1
23+
sum = 0
24+
while (count <= 100):
25+
sum = sum + count
26+
count = count + 1
27+
print(sum)
28+
```
29+
30+
输出的结果:
31+
32+
```txt
33+
5050
34+
```
35+
36+
当然 while 语句时还有另外两个重要的命令 continue,break 来跳过循环,continue 用于跳过该次循环,break 则是用于退出循环
37+
38+
比如,上面的例子是计算 1 到 100 所有整数的和,当我们需要判断 sum 大于 1000 的时候,不在相加时,可以用到 break ,退出整个循环
39+
40+
```python
41+
count = 1
42+
sum = 0
43+
while (count <= 100):
44+
sum = sum + count
45+
if ( sum > 1000): #当 sum 大于 1000 的时候退出循环
46+
break
47+
count = count + 1
48+
print(sum)
49+
```
50+
51+
输出的结果:
52+
53+
```txt
54+
1035
55+
```
56+
57+
有时候,我们只想统计 1 到 100 之间的奇数和,那么也就是说当 count 是偶数,也就是双数的时候,我们需要跳出当次的循环,不想加,这时候可以用到 break
58+
59+
```python
60+
count = 1
61+
sum = 0
62+
while (count <= 100):
63+
if ( count % 2 == 0): # 双数时跳过输出
64+
count = count + 1
65+
continue
66+
sum = sum + count
67+
count = count + 1
68+
print(sum)
69+
```
70+
71+
输出的语句:
72+
73+
```txt
74+
2500
75+
```
76+
77+
在 Python 的 while 循环中,还可以使用 else 语句,while … else 在循环条件为 false 时执行 else 语句块
78+
79+
比如:
80+
81+
```python
82+
count = 0
83+
while count < 5:
84+
print (count)
85+
count = count + 1
86+
else:
87+
print (count)
88+
```
89+
90+
输出的结果:
91+
92+
```txt
93+
0
94+
1
95+
2
96+
3
97+
4
98+
5
99+
```
100+
101+
## 2、 for 循环语句 ##
102+
103+
for循环可以遍历任何序列的项目,如一个列表或者一个字符串
104+
105+
它的流程图基本如下:
106+
107+
108+
![for循环的流程图](https://user-gold-cdn.xitu.io/2017/6/26/235ff5c72862b213486e0bf23852a245)
109+
110+
基本的语法格式:
111+
112+
```python
113+
for iterating_var in sequence:
114+
statements(s)
115+
```
116+
117+
实例:
118+
119+
```python
120+
for letter in 'Hello 两点水':
121+
print(letter)
122+
```
123+
124+
输出的结果如下:
125+
126+
```txt
127+
H
128+
e
129+
l
130+
l
131+
o
132+
133+
134+
135+
136+
```
137+
138+
有 while … else 语句,当然也有 for … else 语句啦,for 中的语句和普通的没有区别,else 中的语句会在循环正常执行完(即 for 不是通过 break 跳出而中断的)的情况下执行,while … else 也是一样。
139+
140+
```python
141+
for num in range(10,20): # 迭代 10 到 20 之间的数字
142+
for i in range(2,num): # 根据因子迭代
143+
if num%i == 0: # 确定第一个因子
144+
j=num/i # 计算第二个因子
145+
print ('%d 是一个合数' % num)
146+
break # 跳出当前循环
147+
else: # 循环的 else 部分
148+
print ('%d 是一个质数' % num)
149+
```
150+
151+
输出的结果:
152+
153+
```txt
154+
10 是一个合数
155+
11 是一个质数
156+
12 是一个合数
157+
13 是一个质数
158+
14 是一个合数
159+
15 是一个合数
160+
16 是一个合数
161+
17 是一个质数
162+
18 是一个合数
163+
19 是一个质数
164+
```
165+
166+
## 3、嵌套循环 ##
167+
168+
Python 语言允许在一个循环体里面嵌入另一个循环。上面的实例也是使用了嵌套循环的,这里就不给出实例了。
169+
170+
具体的语法如下:
171+
172+
**for 循环嵌套语法**
173+
174+
```python
175+
for iterating_var in sequence:
176+
for iterating_var in sequence:
177+
statements(s)
178+
statements(s)
179+
```
180+
181+
**while 循环嵌套语法**
182+
183+
```python
184+
while expression:
185+
while expression:
186+
statement(s)
187+
statement(s)
188+
```
189+
190+
除此之外,你也可以在循环体内嵌入其他的循环体,如在 while 循环中可以嵌入 for 循环, 反之,你可以在 for 循环中嵌入 while 循环

python5/Example.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# 三、条件语句和循环语句综合实例 #
2+
3+
## 1、打印九九乘法表 ##
4+
5+
```python
6+
# -*- coding: UTF-8 -*-
7+
8+
# 打印九九乘法表
9+
for i in range(1, 10):
10+
for j in range(1, i+1):
11+
# 打印语句中,大括号及其里面的字符 (称作格式化字段) 将会被 .format() 中的参数替换,注意有个点的
12+
print('{}x{}={}\t'.format(i, j, i*j), end='')
13+
print()
14+
```
15+
16+
输出的结果:
17+
18+
```txt
19+
1x1=1
20+
2x1=2 2x2=4
21+
3x1=3 3x2=6 3x3=9
22+
4x1=4 4x2=8 4x3=12 4x4=16
23+
5x1=5 5x2=10 5x3=15 5x4=20 5x5=25
24+
6x1=6 6x2=12 6x3=18 6x4=24 6x5=30 6x6=36
25+
7x1=7 7x2=14 7x3=21 7x4=28 7x5=35 7x6=42 7x7=49
26+
8x1=8 8x2=16 8x3=24 8x4=32 8x5=40 8x6=48 8x7=56 8x8=64
27+
9x1=9 9x2=18 9x3=27 9x4=36 9x5=45 9x6=54 9x7=63 9x8=72 9x9=81
28+
```
29+
30+
31+
## 2、判断是否是闰年 ##
32+
33+
```python
34+
35+
# 判断是否是闰年
36+
37+
year = int(input("请输入一个年份: "))
38+
if (year % 4) == 0 and (year % 100) != 0 or (year % 400) == 0:
39+
print('{0} 是闰年' .format(year))
40+
else:
41+
print('{0} 不是闰年' .format(year))
42+
43+
```
44+
45+

python5/If.md

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# 一、条件语句 #
2+
3+
Python 条件语句跟其他语言基本一致的,都是通过一条或多条语句的执行结果( True 或者 False )来决定执行的代码块。
4+
5+
Python 程序语言指定任何非 0 和非空(null)值为 True,0 或者 null为 False。
6+
7+
执行的流程图如下:
8+
9+
![if语句流程图](https://user-gold-cdn.xitu.io/2017/6/23/9b796437c2f0ac3ba1dc5009de091031)
10+
11+
## 1、if 语句的基本形式 ##
12+
13+
Python 中,if 语句的基本形式如下:
14+
15+
```python
16+
if 判断条件:
17+
执行语句……
18+
else
19+
执行语句……
20+
```
21+
22+
前面也提到过,Python 语言有着严格的缩进要求,因此这里也需要注意缩进,也不要少写了冒号 `:`
23+
24+
if 语句的判断条件可以用>(大于)、<(小于)、==(等于)、>=(大于等于)、<=(小于等于)来表示其关系。
25+
26+
例如:
27+
28+
```python
29+
# -*-coding:utf-8-*-
30+
31+
results=59
32+
33+
if results>=60:
34+
print ('及格')
35+
else :
36+
print ('不及格')
37+
38+
```
39+
40+
输出的结果为:
41+
42+
```txt
43+
不及格
44+
```
45+
46+
上面也说道,非零数值、非空字符串、非空 list 等,判断为True,否则为False。因此也可以这样写:
47+
48+
```python
49+
num = 6
50+
if num :
51+
print('Hello Python')
52+
```
53+
54+
## 2、if 语句多个判断条件的形式 ##
55+
56+
有些时候,我们的判断语句不可能只有两个,有些时候需要多个,比如上面的例子中大于 60 的为及格,那我们还要判断大于 90 的为优秀,在 80 到 90 之间的良好呢?
57+
58+
这时候需要用到 if 语句多个判断条件,
59+
60+
用伪代码来表示:
61+
62+
```python
63+
if 判断条件1:
64+
执行语句1……
65+
elif 判断条件2:
66+
执行语句2……
67+
elif 判断条件3:
68+
执行语句3……
69+
else:
70+
执行语句4……
71+
```
72+
73+
实例:
74+
75+
```python
76+
# -*-coding:utf-8-*-
77+
78+
results = 89
79+
80+
if results > 90:
81+
print('优秀')
82+
elif results > 80:
83+
print('良好')
84+
elif results > 60:
85+
print ('及格')
86+
else :
87+
print ('不及格')
88+
89+
```
90+
91+
输出的结果:
92+
93+
```txt
94+
良好
95+
```
96+
97+
## 3、if 语句多个条件同时判断 ##
98+
99+
Python 不像 Java 有 switch 语句,所以多个条件判断,只能用 elif 来实现,但是有时候需要多个条件需同时判断时,可以使用 or (或),表示两个条件有一个成立时判断条件成功;使用 and (与)时,表示只有两个条件同时成立的情况下,判断条件才成功。
100+
101+
```python
102+
# -*-coding:utf-8-*-
103+
104+
java = 86
105+
python = 68
106+
107+
if java > 80 and python > 80:
108+
print('优秀')
109+
else :
110+
print('不优秀')
111+
112+
if ( java >= 80 and java < 90 ) or ( python >= 80 and python < 90):
113+
print('良好')
114+
115+
```
116+
117+
输出结果:
118+
119+
```txt
120+
不优秀
121+
良好
122+
```
123+
124+
注意:if 有多个条件时可使用括号来区分判断的先后顺序,括号中的判断优先执行,此外 and 和 or 的优先级低于 >(大于)、<(小于)等判断符号,即大于和小于在没有括号的情况下会比与或要优先判断。

python5/Preface.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# 前言 #
2+
3+
第一次建学习群,而且是 Python 的学习群,虽然之前深入学习和工作都是 Android 相关的,最近学起来 Python ,真的很好玩,所以创了个微信群,希望童鞋们进群学习讨论。也可以直接加我微`androidwed`拉进群。也欢迎大家在 [Gitbook](https://www.readwithu.com/) 中提出文章的不足。
4+
5+
![Python学习群](https://user-gold-cdn.xitu.io/2017/6/27/aaeb2c5817779cef45fee80bf544ff30)
6+
7+
8+
# 目录 #
9+
10+
![草根学Python(五) 条件语句和循环语句](https://user-gold-cdn.xitu.io/2017/6/27/b09d98a2f40724326f8b04b2049236b5)

0 commit comments

Comments
 (0)