Your First Program
Write and run your first Python program. Learn how code executes and how to see output.
Prerequisites
Let's write your first program. By tradition, programmers start by writing a program that displays "Hello, World!" on the screen. It's simple, but it proves everything is working.
The print() Function
To display output in Python, we use print():
print("Hello, World!")Run this and you'll see:
Hello, World!Let's break down what happened:
- print is a built-in function—a command that Python already knows
- The parentheses () contain what we want to print
- The quotes "" mark the beginning and end of text (called a "string")
- Python executed the instruction and displayed the result
Multiple Lines
Programs run from top to bottom. Each line is an instruction:
print("Line 1")
print("Line 2")
print("Line 3")Output:
Line 1
Line 2
Line 3Printing Numbers
You can print numbers without quotes:
print(42)
print(3.14159)
print(2 + 2)Output:
42
3.14159
4Notice that print(2 + 2) displayed 4, not "2 + 2". Python calculated the result first, then printed it.
Combining Text and Numbers
Use commas to print multiple things on one line:
print("The answer is", 42)
print("2 + 2 =", 2 + 2)Output:
The answer is 42
2 + 2 = 4Comments
Lines starting with # are comments. Python ignores them completely—they're notes for humans:
# This is a comment - Python ignores it
print("This runs") # Comments can go at the end of lines too
# Use comments to explain WHY, not what
# Bad: Add 1 to x
# Good: Account for zero-indexingPractice
Try these exercises:
- Print your name
- Print your age
- Print "My name is [name] and I am [age] years old" (use commas)
- Print the result of 100 * 365 (days in 100 years)
- Add comments explaining what each line does