Python History and Installation
Objectives
By the end of this chapter, you should be able to:
- Explain what Python is used for
- Explain why this course teaches Python 3
- Install Python and run your first script
💡 Why this matters: Everything else in this course assumes Python 3 is installed and working on your machine.
What Is Python?
Python is a general purpose programming language used for web backends, data science, automation, and more. It has a reputation for being readable and beginner friendly.
Python 3, Not Python 2
Python 2 is no longer maintained. This course teaches Python 3, the current standard.
Installing Python
- Download Python 3 from python.org.
- Confirm the install by running this in your terminal:
python3 --version
- Confirm
pip3(Python’s package manager) is available too:
pip3 --version
Running Python Code
There are two ways to run Python code.
The REPL (type python3 with nothing after it) is an interactive prompt. You type one line, press Enter, and see the result immediately. It’s good for quick checks.
python3
>>> 2 + 2
4
>>> exit()
A script is a file ending in .py that holds all your code at once. This is how real programs run.
python3 first.py
Try it now. Create a file called first.py with one line:
print("Hello, Python")
Then run it:
python3 first.py
# Hello, Python
Virtual Environments
Every project eventually needs third-party packages. Installing them globally causes conflicts once you have more than one project (Project A needs version 1 of a library, Project B needs version 2). A virtual environment gives each project its own isolated packages.
python3 -m venv env # create it
source env/bin/activate # activate it (macOS/Linux)
env\Scripts\activate # activate it (Windows)
deactivate # leave it
Create a fresh virtual environment for every project.
Saving Your Dependencies
Once a project has packages installed, record them so anyone can recreate the same setup:
pip freeze > requirements.txt
pip3 install -r requirements.txt # recreates the environment elsewhere
Python’s Built-In Data Types
A preview. Each of these gets its own lesson soon.
| Type | Example |
|---|---|
bool |
True, False |
int / float |
4, -10, 1.3 |
str |
"hi" |
list |
[1, 2, 3] |
tuple |
(4, 2, 1) |
set |
{"a", "b", "c"} |
dict |
{"key": "value"} |
Try It
- Run
python3 --versionin your terminal. - Create
first.pywithprint("Hello, Python")and run it. - Create a virtual environment with
python3 -m venv env, then activate it. Your terminal prompt should change to show(env). Rundeactivateto leave it.
Recap
- This course teaches Python 3.
python3opens a REPL;python3 file.pyruns a script.python3 -m venv envcreates an isolated environment per project.pip freeze > requirements.txtrecords your project’s dependencies.
Next lesson: variables, and the rules for naming them well.