目录
1.Introduction to Control Flow?
3.Relational Operators: Equals and Not Equals
This is the control flow of your program. In Python, your script will execute from?the top down, until there is nothing left to run. It is your job to include gateways, known as conditional?statements, to tell the computer when it should execute certain blocks of code. If these conditions are met,then run this function.
In order to build control flow into our program, we want to be able to check if something is true or not.A boolean expression is a statement that can either be True or False.
Example statement:My dog is the cutest dog in the world.
example_statement = "No"
This is an opinion and not a boolean expression, so you would set example_statement to "No" in the editor to the?right. Okay, now it’s your turn:?
# Statement one:Dogs are mammals.
statement_one = "Yes"
# Statement two:My dog is named Pavel.
statement_two = "Yes"
# Statement three:Dogs make the best pets.
statement_three = "No"
# Statement four:Cats are female dogs.
statement_four = "Yes"
# Statement one:(5 * 2) - 1 == 8 + 1
statement_one = True
# Statement two:13 - 6 != (3 * 2) + 1
statement_two = False
# Statement three:3 * (2 - 1) == 4 - 1
statement_three = True
True and False are the only bool types, and any variable that is assigned one of these values is called a booleanvariable.
my_baby_bool = "true"
print(type(my_baby_bool))
my_baby_bool_two = True
print(type(my_baby_bool_two))
You’ll notice that instead of “then” we have a colon, :. That tells the computer that what’s comingnext is what should be executed if the condition is met. Enter a user name here, make sure to make it a string.
user_name = "Dave"
if user_name == "Dave":
print("Get off my computer Dave!")
user_name = "angela_catlady_87"
if user_name == "angela_catlady_87":
print("I know it is you, Dave! Go away!")
Now that we’ve added conditional statements to our toolkit for building control flow
> greater than
>= greater than or equal to
< less than
<= less than or equal to
x = 20
y = 20
# Write the first if statement here:
if x == y:
print("These numbers are the same")
credits = 120
# Write the second if statement here:
if credits >= 120:
print("You have enough credits to graduate!")
Often, the conditions you want to check in your conditional statement will require more than one boolean expression to cover.In these cases, you can build larger boolean expressions using boolean operators.There are three boolean operators that we will cover:and or not?and combines two boolean expressions and evaluates as True if both its components are True, but False otherwise.
credits = 120
gpa = 3.4
if credits >= 120 and gpa >= 2.0:
print("You meet the requirements to graduate!")
The boolean operator or combines two expressions into a larger expression that is True if either component is True.
credits = 118
gpa = 2.0
if credits >= 120 or gpa >= 2.0:
print("You have met at least one of the requirements.")
The final boolean operator we will cover is not. This operator is straightforward: when?applied to any boolean expression it reverses the boolean value. So if we have a True statement and apply a not?operator we get a False statement.
credits = 120
gpa = 1.8
if not (credits >= 120):
print("You do not have enough credits to graduate.")
if not (gpa >= 2.0):
print("Your GPA is not high enough to graduate.")
if not (credits >= 120) and not (gpa >= 2.0):
print("You do not meet either requirement to graduate!")
else statements allow us to elegantly describe what we want our code to do when certain conditions are not met.
credits = 120
gpa = 1.9
if (credits >= 120) and (gpa >= 2.0):
print("You meet the requirements to graduate!")
else:
print("You do not meet the requirements to graduate.")
An elif statement checks another condition after the previous if statements conditions aren’t met.
grade = 86
if grade >= 90:
print("A")
elif grade >= 80:
print("B")
elif grade >= 70:
print("C")
elif grade >= 60:
print("D")
else:
print("F")