Introduction to Python
What Python is, how the interpreter runs a program from top to bottom, and how to write, run and read your first programs with print(), comments and input().
What Python is
A program is a list of instructions written precisely enough for a computer to follow. Python is a programming language designed to keep those instructions readable: it uses ordinary English words (if, for, while, return), very little punctuation, and indentation to show structure.
Guido van Rossum released the first version in 1991. Today the same language is used for automation scripts, web back ends, data analysis, scientific computing and machine learning, so what you learn here carries straight into whatever you build next.
Python is an interpreted language. You do not compile your program into a separate executable before running it; the Python interpreter reads your source file and carries out the statements one after another. That makes the cycle of write → run → read the result → fix very short, which is exactly what you want while learning.
The program (`python` or `python3`) that reads Python source code and executes it statement by statement. When this course says "Python does X", it means the interpreter does X.
The examples in this course run on Python 3.8 and every later version. Python 2 is a different, retired language; if a tutorial writes `print "hi"` without parentheses, it is Python 2.
Your first program
The traditional first program prints a greeting. In Python it is a single line, shown in the example below. Three things happen in it:
printis the name of a built-in function, a ready-made piece of behaviour that ships with Python.- The parentheses call the function.
"Hello, world!"is a string: text, written between quotes. Single quotes ('...') and double quotes ("...") mean the same thing; pick one style and stay consistent.
Run the example and compare the output with the code. Notice that the quotes are not printed: they only tell Python where the text starts and ends.
1print("Hello, world!")2print('Python is ready.')
Hello, world! Python is ready.
Everything about print()
print() accepts any number of values separated by commas. It converts each one to text, puts a space between them and finishes the line. Two optional settings change that behaviour:
sepis the text placed between values (a space by default).endis the text written after the last value (a newline,"\n", by default).
Calling print() with nothing inside prints an empty line. Numbers do not need quotes: Python prints their value.
`print` is for showing results to a person. Later you will write functions that `return` values instead of printing them, so other code can use the result.
1print("Total:", 3, "items")2print("2026", "10", "20", sep="-")3print("Loading", end="...")4print("done")5print()6print("The line above is empty.")
Total: 3 items 2026-10-20 Loading...done The line above is empty.
A program runs from top to bottom
The interpreter executes statements in the order they appear, one line at a time. Each line finishes before the next one starts. This sounds obvious, but most beginner bugs come from forgetting it: using a value before the line that creates it, or expecting a later line to change something that was already printed.
In the example, the second print shows the current value of count, and the value only changes when the line that changes it runs. You will learn about variables properly in the next lesson; for now, read count = 1 as "store 1 under the name count".
- •Statements run in order, top to bottom, one at a time.
- •A line can only use names that earlier lines have already created.
- •Output appears in the order the print calls run.
1count = 12print("count is", count)3count = count + 14print("count is now", count)
count is 1 count is now 2
Reading input from the keyboard
input() pauses the program, shows an optional prompt, waits for the user to type a line and press Enter, and gives back what they typed:
name = input("What is your name? ")
print("Nice to meet you,", name)
Two facts about input() matter from day one:
- It always returns a string. If the user types
42, you get the text"42", not the number 42. To calculate with it you must convert it:age = int(input("Age: ")). The lesson on variables and types covers conversion in detail. - It waits for a person. Programs that use
input()need someone at the keyboard. That is why lesson examples on this page set values directly, and why practice problems give your function its input as arguments instead of callinginput(): the judge supplies the values automatically.
The example below shows the same greeting with the typed text written into the program, so you can run it here.
`"36" + 1` is an error, because Python will not add a number to text. Convert first: `int("36") + 1` is 37.
1name = "Ada" # in an interactive program: name = input("What is your name? ")2age_text = "36" # input() would also give you text3age = int(age_text)4print("Nice to meet you,", name)5print("Next year you will be", age + 1)
Nice to meet you, Ada Next year you will be 37
Reading an error message
Every programmer sees error messages all day; the skill is reading them calmly. When something goes wrong, Python stops and prints a traceback. Read it from the bottom up:
Traceback (most recent call last):
File "greet.py", line 2, in <module>
print("Hello, " + nme)
NameError: name 'nme' is not defined
- The last line names the kind of error (
NameError) and explains it: there is no variable callednme. It is a typo forname. - The lines above it point to the file and line number where it happened, and show that line.
There are two broad families. A syntax error means the code is not valid Python, for example a missing closing parenthesis, so nothing runs at all. A runtime error (also called an exception) happens while the program is running, at the line that fails; the lines before it have already run. The lesson on errors and exceptions shows how to handle runtime errors instead of crashing.
Errors you will meet in your first week
| Error | Typical cause | Example |
|---|---|---|
| SyntaxError | The code breaks Python's grammar | print("hi" |
| IndentationError | Inconsistent or missing indentation | a block with no indented line under it |
| NameError | A name that was never created, often a typo | print(nme) |
| TypeError | An operation on the wrong kind of value | "36" + 1 |
| ValueError | Right kind of value, unusable content | int("forty") |
Indentation is part of the language
Many languages use braces { } to group statements. Python uses indentation: the lines that belong to an if, a loop or a function are indented under it, conventionally by four spaces. A colon : at the end of a line announces that an indented block follows.
Because indentation carries meaning, it is not decoration. Moving a line in or out changes what the program does. The example prints different output depending only on whether the last line is indented.
Configure your editor to insert four spaces when you press Tab. Mixing tabs and spaces is a classic cause of IndentationError.
1temperature = 312if temperature > 30:3 print("It is hot today.")4 print("Drink some water.")5print("This line always runs.")
It is hot today. Drink some water. This line always runs.
Python as a calculator
Python evaluates expressions, combinations of values and operators that produce a new value, and print shows the result. The usual rules of arithmetic apply: multiplication before addition, parentheses first.
Python also has an interactive mode (run python with no file) where each expression you type is evaluated immediately and its value is shown after a >>> prompt. It is ideal for quick experiments. In a script, nothing is shown unless you print it.
- •An expression produces a value; print shows it.
- •`/` always gives a decimal result, even for 8 / 2.
- •`**` is exponentiation and `+` also joins strings.
1print(2 + 3 * 4)2print((2 + 3) * 4)3print(7 / 2)4print(2 ** 10)5print("Py" + "thon")
14 20 3.5 1024 Python
Check your understanding
What do `print("A", "B", sep="")` and then `print("C")` print?
Practice
Write the logic of a first interactive program: greet the user and work out the year they were born, handling an age that is not a number. The problem gives you the typed text as arguments, exactly as input() would.
Hello World & Input/Output
Reveal Solution & AnalysisHide Solution
Quick Syntax & Reference Sheet
| Concept / Rule | Syntax / Usage |
|---|---|
| Print text | print("Hello") |
| Print several values | print("Total:", 3) |
| Custom separator / ending | print(a, b, sep=", ", end="!\n") |
| Comment | # explains why, not what |
| Read a line of text | name = input("Name: ") |
| Read a whole number | age = int(input("Age: ")) |
| Run a script | python my_program.py |
Common Pitfalls & Developer Mistakes
Writing print "hello" without parentheses
Why it happens: That is Python 2 syntax; in Python 3 print is a function and must be called.
Doing arithmetic directly on input()
Why it happens: input() returns a string, so input() + 1 raises TypeError and input() * 2 repeats the text.
Mismatched or missing quotes
Why it happens: Python cannot tell where the text ends and reports a SyntaxError.
Random indentation
Why it happens: Indentation defines blocks, so an unexpected indent is an error and a missing one changes the meaning.
Comments: notes for people
A
#starts a comment. Python ignores everything from the#to the end of the line, so comments are notes for the people who read the code, including you in three weeks.Good comments explain why the code does something, not what it does, because the code itself already says what.
# add 1 to countabovecount = count + 1adds nothing;# the first page is numbered 1, not 0explains a decision.