Hello World
Welcome to Python! In this first lesson, you'll write your very first Python program. Unlike many other languages, Python doesn't require complex setup - you can start coding immediately with just a single line of code.
The print() Function
The print() function is Python's way of displaying output to the screen. It's the most
basic and most-used function in Python - you'll use it constantly for debugging and displaying results.
Click Run to execute your code
main() function just to print something. This simplicity is one of Python's
greatest strengths for beginners and experts alike.
Printing Different Values
The print() function can output much more than just text. It can display numbers,
the results of calculations, and multiple values at once.
Click Run to execute your code
Comments
Comments are notes you leave in your code for yourself or other programmers. Python ignores comments completely - they're just for humans to read.
| Comment Type | Syntax | Use Case |
|---|---|---|
| Single-line | # comment here |
Short notes, inline explanations |
| Multi-line | """ or ''' |
Longer explanations, documentation |
Click Run to execute your code
x = x + 1 # add 1 to x is a bad comment because it's obvious.
Python Syntax Basics
Python has a clean, readable syntax that sets it apart from other languages. Here are the key things to know:
Click Run to execute your code
{},
Python uses indentation (spaces) to define code blocks. Standard practice is 4 spaces per level.
We'll explore this more in the Control Flow lessons.
Common Mistakes
1. Forgetting quotes around strings
# Wrong - Python thinks Hello is a variable
print(Hello World) # NameError!
# Correct - strings need quotes
print("Hello World")
print('Hello World') # Single quotes work too
2. Mismatched quotes
# Wrong - started with " but ended with '
print("Hello World') # SyntaxError!
# Correct - matching quotes
print("Hello World")
print('Hello World')
3. Using Print instead of print
# Wrong - Python is case-sensitive!
Print("Hello") # NameError: name 'Print' is not defined
# Correct - lowercase
print("Hello")
4. Forgetting parentheses (Python 3)
# Wrong - this was valid in Python 2, not Python 3
print "Hello" # SyntaxError!
# Correct - print is a function in Python 3
print("Hello")
Exercise: Your First Program
Task: Create a program that introduces yourself!
Requirements:
- Print your name
- Print your favorite programming language
- Print a calculation (like your birth year)
- Include at least one comment
Click Run to execute your code
Show Solution
# My first Python program!
# Print my name
print("Hello, my name is Alex!")
# Print my favorite language
print("My favorite language is Python")
# Print a calculation - my birth year
current_year = 2024
age = 25
print("I was born in", current_year - age)
# Print multiple things
print("I started coding", age - 15, "years ago")
Summary
- print(): Displays output to the screen - the most basic Python function
- Strings: Text must be wrapped in quotes (
"text"or'text') - Comments: Use
#for notes that Python ignores - Case-sensitive:
printandPrintare different! - No boilerplate: Python doesn't need classes or main functions for simple programs
- Indentation: Python uses spaces (not braces) to define code blocks
What's Next?
Congratulations on writing your first Python program! In the next lesson, we'll explore variables - how to store and work with data in Python. You'll learn about different data types and how to give meaningful names to your values.
Enjoying these tutorials?