Python Setup And First Script
Python is a general-purpose programming language used for automation scripts, data work, web backends, and small utilities that glue systems together. In this course, you will use Python to write small programs that run from a file, read input, and produce output.
Python runs code through an interpreter. When you run a file, the interpreter reads the file from top to bottom, executes statements in order, and stops when the file ends or an error occurs. A script is simply a Python file intended to be executed to do a task, rather than imported as a library for other code.
Install Python and pick an editor
Install Python 3.12 from the official installer for your operating system, then confirm it is available on your terminal. The check is simple because it validates the full chain, meaning your shell can find Python and Python can report its version.
python3 --version
If that command fails on your system, try:
python --version
Choose an editor that can open folders, edit plain text, and run Python. The key requirement is that it saves files with the .py extension and does not add formatting that changes the code.
A virtual environment is an isolated folder that contains its own Python interpreter and installed packages, which prevents one project’s dependencies from affecting another. Create one per project, activate it, then install packages with pip, which is Python’s package installer.
python3 -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip
Rule of thumb
If you see packages installing globally, create a virtual environment and runpython -m pip ...inside it.
How a Python file executes
A Python file is a module. When you run python path/to/file.py, Python sets the module name to __main__, then executes the file’s top-level code. When you import that same file from another module, Python executes it once at import time and sets its name to the filename, which changes how you should structure executable code.
The __name__ variable lets a module detect whether it is being run directly or imported. A main guard is the pattern that keeps run-only code from executing during import.
if __name__ == "__main__":
main()
Try running a tiny module and compare output with and without the __name__ == "__main__" guard.
The key behavior is that top-level statements execute on import, so the main guard prevents accidental side effects when another file imports your module.
Your first script with input and output
A first script usually needs three pieces. It stores values in variables, reads text from the user with input(), and prints results with print(). Because input() returns a string, numeric math requires converting with int() or float() before you compute an expression.
The syntax is straightforward, and the order matters because each line uses values created earlier.
name = input("Name: ")
age = int(input("Age: "))
next_year = age + 1
print(f"{name} will be {next_year} next year.")
Complete a small script that asks for a name and age, then prints a formatted summary.
Sign up for free
Generate custom courses on any topic — with hands-on practice, AI guidance, and visuals built in.
Already have an account?