Conditionals

1. Modulus operator (%)

The modulus operator works on integers and yields the remainder when the first operand is divided by the second. In Python, the modulus operator is a percent sign (%). The syntax is the same as for the other operators:

                       

The modulus operator turns out to be surprisingly useful. For example, you can check whether one number is divisible by another – if x % y is zero, then x is divisible by y.

Also, you can extract the right-most digit or digits from a number. For example, x % 10 yields the right-most digit of x (in base 10). Similarly x % 100 yields the last two digits.

2. Boolean expressions

A Boolean expression is an expression that is either true or false. The following examples use the operator ==, which compares two operands and produces True if they are equal and False otherwise:

The == operator is one of the comparison operators; the others are: !=, >, >=, <, <=. Remember that = is an assignment operator and == is a comparison operator. There is no such thing as =< or =>.

3. Logical operators

There are three logical operators: and, or, not. The semantics of these operators is similar to their meaning in English. Strictly speaking, the operands of the logical operators should be Boolean expressions but Python is not very strict. Any nonzero number is interpreted as True.

 

4. Conditional execution

In order to write useful programs, we almost always need the ability to check conditions and change the behavior of the program accordingly. Conditional statements give us this ability. The simplest form is the if statement. The Boolean expression after the if statement is called the condition. If it is true, then the indented statement gets executed. If not, nothing happens.

If statements have the same structure as function definitions: a header followed by and indented block. Statements like this are called compound statements.

There is no limit on the number of the statements that can appear in the body, but there has to be at least one. Occasionally, it is useful to have a body with no statements (usually as a place keeper for code you haven’t written yet). In that case, you can use the pass statement, which does nothing.

 

5. Alternative execution

A second form of the if statement is alternative execution, in which there are two possibilities and the condition determines which one gets executed. The syntax looks like this:

 

6. Chained conditionals

Sometimes there are more than two possibilities and we need more than two branches. One way to express a computation like that is a chained conditional:

 

elif is an abbreviation of “else if”. Again, exactly one branch will be executed. There is no limit on the number of elif statements. If there is an else clause, it has to be at the end, but there doesn’t have to be one. Each condition is checked in order. If the first false, the next is checked, and so on. If one of them is true, the corresponding branch executes, and the statement ends. Even if more than one condition is true, only the first true branch executes.

posted @ 2014-04-28 18:10  平静缓和用胸音说爱  阅读(514)  评论(0编辑  收藏  举报