Skip to main content

Python for Beginners: Your First Program in 5 Minutes

Beginner20 min4 exercises60 XP
0/4 exercises

Have you ever wondered how apps like Instagram, Spotify, or YouTube actually work? Behind every app, website, and game, there's code — instructions written by a person telling the computer what to do.

Python is one of the most popular languages for writing those instructions. It reads almost like plain English, so you can focus on solving problems instead of fighting confusing syntax.

In this tutorial, you'll write your first Python program, learn how Python handles text and numbers, and start using variables to store information. Every code example on this page is live — you can click Run, change things, and see what happens instantly. No setup needed.

How to Write Your First Python Program

There's a fun tradition in programming: the very first thing you do in any new language is make it say "Hello, World!" It's a quick handshake between you and the language — proof that everything works.

In some languages, this takes 5 or more lines of code. In Python, it takes just one:

Your first Python program
Loading editor...

Click the Run button above. You should see Hello, World! appear below the code. That's it — you just ran a Python program!

Let's break down what just happened:

  • `print()` is a built-in function. Think of it like a machine — you put something in, and it does something with it. This one displays text on screen.
  • The parentheses () are where you pass in data. Programmers call this the "argument."
  • `"Hello, World!"` is a string — just a fancy word for text. The quotation marks tell Python: "this is text, not code."
  • Try changing the message inside the quotes and running it again. Try printing your own name. Try adding a second print() on a new line:

    Printing multiple lines
    Loading editor...

    Each print() shows up on its own line. Python reads your code from top to bottom, one line at a time, like reading a book.

    The order you write things is the order they run. This is one of the most important ideas in programming.

    What Are Comments in Python?

    A comment is a note you write inside your code that Python completely ignores. You create one by starting a line with the # symbol.

    Why would you want Python to ignore something? Because comments are for humans, not computers. They help you (and anyone else reading your code) understand what's going on.

    How comments work in Python
    Loading editor...

    Think of comments like sticky notes on a recipe. The oven doesn't read them, but they help you remember why you added extra salt or what temperature worked best.

    Python Strings vs Numbers: What's the Difference?

    Everything in Python has a type. A type is like a label that tells Python what kind of data something is and what you can do with it.

    The two most basic types are strings (text) and numbers. Understanding the difference between them will save you from a lot of confusing errors.

    How to Create Strings in Python

    A string is any text wrapped in quotation marks. You can use single quotes ('hello') or double quotes ("hello") — they work exactly the same way.

    Creating strings
    Loading editor...

    Strings can hold anything — letters, numbers, spaces, punctuation, even emoji. As long as it's inside quotes, Python treats it as text.

    Python Number Types: Integers and Floats

    Numbers in Python come in two flavors:

  • Integers (`int`) — whole numbers like 42, -7, or 1000000. No decimal point.
  • Floats (`float`) — decimal numbers like 3.14, -0.5, or 100.0. They always have a decimal point.
  • Integer and float examples
    Loading editor...

    Why Does the Type of Data Matter in Python?

    Here's something that trips up almost every beginner. "42" (with quotes) and 42 (without quotes) are completely different things to Python.

    One is text. The other is a number. They look the same when printed, but they behave very differently:

    The difference between "42" and 42
    Loading editor...

    "42" + "8" gives "428" because + between strings means "stick them together." This is called concatenation.

    42 + 8 gives 50 because + between numbers means "add them." Same operator, completely different behavior depending on the type.

    The type() function tells you exactly what kind of data you're working with. When something isn't behaving the way you expect, checking the type is usually the first thing to try.

    How to Do Math in Python

    Python works great as a calculator. It handles numbers of any size without breaking a sweat — unlike some other languages that choke on big numbers.

    Here are all the math operators you'll use:

    Python math operators
    Loading editor...

    Most of these work like regular math class. But a few might be new to you.

    `/` (division) always gives you a decimal result, even when it divides evenly. 10 / 2 gives 5.0, not 5. This catches a lot of people off guard.

    `//` (floor division) divides and then rounds down to the nearest whole number. Think of it as "division, but throw away everything after the decimal point."

    `%` (modulo) gives you the remainder after dividing. For example, 15 ÷ 4 = 3 with a remainder of 3. This is surprisingly useful — you can check if a number is even (n % 2 == 0), make values wrap around (like hours on a clock: 13 % 12 = 1), and much more.

    ` (exponent)** raises a number to a power. 2 ** 10` means "2 multiplied by itself 10 times," which gives 1024.

    Python follows the standard math order of operations (PEMDAS). You can use parentheses to change the order:

    Order of operations and parentheses
    Loading editor...
    ❌ What beginners expect
    # "Division gives a whole number, right?"
    result = 10 / 3
    print(result)  # Expects: 3
    ✓ What Python actually does
    # / always gives a decimal (float)
    result = 10 / 3
    print(result)  # 3.3333333333333335
    
    # Use // to get a whole number
    result = 10 // 3
    print(result)  # 3

    What Are Variables in Python and How Do You Use Them?

    So far, we've been working with values directly — print(42), print(2 + 3). That works for quick calculations, but real programs need to remember values and use them later.

    That's what variables are for. Think of a variable as a labeled container. You give it a name, put a value inside, and then use that name whenever you need the value.

    Creating and using variables
    Loading editor...

    name = "Alice" creates a variable called name and stores the string "Alice" in it. The = sign is the assignment operator — it doesn't mean "equals" like in math. It means "store this value in this name."

    Read name = "Alice" as: "name gets Alice." This distinction matters and will save you confusion later.

    Notice there's no let, var, or const keyword. In JavaScript you'd write let name = "Alice". In Python, you just write name = "Alice". Python also doesn't need type declarations — it figures out the type from the value automatically.

    Variables can change. You can give them a new value at any time:

    Updating variables
    Loading editor...

    The line score = score + 5 looks weird if you think of = as "equals." But remember, = means "assign."

    Read it as: "take the current value of score (10), add 5 to get 15, and store that result back in score." This pattern — updating a variable based on its current value — shows up everywhere in programming.

    How to Use Python f-strings (String Formatting)

    In almost every program, you'll need to mix variables into text. Showing a welcome message with someone's name, displaying a score, printing a receipt — these all require combining data with text.

    Python has a wonderful tool for this called f-strings (the "f" stands for "formatted"). Put the letter f before the opening quote, then put any variable or expression inside {curly braces}:

    f-strings: mixing variables into text
    Loading editor...

    The magic is in the {curly braces}. Python evaluates whatever expression is inside, converts the result to text, and plugs it into the string.

    You can put almost anything inside the braces — variables, math, function calls, even comparisons. Let's see a more practical example:

    Practical f-string example
    Loading editor...

    Compare that to the old way of building strings — using + to stitch pieces together:

    ❌ Without f-strings (messy)
    name = "Alice"
    age = 15
    
    # Have to convert age, lots of + signs
    print("Hi, my name is " + name + "!")
    print("I am " + str(age) + " years old.")
    ✓ With f-strings (clean)
    name = "Alice"
    age = 15
    
    # Just put variables inside {braces}
    print(f"Hi, my name is {name}!")
    print(f"I am {age} years old.")

    The old way requires str(age) to manually convert the number to text. Remember — you can't use + to join a string and a number directly.

    f-strings handle that conversion automatically. They're cleaner, faster, and less error-prone. Always use them for new code.

    Putting It All Together: A Mini Project

    Let's combine everything into one small program. This shows the pattern you'll use in every Python project: store data in variables, do calculations, and display the results.

    Mini project: tip calculator
    Loading editor...

    Don't worry about the :.2f part — that just means "show exactly 2 decimal places" (so 4.5 displays as 4.50). We'll cover formatting in detail later.

    The important thing is the pattern: store data → do calculations → display results. That's the core of every program, whether it's 10 lines or 10,000.

    Try changing the meal_cost, tax_rate, or tip_rate and running it again!


    Practice Exercises

    Now it's your turn! These exercises go in order from easiest to hardest, and each one uses what you've learned.

    A few tips before you start:

  • Try first, peek later. Struggling a bit is how your brain actually learns.
  • Read error messages. They look scary but usually tell you exactly what went wrong.
  • Use hints sparingly. Each exercise has hints that reveal one at a time.
  • Experiment after solving. Once you get the green checkmark, try changing things to see what happens.
  • Exercise 1: Print a Greeting
    Write Code

    Write a program that prints exactly:

    Hello, Python!

    The test checks for an exact match — including the comma, space, and exclamation mark.

    Loading editor...
    Exercise 2: Restaurant Bill
    Write Code

    A meal costs $24.50, tax is $2.45, and the tip is $5.00. The variables are already created for you.

    Calculate the total and print it in this exact format:

    Total: $31.95
    Loading editor...
    Exercise 3: Personal Introduction
    Write Code

    Create two variables:

  • name set to "Alice"
  • language set to "Python"
  • Then use an f-string to print:

    Hi, I'm Alice and I love Python!
    Loading editor...
    Exercise 4: Fix the Bug — Broken Calculator
    Fix the Bug

    This code should calculate the area of a rectangle (width × height) and print it. But it has two bugs!

    Try running the broken code first and read the error message. Then fix both bugs so the output is:

    Area: 35
    Loading editor...

    Summary: What You Learned in This Tutorial

    Nice work getting through your first Python tutorial! Here's everything you now know:
    ---------
    print()Displays output on screenprint("hello")
    Comments (#)Notes that Python ignores# this is a note
    StringsText data in quotes"hello" or 'hello'
    IntegersWhole numbers42, -7
    FloatsDecimal numbers3.14, 0.5
    Math operators+ - * / // % **2 ** 101024
    type()Tells you what type data istype(42)int
    VariablesNamed containers for dataage = 15
    f-stringsEmbed variables in textf"Age: {age}"

    These aren't just beginner concepts you'll outgrow. They're the foundation of every Python program. Even the most advanced code is creative combinations of these building blocks.

    What's Next?

    In the next tutorial — [Variables and Data Types](/python/variables-and-data-types) — you'll go deeper into how Python stores data. You'll learn about booleans (True/False), None, and type conversion.

    You'll also discover why 0.1 + 0.2 doesn't equal 0.3 in Python (or any programming language) — and what to do about it.