Site

Hello — Your First Python Program

Tutorial 1.0  •  Python / Learn

1.0 What This Teaches

This tutorial introduces the smallest useful Python program. It covers:

1.1 Your First Program

A Python program is just a plain text file with a .py extension. The simplest program is a single line:
# hello.py - first Python program.
print("Hello, World!")
There is no main function, no class, and no boilerplate. Python runs the file from top to bottom. The # starts a comment - the compiler ignores everything after it on that line. Python is interpreted, not compiled to a native binary. The Python interpreter reads and executes your source directly.

1.2 Running a Script

Open a terminal in the same directory as hello.py and run:
python hello.py
You will see:
Hello, World!
On systems where both Python 2 and Python 3 are installed, use python3 explicitly. On Windows with the Python Launcher, py hello.py also works.

1.3 The Interactive Interpreter

Running python with no arguments opens the REPL (Read-Eval-Print Loop). Type any Python expression and it evaluates immediately:
$ python
>>> print("Hello")
Hello
>>> 2 + 2
4
>>> "world".upper()
'WORLD'
>>> exit()
The REPL is excellent for experimentation. Use it to test small snippets before adding them to a script. Press Ctrl+D (Linux/macOS) or Ctrl+Z then Enter (Windows) to exit.

1.4 The print() Function

print() is a built-in function. It writes its arguments to standard output followed by a newline. You can pass multiple arguments - they are separated by a space by default:
print("Hello", "World")       # Hello World
print("Hello", "World", sep=", ")  # Hello, World
print("Hello", end="")        # no newline
print(" World")               # continues on same line

1.5 Reading Input

input() displays a prompt and returns whatever the user types as a string:
name = input("Enter your name: ")
print(f"Hello, {name}!")
Expected interaction:
Enter your name: Alice
Hello, Alice!
The f"..." prefix marks an f-string (formatted string literal). Expressions inside {} are evaluated and inserted into the output. input() always returns a string - convert with int() or float() if you need a number.

1.6 Example - All Together

# hello.py - greet the user with their name and a welcome message.
from datetime import date

name = input("Enter your name: ")
today = date.today()
print(f"Hello, {name}!")
print(f"Today is {today}.")
print("Welcome to Python.")
Expected output (with input "Alice"):
Enter your name: Alice
Hello, Alice!
Today is 2026-07-20.
Welcome to Python.

1.7 Exercise

Exercise Ask the user for their first and last name separately using two input() calls. Print a greeting using both names. Then print the total character count of the full name (first + space + last) using len().

1.8 Common Mistakes

Calling print without parentheses

In Python 2, print "Hello" was valid syntax. In Python 3, print is a function and requires parentheses: print("Hello"). Omitting them raises a SyntaxError.

Forgetting quotes around a string

print(Hello)   # NameError: name 'Hello' is not defined
print("Hello") # correct
Without quotes, Python treats Hello as a variable name. If no variable named Hello exists, it raises NameError.

Treating input() result as a number

age = input("Age: ")
next_year = age + 1   # TypeError: can only concatenate str to str
input() always returns a str. Convert to a number first: age = int(input("Age: ")).

1.9 Key Terms

TermMeaning
print()Built-in function that writes to stdout followed by a newline
input()Built-in function that displays a prompt and returns a line of stdin as str
scriptA .py file run from the terminal with the python command
interpreterThe program that reads and executes Python source code directly
REPLRead-Eval-Print Loop - the interactive Python prompt
f-stringString prefixed with f that evaluates {} expressions inline
commentText after # ignored by the interpreter; used for explanation