Fundamentals: Conditionals Flashcards

1
Q

Conditional Expression

A

An expression that evaluates to either truthy or falsy, typically used in order to determine if a conditional requirement is met. Usually used with if statements.

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

Boolean

A

A data type that has only two valid values, True and False, and represent true and false

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

Truthy

A

A value that is considered True inside a conditional expression, even if it is not literally True

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

Falsy

A

A value that is considered False, even if it is not literally False

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

Truthiness vs. Falsiness

A

There are two boolean values: True and False, and they represent the concept of true and false.

When faced with the question of “is this value true or false?,” values that are not booleans become truthy or falsey. A truthy value is something that would be considered True, even if it is not literally the value True. A falsey value is considered False, even if it’s not literally False.

In Python, all values are truthy… with some exceptions.

The following values are falsey, and will resolve to False in a conditional statement:

    None
    0 and 0.0
    "" (empty string)
    [] (empty list)
    {} (empty dictionary)
    There are a few more values; look them up!
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

if, elif, else Control Statements

A

Using truthiness and falsiness, we can create control statements in Python. These statements allow for conditional logic: in some contexts, one set of code should run, in contrast to other contexts. What those contexts are depends on the control statement!

this_expression_is_truthy = True
a_different_expression_is_truthy = True

if this_expression_is_truthy:
print(“The expression is truthy!”)
print(“We will run all code in the if clause.”)
elif a_different_expression_is_truthy:
print(“The above if clause did NOT run…”)
print(“AND this different expression IS truthy”)
print(“So now we run the code is in this clause.”)
else:
print(“The expression is falsey!”)
print(“We will run all code in the else clause.”)

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

and vs. or Boolean Operations

A

We can combine two comparisons with a boolean operation. These expressions also eventually evaluate to True or False.

The boolean operations are:

and
or
not

For the operations and and or, both of these have two sides: a left side and a right side. The operations use these two sides.

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

and

A
For and, if and only if both sides are truthy, then the entire expression is True. Otherwise, it's False. The following examples use and and evaluate as True:
True and True
True and 100
1 and True
3 > 0 and 100 <= 999
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

or

A

For or, if at least one side is truthy, then the entire expression is True. Otherwise, it’s False. The following examples evaluate as True:

False or True
False or 1
0 or True
3 > 0 or 100 > 999

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

Negation With not

A

The not operator goes before any expression and negates that expression. When not is in front of a truthy expression, the entire expression becomes False. When not is in front of a falsy expression, the entire expression becomes True.

The following examples all evaluate to True:

not False
not 0
True and not False
not False or False
not None
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Chaining Relational Operators of Equal Priority

A

Python treats the group of relational operators—the group containing , >=, !=, ==, in, not in, is, and is not—a little differently from other operators.

Consider the following, which evaluates to True: 1 < 2 < 3
In this case, the usual left-to-right reading appears to give us the expected result. 1 < 2 is True, and True when interpreted as an int is 1, which is less than 3, resulting in True.

This is misleading.

Consider this example, which also evaluates to True:
1 < 2 == 2

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