•Chief Idiot•2 min read
Python 1.3: The Chores (Loops & Logic)
Making the computer do the boring work while you take a nap. Intro to Loops and If/Else.
The Art of Being Lazy
The whole point of coding is to make the computer do work so you don't have to. Using Loops, you can make it do a task 1,000,000 times without complaining.

1. The Decision (If / Else)
This is the brain of your code. It checks a condition.
is_hungry = True
if is_hungry:
print("Order Pizza")
else:
print("Go back to sleep")Pro Tip: You can chain these (elif) but don't go crazy. If you have 50 if statements, you are doing it wrong (or you are building an 'AI' from the 1980s).
2. The Loop (For)
This is for doing chores. "For every item in this pile, do X."
# A list of chores
chores = ["Dishes", "Laundry", "Tax Evasion"]
for task in chores:
print("I am currently doing: " + task)Output:
I am currently doing: Dishes
I am currently doing: Laundry
I am currently doing: Tax Evasion3. The Infinite Loop (While)
WARNING: This one is dangerous. It runs forever until you tell it to stop.
# Don't run this (or do, I'm not your mom)
while True:
print("I will never stop screaming")Putting it Together
Let's build a "Smart" Thermostat.
temperature = 80
if temperature > 75:
for i in range(5):
print("Blowing cold air...")
else:
print("Temperature is fine.")See? You just replaced a $200 Nest thermostat with 5 lines of code.