

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The concept of conditional statements in python, including the structure of if, elif, and else clauses, boolean contexts, and the use of boolean operators. It also covers the distinction between assignment (=) and equality comparison (==), and provides examples of boolean expressions and their evaluation.
Typology: Study notes
1 / 3
This page cannot be seen from the preview
Don't miss anything!


This implementation of absolute_value raises several important issues:
A conditional statement in Python consists of a series of headers and suites: a required if clause, an optional sequence of elif clauses, and finally an optional else clause:
When executing a conditional statement, each clause is considered in order. The computational process of executing a conditional clause follows.
If the else clause is reached (which only happens if all if and elif expressions evaluate to false values), its suite is executed.
Boolean contexts. Above, the execution procedures mention "a false value" and "a true value." The expressions inside the header statements of conditional blocks are said to be in boolean contexts : their truth values matter to control flow, but otherwise their values are not assigned or returned. Python includes several false values, including 0, None, and the boolean value False. All other numbers are true values. In Chapter 2, we will see that every built-in kind of data in Python has both true and false values.
Boolean values. Python has two boolean values, called True and False. Boolean values represent truth values in logical expressions. The built-in comparison operations, >, <, >=, <=, ==, !=, return these values.
This second example reads "5 is greater than or equal to 5", and corresponds to the function ge in the operator module.
This final example reads "0 equals -0", and corresponds to eq in the operator module. Notice that Python distinguishes assignment (=) from equality comparison (==), a convention shared across many programming languages.
Boolean operators. Three basic logical operators are also built into Python:
Logical expressions have corresponding evaluation procedures. These procedures exploit the fact that the truth value of a logical expression can sometimes be determined without evaluating all of its subexpressions, a feature called short-circuiting.
To evaluate the expression
To evaluate the expression