Conditions and Branching Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Conditional statements - comparison operators

A
equal: ==
not equal: !=
greater than: >
less than: <
greater than or equal to: >=
less than or equal to: <=
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Comparison operators example

A

Use Equality sign to compare the strings
“ACDC”==”Michael Jackson”
“ACDC” == “Michael Jackson”
False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Branching - IF statements

A

If statement example

age = 19
#age = 18
#expression that can be true or false
if age > 18:
    #within an indent, we have the expression that is run if the condition is true
    print("you can enter" )
#The statements after the if statement will run regardless if the condition is true or false 
print("move on")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Else statements

A
The else statement runs a block of code if none of the conditions are True before this else statement.
# Else statement example
​
age = 18
# age = 19
​
if age > 18:
    print("you can enter" )
else:
    print("go see Meat Loaf" )

print(“move on”)
go see Meat Loaf
move on

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

elif statement

A
The elif statement, short for else if, allows us to check additional conditions if the condition statements before it are False. 
# Elif statment example
​
age = 18
​
if age > 18:
    print("you can enter" )
elif age == 18:
    print("go see Pink Floyd")
else:
    print("go see Meat Loaf" )

print(“move on”)
go see Pink Floyd
move on

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Logical operators

A

Logical operators allow you to combine or modify conditions.

and
or
not

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Logical operator - “AND”

album_year = 1980

if(album_year > 1979) and (album_year < 1990):
print (“Album year was in between 1980 and 1989”)

print(“”)
print(“Do Stuff..”)

A

Album year was in between 1980 and 1989

Do Stuff..

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

logical operator - “OR”
album_year = 1990

if(album_year < 1980) or (album_year > 1989):
print (“Album was not made in the 1980’s”)
else:
print(“The Album was made in the 1980’s “)

A

Album was not made in the 1980’s

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

logical operator - “NOT”

album_year = 1983

if not (album_year == '1984'):
    print ("Album year is not 1984")
A

Album year is not 1984

How well did you know this?
1
Not at all
2
3
4
5
Perfectly