W3docs

Python Get Started

Step-by-step guide to installing Python, running your first program, and learning the core building blocks: variables, operators, control flow, and functions.

This page walks you through everything you need to go from zero to a working Python program. You will install Python, run it interactively, write a script, and explore the five core building blocks every Python program relies on: variables and data types, operators, control structures, and functions.

If you have not read the language overview yet, start with Python Intro.

Installing Python

Python is free and open-source. Download the installer for your operating system from python.org.

OSWhat to do
WindowsRun the .exe installer. On the first screen, check "Add Python to PATH" before clicking Install.
macOSDownload the .pkg installer, or install via Homebrew: brew install python.
LinuxMost distributions ship Python 3. Check with python3 --version; install via your package manager if needed (e.g. sudo apt install python3).

Verify the installation

Open a terminal (Command Prompt on Windows, Terminal on macOS/Linux) and run:

python3 --version

You should see output like Python 3.12.0. On Windows the command may be python instead of python3.

The interactive shell (REPL)

Python ships with an interactive shell — a Read-Eval-Print Loop (REPL) — that lets you type expressions and see results immediately. It is perfect for experimenting:

$ python3
Python 3.12.0 (...)
>>> 2 + 3
5
>>> 10 / 3
3.3333333333333335
>>> type(42)
<class 'int'>
>>> type('hello')
<class 'str'>
>>> exit()

Type exit() or press Ctrl+D (Ctrl+Z then Enter on Windows) to quit the REPL.

Choosing an editor or IDE

For anything beyond quick experiments, write your code in a file. Popular choices:

  • VS Code with the Python extension — the most-used free option.
  • PyCharm Community — a full-featured Python IDE.
  • IDLE — bundled with Python; minimal but always available.

Writing Your First Python Program

Create a new file called hello.py in any folder, and type:

print("Hello, World!")

Save the file, then run it from the terminal:

python3 hello.py

Expected output:

Hello, World!

print() is a built-in function that writes text to the console. The string "Hello, World!" is the argument you pass to it.

Variables and Data Types

A variable is a named container for a value. You create one by writing a name, the = sign, and a value — no type declaration needed:

my_age = 30           # int   — whole numbers
my_weight = 65.5      # float — decimal numbers
my_name = "Alice"     # str   — text
is_python_fun = True  # bool  — True or False

print(my_age)         # 30
print(my_name)        # Alice
print(is_python_fun)  # True

print(type(my_age))   # <class 'int'>
print(type(my_name))  # <class 'str'>

Python infers the type from the value you assign. You can confirm the type of any value with the built-in type() function.

F-strings: embedding variables in text

The clearest way to build strings that contain variable values is an f-string (available since Python 3.6). Prefix the string with f and wrap expressions in curly braces:

name = "Alice"
age = 30
print(f"My name is {name} and I am {age} years old.")
# My name is Alice and I am 30 years old.

A first look at lists

Python also has lists — ordered collections of values. You will explore them in depth later, but they appear in almost every beginner program:

fruits = ["apple", "banana", "cherry"]
print(fruits[0])   # apple   (indexes start at 0)
print(len(fruits)) # 3

For a full breakdown of all built-in types, see Python Data Types.

Try it Yourself isn't available for this example.

Operators

Operators let you compute new values from existing ones. Python groups them by purpose:

Arithmetic operators

x = 10
y = 3

print(x + y)   # 13  — addition
print(x - y)   # 7   — subtraction
print(x * y)   # 30  — multiplication
print(x / y)   # 3.3333333333333335 — true division (always float)
print(x // y)  # 3   — floor division (rounds down to int)
print(x % y)   # 1   — modulus (remainder)
print(x ** y)  # 1000 — exponentiation

Notice the difference between / (true division, always produces a float) and // (floor division, discards the remainder).

Comparison operators

Comparison operators return True or False:

a = 10
b = 20

print(a == b)   # False — equal to
print(a != b)   # True  — not equal to
print(a > b)    # False — greater than
print(a < b)    # True  — less than
print(a >= b)   # False — greater than or equal to
print(a <= b)   # True  — less than or equal to

Logical operators

p = True
q = False

print(p and q)  # False — both must be True
print(p or q)   # True  — at least one must be True
print(not p)    # False — inverts the value

See Python Operators for bitwise, assignment, and identity operators.

Control Structures

Control structures decide which code runs and how many times it runs.

Info

Python uses indentation (4 spaces by convention) to define code blocks. There are no curly braces. Misaligned indentation causes an IndentationError.

If / elif / else

temperature = 25

if temperature > 30:
    print("hot")
elif temperature > 20:
    print("warm")   # prints this — 25 > 20
else:
    print("cool")

Python checks each condition in order and runs the first block that is True. The elif and else branches are optional.

For loops

A for loop iterates over any sequence — a list, a string, or a range:

# Loop over a list
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)
# apple
# banana
# cherry
# Loop a fixed number of times with range()
for i in range(1, 6):
    print(i)
# 1  2  3  4  5

range(start, stop) generates integers from start up to (but not including) stop.

While loops

A while loop repeats as long as its condition is True:

count = 0
while count < 3:
    print("count:", count)
    count += 1
# count: 0
# count: 1
# count: 2

Always make sure the condition eventually becomes False, or the loop runs forever.

For deeper coverage of loops, see Python For Loops and Python While Loops.

Try it Yourself isn't available for this example.

Functions

A function is a named, reusable block of code. You define it once with def and call it as many times as you like:

def greet(name):
    return f"Hello, {name}!"

print(greet("World"))   # Hello, World!
print(greet("Alice"))   # Hello, Alice!

Functions can have default parameter values, which are used when the caller does not supply that argument:

def power(base, exponent=2):
    return base ** exponent

print(power(3))     # 9  — exponent defaults to 2
print(power(3, 3))  # 27 — caller supplies exponent

The if __name__ == "__main__" guard

When Python runs a .py file directly, it sets the special variable __name__ to "__main__". When the file is imported by another module, __name__ is the file's name instead. Wrapping your startup code in this guard prevents it from running on import:

def greet(name):
    return f"Hello, {name}!"

if __name__ == "__main__":
    print(greet("World"))

This is a best practice for any script you also plan to import as a module.

For more on functions, see Python Functions.

Installing Packages with pip

Python's package manager, pip, lets you add thousands of third-party libraries. For example, to install the popular requests library for making HTTP calls:

pip3 install requests

After installation, import and use it in your code:

import requests

response = requests.get("https://api.github.com")
print(response.status_code)   # 200

Run pip3 list to see all installed packages, and pip3 install --upgrade <package> to update one.

What to Learn Next

You now have Python installed and understand the five essential building blocks. Good next steps:

Practice

Practice
Which command verifies that Python is installed and shows its version?
Which command verifies that Python is installed and shows its version?
Was this page helpful?