Functions, Loops, and Logic
The core of Python programming is conditional if/else/elif control statements, loops, and functions. These are powerful tools that you must master to solve problems and implement algorithms. Use these materials to practice their syntax. If/else/elif blocks allow your program to make conditional decisions based upon values available at the time the block is executed. Loops allow you to perform a set of operations a set number of times based on a set of conditions. Python has two types of loops: for loops and while loops. For loops work by counting and terminating when the loop has executed the prescribed number of times. While loops work by testing a logical condition, and the loop terminates when the logical condition evaluates to a value of false. Functions are designed to take in a specific set of input values, operate on those values, and then return a set of output results.
Control Statements
The if
Statement and Conditionals
if
in Python means: only run the rest of this code once, if the condition evaluates to True
.
Anatomy of an if
statement:
- Start with the
if
keyword, followed by a boolean value, an expression that evaluates toTrue
, or a value with "Truthiness". - Add a colon
:
and a new line - Write the code that will run if the statement is
True
under a level of indentation
Note: Remember, just like with functions, we know that code is associated with an if
statement by it's level of indentation. All the lines indented under the if
statement will run if it evaluates to True
.
>>> if 3 < 5:
... print("Hello, World!")
...
Hello, World!
Tip: Remember, your
if
statements only run if the expression in them evaluates to True
and just like with functions, you'll need to enter an extra space in the REPL to run it.
Using not
With if
Statements
If you only want your code to run if the expression is False
, use the not
keyword.
>>> b = False
>>> if not b:
... print("Negation in action!")
...
Negation in action!
if
Statements and Truthiness
if
statements also work with items that have a “truthiness” to them.
For example:
- The number 0 is False-y, any other number (including negatives) is Truth-y
- An empty
list
,set
,tuple
ordict
is False-y - Any of those structures with items in them are Truth-y
>>> message = "Hi there."
>>> a = 0
>>> if a: # 0 is False-y
... print(message)
...
>>> b = -1
>>> if b: # -1 is Truth-y
... print(message)
...
Hi there.
>>> c = []
>>> if c: # Empty list is False-y
... print(message)
...
>>> d = [1, 2, 3]
>>> if d: # List with items is Truth-y
... print(message)
...
Hi there.
>>> e = ""
>>> if e:
... print(message)
...
if
Statements and functions
You can easily declare if
statements in your functions, you just need to mindful of the level of indentation.
Notice how the code belonging to the if
statement is indented at two levels.
>>> def modify_name(name):
... if len(name) < 5:
... return name.upper()
... else:
... return name.lower()
...
>>> name = "Nina"
>>> modify_name(name)
'NINA'
Nested if
Statements
Using the same technique, you can also nest your if
statements.
>>> def num_info(num):
... if num > 0:
... print("Greater than zero")
... if num > 10:
... print("Also greater than 10.")
...
>>> num_info(1)
Greater than zero
>>> num_info(15)
Greater than zero
Also greater than 10.
How not To Use if
Statements
Remember, comparisons in Python evaluate to True
or False
. With conditional statements, we check for that value implicitly. In Python, we do not want to compare to True or False with ==.
Warning - pay attention, because the code below shows what you shouldn't do.
# Warning: Don't do this!
>>> if (3 < 5) is True: # Warning: Don't do this!
... print("Hello")
...
Hello
Tip: Do this instead:
>>> if 3 < 5:
... print("Hello")
...
Hello
If we want to explicitly check if the value is explicitly set to
True
or False
, we can use the is
keyword.
>>> a = True # a is set to True
>>> b = [1, 2, 3] # b is a list with items, is "truthy"
>>>
>>> if a is True: # we can explicitly check if a is True
... print("Hello")
...
Hello
>>> if b: # b is a list with items, is "truthy"
... print("Hello")
...
Hello
>>> if b is True: # b does not contain the actual value of True.
... print("Hello")
...
>>>
else
The else
statement is what you want to run if and only if your if
statement wasn't triggered.
An else
statement is part of an if
statement. If your if
statement ran, your else
statement will never run.
>>> a = True
>>> if a:
... print("Hello")
... else:
... print("Goodbye")
...
Hello
And vice-versa.
>>> a = False
>>> if a:
... print("Hello")
... else:
... print("Goodbye")
...
Goodbye
In the REPL it must be written on the line after your last line of indented code. In Python code in a file, there can't be any other code between the
if
and the else
.Info: You'll see
SyntaxError: invalid syntax
if you try to write an else
statement on its own, or put extra code between the if
and the else
in a Python file.>>> if a:
... print("Hello")
...
Hello
>>> else:
File "<stdin>", line 1
else:
^
SyntaxError: invalid syntax
elif
Means Else, If.
elif
means else if. It means, if this if
statement isn't considered True
, try this instead.
You can have as many elif
statements in your code as you want. They get evaluated in the order that they're declared until Python finds one that's True
. That runs the code defined in that elif
, and skips the rest.
>>> a = 5
>>> if a > 10:
... print("Greater than 10")
... elif a < 10:
... print("Less than 10")
... elif a < 20:
... print("Less than 20")
... else:
... print("Dunno")
...
Less than 10