DEV Community

Cover image for Python Basics: The Fundamentals That Let You Build Something Real in Week One
Neema Kirui
Neema Kirui

Posted on

Python Basics: The Fundamentals That Let You Build Something Real in Week One

Introduction

Here's the truth nobody tells you when you start learning Python: you don't need to finish a course before you build something real. You need print(), a few variables, an understanding of what's actually being stored, a way to talk to the user, some basic syntax rules, and a little patience, and you can already write a program that does something real. This post walks through all of that, starting from the very first line you'll ever type.

READING TIP
Type every snippet yourself instead of copying it. Typing builds memory. Copying just builds a scrollbar.

1. Your First Python Program

Every Python journey starts the same way, with print():

print("hello world")
print("this is my first python program")
Enter fullscreen mode Exit fullscreen mode

print() displays whatever you put between the parentheses. The text inside the quotes is called a string. Python reads your file top to bottom, so the second line runs right after the first one finishes.

hello world
this is my first python program
Enter fullscreen mode Exit fullscreen mode

That's it. That's a real, complete Python program. It doesn't need to be more complicated than that to count.

IMPORTANT
A string has to open and close with the same kind of quote. "hello" is fine. "hello' (mixing a double quote with a single quote) raises an error. And print always needs its parentheses, even for something this small.

2. Variables: Giving a Value a Name

A variable is just a name pointing at a value. That's it, that's the whole concept, everything else is details.

balance = 200
name = "Amina"
is_member = True
Enter fullscreen mode Exit fullscreen mode

What makes Python forgiving here is that a variable isn't locked to one type forever. You can point balance at a number, then later point it at something else entirely:

balance = 200
balance = "two hundred"   # totally legal, if a little confusing
Enter fullscreen mode Exit fullscreen mode

That flexibility is nice, but it's also exactly how bugs sneak in. Which brings us to the rule that will save you more headaches than anything else in this post.

GOLDEN RULE
input() always returns a string, even if the person typed a number. total = "5" + 5 raises a TypeError. Always wrap input() in int() or float() before doing math with it. This one habit prevents dozens of crashes.

3. Data Types: What's Actually Inside the Name

Every value has a type, whether you think about it or not. The ones you'll reach for constantly:

Type Example What it's for
int 42 Whole numbers
float 3.14 Numbers with decimals
str "hello" Text
bool True, False Yes/no, on/off
list [1, 2, 3] An ordered, changeable collection

You can check what you're actually holding with type(), which is one of the first honestly useful debugging habits:

price = 200
print(type(price))       # <class 'int'>

price = 200.00
print(type(price))       # <class 'float'>
Enter fullscreen mode Exit fullscreen mode

GOOD TO KNOW
Dividing two whole numbers in Python always gives you a float back, whether you wanted one or not. 700 / 3 prints 233.33333333333334, not a clean 233. That's not a bug, it's just how division works here, formatting is a separate step.

4. Input and Output: Making Your Program Talk Back

A program that just prints the same thing every time gets boring fast. The moment it actually asks you something and reacts to your answer is the moment it starts feeling like a real program. Here's a small booking script that pulls together everything so far:

passenger_name = input("Enter passenger name: ")
route = input("Enter route: ")
fare = int(input("Enter fare (Ksh): "))

booking_fee = 10
total = fare + booking_fee

print()
print("=" * 40)
print("            TICKET")
print("=" * 40)
print(f"Passenger   : {passenger_name}")
print(f"Route       : {route}")
print(f"Fare        : Ksh {fare}")
print(f"Booking Fee : Ksh {booking_fee}")
print("-" * 40)
print(f"TOTAL       : Ksh {total}")
print("=" * 40)
Enter fullscreen mode Exit fullscreen mode

Three small ideas are doing all the work in that script: input() collects text, int() converts the fare so it can be added to booking_fee, and the f-strings (f"Fare : Ksh {fare}") drop each variable straight into the printed text without any clumsy string-joining. That "=" * 40 trick is a nice bonus, too, repeating a character to build a clean divider line for free.

MINI-CHALLENGE
Add a luggage fee of Ksh 50 to the total. Only two lines actually need to change, try it yourself before peeking.

booking_fee = 10
luggage_fee = 50
total = fare + booking_fee + luggage_fee

5. Basic Syntax: The Rules That Bite You Under Pressure

Python doesn't use curly braces to mark a block of code, it uses indentation. That means a misaligned line isn't just messy, it can change what your program actually does, or crash it outright.

age = 20

if age >= 18:
    print("You're an adult.")
else:
    print("You're a minor.")
Enter fullscreen mode Exit fullscreen mode

Everything indented under if only runs when the condition is true. Everything under else only runs when it isn't.

COMMON BUG
== compares two things. = assigns a value. Writing if age = 18 instead of if age == 18 is one of the most common mistakes beginners make, and Python will usually stop you with a clear error if you do it inside a condition, so don't panic if it happens to you.

Comments start with # and get skipped entirely when the program runs, handy for leaving yourself a note about why something is written a certain way:

# Convert Celsius to Fahrenheit
fahrenheit = (celsius * 9/5) + 32
Enter fullscreen mode Exit fullscreen mode

6. Putting It All Together: A Small Practical Example

None of this means much sitting alone, so here's everything from this post in one small program, a grocery total calculator that keeps running until you tell it you're done.

items = []
total = 0.0

print("Enter your grocery items. Type 'done' when finished.\n")

while True:
    name = input("Item name: ")
    if name.lower() == "done":
        break

    price = float(input(f"Price of {name}: Ksh "))
    quantity = int(input(f"Quantity of {name}: "))

    subtotal = price * quantity
    items.append((name, quantity, subtotal))
    total += subtotal

print("\n--- Receipt ---")
for name, quantity, subtotal in items:
    print(f"{name} x{quantity}: Ksh {subtotal:.2f}")

print(f"\nTotal: Ksh {total:.2f}")
Enter fullscreen mode Exit fullscreen mode

Every idea from this post shows up in those twenty lines. total and items are variables getting reassigned as the loop runs. str, float, int, and list all show up as the actual data types doing the work. input() and print() handle the whole back-and-forth with the user. float() and int() convert raw text into numbers you can actually do math with. And indentation is quietly deciding what belongs inside the loop and what runs after it.

WHY IT MATTERS
This isn't a toy example for its own sake. Swap "grocery items" for "bus tickets" or "airtime bundles" and you've got the skeleton of half the beginner projects out there, a loop that keeps taking input, a running total, and a receipt at the end.

You're Closer Than You Think

It's tempting to rush past print() and variables to get to the "real" programming, loops, functions, whatever the next shiny topic is. But almost every early beginner mistake traces back to one of these fundamentals being slightly off: forgetting that input() returns a string, dividing two integers and being surprised by a float, an indentation slip that quietly changed what a block of code actually did, a mismatched quote mark crashing the very first line you ever wrote.

None of that means you're bad at this. It means you're doing exactly what everyone does on the way to getting comfortable. And these basics aren't a phase you outgrow once you get to "real" programming, they're the building blocks everything else in Python is made of. Loops are just variables changing inside a block of indented code. Functions are just input and output wrapped up with a name. Every library and framework you'll eventually touch is still, underneath, print(), variables, and a few types of data, just arranged in more elaborate ways. Get genuinely comfortable with these now, and the rest of Python stops looking like new material and starts looking like the same five or six ideas wearing different clothes.

If you've followed along and typed the code yourself, from that first print("hello world") all the way to the grocery receipt, you already have everything you need to build something small and real this week. Go build it.

Top comments (0)