beginner

Variables & Basic Types

6 min readLast updated: 2026-07-12

Overview

A variable is simply a name that stores a value. Python remembers where that value is stored so you can use it later.

Learning Objectives

  • Create variables to store numbers, text, and true/false values.
  • Learn about basic data types: integers, floats, booleans, and strings.
  • Learn how to swap and update variable values.

Concept Explanation

Think of a variable as a sticky label. When you write age = 20, you are placing the label 'age' onto the value 20. Python stores the value 20 in memory, and whenever you use the word 'age', Python looks up that value. Python has simple types for different data: integers for whole numbers, floats for decimals, booleans for True/False, and strings for text.

Code Examples

Example 1 — Basics

This example introduces the fundamental syntax and concepts.

python
age = 25
height = 5.9
is_student = True
name = 'Charlie'

Example 2 — Everyday Usage

This example demonstrates a realistic scenario handling business parameters.

python
# Calculate checkout totals
price = 19.99
tax = 1.60
total_price = price + tax
print('Total is:', total_price)

Example 3 — Advanced Example

This example shows clean, production-grade code structure following senior development standards.

python
# Swap values in a single line
first_name = 'Alice'
last_name = 'Smith'
first_name, last_name = last_name, first_name # Swaps labels instantly
print(first_name, last_name)

Visual Flow

The following execution flow represents the step-by-step evaluation inside the interpreter:

text
Create Value in Memory (20) → Attach Name Label (age) → Look up Name → Return Value (20)

Common Mistakes

Review these common pitfalls when working with this topic:

  • Naming variables with standard keywords like class or def.
  • Trying to use a variable before creating/defining it.
  • Adding spaces inside variable names (use underscores like user_age instead).
  • Starting a variable name with a number (e.g. 1st_place is invalid).

Best Practices

Enforce these Pythonic best practices in your codebase:

❌ Don't
python
a = 25
h = 5.9
s = 'Bob' # Confusing short names
✅ Do
python
age = 25
height = 5.9
student_name = 'Bob' # Descriptive names

Performance Notes

Keep these optimization guidelines in mind for performance-sensitive hotpaths:

  • Small integers (from -5 to 256) are cached in memory for quick reuse, saving memory allocations.

Quick Revision

Use these key summaries for last-minute revision:

  • Variables are descriptive names for values.
  • Integer represents whole numbers; float represents decimals.
  • String holds text values inside quotes.
  • Boolean represents only True or False values.
  • Create variables using the single equals sign (=).