DEV Community

Nelly Triza
Nelly Triza

Posted on

Finally Teaching Python to Think ๐Ÿ

When I started learning Python, I was mostly telling it what to print.

Then I discovered if statements and loops.

And suddenly, Python could make decisions, repeat tasks, and even help me analyse data. ๐Ÿ‘€

That was when things started getting interesting.

๐Ÿง  Teaching Python to Make Decisions

I learned that comparison operators allow Python to ask questions:

score = 75

print(score >= 50)  # True
print(score < 50)   # False
Enter fullscreen mode Exit fullscreen mode

Then came if, elif, and else:

score = int(input("Enter your score: "))

if score >= 80:
    print("Grade A")
elif score >= 70:
    print("Grade B")
elif score >= 60:
    print("Grade C")
elif score >= 50:
    print("Grade D")
else:
    print("Grade F")
Enter fullscreen mode Exit fullscreen mode

Basically, I was teaching Python:

โ€œIf this happens, do this. Otherwise, try something else.โ€


๐Ÿ” Decisions Inside Decisions

I also learned about nested if statements.

For example, checking a login:

username = input("Username: ")

if username == "admin":
    password = input("Password: ")

    if password == "Kenya2025":
        print("Welcome Admin")
    else:
        print("Wrong Password")
else:
    print("User not found")
Enter fullscreen mode Exit fullscreen mode

One decision leads to another decision.

Suddenly, my code was starting to look like a little flowchart.


๐Ÿ” Then I Discovered Loops

If statements help Python decide.

Loops help Python repeat.

Instead of writing:

print("Good Morning Alice")
print("Good Morning Brian")
print("Good Morning Jane")
Enter fullscreen mode Exit fullscreen mode

I can simply write:

names = ["Alice", "Brian", "Jane"]

for name in names:
    print(f"Good Morning, {name}")
Enter fullscreen mode Exit fullscreen mode

One instruction. Multiple results.

That felt like unlocking a new level. ๐ŸŽฎ


๐Ÿ’ฐ My Accounting Brain Got Excited

One of my favourite exercises was adding M-Pesa transactions:

transactions = [1500, 3000, 800, 12000, 450, 2700]

total = 0

for amount in transactions:
    total += amount

print(f"Final total: Ksh {total}")
Enter fullscreen mode Exit fullscreen mode

Now Python can go through each transaction and calculate the total for me.

This is where I started seeing the connection between Python, finance and data analytics.


๐ŸŽฏ And Then I Built a Savings Calculator

I used a while loop to calculate how long it would take to reach a savings goal:

saving_goal = int(input("Savings goal: "))
monthly_savings = int(input("Monthly savings: "))

total = 0
months = 0

while total < saving_goal:
    total += monthly_savings
    months += 1

print(f"It took {months} months to reach Ksh {total}")
Enter fullscreen mode Exit fullscreen mode

For a goal of Ksh 50,000 and monthly savings of Ksh 8,000, Python tells me it takes 7 months, reaching Ksh 56,000.

And honestly, watching a few lines of code solve a real-world problem is becoming addictive. ๐Ÿ˜‚

๐Ÿš€ What I'm Learning

My Python journey is slowly changing from:

โ€œWhat does this code mean?โ€

to:

โ€œWhat problem can I solve with this?โ€

So far, I've learned:

if โ†’ make decisions
for โ†’ repeat through items
while โ†’ keep going until a condition changes
and / or โ†’ combine conditions
break โ†’ stop a loop
accumulators โ†’ keep running totals

I'm still very much a beginner, but I'm starting to understand something important:

Programming isn't just about writing code. It's about learning how to break a real-world problem into instructions a computer can understand.

And I'm enjoying the process, one bug at a time. ๐Ÿ๐Ÿ’ป

Top comments (0)