Skip to main content
Python•Last Updated : 20 Oct 2026
~30 min read

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.

Definition: Interpreter

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.

Note

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:

  • print is 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.

PythonHello, world
1print("Hello, world!")
2print('Python is ready.')
Output
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:

  • sep is the text placed between values (a space by default).
  • end is 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.

Tip

`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.

PythonSeveral values, sep and end
1print("Total:", 3, "items")
2print("2026", "10", "20", sep="-")
3print("Loading", end="...")
4print("done")
5print()
6print("The line above is empty.")
Output
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".

Key Takeaways & Summary
  • •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.
PythonOrder matters
1count = 1
2print("count is", count)
3count = count + 1
4print("count is now", count)
Output
count is 1
count is now 2

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 count above count = count + 1 adds nothing; # the first page is numbered 1, not 0 explains a decision.

Best Practices (Do)
  • ✓Explain intent and decisions: why a value is 7, why a step is needed.
  • ✓Temporarily comment out a line with # while you investigate a problem.
  • ✓Keep comments up to date when you change the code.
Common Pitfalls (Don't)
  • ✕Repeat what the code obviously does.
  • ✕Leave large blocks of old, commented-out code in finished programs.
  • ✕Use comments to excuse unclear names; rename the variable instead.
PythonComments are ignored when the program runs
1# This whole line is a comment.
2print("Comments do not appear in the output") # neither does this one
3# print("This line is switched off")
Output
Comments do not appear in the output

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:

  1. 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.
  2. 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 calling input(): 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.

Caution

`"36" + 1` is an error, because Python will not add a number to text. Convert first: `int("36") + 1` is 37.

PythonThe greeting, with the input filled in
1name = "Ada" # in an interactive program: name = input("What is your name? ")
2age_text = "36" # input() would also give you text
3age = int(age_text)
4print("Nice to meet you,", name)
5print("Next year you will be", age + 1)
Output
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 called nme. It is a typo for name.
  • 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

ErrorTypical causeExample
SyntaxErrorThe code breaks Python's grammarprint("hi"
IndentationErrorInconsistent or missing indentationa block with no indented line under it
NameErrorA name that was never created, often a typoprint(nme)
TypeErrorAn operation on the wrong kind of value"36" + 1
ValueErrorRight kind of value, unusable contentint("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.

Tip

Configure your editor to insert four spaces when you press Tab. Mixing tabs and spaces is a classic cause of IndentationError.

PythonThe indented block belongs to the if
1temperature = 31
2if temperature > 30:
3 print("It is hot today.")
4 print("Drink some water.")
5print("This line always runs.")
Output
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.

Key Takeaways & Summary
  • •An expression produces a value; print shows it.
  • •`/` always gives a decimal result, even for 8 / 2.
  • •`**` is exponentiation and `+` also joins strings.
PythonExpressions and their values
1print(2 + 3 * 4)
2print((2 + 3) * 4)
3print(7 / 2)
4print(2 ** 10)
5print("Py" + "thon")
Output
14
20
3.5
1024
Python

Check your understanding

Suggested Quiz
Question 1 of 4

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.

[Problem]

Hello World & Input/Output

Reveal Solution & Analysis
Model Solution

Quick Syntax & Reference Sheet

Concept / RuleSyntax / Usage
Print textprint("Hello")
Print several valuesprint("Total:", 3)
Custom separator / endingprint(a, b, sep=", ", end="!\n")
Comment# explains why, not what
Read a line of textname = input("Name: ")
Read a whole numberage = int(input("Age: "))
Run a scriptpython my_program.py

Common Pitfalls & Developer Mistakes

✕ Mistake 1:

Writing print "hello" without parentheses

Why it happens: That is Python 2 syntax; in Python 3 print is a function and must be called.

Correction: print("hello")
✕ Mistake 2:

Doing arithmetic directly on input()

Why it happens: input() returns a string, so input() + 1 raises TypeError and input() * 2 repeats the text.

Correction: Convert first: int(input()) + 1.
✕ Mistake 3:

Mismatched or missing quotes

Why it happens: Python cannot tell where the text ends and reports a SyntaxError.

Correction: Open and close every string with the same kind of quote.
✕ Mistake 4:

Random indentation

Why it happens: Indentation defines blocks, so an unexpected indent is an error and a missing one changes the meaning.

Correction: Indent only after a line ending in a colon, by four spaces.

Frequently Asked Questions

Should I learn Python 2 or Python 3?

Python 3. Python 2 stopped receiving updates in 2020 and every current library targets Python 3.

Do I need to install anything to follow this course?

No. Lesson examples run in the browser and practice problems run in the platform IDE. Installing Python from python.org is useful once you start the course projects in the Workspace or on your own machine.

Why does indentation matter so much?

Python uses it instead of braces to decide which statements belong together. The upside is that well-formatted code and correct code look the same.

What is the difference between running a script and the interactive prompt?

A script runs a whole file from top to bottom and only shows what you print. The interactive prompt (>>>) evaluates one statement at a time and echoes the value of each expression, which is handy for experiments.

Hands-On Practice Problems

Practice Coding Challenges (1)Solve in Cloud IDE
Article Tags:#Python#print#input#Interpreter#First Program
Was this lesson clear and helpful?

Discussion (0)

Sign in to ask questions and reply.
Loading discussion…
Discuss in community