+ - 0:00:00
Notes for current slide
Notes for next slide

Control Flow: Loops and Conditionals

Introduction to Python

A part of the series at pythonslides.review

Last Updated: 2018-06-14

1 / 22


Flow Charts From xkcd.

2 / 22

A typical Python program

def function_1(...,...):
...
def function_2(...,...):
...
.
.
.
def function_k(...,...):
...
#Comments and so on.
statement_1
statement_2
.
.
.
statement_n

How Python Sees This


  • The Python interpreter executes statements from top to bottom.
3 / 22

A typical Python program

def function_1(...,...):
...
def function_2(...,...):
...
.
.
.
def function_k(...,...):
...
#Comments and so on.
statement_1
statement_2
.
.
.
statement_n

How Python Sees This


  • The Python interpreter executes statements from top to bottom.

  • The function definitions and comments (lines starting with the # character) are parsed but not executed.

4 / 22

A typical Python program

def function_1(...,...):
...
def function_2(...,...):
...
.
.
.
def function_k(...,...):
...
#Comments and so on.
statement_1
statement_2
.
.
.
statement_n

How Python Sees This


  • The Python interpreter executes statements from top to bottom.

  • The function definitions and comments (lines starting with the # character) are parsed but not executed.

  • Actual computation starts from statement_1

5 / 22

Control Flow

  • Need to vary computation sequence as values evolve.
6 / 22

Control Flow

  • Need to vary computation sequence as values evolve.

  • Control flow determines the order in which statements are executed.

6 / 22

Control Flow

  • Need to vary computation sequence as values evolve.

  • Control flow determines the order in which statements are executed.

    • Conditional Execution
6 / 22

Control Flow

  • Need to vary computation sequence as values evolve.

  • Control flow determines the order in which statements are executed.

    • Conditional Execution

      • Do something if something happens.
6 / 22

Control Flow

  • Need to vary computation sequence as values evolve.

  • Control flow determines the order in which statements are executed.

    • Conditional Execution

      • Do something if something happens.
    • Repeated Execution or Loops

6 / 22

Control Flow

  • Need to vary computation sequence as values evolve.

  • Control flow determines the order in which statements are executed.

    • Conditional Execution

      • Do something if something happens.
    • Repeated Execution or Loops

      • Do something again and again as long as something is true.
6 / 22

Control Flow

  • Need to vary computation sequence as values evolve.

  • Control flow determines the order in which statements are executed.

    • Conditional Execution

      • Do something if something happens.
    • Repeated Execution or Loops

      • Do something again and again as long as something is true.
    • Function definitions

6 / 22

Control Flow

  • Need to vary computation sequence as values evolve.

  • Control flow determines the order in which statements are executed.

    • Conditional Execution

      • Do something if something happens.
    • Repeated Execution or Loops

      • Do something again and again as long as something is true.
    • Function definitions

      • These blocks don't even execute until you call them.

      • Keeps your codebase organized 😎

6 / 22

Control Flow

Conditionals



7 / 22

If-Then-Else



IFStory

8 / 22

If-Then-Else



From the website IFTTT:

IFTTT1

9 / 22

If-Then-Else

IFTTT2

10 / 22

If-Then-Else

We use conditional executions all the time.

11 / 22

If-Then-Else

We use conditional executions all the time.

if (sleepy == True):
snooze_alarm()
else:
wakeup()
11 / 22

If-Then-Else

We use conditional executions all the time.

if (sleepy == True):
snooze_alarm()
else:
wakeup()
  • The statements snooze_alarm() and wakeup() are executed based on whether sleepy is True or False.
11 / 22

If-Then-Else

We use conditional executions all the time.

if (sleepy == True):
snooze_alarm()
else:
wakeup()
  • The statements snooze_alarm() and wakeup() are executed based on whether sleepy is True or False.

  • The if/else blocks are indented by spaces/tabs. It's important that you are consistent with your usage of spaces and tabs!

11 / 22

If-Then-Else

We use conditional executions all the time.

if (sleepy == True):
snooze_alarm()
else:
wakeup()
  • The statements snooze_alarm() and wakeup() are executed based on whether sleepy is True or False.

  • The if/else blocks are indented by spaces/tabs. It's important that you are consistent with your usage of spaces and tabs!

  • The else block is not mandatory.

11 / 22

If-Then-Else

We use conditional executions all the time.

if (sleepy == True):
snooze_alarm()
else:
wakeup()
  • The statements snooze_alarm() and wakeup() are executed based on whether sleepy is True or False.

  • The if/else blocks are indented by spaces/tabs. It's important that you are consistent with your usage of spaces and tabs!

  • The else block is not mandatory.

  • You can handle multiple scenario cases by using if-elif-else statements.

11 / 22

If-Then-Else

We use conditional executions all the time.

if (sleepy == True):
snooze_alarm()
else:
wakeup()
  • The statements snooze_alarm() and wakeup() are executed based on whether sleepy is True or False.

  • The if/else blocks are indented by spaces/tabs. It's important that you are consistent with your usage of spaces and tabs!

  • The else block is not mandatory.

  • You can handle multiple scenario cases by using if-elif-else statements.

if (sleepiness == 10):
snooze()
elif (sleepiness == 5):
coffee()
else:
wakeup()
11 / 22

If-Then-Else



Flow Charts

*From Programiz.

12 / 22

If-Then-Else



Flow Charts

*From Programiz.

13 / 22

If-Then-Else

Flow Charts

*From Programiz.

14 / 22

If-Then-Else

Coin Game

*From Trust by Nicky Case..

15 / 22

Control Flow

Repetition



16 / 22

Loops

Loops

This is xkcd again.
17 / 22

Loops - Fixed Iterations

Repeating something n times

18 / 22

Loops - Fixed Iterations

Repeating something n times

  • Use the for statement in such scenarios.
18 / 22

Loops - Fixed Iterations

Repeating something n times

  • Use the for statement in such scenarios.
n = 10
for i in range(n):
print(i, end=" ")
print() # Prints a newline
18 / 22

Loops - Fixed Iterations

Repeating something n times

  • Use the for statement in such scenarios.
n = 10
for i in range(n):
print(i, end=" ")
print() # Prints a newline
0 1 2 3 4 5 6 7 8 9
18 / 22

Loops - Fixed Iterations

Repeating something n times

  • Use the for statement in such scenarios.
n = 10
for i in range(n):
print(i, end=" ")
print() # Prints a newline
0 1 2 3 4 5 6 7 8 9
  • The code below will print numbers from 5 (inclusive) to 10 (exclusive).
18 / 22

Loops - Fixed Iterations

Repeating something n times

  • Use the for statement in such scenarios.
n = 10
for i in range(n):
print(i, end=" ")
print() # Prints a newline
0 1 2 3 4 5 6 7 8 9
  • The code below will print numbers from 5 (inclusive) to 10 (exclusive).
x = 5
y = 10
for i in range(x,y):
print(i, end=" ")
print() #Prints a newline
18 / 22

Loops - Fixed Iterations

Repeating something n times

  • Use the for statement in such scenarios.
n = 10
for i in range(n):
print(i, end=" ")
print() # Prints a newline
0 1 2 3 4 5 6 7 8 9
  • The code below will print numbers from 5 (inclusive) to 10 (exclusive).
x = 5
y = 10
for i in range(x,y):
print(i, end=" ")
print() #Prints a newline
5 6 7 8 9
18 / 22

Loops - Example

19 / 22

Loops - Example

Factors of a number n

19 / 22

Loops - Example

Factors of a number n

List all the factors of a number n.

19 / 22

Loops - Example

Factors of a number n

List all the factors of a number n.

n = 50
for i in range(1,n+1):
if n%i == 0:
print(i)
19 / 22

Loops - Example

Factors of a number n

List all the factors of a number n.

n = 50
for i in range(1,n+1):
if n%i == 0:
print(i)

Cutting down the number of iterations:

19 / 22

Loops - Example

Factors of a number n

List all the factors of a number n.

n = 50
for i in range(1,n+1):
if n%i == 0:
print(i)

Cutting down the number of iterations:

n = 50
from math import sqrt
for i in range(1,int(sqrt(n))+1):
if n%i == 0:
print(i)
print(int(n/i))
19 / 22

Loops - Iterations based on Conditions

20 / 22

Loops - Iterations based on Conditions

Often we don't know the number of repetitions in advance.

20 / 22

Loops - Iterations based on Conditions

Often we don't know the number of repetitions in advance.

sleepy = 10
while(sleepy):
snooze()
sleepy = sleepy - 1
20 / 22

Loops - Iterations based on Conditions

Often we don't know the number of repetitions in advance.

sleepy = 10
while(sleepy):
snooze()
sleepy = sleepy - 1

Executes the body if condition evaluates to True.

20 / 22

Loops - Iterations based on Conditions

Often we don't know the number of repetitions in advance.

sleepy = 10
while(sleepy):
snooze()
sleepy = sleepy - 1

Executes the body if condition evaluates to True.

After each iteration, check if condition is True again.

20 / 22

Loops - Iterations based on Conditions

Often we don't know the number of repetitions in advance.

sleepy = 10
while(sleepy):
snooze()
sleepy = sleepy - 1

Executes the body if condition evaluates to True.

After each iteration, check if condition is True again.

☠️ Note that this execution will not terminate if the condition never evaluates to False!

20 / 22

While Control Flow

Flow Charts

*From Programiz.

21 / 22

🎉

22 / 22


Flow Charts From xkcd.

2 / 22
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow