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

from random import randint
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.

"Quiz me with 5 questions, one at a time, on Python conditionals: the difference between = and ==, how if / elif / else choose exactly one branch and what happens when conditions overlap, how and, or, and not combine comparisons, why indentation is part of the syntax, and what 'x < 5 or x > 10' does differently from 'x < 5 and x > 10'. Ask me to predict the output of a short snippet for at least two of the questions. Grade my answers and list my misconceptions."
"Write a short Python if/elif/else block that classifies a reactor temperature reading into LOW / NORMAL / HIGH / ALARM bands, but plant ONE bug (a boundary handled with the wrong comparison, an elif order problem, or = instead of ==). Show me the code and 3 test inputs. I will predict the output for each input and find the bug. Only then reveal it and show the fix."

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:

  1. 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.
  2. Write out the truth table for your compound condition and confirm your program matches it for all four input combinations.
  3. 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?
  4. From the quiz prompt: one question you missed and the corrected answer.

Course Information

Excel and VBA

Python

MATLAB

MathCAD

Related Courses

Admin