knowt logo

PYTHON LESSON 3: Conditionals & Control Flow

Go with the Flow;

Just like in real life, sometimes we’d like our code to be able to make decisions.

The Python programs we’ve written so far have had one-track minds: they can add two numbers or print something, but they don’t have the ability to pick one of these outcomes over the other.

Control flow gives us this ability to choose among outcomes based on what else is happening in the program.

For Example;

def clinic():
print "You've just entered the clinic!"
print "Do you take the door on the left or the right?"
answer = raw_input("Type left or right and hit 'Enter'.").lower()
if answer == "left" or answer == "l":
print "This is the Verbal Abuse Room, you heap of parrot droppings!"
elif answer == "right" or answer == "r":
print "Of course this is the Argument Room, I've told you that already!"
else:
print "You didn't pick left or right! Try again."
clinic()

clinic()

Compare Closely;

Let’s start with the simplest aspect of control flow: comparators. There are six:

Equal to (==)

>>>2 == 2
True
>>>2 == 5
False

Not equal to (!=)

>>>2 != 5
True
>>>2 != 2
False

Less than (<)

>>>2 < 5
True
>>>5 < 2
False

Less than or equal to (<=)

>>>2 <= 2
True
>>>5 <= 2
False

Greater than (>)

>>>5 > 2
True
>>>2 > 5
False

Greater than or equal to (>=)

>>>5 >= 5
True
>>>2 >= 5
False

Comparators check if a value is (or is not) equal to, greater than (or equal to), or less than (or equal to) another value.

Note that == compares whether two things are equal, and = assigns a value to a variable.

To be and/or Not to Be;

Boolean operators compare statements and result in boolean values. There are three boolean operators:

  • and, which checks if both the statements are True;

  • or, which checks if at least one of the statements is True;

  • not, which gives the opposite of the statement.

We’ll go through the operators one by one.

The boolean operator and returns True when the expressions on both sides of and are true. For instance:

  • 1 < 2 and 2 < 3 is True.

  • 1 < 2 and 2 > 3 is False.

The boolean operator or returns True when at least one expression on either side of or is true. For example:

  • 1 < 2 or 2 > 3 is True;

  • 1 > 2 or 2 > 3 is False.

The boolean operator not returns True for false statements and False for true statements. For example:

  • not False will evaluate to True, while not 41 > 40 will return False.

This and That (or This, but Not That!);

Boolean operators aren’t just evaluated from left to right. Just like with arithmetic operators, there’s an order of operations for boolean operators:

  1. not is evaluated first;

  2. and is evaluated next;

  3. or is evaluated last.

For example, True or not False and False returns True. If this isn’t clear, look at the Hint.

Parentheses () ensure your expressions are evaluated in the order you want. Anything in parentheses is evaluated as its own unit.

Hint: True or not False and False. not gets evaluated first, so we have True or True and False. and goes next, so we get True or False. As we’ve seen, True or False is True, so the value finally returned is True!

Conditional Statement Syntax;

if is a conditional statement that executes some specified code after checking if its expression is True.

Here’s an example of if statement syntax:

if 8 < 9:
print "Eight is less than nine!"

In this example, 8 < 9 is the checked expression and print "Eight is less than nine!" is the specified code.

Pay attention to the indentation before the print statement. This space, called white space, is how Python knows we are entering a new block of code. Python accepts many different kinds of indentation to indicate blocks. In this lesson, we use four spaces but elsewhere you might encounter two-space indentation or tabs (which Python will see as different from spaces).

If the indentation from one line to the next is different and there is no command (like if) that indicates an incoming block then Python will raise an IndentationError. These errors could mean, for example, that one line had two spaces but the next one had three. Python tries to indicate where this error happened by printing the line of code it couldn’t parse and using a  ^  to point to where the indentation was different from what it expected.

Let’s get some practice with if statements. Remember, the syntax looks like this:

if some_function():
# block line one
# block line two
# et cetera

Looking at the example above, in the event that some_function() returns True, then the indented block of code after it will be executed. In the event that it returns False, then the indented block will be skipped.

Also, make sure you notice the colons at the end of the if statement. We’ve added them for you, but they’re important.

Else Problems, I Feel Bad for You, Son;

The else statement complements the if statement. An if/else pair says: “If this expression is true, run this indented code block; otherwise, run this code after the else statement.”

Unlike if, else doesn’t depend on an expression. For example:

if 8 > 9:
print "I don't get printed!"
else:
print "I get printed!"

I Got 99 Problems, But a Switch Ain’t One;

elif is short for “else if.” It means exactly what it sounds like: “otherwise, if the following expression is true, do this!”

if 8 > 9:
print "I don't get printed!"
elif 8 < 9:
print "I get printed!"
else:
print "I also don't get printed!"

In the example above, the elif statement is only checked if the original if statement is False.

Review;

Really great work! Here’s what you’ve learned in this unit:

Comparators

3 < 4
5 >= 5
10 == 10
12 != 13

Boolean Operators

True or False
(3 < 4) and (5 >= 5)
this() and not that()

Conditional Statements

if this_might_be_true():
print "This really is true."
elif that_might_be_true():
print "That is true."
else:
print "None of the above."

Let’s get to the grand finale.

Grand Finale;

There is the outline of a function called grade_converter().

The purpose of this function is to take a number grade (1-100), defined by the variable grade, and to return the appropriate letter grade (A, B, C, D, or F).

Your task is to complete the function by creating appropriate if and elif statements that will compare the input grade with a number and then return a letter grade.

Your function should return the following letter grades:

  • 90 or higher should get an “A”

  • 80 - 89 should get a “B”

  • 70 - 79 should get a “C”

  • 65 - 69 should get a “D”

  • Anything below a 65 should receive an “F”

def grade_converter(grade):
if grade >= 90:
return "A"
elif grade >= 80:
return "B"
elif grade >= 70:
return "C"
elif grade >= 65:
return "D"
else:
return "F"

# This should print an "A"
print grade_converter(92)

# This should print a "C"
print grade_converter(70)

# This should print an "F"
print grade_converter(61)

IC

PYTHON LESSON 3: Conditionals & Control Flow

Go with the Flow;

Just like in real life, sometimes we’d like our code to be able to make decisions.

The Python programs we’ve written so far have had one-track minds: they can add two numbers or print something, but they don’t have the ability to pick one of these outcomes over the other.

Control flow gives us this ability to choose among outcomes based on what else is happening in the program.

For Example;

def clinic():
print "You've just entered the clinic!"
print "Do you take the door on the left or the right?"
answer = raw_input("Type left or right and hit 'Enter'.").lower()
if answer == "left" or answer == "l":
print "This is the Verbal Abuse Room, you heap of parrot droppings!"
elif answer == "right" or answer == "r":
print "Of course this is the Argument Room, I've told you that already!"
else:
print "You didn't pick left or right! Try again."
clinic()

clinic()

Compare Closely;

Let’s start with the simplest aspect of control flow: comparators. There are six:

Equal to (==)

>>>2 == 2
True
>>>2 == 5
False

Not equal to (!=)

>>>2 != 5
True
>>>2 != 2
False

Less than (<)

>>>2 < 5
True
>>>5 < 2
False

Less than or equal to (<=)

>>>2 <= 2
True
>>>5 <= 2
False

Greater than (>)

>>>5 > 2
True
>>>2 > 5
False

Greater than or equal to (>=)

>>>5 >= 5
True
>>>2 >= 5
False

Comparators check if a value is (or is not) equal to, greater than (or equal to), or less than (or equal to) another value.

Note that == compares whether two things are equal, and = assigns a value to a variable.

To be and/or Not to Be;

Boolean operators compare statements and result in boolean values. There are three boolean operators:

  • and, which checks if both the statements are True;

  • or, which checks if at least one of the statements is True;

  • not, which gives the opposite of the statement.

We’ll go through the operators one by one.

The boolean operator and returns True when the expressions on both sides of and are true. For instance:

  • 1 < 2 and 2 < 3 is True.

  • 1 < 2 and 2 > 3 is False.

The boolean operator or returns True when at least one expression on either side of or is true. For example:

  • 1 < 2 or 2 > 3 is True;

  • 1 > 2 or 2 > 3 is False.

The boolean operator not returns True for false statements and False for true statements. For example:

  • not False will evaluate to True, while not 41 > 40 will return False.

This and That (or This, but Not That!);

Boolean operators aren’t just evaluated from left to right. Just like with arithmetic operators, there’s an order of operations for boolean operators:

  1. not is evaluated first;

  2. and is evaluated next;

  3. or is evaluated last.

For example, True or not False and False returns True. If this isn’t clear, look at the Hint.

Parentheses () ensure your expressions are evaluated in the order you want. Anything in parentheses is evaluated as its own unit.

Hint: True or not False and False. not gets evaluated first, so we have True or True and False. and goes next, so we get True or False. As we’ve seen, True or False is True, so the value finally returned is True!

Conditional Statement Syntax;

if is a conditional statement that executes some specified code after checking if its expression is True.

Here’s an example of if statement syntax:

if 8 < 9:
print "Eight is less than nine!"

In this example, 8 < 9 is the checked expression and print "Eight is less than nine!" is the specified code.

Pay attention to the indentation before the print statement. This space, called white space, is how Python knows we are entering a new block of code. Python accepts many different kinds of indentation to indicate blocks. In this lesson, we use four spaces but elsewhere you might encounter two-space indentation or tabs (which Python will see as different from spaces).

If the indentation from one line to the next is different and there is no command (like if) that indicates an incoming block then Python will raise an IndentationError. These errors could mean, for example, that one line had two spaces but the next one had three. Python tries to indicate where this error happened by printing the line of code it couldn’t parse and using a  ^  to point to where the indentation was different from what it expected.

Let’s get some practice with if statements. Remember, the syntax looks like this:

if some_function():
# block line one
# block line two
# et cetera

Looking at the example above, in the event that some_function() returns True, then the indented block of code after it will be executed. In the event that it returns False, then the indented block will be skipped.

Also, make sure you notice the colons at the end of the if statement. We’ve added them for you, but they’re important.

Else Problems, I Feel Bad for You, Son;

The else statement complements the if statement. An if/else pair says: “If this expression is true, run this indented code block; otherwise, run this code after the else statement.”

Unlike if, else doesn’t depend on an expression. For example:

if 8 > 9:
print "I don't get printed!"
else:
print "I get printed!"

I Got 99 Problems, But a Switch Ain’t One;

elif is short for “else if.” It means exactly what it sounds like: “otherwise, if the following expression is true, do this!”

if 8 > 9:
print "I don't get printed!"
elif 8 < 9:
print "I get printed!"
else:
print "I also don't get printed!"

In the example above, the elif statement is only checked if the original if statement is False.

Review;

Really great work! Here’s what you’ve learned in this unit:

Comparators

3 < 4
5 >= 5
10 == 10
12 != 13

Boolean Operators

True or False
(3 < 4) and (5 >= 5)
this() and not that()

Conditional Statements

if this_might_be_true():
print "This really is true."
elif that_might_be_true():
print "That is true."
else:
print "None of the above."

Let’s get to the grand finale.

Grand Finale;

There is the outline of a function called grade_converter().

The purpose of this function is to take a number grade (1-100), defined by the variable grade, and to return the appropriate letter grade (A, B, C, D, or F).

Your task is to complete the function by creating appropriate if and elif statements that will compare the input grade with a number and then return a letter grade.

Your function should return the following letter grades:

  • 90 or higher should get an “A”

  • 80 - 89 should get a “B”

  • 70 - 79 should get a “C”

  • 65 - 69 should get a “D”

  • Anything below a 65 should receive an “F”

def grade_converter(grade):
if grade >= 90:
return "A"
elif grade >= 80:
return "B"
elif grade >= 70:
return "C"
elif grade >= 65:
return "D"
else:
return "F"

# This should print an "A"
print grade_converter(92)

# This should print a "C"
print grade_converter(70)

# This should print an "F"
print grade_converter(61)

robot