§ — — Integrative Programming and Technologies 1
Programs often need to make choices. In Python we use if, elif, and else statements for decisions. Example:
score = int(input("Enter exam score: "))
if score >= 75:
print("You passed!")
else:
print("You failed.")
Here, if score is 75 or higher, the program prints "You passed!", otherwise "You failed." Indentation (4 spaces) defines the block inside the if. We can chain conditions:
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 75:
grade = "C"
else:
grade = "F"
print("Your grade is", grade)
The conditions are checked in order; the first true branch runs. Use these to make your program adapt to different inputs (e.g., giving feedback, choosing between actions).
Loops let us repeat tasks. The for loop iterates over a sequence or range. Example:
for i in range(1, 6): # i goes from 1 to 5
print("Step", i)
This prints steps 1 through 5. Use range(start, stop) or range(stop) for numbers. Or loop through items in a list:
fruits = ["mango", "banana", "apple"]
for fruit in fruits:
print("I like", fruit)
A while loop runs as long as a condition holds true. Example:
count = 0
while count < 3:
print("Count is", count)
count += 1 # increment counter
This prints count = 0, 1, 2. Be careful with while loops to update the condition (like count += 1) or they may never stop (infinite loop).
ProReviewer — locked
Drills, code labs, and full solutions.
ProReviewer — locked
Drills, code labs, and full solutions.
ProReviewer — locked
Drills, code labs, and full solutions.
Done with this module? Track it — your progress shows on the subject list.
Up next
Lesson 12: Python Refresher — Lists, Tuples, and Dictionaries→←Previous: Lesson 10: Python Refresher — Variables and Data Types