前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >两天研习Python基础(六)控制结构

两天研习Python基础(六)控制结构

作者头像
王诗翔呀
发布2020-07-02 15:55:13
4940
发布2020-07-02 15:55:13
举报
文章被收录于专栏:优雅R

条件检查

  • 单个和组合测试
代码语言:javascript
复制
>>> num = 5
>>> num > 2
True
>>> num > 3 and num <= 5
True
>>> 3 < num <= 5
True
>>> num % 3 == 0 or num % 5 == 0
True

>>> fav_fiction = 'Harry Potter'
>>> fav_detective = 'Sherlock Holmes'
>>> fav_fiction == fav_detective
False
>>> fav_fiction == "Harry Potter"
True
  • 测试变量和值本身
代码语言:javascript
复制
>>> bool(num)
True
>>> bool(fav_detective)
True
>>> bool(3)
True
>>> bool(0)
False
>>> bool("")
False
>>> bool(None)
False

>>> if -1:
...     print("-1 evaluates to True in condition checking")
...
-1 evaluates to True in condition checking
  • 条件测试中in操作符的使用

对比这种检查方式

代码语言:javascript
复制
>>> def num_chk(n):
...     if n == 10 or n == 21 or n == 33:
...         print("Number passes condition")
...     else:
...         print("Number fails condition")
...
>>> num_chk(10)
Number passes condition
>>> num_chk(12)
Number fails condition

和另一种

代码语言:javascript
复制
>>> def num_chk(n):
...     if n in (10, 21, 33):
...         print("Number passes condition")
...     else:
...         print("Number fails condition")
...
>>> num_chk(12)
Number fails condition
>>> num_chk(10)
Number passes condition
  • (10, 21, 33)是一个元组数据类型,会在后面的章节讲解
  • Python文档 - 真值检验[1]

if

代码语言:javascript
复制
#!/usr/bin/python3

num = 45

# 单个if
if num > 25:
    print("Hurray! {} is greater than 25".format(num))

# if-else
if num % 2 == 0:
    print("{} is an even number".format(num))
else:
    print("{} is an odd number".format(num))

# if-elif-else
# 可以使用任意数目的elif
if num < 0:
    print("{} is a negative number".format(num))
elif num > 0:
    print("{} is a positive number".format(num))
else:
    print("{} is neither postive nor a negative number".format(num))
  • 函数代码块、控制结构等等都是通过缩进区分
    • 推荐使用4个空格缩进
    • Python文档 - 编码风格[2]
  • 一个常见的语法错误是忘记了控制结构语句后的:
  • 条件周围的()是可选的
  • 缩进代码块可以有任意数目的语句,包括空行
代码语言:javascript
复制
$ ./if_elif_else.py
Hurray! 45 is greater than 25
45 is an odd number
45 is a positive number

if-else作为条件操作符

代码语言:javascript
复制
#!/usr/bin/python3

num = 42

num_type = 'even' if num % 2 == 0 else 'odd'
print("{} is an {} number".format(num, num_type))
  • 不像其他许多语言,Python没有?:条件操作符
  • 单行if-else的使用是一种变通方法
  • 模拟三元操作符的更多方法[3]
代码语言:javascript
复制
$ ./if_else_oneliner.py
42 is an even number

for

代码语言:javascript
复制
#!/usr/bin/python3

number = 9
for i in range(1, 5):
    mul_table = number * i
    print("{} * {} = {}".format(number, i, mul_table))
  • 传统基于循环的迭代可以通过使用range函数实现
    • 默认参数start=0step=1,不含stop
  • 针对列表、元组等等变量的迭代会在后续章节讲述
  • Python文档 - 迭代工具[4]
代码语言:javascript
复制
$ ./for_loop.py
9 * 1 = 9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36

while

代码语言:javascript
复制
#!/usr/bin/python3

# 持续地询问直到用户输入一个正整数
usr_string = 'not a number'
while not usr_string.isnumeric():
    usr_string = input("Enter a positive integer: ")
  • while循环允许我们直到某个条件被满足之前不断执行语句块
  • Python docs - 字符串方法[5]
代码语言:javascript
复制
$ ./while_loop.py
Enter a positive integer: abc
Enter a positive integer: 1.2
Enter a positive integer: 23
$

continue和break

continuebreak关键字用于在某些条件下改变正常的循环操作

continue - 跳过循环代码块余下的语句并进入下一次迭代

代码语言:javascript
复制
#!/usr/bin/python3

prev_num = 0
curr_num = 0
print("The first ten numbers in fibonacci sequence: ", end='')

for num in range(10):
    print(curr_num, end=' ')

    if num == 0:
        curr_num = 1
        continue

    temp = curr_num
    curr_num = curr_num + prev_num
    prev_num = temp

print("")
  • continue放置在循环代码块中的任意位置而不用担心复杂的代码流
  • 这个例子仅仅展示continue的使用,查看这里[6]获取更加Python化的操作方式
代码语言:javascript
复制
$ ./loop_with_continue.py
The first ten numbers in fibonacci sequence: 0 1 1 2 3 5 8 13 21 34

break - 跳过循环代码块余下的语句(如果有)并进入退出循环代码块

代码语言:javascript
复制
#!/usr/bin/python3

import random

while True:
    # 使用range函数注意500没有包含在内
    random_int = random.randrange(500)
    if random_int % 4 == 0 and random_int % 6 == 0:
        break
print("Random number divisible by 4 and 6: {}".format(random_int))
  • while True:是常用作无限循环
  • randrangerange[7]函数相似,有start, stop, step参数
  • Python文档 - random[8]
代码语言:javascript
复制
$ ./loop_with_break.py
Random number divisible by 4 and 6: 168
$ ./loop_with_break.py
Random number divisible by 4 and 6: 216
$ ./loop_with_break.py
Random number divisible by 4 and 6: 24

这个while_loop.py例子可以用break语句重写

代码语言:javascript
复制
>>> while True:
         usr_string = input("Enter a positive integer: ")
         if usr_string.isnumeric():
             break

Enter a positive integer: a
Enter a positive integer: 3.14
Enter a positive integer: 1
>>>
  • 在嵌套循环中,continuebreak仅影响中间一层对应的循环
  • Python文档 - 循环中的else从句[9]

参考资料

[1]Python文档 - 真值检验: https://docs.python.org/3/library/stdtypes.html#truth

[2]Python文档 - 编码风格: https://docs.python.org/3/tutorial/controlflow.html#intermezzo-coding-style

[3]模拟三元操作符的更多方法: http://stackoverflow.com/questions/394809/does-python-have-a-ternary-conditional-operator

[4]Python文档 - 迭代工具: https://docs.python.org/3/library/itertools.html

[5]Python docs - 字符串方法: https://docs.python.org/3/library/stdtypes.html#string-methods

[6]这里: https://docs.python.org/3/tutorial/controlflow.html#defining-functions

[7]range: ./Functions.md#range-function

[8]Python文档 - random: https://docs.python.org/3/library/random.html

[9]Python文档 - 循环中的else从句: https://docs.python.org/3/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-02-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 优雅R 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 条件检查
  • if
  • for
  • while
  • continue和break
  • 参考资料
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档