Understanding Conditional Statements and Boolean Values in Python, Study notes of Programming Languages

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

2021/2022

Uploaded on 09/27/2022

mdbovary
mdbovary 🇬🇧

4.8

(8)

215 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Conditional Statements in Python
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:
if <expression>:
<suite>
elif <expression>:
<suite>
else:
<suite>
When executing a conditional statement, each clause is considered in order. The
computational process of executing a conditional clause follows.
1. Evaluate the header's expression.
2. If it is a true value, execute the suite. Then, skip over all subsequent clauses in
the conditional statement.
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.
pf3

Partial preview of the text

Download Understanding Conditional Statements and Boolean Values in Python and more Study notes Programming Languages in PDF only on Docsity!

Conditional Statements in Python

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:

if :

elif :

else:

When executing a conditional statement, each clause is considered in order. The computational process of executing a conditional clause follows.

  1. Evaluate the header's expression.
  2. If it is a true value, execute the suite. Then, skip over all subsequent clauses in the conditional statement.

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.

False

True

This second example reads "5 is greater than or equal to 5", and corresponds to the function ge in the operator module.

True

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:

>>> True and False

False

>>> True or False

True

>>> not False

True

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 and :

  1. Evaluate the subexpression .
  2. If the result is a false value v, then the expression evaluates to v.
  3. Otherwise, the expression evaluates to the value of the subexpression .

To evaluate the expression or :

  1. Evaluate the subexpression .