Python -- Conditionals (If, Else, Elif) Flashcards

1
Q

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.

A

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’)

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

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?

A

No Match

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

In the ‘and’ operator, how many ways can there be for it to evaluate to true?

A

1

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

user = ‘Admin’
logged_in = False

if user == ‘Admin’ and logged_in:
print(‘Successfully logged in!’)
else:
print(‘Hacker Alert!)

What does this code evaluate?

A

Hacker Alert!

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

user = ‘Admin’
logged_in = False

if user == ‘Admin’ or logged_in:
print(‘Successfully logged in!’)
else:
print(‘Hacker Alert!’)

What does this code evaluate?

A

Successfully logged in!

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

In the ‘or’ operator, how many ways can there be for it to evaluate to true?

A

As long as there is a true, it will be true

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

user = ‘Admin’
logged_in = False

if not logged_in:
print(‘Please log in!’)
else:
print(‘Welcome!’)

A

Please log in!

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

What is the ‘not’ operator?

A

‘not’ switches a true to false, so if something is false, then if you say that thing and ‘not’, it will become true.

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

a = [1, 2, 3]
b = [1, 2, 3]
print(a == b)

What does this code evaluate?

A

True

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

a = [1, 2, 3]
b = [1, 2, 3]
print(a is b)

What does this code evaluate?

A

False

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

What does the ‘is’ operator do?

A

It checks if the two objects have the same ID

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

What are the most common ways to test for False?

A
  • False
  • None
  • Zero of any numeric type
  • Any empty sequence (e.g. ‘’, [] )
  • Any empty mapping (e.g. {})
How well did you know this?
1
Not at all
2
3
4
5
Perfectly