Conditional Operators Flashcards

if, elif, else

1
Q

Conditional Operators

A

Used to take decisions in our code based on a condition.
If signal is green, we go
If signal is yellow, we slow down
If signal is red, we stop

Here, signal is the condition that determines the decision

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

Family of conditional operators

A

To take a single decision, we might have different factors (like green, yellow and red). So we group them into a family of conditional operators. For example,

If signal is green, we go (one specific condition)
Else if the signal is yellow, we slow down (not the first condition, but a second specific condition)
Else if the single is red, we stop (not the first two conditions but this is also a specific condition that the light HAS TO BE red - so, else if)
Else raise a complaint (this means, none of the three specific conditions are met, so anything other than those three conditions, use this - else)

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

if condition

A

if (myAge > sisterAge):
print(‘I am the elder sister’)

is same as

condition = myAge > sisterAge
if (condition == True):
print(‘I am the elder sister’)

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

Else if condition

A

Else if in python is written as elif

elif (myAge < sisterAge):
print(‘I am the younger sister’)

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

Can you think what would be the else case in this example?

If myAge is greater, we print I’m the elder sister
else if myAge is smaller, we print I’m the younger sister

What do you think would be the else part?

A

else:
print(‘We are twins!’)

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