After this lesson, you will be able to: Write Python code that makes decisions with if/elif/else and repeats actions with for and while loops.
Programs are useful because they can react differently to different inputs. In this lesson you will learn the building blocks of decision-making, comparison, boolean logic, and conditional statements, and how to repeat work efficiently with loops.
A variable is a labeled container for a value. Python figures out the type automatically, integers (1, 42), floats (3.14), strings ("hello"), and booleans (True, False). You assign a value with =, and you can reassign it any time. Variable names should be lowercase and use_underscores.
Comparisons return True or False. Combine them with and, or, not.
age = 17is_student = Trueprint(age >= 16) # Trueprint(age == 18) # Falseprint(age >= 16 and is_student) # Trueprint(not is_student) # False
Branches let your program react differently based on conditions.
score = 87if score >= 90:grade = "A"elif score >= 80:grade = "B"elif score >= 70:grade = "C"else:grade = "F"print("Your grade is: " + grade)
Diagram coming soon!
Flowchart showing diamond decision points: score >= 90? → A; otherwise score >= 80? → B; otherwise → continue
Loops repeat a block of code. Use for when you know how many times; while when a condition decides.
# Count from 1 to 5for i in range(1, 6):print(i)# Keep guessing until correctsecret = 7guess = 0while guess != secret:guess = int(input("Guess a number: "))print("You got it!")
Write a program that asks for a numeric score and prints the matching letter grade. The score 87 is pre-loaded as input. Pass once the grade is right and your code uses input(), float(), and if/elif/else.
Hint: think about whether 3 is included.
Sign in and purchase access to unlock this lesson.