Your First Python Program: Write, Run, and Understand It

Your first Python program can be as small as one line: print(). That one line teaches you the basic cycle of writing code, running it, and seeing output in the terminal. This guide shows you how to make that first program, what each part means, and how to avoid the beginner mistakes that stop it from running.

Difficulty: Beginner

Helpful to know first: You should know how to open a text editor and a terminal, and it helps to understand that a program is just a file of instructions.

How to Write and Run Your First Python Program

The shortest useful Python program prints a message to the screen. Save the file, run it with python3, and Python reads the file line by line from top to bottom.

Here is the smallest complete program:

Python
print("Hello, world!") # Output: Hello, world!

That single line does three things. print() is a built-in function, "Hello, world!" is a string literal, and the parentheses tell Python to call the function with that text.

To run it, put the code in a file named hello.py and use:

Terminal
python3 hello.py

If everything is set up correctly, the terminal shows the text from print(). That is the first full loop of programming: write code, run code, see output.

Building Your First Program Line by Line

A first program is easier to understand when you add one idea at a time. The next examples keep the same small structure, then introduce new parts of Python.

Printing text and numbers

You can print words, numbers, or both together. Python converts values to text for you when needed.

Python
print("Python") # Output: Python
print(42) # Output: 42
print("The answer is", 42) # Output: The answer is 42

The comma inside print() separates values. Python prints them with a space between them by default. That is often easier than building one long string by hand.

Using variables for names and values

A variable stores a value so you can reuse it later. This is one of the first ideas that makes code feel useful instead of just decorative.

Python
name = "Ava"
print("Hello,", name) # Output: Hello, Ava

The first line stores the text "Ava" in name. The second line uses that stored value. If you change name, the next print() call uses the new value.

Adding comments to explain code

Comments are notes for humans. Python ignores them when the program runs.

Terminal
# This is a comment
print("Comments do not print") # Output: Comments do not print

Comments are useful in a first program because they help you remember what each line does. They are also useful when you come back to your code later.

Combining user-friendly output

Most real programs print more than one line. Use separate print() calls when you want a readable sequence.

Python
print("Welcome to Python")
print("This is your first program.") # Output:
# Welcome to Python
# This is your first program.

This style is simple, clear, and easy to debug. If one line is wrong, you can find it quickly.

How Python Runs a File from Top to Bottom

Python executes your program in order. It starts at the first line, then moves to the next line, and so on. That means the order of your statements matters.

For example, this works because the variable exists before it is used:

Python
greeting = "Hello"
print(greeting) # Output: Hello

If you switch the order, Python cannot use a name that has not been created yet. This is a very common beginner mistake and usually leads to a NameError.

Python also cares about indentation inside blocks. You will see that more clearly when you learn if statements and loops, but even now it helps to know that spaces are part of Python’s syntax.

What Happens When You Make a Small Change

Your first program becomes more interesting when you change a value and run it again. This is how you learn that code is not just text: it produces different results when the instructions change.

Here is a small example with a variable that changes:

Python
score = 10
print(score)

score = 12
print(score) # Output:
# 10
# 12

Python does not treat score as a fixed box. It treats it as a name that points to a value. When you assign a new value, the name points to the new value.

A Python file is sometimes called a script when you run it directly, but it is still just a normal Python program.

How Your First Program Differs from Other Beginner Languages

If you have seen other languages, Python may look simpler because it does not require semicolons at the end of each line. It also uses indentation to show blocks instead of braces.

Language styleHow the first program looksWhat stands out
Pythonprint("Hello, world!")Readable syntax and very little ceremony
C-style languagesprintf("Hello, world!\n");More punctuation and setup
Shell scriptsecho "Hello, world!"Good for commands, not general-purpose programming

For your first Python program, the goal is not to learn everything at once. The goal is to understand one simple path from source code to output.

Common Mistakes When Writing Your First Python Program

Forgetting the parentheses on print

New Python learners often write print "Hello" because they have seen older examples or examples from other languages. Python 3 requires parentheses.

Problem: Python treats print as a function in Python 3, so leaving out the parentheses causes a syntax error.

Python
print "Hello, world!"

Fix: Call the function with parentheses.

Python
print("Hello, world!")

The corrected version works because Python can now parse it as a function call.

Typing the wrong file name at the command line

If the file name in the terminal does not match the file you saved, Python cannot open it. This is especially common when the file is in a different folder than the one you are using.

Problem: The command points to a file that does not exist in the current directory, so Python cannot start your program.

Terminal
# You saved hello.py, but you typed:
python3 hell0.py

Fix: Use the exact file name and make sure you are in the right folder.

Terminal
python3 hello.py

The fixed command works because it points to the actual file.

Using a variable before assigning it

A variable must be given a value before you can print it. If you try to use it too early, Python raises a NameError.

Problem: The name message does not exist yet when Python reaches the print() call.

Python
print(message)
message = "Hello"

Fix: Assign the value before using the name.

Python
message = "Hello"
print(message)

The corrected version works because message exists before Python tries to print it.

Mixing up spaces and tabs in indentation

Even in a first Python program, you may eventually add an indented block. Python is strict about indentation, and inconsistent spacing can stop the program.

Problem: The lines inside the block use different indentation styles, which can lead to an IndentationError.

Python
if 3 > 2:
    print("True")
	print("Still inside?")

Fix: Use one indentation style consistently, and prefer 4 spaces.

Python
if 3 > 2:
    print("True")
    print("Still inside?")

The corrected version works because Python can clearly see which lines belong to the block.

Predict the Output

These short examples test mistakes that often confuse first-time Python programmers. Read the code carefully before you look at the answer.

Predict the output of simple reassignment

Python
value = 5
value = value + 2
print(value)

What does this print?

Show answer

It prints 7.

Python first stores 5 in value. The second line reads the current value, adds 2, and stores the result back in the same name. The final print() shows the new value.

Terminal
# Output: 7

Predict the output of a changed string

Python
name = "Mina"
print("Hello,", name)
name = "Leo"
print("Hello,", name)

What happens?

Show answer

It prints two lines:

Terminal
# Output:
# Hello, Mina
# Hello, Leo

The first print() uses the first value stored in name. After reassignment, the second print() uses the new value.

Find the Bug

This code looks like a normal first program, but it has a subtle mistake that prevents it from working the way the author expects. Find the bug before opening the answer.

Python
name = "Sam"
print("Hello" name)
Show answer

The bug is that the second line is missing a comma or another way to join the text and the variable. Python cannot place two values next to each other inside a function call like that.

Fix it by separating the arguments with a comma:

Python
name = "Sam"
print("Hello", name) # Output: Hello Sam

The fixed version works because print() now receives two valid arguments.

Try It Yourself

Make a tiny program that introduces you to Python output and variables.

  • Create a file named my_first_program.py.
  • Store your name in a variable.
  • Store your favorite number in another variable.
  • Print a sentence that includes both values.
  • Run the file with python3.

Expected output: The program should print one sentence that includes your name and number. The exact text depends on the values you choose.

Hint: Put the text in quotes, keep the number unquoted, and use print() with commas or string formatting.

Show solution
Python
name = "Ava"
favorite_number = 7

print("My name is", name, "and my favorite number is", favorite_number) # Output: My name is Ava and my favorite number is 7

Summary

  • Your first Python program can be as small as a single print() call.
  • Python runs code from top to bottom, so order matters.
  • Variables store values that you can reuse later.
  • Comments help explain code, but Python ignores them at runtime.
  • Use python3 to run the file and check the output in the terminal.
  • Most beginner errors come from small syntax mistakes, wrong file names, or using names before assignment.

Next, learn about Python variables and data types so you can store text, numbers, and true/false values with confidence.