Python Basics: How to Install Python and Write Your First Script (2026 Guide)
Python explained from scratch: how to install it on Mac and Windows, how to write and run your first script, and the core concepts — variables, functions, loops, and pip — that you'll use in every Python project.
Python is the most-used programming language in AI, data science, and automation. It's also widely considered the easiest language to learn first — the syntax is close to plain English and the feedback loop is immediate.
This guide gets you from nothing to a working Python setup with a real script in under 30 minutes.
Install Python and write your first scripts — full beginner walkthrough.
Step 1: Install Python
macOS
Check if Python is already installed:
bash
python3 --version
If you see Python 3.10 or higher, you can skip to Step 2. If not, or if you want the latest version:
Important: Check the box that says "Add Python to PATH" before clicking Install
Open Command Prompt or PowerShell and verify:
bash
python --version
On Windows, the command is python (not python3). On Mac/Linux, use python3 to avoid accidentally using an old Python 2 installation.
Step 2: Install a code editor
If you haven't already, install Cursor or VS Code. Both have excellent Python support.
In VS Code or Cursor, install the Python extension by Microsoft — it adds syntax highlighting, autocomplete, and the ability to run Python files directly in the editor.
print("Hello, Python!")
name = "Alice"print(f"Hello, {name}!")
numbers = [1, 2, 3, 4, 5]
total = sum(numbers)
print(f"The sum of {numbers} is {total}")
Run it:
bash
# Mac/Linux
python3 hello.py
# Windows
python hello.py
Output:
snippet
Hello, Python!
Hello, Alice!
The sum of [1, 2, 3, 4, 5] is 15
The core concepts
Variables
Variables store values. No type declaration needed — Python figures it out:
python
name = "Alice"# string
age = 28# integer
height = 5.7# float
is_student = True# booleanprint(name, age, height, is_student)
Strings and f-strings
python
first = "Alice"
last = "Smith"# Concatenation (old way)
full = first + " " + last
# f-strings (modern way — preferred)
full = f"{first}{last}"
greeting = f"Hello, {first}! You are {age} years old."print(greeting)
Lists
python
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # apple — indexing starts at 0print(fruits[-1]) # cherry — last item
fruits.append("mango") # add to end
fruits.remove("banana") # remove by valueprint(len(fruits)) # 3 — number of items
Dictionaries
python
person = {
"name": "Alice",
"age": 28,
"city": "London"
}
print(person["name"]) # Alice
person["job"] = "Engineer"# add a new keyprint(person)
If / else
python
age = 20if age >= 18:
print("You are an adult")
elif age >= 13:
print("You are a teenager")
else:
print("You are a child")
For loops
python
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(f"I like {fruit}")
# Loop over a range of numbersfor i inrange(5):
print(i) # prints 0, 1, 2, 3, 4# Loop with indexfor i, fruit inenumerate(fruits):
print(f"{i}: {fruit}")
Functions
python
defgreet(name):
returnf"Hello, {name}!"defadd(a, b):
return a + b
message = greet("Alice")
print(message)
result = add(3, 4)
print(result) # 7
Save your dependencies so others can replicate your environment:
bash
pip freeze > requirements.txt
To install from a requirements.txt on a new machine:
bash
pip install -r requirements.txt
Step 6: A real beginner project
Build a script that fetches today's weather for any city using a free API.
Install the requests library if you haven't:
bash
pip install requests
Create weather.py:
python
import requests
defget_weather(city):
url = f"https://wttr.in/{city}?format=j1"
response = requests.get(url)
if response.status_code != 200:
returnf"Could not fetch weather for {city}"
data = response.json()
current = data["current_condition"][0]
temp_c = current["temp_C"]
feels_like = current["FeelsLikeC"]
description = current["weatherDesc"][0]["value"]
returnf"{city}: {description}, {temp_c}°C (feels like {feels_like}°C)"
cities = ["London", "New York", "Tokyo", "Mumbai"]
for city in cities:
print(get_weather(city))
Run it:
bash
python3 weather.py
You're fetching live data, parsing JSON, and printing formatted output — with less than 25 lines of code.
Common beginner mistakes
Not activating the virtual environment. If you install packages and they "don't work", check that (venv) is showing in your terminal prompt.
Using python vs python3. On Mac/Linux, always use python3. On Windows, python usually points to Python 3 if you installed it correctly.
Indentation errors. Python uses indentation (spaces) instead of curly braces to structure code. Mixing tabs and spaces causes errors. Use spaces consistently (4 spaces per indent is the standard).
Forgetting to save the file. Python runs whatever is on disk, not what's in your editor. Save before running.
What to learn next
File I/O — reading and writing files with open()
Error handling — try / except blocks
List comprehensions — [x * 2 for x in numbers] — a concise way to build lists
Classes — object-oriented programming with class
Common libraries — pandas for data, flask for web APIs, anthropic for Claude AI