Section 4: Flow Control
Welcome to Section 4! In this section, we will cover how to control the flow of our code. Let's get started!
4.1 Operators
The most important part of conditions is learning the operators we will use. You will be familiar with most, but some may be new to you. Here are the main six ...
> - greater than
< - less than
>= - greater than or equal to
<= - less than or equal to
!= - not equal to
== - equal to
The first 4 will be pretty familiar from your math classes, but the second two are a bit confusing. != is what we use if we want two things which are different. == and = are two different things in Python. = is for assignment of variables, and == is for checking if two things are the same. We will use these later in the section.
When checking two conditions together, we can either use the and operator, the or operator, or the not operator. Here are some examples ...
print(2 == 2 and 1 != 1)
print(2 == 2 or 1 != 1)
print(not 2 <= 1)
The results of these three operations will be booleans, True and False. The and function needs both arguments to be correct to return True, but only the first argument is correct, so it will return True. The or function needs one of the two arguments to be true, and since the first argument is true, it will have to return True. Lastly, the not function will return the opposite of the answer, and the answer to the operation is False, so the last line will print True.
Now that you have the operators down, let's move on to the next part!
4.2 If
Now that we know about these 6 operators and these 3 functions, let's put these into use using if statements. Here is an example ...
n = int(input("Number Please: "))
if number == 1:
print("Number is 1.")
What this code will do is take an input from the user, check if it is one, and print a string if the condition is True. The syntax of an if statement is slightly complicated. You have to start by writing "if", adding a condition, and adding a colon at the end. You can then right whatever you want to happen in indented lines directly after the starting line. These indented lines will only be run if the condition is True.
Now that you have if statements down, let's move on to the next part!
4.3 Else
When we have a condition in an if statement and that condition evaluates to False, the if is not run. If we want to run something else instead of the if when the condition is false, we can use the else condition. Here is an example ...
if 2 + 2 == 2:
print("2 + 2 = 2")
else:
print("2 + 2 != 2")
The code above first evaluates if 2 + 2 = 2. Since the condition is False, we will not run the code in the if, but we will instead run the code in the else. The code above would print "2 + 2 != 2". The syntax of else is extremely similar to if. Everything is the same except that an else statement has to be after an if statement, and an else statement has no condition, as it is run only if the if statement is false.
Now that you have else statements down, let's move on to the next part!
4.4 Elif
Sometimes, we will have two or more conditions which will all evaluate true for an input. However, we only want the first true statement to be run, and the others to be ignored. In this situation, we can use elif, a combination of else and if.
grade = int(input("Grade please: "))
if grade >= 90:
print("A")
elif grade >= 80:
print("B")
elif grade >= 70:
print("C")
elif grade >= 60:
print("D")
else:
print("F")
The code above will tell what my letter grade is when I give it my numerical grade. If I entered 100, even though all the conditions are True, it will only print "A". This is because only the highest position that evaluates to True will be executed. On the other hand, if I put in 75, I would get "D", and if I put in 50, I would get an "F". Statements are always run from top to bottom. The syntax for elif is the same as the syntax for if, except an elif statement has to come after either an if statement or another elif statement.
Now that you have elif statements down, let's move on to the next section!
Sample Problems