Python -- Conditionals (If, Else, Elif) Flashcards
Ask the user their age, and if it is over or equal to 18, tell them that they are grown up. If under 18, tell them that they are still a child.
user_num = int(input(‘Enter your age: ‘))
if user_num >= 18:
print(‘You are an adult now!’)
else:
print(‘Sorry, you are still a child’)
language == ‘JavaScript’
if language == ‘Java’:
print(‘Language is Java’)
elif language == ‘Python’:
print(‘Language is Python’)
else:
print(‘No Match’)
What does this code evaluate?
No Match
In the ‘and’ operator, how many ways can there be for it to evaluate to true?
1
user = ‘Admin’
logged_in = False
if user == ‘Admin’ and logged_in:
print(‘Successfully logged in!’)
else:
print(‘Hacker Alert!)
What does this code evaluate?
Hacker Alert!
user = ‘Admin’
logged_in = False
if user == ‘Admin’ or logged_in:
print(‘Successfully logged in!’)
else:
print(‘Hacker Alert!’)
What does this code evaluate?
Successfully logged in!
In the ‘or’ operator, how many ways can there be for it to evaluate to true?
As long as there is a true, it will be true
user = ‘Admin’
logged_in = False
if not logged_in:
print(‘Please log in!’)
else:
print(‘Welcome!’)
Please log in!
What is the ‘not’ operator?
‘not’ switches a true to false, so if something is false, then if you say that thing and ‘not’, it will become true.
a = [1, 2, 3]
b = [1, 2, 3]
print(a == b)
What does this code evaluate?
True
a = [1, 2, 3]
b = [1, 2, 3]
print(a is b)
What does this code evaluate?
False
What does the ‘is’ operator do?
It checks if the two objects have the same ID
What are the most common ways to test for False?
- False
- None
- Zero of any numeric type
- Any empty sequence (e.g. ‘’, [] )
- Any empty mapping (e.g. {})