Week 6 - Conditionals and Recursion
Conditionals and Recursion in Python¶
Objective¶
By the end of this lesson, you will understand and implement Python conditionals, logical operations, recursion, and error handling. You will be able to:
- Apply the modulus operator and boolean expressions
- Write programs using if
, elif
, and else
statements
- Utilize recursion effectively
- Handle basic input and debugging in Python
Integer Division and the Modulus Operator¶
Key Concepts:¶
- Integer Division
//
- Modulus Operator
%
Explanation:¶
Integer division truncates the decimal part of a division, while the modulus operator returns the remainder of a division operation. These concepts can be useful for tasks like:
- Converting time formats
- Checking divisibility
- Handling clock arithmetic
Code Example:¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Boolean Expressions and Logical Operators¶
Key Concepts:¶
- Relational operators:
==
,!=
,<
,>
,<=
,>=
- Logical operators:
and
,or
,not
Explanation:¶
Boolean expressions result in True
or False
. Combining these expressions with logical operators allows for more complex decision-making in programs.
Code Example:¶
1 2 3 4 |
|
Boolean expressions that are complex can often be simplified via logic laws, for example:
Code Example:¶
1 2 3 4 5 6 7 8 9 10 |
|
DeMorgan’s law states:¶
- not (A or B) is equivalent to not A and not B
- not (A and B) is equivalent to not A or not B
Truth Table for not (A or B)
and not A and not B
¶
A | B | A or B | not (A or B) | not A | not B | not A and not B |
---|---|---|---|---|---|---|
True | True | True | False | False | False | False |
True | False | True | False | False | True | False |
False | True | True | False | True | False | False |
False | False | False | True | True | True | True |
As you can see, the columns for not (A or B)
and not A and not B
produce the same values, which proves the equivalence of the first DeMorgan’s law.
Truth Table for not (A and B)
and not A or not B
¶
A | B | A and B | not (A and B) | not A | not B | not A or not B |
---|---|---|---|---|---|---|
True | True | True | False | False | False | False |
True | False | False | True | False | True | True |
False | True | False | True | True | False | True |
False | False | False | True | True | True | True |
Here, the columns for not (A and B)
and not A or not B
also produce identical values, proving the second DeMorgan’s law.
So the above example becomes more clear written like so:
Code Example¶
1 2 3 4 5 6 7 8 9 10 |
|
Truth Table for Code¶
We will evaluate the function for all possible combinations of a
, b
, and c
(True and False). Both the original and simplified logic should produce the same results.
a | b | c | Original Expression (not (a or b) and not c ) |
Simplified Expression (not a and not b and not c ) |
Equivalent |
---|---|---|---|---|---|
True | True | True | False | False | Yes |
True | True | False | False | False | Yes |
True | False | True | False | False | Yes |
True | False | False | False | False | Yes |
False | True | True | False | False | Yes |
False | True | False | False | False | Yes |
False | False | True | False | False | Yes |
False | False | False | True | True | Yes |
As shown in the truth table, both the original and simplified logic yield the same results for all possible combinations of a
, b
, and c
. This confirms that applying DeMorgan’s law results in equivalent expressions.
Conditional Statements (if
, else
, elif
)¶
Key Concepts:¶
if
,else
,elif
for branching logic
Explanation:¶
Conditional statements let programs decide which code to run based on specific conditions. Branching logic can include multiple possibilities using if
, else
, and elif
.
Code Example:¶
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Nested Conditionals and Logical Simplification¶
Key Concepts:¶
- Nested
if
statements - Simplifying conditionals with logical operators
Explanation:¶
You can nest conditionals inside other conditionals, but this can lead to confusing code. Logical operators like and
and or
can often simplify these nested conditionals.
Code Example:¶
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Introduction to Recursion¶
Key Concepts:¶
- Recursive functions
- Base case and recursive case
Explanation:¶
A recursive function is a function that calls itself. It MUST have a base case to prevent infinite recursion. Recursion is useful for problems like countdowns, fractals, and some mathematical algorithms.
Code Example:¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
It’s worth noting that recursion is a powerful tool, BUT loops are more efficient.
Debugging Recursive Functions¶
Key Concepts:¶
- Understanding recursion errors (e.g., infinite recursion)
- Stack frames and base cases
Explanation:¶
Recursion without a proper base case leads to infinite recursion and crashes the program. Use print statements and stack diagrams to debug recursive functions. * Remember for Stack Diagrams there’s -> Python Tutor
Code Example:¶
1 2 3 4 5 6 7 8 |
|
Review of Handling User Input¶
Key Concepts:¶
input()
function- Converting string input to integers, floats, etc.
Explanation:¶
You can prompt the user for input using input()
. Always remember to handle the conversion of strings to numbers carefully to avoid errors.
Code Example:¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
Exercises and Practice¶
Practice Exercises:¶
-
Exercise 1: Modulus Operator Write a program that checks whether a number is divisible by both 3 and 5.
-
Exercise 2: Recursion
Write a recursive function that prints the sum of all integers from 1 ton
. -
Exercise 3: User Input with Error Handling
Ask the user for two integers and divide them. Handle errors like division by zero.
Answers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|