Python Conditional Statements
The following tutorial is an introduction to Python conditional statements using the IF command and comparison statements such as those shown below.
Operator < - less than <= - less than or equal to > - greater than >= - greater than or equal to == - equal != - not equal to
Certain keywords such as AND, OR, and NOT can be used to combine, select, or reverse the outcome of conditional statements. These logical operations change the results based on input data.
Function if - execute code with true logical condition and - return True if both statements are True or - return True if either statement is True not - return the opposite of the input True/False
The following video tutorial demonstrates logical operations with a number guessing game.
Number Guessing Game in Python
answer = randint(0,100)
correct = False
while not(correct):
guess = int(input("Guess a number: "))
if guess < answer:
print("Too low")
elif guess > answer:
print("Too high")
else:
print("Correct!")
correct = True
Generative AI Learning
Write-it-yourself class: the AI asks, plants bugs, and grades - it does not write your code. Use these prompts after building the examples yourself.
Tip: Prediction before execution is the skill: for any conditional you write, choose test values ON each boundary, not just between them. Off-by-one boundary bugs (>= vs >) survive casual testing and bite in alarms and safety logic.
What to Turn In
Submit a short report (PDF, 1-2 pages) that curates your results into a demonstration of what you learned. You may use Generative AI to help write the report, but the code must be your own. Answer these questions:
- Include a conditional program you wrote yourself with at least if/elif/else and one compound (and/or) condition - the number-guessing game from this page or an engineering variant (temperature band alarm, pump status logic). Show output for test cases on both sides of each boundary AND exactly on each boundary.
- Write out the truth table for your compound condition and confirm your program matches it for all four input combinations.
- From the planted-bug prompt: show the buggy snippet, your prediction for each of the 3 test inputs, the bug you found, and the fix. Did you catch it before the reveal?
- From the quiz prompt: one question you missed and the corrected answer.
