Python Control Flow
Conditional Structures (If Statements)
Used to vary what a program does based on a condition.
Also called an 'if' structure.
Starts with the word if followed by a logical test.
Commands inside the if structure are only carried out if the test is True.
Logical Tests
The result of a logical test can be True or False.
A logical test usually compares two values using relational operators.
Relational Operators
Used to compare values in logical tests.
Python has the following relational operators:
==: equal to
!=: not equal to
>: greater than
<: less than
>=: greater than or equal to
<=: less than or equal to
If...Else Structures
if...else structures provide two spaces:
The commands in the top space are carried out if the test is True.
The commands in the lower space are carried out if the test is False.
Python Syntax for If Statements
Put a colon (:) at the end of the logical test.
Indent all the commands that follow the colon.
The computer will only carry out the indented commands if the test is True.
The else statement also ends with a colon and its commands are indented.
if answer == "y":
result = number1 + number2
print(result)
else:
result = number1 - number2
print(result)
Loops
Allow you to put program commands inside a loop.
Commands inside a loop are carried out many times.
Types of Loops
Counter Loop (Fixed Loop): Repeats a set number of times then stops.
Conditional Loop: Controlled by a logical test.
Counter Loops (For Loops)
In Python, a counter loop is called a for loop.
Syntax: for i in range(n):, where n is the number of times the loop will repeat.
i is the counter variable (can be any name, but i is common).
for i in range(10):
number1 = int(input("enter a number "))
number2 = int(input("enter a number "))
result = number1 + number2
print(result)
Increasing the Value of a Variable
Use the command total = total + number to add a number to the current value of the total
variable.
total = 0
number = int(input("enter a number "))
total = total + number
print(total)
Conditional Loops (While Loops)
In Python, a conditional loop is called a while loop.
The first line of a while loop has the word while, a logical test, and a colon.
The loop will repeat while the condition is True.
If the condition is True, the loop will continue.
If the condition is False, the loop will stop.
Important: Assign a starting value to the test variable before the loop begins and give the user
a chance to change the test variable inside the loop.
total = 0
number = int(input("enter a number "))
while number != 0:
total = total + number
number = int(input("enter a number "))
print(total)
Program Errors
Syntax Errors
Occur when you break the rules (syntax) of the programming language.
The computer cannot translate the commands into machine code.
The computer will stop and show an error message.
Common syntax errors:
Using the wrong word (e.g., repeat until instead of while).
Not using indent.
Leaving out the colon.
Using a single equals sign (=) for a logical test instead of a double equals sign (==).
Logical Errors
Occur when the logic of the program is wrong.
The program works, but it does the wrong thing.
No error message is displayed.
Harder to spot than syntax errors.
Making Programs User Friendly
Simple inputs with prompts
Clear outputs
Other messages to explain the program
Tips for User-Friendly Programs
Make the input short and simple to enter.
Add a prompt to tell the user what they should enter.
Add text to the print command to explain what the output is.
Use print commands to add messages, a title, and explain what the program does.
Use \n in a print command to make a new line.
Making Programs Readable
Well-chosen variable names
Comments
Variable Names
The name of a variable should tell you what value the variable stores.
Comments
Messages you add to your program to explain what the code does.
In Python, you mark a comment with the hash symbol #.
The computer will ignore the rest of the line after the # symbol.