1 of 42

Basics of Python

Conditional Execution

2 of 42

Lecture #4

Conditional Execution

3 of 42

Conditional Execution

All the programs in the preceding lectures execute exactly the same statements regardless of the input, if any, provided to them. They follow a linear sequence: Statement 1, Statement 2, etc. until the last statement is executed and the program terminates. Linear programs like these are very limited in the problems they can solve. This lecture introduces constructs that allow program statements to be optionally executed, depending on the context of the program’s execution.

4 of 42

Boolean Expressions

  • Arithmetic expressions evaluate to numeric values;
  • A Boolean expression, sometimes called a predicate, may have only one of two possible values: false or true.
  • While on the surface Boolean expressions may appear very limited compared to numeric expressions;
  • They are essential for building more interesting and useful programs.

5 of 42

Boolean Expressions

6 of 42

Boolean Expressions

7 of 42

Boolean Expressions

8 of 42

Boolean Expressions

9 of 42

Boolean Expressions

Example:

10 of 42

Boolean Expressions

Output:

11 of 42

Boolean Expressions

The relational operators are binary operators and are all left associative. They all have a lower precedence than any of the arithmetic operators; therefore, Python evaluates the expression:

x + 2 < y / 10

12 of 42

The Simple if Statement

Boolean expressions are essential for a program to be able to adapt its behavior at run time. Most truly useful and practical programs would be impossible without the availability of Boolean expressions.

13 of 42

The Simple if Statement

The general form of the if statement is:

14 of 42

The Simple if Statement

  • The reserved word if begins a if statement.
  • The condition is a Boolean expression that determines whether or not the body will be executed. A colon (:) must follow the condition.
  • The block is a block of one or more statements to be executed if the condition is true. The statements within the block must all be indented the same number of spaces from the left.

15 of 42

The Simple if Statement

16 of 42

17 of 42

The Simple if Statement

How many spaces should you indent?

  • Python requires at least one;
  • some programmers consistently use two;
  • four is the most popular number;
  • but some prefer a more dramatic display and use eight;

18 of 42

The Simple if Statement

A four space indentation for a block is the recommended Python style.

[Whichever indent distance you choose, you must use this same distance consistently throughout a Python program.]

19 of 42

The Simple if Statement

Example:

20 of 42

The if/else Statement

The if statement has an optional else block that is executed only if the Boolean condition is false.

21 of 42

The if/else Statement

22 of 42

Compound Boolean Expressions

  • We can combine simple Boolean expressions, each involving one relational operator, into more complex Boolean expressions using the logical operators and, or, and not.
  • A combination of two or more Boolean expressions using logical operators is called a compound Boolean expression.

23 of 42

Compound Boolean Expressions

24 of 42

Compound Boolean Expressions

25 of 42

Compound Boolean Expressions

26 of 42

Compound Boolean Expressions

27 of 42

Compound Boolean Expressions

Now, consider this example:

28 of 42

The pass Statement

29 of 42

The pass Statement

30 of 42

Nested Conditionals

  • The statements in the block of the if or the else may be any Python statements, including other if/else statements.
  • We can use these nested if statements to develop arbitrarily complex program logic.

31 of 42

Nested Conditionals

32 of 42

Multi-way Decision Statements

  • A simple if/else statement can select from between two execution paths.
  • What if exactly one of many actions should be taken?
  • Nested if/else statements are required

33 of 42

Multi-way Decision Statements

34 of 42

Multi-way Decision Statements

35 of 42

Multi-way Decision Statements

36 of 42

37 of 42

Multi-way Versus Sequential Conditionals

38 of 42

Multi-way Versus Sequential Conditionals

39 of 42

Conditional Expressions

  • This code assigns to variable c one of two possible values.
  • As purely a syntactical convenience, Python provides an alternative to the if/else construct called a conditional expression.
  • A conditional expression evaluates to one of two values depending on a Boolean condition.

40 of 42

Conditional Expressions

We can rewrite the above code as:

41 of 42

Conditional Expressions

Example

42 of 42

Q&A