0% found this document useful (0 votes)
12 views

003 Python-Commands

This document provides information on Python control flow commands including conditional branching with if/else statements and iteration with while and for loops. Key points covered include: - Using Boolean expressions and conditional keywords like if, elif, else for branching program execution. - Indentation being used to denote code blocks for conditional statements. - While loops to repeatedly execute a block of code as long as a condition is true. - For loops to iterate over a fixed number of iterations by using the range function. - Break and continue statements to control loop execution.

Uploaded by

Bhavesh Jha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

003 Python-Commands

This document provides information on Python control flow commands including conditional branching with if/else statements and iteration with while and for loops. Key points covered include: - Using Boolean expressions and conditional keywords like if, elif, else for branching program execution. - Indentation being used to denote code blocks for conditional statements. - While loops to repeatedly execute a block of code as long as a condition is true. - For loops to iterate over a fixed number of iterations by using the range function. - Break and continue statements to control loop execution.

Uploaded by

Bhavesh Jha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Python Commands

Control Flow
Objectives

• Boolean Expressions & Conditional Branching


• Language building blocks for branching
• Iteration, and program loops
• Iterating over sections of code.
• Make richer and more functional programs
Making Decisions if <condition>:
<expression>
<expression>
• if, then, else, elif keywords
...
• Use Boolean conditionals elif <condition2>:
if <condition>: <expression>
<expression> <expression>
if <condition>:
<expression> ...
<expression>
… else:
<expression>
else: <expression>
...
<expression> <expression>
...... ...
Branching
Indentation
• Each conditional uses : to denote the beginning of a ‘block’
• Each block has it’s own indentation level
• This is typically done by pressing TAB
• Tabs vs Spaces – Ideally pressing <TAB> should insert 4 spaces.
• This is configurable in most editors.
use input to prompt the user to enter values.
float() converts what would be a string
Example into a number for us to do calculation

money = float(input(“Enter the amount of money you have: “))


cost = float(input(“Enter the cost of the item: ”))
<TAB> = 4 spaces if cost == money: Don’t confuse = with ==
print(“This will cost all of your money!”)
if money > 100000:
print(“Are these your life savings?”)
elif cost < money:
print(“You can afford it. Treat yourself”)
else:
print(“Stop trying to spend money you don’t have.”)
print(“Thank you for buying/not buying our product today :)”)
Repetition, Repetition

• What if we wanted to keep going?


• While loop

while <condition>:
<expression>
<expression>
...
Example While loop

Q: When will the following code execute? print


sleepy = False
while not sleepy:
print(“Work work work work work”)
print(“Zzz”)
● A: Never!
sleepy is always False.
-

It is never set to True anywhere. Therefore, the condition holds for all time
-

Known as an infinite loop – Easy to spot here, but not always so simple.
-
Break out of a loop
finished = False finished = False
while not finished: while not finished:
<do stuff> <do stuff>
if <found what we want>: if <found what we want>:
finished = True finished = True
<do more stuff> break Out of the while entirely!
<do more stuff>
Counting with Loops
• What if we wanted a fixed number of iterations?
counter = 0
while counter < 10:
# We can use pound to denote a commented line.
print(counter)
counter = counter + 1
# Much nicer syntax! This line is a comment.
for n in range(10):
print(n)
For loops
• range( start, stop, step )
• default start = 0, and step = 1 (these are optional)
• In computer science we count from zero
• Loop will iterate until stop – 1 → Stop is exclusive.
• Start is inclusive. sum_evens = 0
sum_numbers = 0 for i in range(0,12,2):
for i in range(5,10): sum_evens = sum_evens + i
sum_numbers = sum_numbers + i print(sum_numbers)
print(sum_numbers)
# This should be 5 + 6 + 7 + 8 + 9 → 35
# 0 + 2 + 4 … + 10 → 30
Revision Quiz
• Consider the following: Q: What’s the final value of
salary_summation = 0 salary_summation once the
for n in range( 5, 20, 5 ): program terminates?
print(n) Q: What if we changed == 15 to == 11 ?
What about >= 11?
salary_summation = salary_summation + n
if salary_summation == 15:
break
print("I’m rich!") ← Q: Does this ever get printed?
print("Current sum: ", salary_summation)
print("Adding up is hard")

You might also like