目录
这是麻省理工大学(MIT)官方编程教程中Python Tutorial的内容,教材为《Think Python: How to Think Like a Computer Scientist》。这是我的学习笔记,因为水品有限,请大家多多包涵。如果有一起学习的同学可以一起交流。如笔记中错误,请一定要告诉我啊,我肯定及时改正。所有笔记的目录详见:MIT:Python Tutorial目录
这是MIT官方编程教程中Python Tutorial中Using if, else, and while的内容。本篇博客为《 Think Python: How to Think Like a Computer Scientist》的第5章 Conditionals and Recursion的笔记内容。(Think Python :Chapter 5: Conditionals and Recursion)
Python Tutorial[3]:Using if, else, and while
Before starting these problems, please read(学习材料):
- Think Python, Chapter 5: Conditionals and Recursion, through section 5.7
- Think Python, Chapter 7: Iteration
Chapter 5 Conditionals(条件语句) and recursion(循环递归语句)
5.1 Modulus operator(模数运算符)
modulus operator(模数运算符): An operator, denoted with a percent sign (%), that works on integers and yields the remainder(余数) when one number is divided by another.
5.2 Boolean expressions(布朗运算表达)
boolean expression: An expression whose value is either True or False.
relational operator: One of the operators that compares its operands: == , != , > , < , >= , and <=.
A common error is to use a single equal sign (=) instead of a double equal sign (==). Remember that = is an assignment operator and == is a relational operator. There is no such thing as =< or =>.(=代表数学任务运算,==代表相关运算)
5.3 Logical operators(逻辑运算)
logical operator: One of the operators that combines boolean expressions: and , or , and not .
5.4 Conditional execution(条件运算)
conditional statement: A statement that controls the flow of execution depending on some condition.
condition: The boolean expression in a conditional statement that determines which branch is executed.
compound statement: A statement that consists of a header and a body. The header ends with a colon (:). The body is indented relative to the header.
5.5 Alternative execution(可选择的运行方式)
branch: One of the alternative sequences of statements in a conditional statement.
#5.5 案例
if x%2==0:
print(x+'is even')
else:
print(x+'is odd')
5.6 Chained conditionals(链状条件语句)
chained conditional: A conditional statement with a series of alternative branches.
def compare_xy(x,y):
i