Table of Contents
Writing and Running a Tiny Python Program
You’re going to create a real Python program, step by step. It will be very small, but it’s a complete program that the computer can run and that you can change and experiment with.
In this chapter you will:
- Write a tiny Python program
- Run it in two ways
- Make simple changes to see what happens
What a “first program” usually does
The classic first program in many languages is one that prints a short message on the screen. In Python that message is often:
Hello, world!Your program will do exactly that: make the computer show a message you choose.
The `print()` function
To show text on the screen in Python, you use the built-in print() function.
Basic pattern:
- The word
print - Followed by parentheses
(and) - Inside the parentheses, your message in quotes
Example:
print("Hello, world!")
Key points specific to print() in this chapter:
- The quotes must match: either both
"or both' - The parentheses must match: one opening
(and one closing) - Text you want to show is called a string (you’ll learn more about that later)
Running Python without worrying about setup (for now)
This course will later show you how to install Python on your computer and use different tools. For your very first program, you can:
- Use an online Python interpreter in your browser, or
- Use any environment your teacher or course platform provides
The important thing right now is seeing Python run, not how it was installed.
Your first program: step by step (interactive way)
- Open a Python interpreter (for example, an online one).
- Find the place where you can type code (often labeled “Console” or “Shell”).
- Type this exactly:
print("Hello, world!")- Press Enter.
You should see something like:
Hello, world!You have now written and run a Python program.
Common first-time mistakes
When typing your first program, small typing mistakes are very common. Here are a few and how to fix them:
- Missing quotes:
print(Hello, world!)This will cause an error. Fix by adding quotes:
print("Hello, world!")- Only one quote:
print("Hello, world!)Fix by closing the quote:
print("Hello, world!")- Missing parenthesis:
print "Hello, world!"In modern Python, this is incorrect. Fix:
print("Hello, world!")Changing the message
Now make the program your own. Change the text inside the quotes.
For example:
print("Hello, Python!")
print("My name is Alex.")
print("I am learning to code.")
Each print() call shows one line. When you run this, you’ll see:
Hello, Python!
My name is Alex.
I am learning to code.Try different variations:
- Your name
- Your favorite food
- A short sentence or joke
First tiny “experiments” to build intuition
Try these small experiments, one at a time, and see what happens.
- No space after the comma:
print("Hello,world!")- Extra spaces:
print(" Hello, world! ")- Empty message:
print("")- Numbers inside quotes:
print("12345")Observe:
- Python prints exactly what’s inside the quotes
- Spaces matter
- An empty string prints a blank line
A tiny “program” with more than one line
A program can have more than one instruction. For example:
print("Welcome to my first program!")
print("This is line 2.")
print("This is line 3.")All three lines together are already a program. When run, Python executes them from top to bottom.
Saving your first script (concept only)
Later chapters will show you in detail how to:
- Create a file with a name like
hello.py - Put your code inside that file
- Run the file as a script
For now, just understand that:
- A Python script is a file that contains Python code
- The code you typed into an interpreter can also live in a
.pyfile
Example content of hello.py:
print("Hello from a file!")You’ll learn how to actually create and run this file in the “Setting Up Python” chapter.
Small practice ideas
Try these in your Python environment:
- Write a program that prints your name on one line, and your favorite color on the next.
- Write a program that prints a short two-line poem or quote.
- Write a program that prints:
I am learning Python.
This is fun!
using two print() statements.
These are all real Python programs, just very small ones. From here, you’ll start learning how to make them do more.