Web Analytics

Hello World

Beginner ~15 min read

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.

Output
Click Run to execute your code
Why Python? Unlike Java or C++, Python doesn't require you to define a class or a 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.

Output
Click Run to execute your code
Pro Tip: When printing multiple items with commas, Python automatically adds a space between them. Use this to your advantage for readable output!

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
Output
Click Run to execute your code
Caution: Don't over-comment! Good code should be self-explanatory. Comments should explain why you're doing something, not what you're doing. 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:

Output
Click Run to execute your code
Indentation Matters! Unlike most languages that use braces {}, 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
Output
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: print and Print are 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.