重新修改文章 0 - 5 章

This commit is contained in:
TwoWater
2019-09-01 15:50:41 +08:00
parent 79937c6f26
commit 3ef01bab4f
35 changed files with 959 additions and 815 deletions

View File

@@ -0,0 +1,190 @@
# 二、循环语句 #
一般编程语言都有循环语句,循环语句允许我们执行一个语句或语句组多次。
循环语句的一般形式如下:
![python循环语句](http://upload-images.jianshu.io/upload_images/2136918-eaaae2fbfec3330f?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
Python 提供了 for 循环和 while 循环,当然还有一些控制循环的语句:
|循环控制语句|描述|
|------|------|
|break|在语句块执行过程中终止循环,并且跳出整个循环|
|continue|在语句块执行过程中终止当前循环,跳出该次循环,执行下一次循环|
|pass|pass 是空语句,是为了保持程序结构的完整性|
## 1、While 循环语句 ##
```python
count = 1
sum = 0
while (count <= 100):
sum = sum + count
count = count + 1
print(sum)
```
输出的结果:
```txt
5050
```
当然 while 语句时还有另外两个重要的命令 continuebreak 来跳过循环continue 用于跳过该次循环break 则是用于退出循环
比如,上面的例子是计算 1 到 100 所有整数的和,当我们需要判断 sum 大于 1000 的时候,不在相加时,可以用到 break ,退出整个循环
```python
count = 1
sum = 0
while (count <= 100):
sum = sum + count
if ( sum > 1000): #当 sum 大于 1000 的时候退出循环
break
count = count + 1
print(sum)
```
输出的结果:
```txt
1035
```
有时候,我们只想统计 1 到 100 之间的奇数和,那么也就是说当 count 是偶数,也就是双数的时候,我们需要跳出当次的循环,不想加,这时候可以用到 break
```python
count = 1
sum = 0
while (count <= 100):
if ( count % 2 == 0): # 双数时跳过输出
count = count + 1
continue
sum = sum + count
count = count + 1
print(sum)
```
输出的语句:
```txt
2500
```
在 Python 的 while 循环中,还可以使用 else 语句while … else 在循环条件为 false 时执行 else 语句块
比如:
```python
count = 0
while count < 5:
print (count)
count = count + 1
else:
print (count)
```
输出的结果:
```txt
0
1
2
3
4
5
```
## 2、 for 循环语句 ##
for循环可以遍历任何序列的项目如一个列表或者一个字符串
它的流程图基本如下:
![for循环的流程图](http://upload-images.jianshu.io/upload_images/2136918-a0728c1c488238af?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
基本的语法格式:
```python
for iterating_var in sequence:
statements(s)
```
实例:
```python
for letter in 'Hello 两点水':
print(letter)
```
输出的结果如下:
```txt
H
e
l
l
o
```
有 while … else 语句,当然也有 for … else 语句啦for 中的语句和普通的没有区别else 中的语句会在循环正常执行完(即 for 不是通过 break 跳出而中断的的情况下执行while … else 也是一样。
```python
for num in range(10,20): # 迭代 10 到 20 之间的数字
for i in range(2,num): # 根据因子迭代
if num%i == 0: # 确定第一个因子
j=num/i # 计算第二个因子
print ('%d 是一个合数' % num)
break # 跳出当前循环
else: # 循环的 else 部分
print ('%d 是一个质数' % num)
```
输出的结果:
```txt
10 是一个合数
11 是一个质数
12 是一个合数
13 是一个质数
14 是一个合数
15 是一个合数
16 是一个合数
17 是一个质数
18 是一个合数
19 是一个质数
```
## 3、嵌套循环 ##
Python 语言允许在一个循环体里面嵌入另一个循环。上面的实例也是使用了嵌套循环的,这里就不给出实例了。
具体的语法如下:
**for 循环嵌套语法**
```python
for iterating_var in sequence:
for iterating_var in sequence:
statements(s)
statements(s)
```
**while 循环嵌套语法**
```python
while expression:
while expression:
statement(s)
statement(s)
```
除此之外,你也可以在循环体内嵌入其他的循环体,如在 while 循环中可以嵌入 for 循环, 反之,你可以在 for 循环中嵌入 while 循环

View File

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

View File

@@ -0,0 +1,124 @@
# 一、条件语句 #
Python 条件语句跟其他语言基本一致的,都是通过一条或多条语句的执行结果( True 或者 False )来决定执行的代码块。
Python 程序语言指定任何非 0 和非空null值为 True0 或者 null为 False。
执行的流程图如下:
![if语句流程图](http://upload-images.jianshu.io/upload_images/2136918-4ee2486190450a1a?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
## 1、if 语句的基本形式 ##
Python 中if 语句的基本形式如下:
```python
if 判断条件
执行语句……
else
执行语句……
```
前面也提到过Python 语言有着严格的缩进要求,因此这里也需要注意缩进,也不要少写了冒号 `:`
if 语句的判断条件可以用>(大于)、<(小于)、==(等于)、>=(大于等于)、<=(小于等于)来表示其关系。
例如:
```python
# -*-coding:utf-8-*-
results=59
if results>=60:
print ('及格')
else :
print ('不及格')
```
输出的结果为:
```txt
不及格
```
上面也说道,非零数值、非空字符串、非空 list 等判断为True否则为False。因此也可以这样写
```python
num = 6
if num :
print('Hello Python')
```
## 2、if 语句多个判断条件的形式 ##
有些时候,我们的判断语句不可能只有两个,有些时候需要多个,比如上面的例子中大于 60 的为及格,那我们还要判断大于 90 的为优秀,在 80 到 90 之间的良好呢?
这时候需要用到 if 语句多个判断条件,
用伪代码来表示:
```python
if 判断条件1:
执行语句1……
elif 判断条件2:
执行语句2……
elif 判断条件3:
执行语句3……
else:
执行语句4……
```
实例:
```python
# -*-coding:utf-8-*-
results = 89
if results > 90:
print('优秀')
elif results > 80:
print('良好')
elif results > 60:
print ('及格')
else :
print ('不及格')
```
输出的结果:
```txt
良好
```
## 3、if 语句多个条件同时判断 ##
Python 不像 Java 有 switch 语句,所以多个条件判断,只能用 elif 来实现,但是有时候需要多个条件需同时判断时,可以使用 or (或),表示两个条件有一个成立时判断条件成功;使用 and (与)时,表示只有两个条件同时成立的情况下,判断条件才成功。
```python
# -*-coding:utf-8-*-
java = 86
python = 68
if java > 80 and python > 80:
print('优秀')
else :
print('不优秀')
if ( java >= 80 and java < 90 ) or ( python >= 80 and python < 90):
print('良好')
```
输出结果:
```txt
不优秀
良好
```
注意if 有多个条件时可使用括号来区分判断的先后顺序,括号中的判断优先执行,此外 and 和 or 的优先级低于 >(大于)、<(小于)等判断符号,即大于和小于在没有括号的情况下会比与或要优先判断。

View File

@@ -0,0 +1,10 @@
# 前言 #
第一次建学习群,而且是 Python 的学习群,虽然之前深入学习和工作都是 Android 相关的,最近学起来 Python ,真的很好玩,所以创了个微信群,希望童鞋们进群学习讨论。也可以直接加我微`androidwed`拉进群。也欢迎大家在 [Gitbook](https://www.readwithu.com/) 中提出文章的不足。
![Python学习群](http://upload-images.jianshu.io/upload_images/2136918-f3f0c60ce12e5a92?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
# 目录 #
![草根学Python 条件语句和循环语句](http://upload-images.jianshu.io/upload_images/2136918-32902eec93d9ffc1?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)