Your First Python Program: Hello, World!
Now that you’ve installed Python and set up your environment, it’s time to write your first real Python program. In this lesson, you’ll create a basic program and display a text message “Hello World!” on screen. This will help you understand how Python code is executed, and see how easy it is to get started with coding using the classic Hello, World! Example.
What You'll Learn
- How to write and run a Python program
- How the Python interpreter works
-
How to save and execute
.pyfiles -
Understanding the
print()function - Common beginner mistakes to avoid
Option 1: Writing Your First Python Program in Python Shell
The Python shell, also known as the interactive interpreter, allows you to execute Python code line by line. It’s an excellent way to test small pieces of code quickly.
Step 1: Open the Python Shell
Depending on your operating system, follow the appropriate method:
On Windows:
- Click the Start menu.
- Search for “Python”.
-
Open either:
- Python (command line) — terminal-based shell
- IDLE (Python 3.x GUI) — graphical interface
On macOS:
- Open the Terminal app.
- Type the following and press Enter:
python3
(Use python if python3 doesn’t work.)
On Linux:
- Open your Terminal.
- Type and run:
python3
(Or use python depending on your distribution.)
Step 2: Type Your Code
When the Python shell opens, you’ll see the prompt:
>>>
Type the following line:
print("Hello, World!")
Step 3: Execute the Code
Press Enter to run the command. You’ll see this output immediately:
Hello, World!
Explanation
-
print()is a built-in function that outputs text to the screen. -
The quoted text
"Hello, World!"is a string literal. - The interpreter immediately runs the command and prints the result.
This simple program demonstrates how to write and execute code in Python’s interactive shell — a great starting point for beginners!
Option II: Writing Your First Python Program in Code Editor
Step 1: Open Your Code Editor
Create a new file and save it as:
hello.py
Step 2: Write the Code
print("Hello, World!")
Running the Program
Run from Terminal / Command Prompt
- Open Terminal or Command Prompt
- Navigate to the folder where your hello.py file is saved.
- Run the file:
Windows:
python hello.py
macOS/Linux:
python3 hello.py
Output:
Hello, World!
How It Works
print("Hello, World!")
-
print()is a built-in Python function for displaying output - The text in quotes is a string
- The function call runs and shows the string in your terminal
You can enclose your text message either in single or double quotes. Both works.
print("Hello World")
print('Hello World')
Displaying Numbers With print() Function
You can also display numbers. In this case, you will enclose numbers in the print function's parenthesis ( ). You don't need to enclose numbers in single or double quotes.
print(10)
This will output 10.
Try another example. Adding and displaying two numbers using a print function.
print(10 + 10)
The output will be 20.
Printing Multiple Values with print()
The print() function in Python allows you to pass multiple values as separate arguments. By default, it inserts a space between each value, making it easy to format readable output without manual string concatenation.
name = "John"
age = 40
city = "New York"
print("Name:", name, "Age:", age, "City:", city)
# Output: Name: John Age: 40 City: New York
Common Mistakes
| Mistake | Explanation |
|---|---|
Print("Hello")
|
Python is case-sensitive. Use lowercase print
|
print(Hello)
|
Missing quotes around text |
print "Hello"
|
Missing parentheses – valid in Python 2, not Python 3 |
Best Practices
-
Save files with a
.pyextension -
Use clear file names like
hello.py - Use proper indentation (Python requires it)
Challenge Exercise
Modify your program to display your name or a custom message:
print("My name is Alex!")
Try printing multiple lines:
print("I am learning Python")
print("What are you doing?")
Mini Quiz
- What does the print() function do?
- How do you run a Python file using the terminal?
- What is the output of print("Welcome to Python!")?
Summary
- You wrote your first Python program: Hello, World!
- You learned about the print() function
- You ran a Python file from the terminal/command
- You practiced writing simple output
What’s Next?
In the next lesson, you’ll learn about Python Variables and Data Types — essential tools for storing and manipulating data in your programs.