Conditionals Flashcards
1
Q
If Statements
A
- Used to make decisions in our program.
- If Condition: the if statement is used to execute a block of code if a specified condition is true.
- An if statement includes a condition and one or more statements that form the body of the if statement.
ex.) if (conditional expression):
body - The conditional expression is evaluated, if it evaluates to True then the body of the if statement executes, followed by the rest of the statements in the program.
- If the if statement’s condition evaluates to False then the body of the if statement is skipped and execution continues at the first line after the body of the if statement.
- An if statement’s condition often includes a relational operator that compares two values, variables, or complex expressions.
- The body must be indented
- Semicolon after condition
- Number of indented spaces must be consistent in a block.
2
Q
Mutually exclusive
A
3
Q
If-Else Statements
A
- The if-else statement executes one block of code if a condition is true, and another block if it’s false.
- Consists of an if part with a condition and a body, and an else part with a body (but no condition).
Format:
if (conditional expression):
body of ‘if’ - when expression is True
else:
body of ‘else’ - when expression is False
remainder of program
- When the statement executes its condition is evaluated.
If the condition evaluates to True then the body of the if part executes and the body of the else part is skipped. - If the condition evaluates to False the body of the if part is skipped and the body of the else part executes.
- Impossible for both bodies to execute.
- Impossible to skip both bodies.
When to use if-else statement: when only one condition needs to be written and evaluated.
4
Q
If-Elif-Else Statements
A
5
Q
If-Elif Statements
A
6
Q
Nested If Statements
A
7
Q
Boolean Expression
A
- Conditionals are questions with answers that are either True or False.
- Variable with only values of True or False, is called a Boolean type.
- Known as bool (short for boolean).
8
Q
Logical (Boolean) Operators
A
Python includes three Boolean operators: not, and, and or.
9
Q
And Operator
A
a and b: Output is True only when a and b are both True.
- If a is False, or b is False, or both a and b are False then a and b evaluates to False.
- Two statements are True.
ex.) not snowing today, temperature is not freezing.
- Same thing as asking: a == true and b === true.
10
Q
Or Operator
A
- a or b: Output True when at least one of a and b are True.
- Evaluates to True if a is True, or if b is True, or if both a and b are True.
- Evaluates to False if both a and b are False.
11
Q
Not Operator
A
- The not operator reverses the truth of a Boolean expression. If the expression a, evaluates to True, then not a evaluates to False.
- If a evaluates to False then not a evaluates to True.
12
Q
Truth Tables
A
- A truth table is a mathematical table used in logic and computer science to represent the possible truth values of logical expressions.
- The behavior of any Boolean expression can be described by a truth table.
- Has one column for each distinct variable in the Boolean expression, and a column for the expression itself.
- Each row in the truth table represents one combination of True and False values for the variables in the expression.
A truth table for an expression having n distinct variables has 2^n rows, each of which shows the result computed by the expression for a different combination of values.
13
Q
A