Fundamentals: Conditionals Flashcards
Conditional Expression
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.
Boolean
A data type that has only two valid values, True and False, and represent true and false
Truthy
A value that is considered True inside a conditional expression, even if it is not literally True
Falsy
A value that is considered False, even if it is not literally False
Truthiness vs. Falsiness
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!
if, elif, else Control Statements
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.”)
and vs. or Boolean Operations
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.
and
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
or
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
Negation With not
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
Chaining Relational Operators of Equal Priority
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