Python Code Reading Fundamentals
Python Setup
Getting Python on Your Machine
Before you can write any Python code, you need to install the Python interpreter on your computer. The interpreter is a program that understands and runs your Python code. The installation process is slightly different depending on your operating system.
Most modern computers don't come with Python pre-installed. Even if yours does, it's often an older version. It's always best to install the latest stable version directly from the official source.
For Windows: Head to the official Python website, python.org, and download the latest installer. During installation, you'll see a checkbox that says "Add Python.exe to PATH." Make sure you check this box. It makes it much easier to run Python from your command prompt.
For macOS: Like Windows, the best way to get Python is from python.org. Download the macOS installer package and run it. The process is straightforward and will set everything up for you.
For Linux: Most Linux distributions come with Python already installed. However, you can ensure you have the latest version by using your system's package manager. For Debian-based systems like Ubuntu, you'd open a terminal and run:
sudo apt update
sudo apt install python3
After installation, you can verify that it worked. Open your terminal (Command Prompt on Windows, Terminal on macOS/Linux) and type python3 --version or python --version. It should print the version number you just installed.
Your Coding Workspace
You can write Python code in a simple text file, but most developers use an Integrated Development Environment, or IDE. An IDE is like a specialized word processor for code. It helps you write code more efficiently with features like syntax highlighting (coloring your code to make it readable) and error checking.
Work on gaining a better understanding of the basics, like what text editors and Integrated Development Environments, or IDEs, are, and how you use them to work with Python.
There are many IDEs to choose from. Here are a few popular options for beginners:
| IDE | Best For | Key Feature |
|---|---|---|
| Thonny | Absolute beginners | Simple, clean interface with Python built-in. |
| PyCharm | Aspiring professionals | Powerful code assistance and debugging tools. |
| Visual Studio Code | General-purpose coding | Highly customizable with a huge extension library. |
| Jupyter Notebook | Data analysis and experimentation | Runs code in interactive blocks called "cells". |
For a versatile tool that grows with you, Visual Studio Code (VS Code) is an excellent choice. After installing it, you'll want to add the official Python extension from Microsoft, which gives you all the key features for Python development.
Running Your First Code
There are two primary ways to run Python code: in an interactive session or by executing a script file.
An interactive session is like having a conversation with Python. You type one command, and it responds immediately. A script file is a set of instructions you save and run all at once.
To start an interactive session, open your terminal and type python or python3. You'll see a >>> prompt. This is Python waiting for your command. Try typing 2 + 2 and hitting Enter.
>>> 2 + 2
4
>>> print('Hello, world!')
Hello, world!
For anything more than a few lines, you'll want to save your code in a file. Open your IDE, create a new file, and save it with a .py extension, like hello.py. In that file, type:
# This is a Python script file
print('This is my first Python script!')
To run this script, navigate to the file's directory in your terminal and type python3 hello.py. The output will appear on the next line. Most IDEs also have a "Run" button (often a green triangle) that executes the script for you.
Adding More Tools
One of Python's greatest strengths is its massive collection of third-party libraries—packages of code that other people have written to solve common problems. You can add these libraries to your Python installation using a tool called pip.
pip
noun
The standard package manager for Python. It allows you to install and manage additional libraries and dependencies that are not part of the standard Python library.
pip is included with modern versions of Python. To install a package, you simply use the pip install command in your terminal. For example, a popular library for making web requests is called requests.
# This command is run in your terminal, not the Python prompt
pip install requests
This downloads the requests library from the Python Package Index (PyPI), a vast repository of software, and installs it. Now you can use it in your scripts by writing import requests at the top of your file. You'll use pip constantly as you build more complex applications.
When installing Python on Windows from the official installer, which option is it highly recommended to check to make running Python from the command line easier?
What is the primary purpose of pip in the Python ecosystem?
With Python installed and an IDE ready, you have everything you need to start programming.
