Python Essentials
上QQ阅读APP看书,第一时间看更新

Chapter 5. Logic, Comparisons, and Conditions

Our exploration of the Python language started with expression statements and the assignment statement. We can view output using the print() function as a simple statement. We can gather input using the input() function in an assignment statement. In order to process data conditionally, we need the if statement.

In order to look at the if statement, we'll need to look at Boolean data and Boolean operators. The and, or, not, and if-else Boolean operators have a "short-circuit" behavior: if the result is defined by just the left-hand operand, the right-hand side is not evaluated. This is an important feature of these logic operators. (The if-else operator is formally called the Boolean expression, but it behaves like the Boolean operators.)

We'll also look at the comparison operators. A comparison is a common way to create the Boolean values used to choose between suites of statements within an if statement.

We'll introduce the pass statement here. This statement does nothing. It's a place-holder to use when an empty suite of statements is all we need.

The assert statement can be used to demonstrate that a particular logical condition is true at some point in the program's execution. This can clarify a potentially confusing algorithm. It can also serve as a handy debugging tool to make a program crash when something has gone awry.