Web Analytics

Variables

Beginner ~20 min read

Variables are fundamental building blocks in Python. They act as containers that store data values, allowing you to reference and manipulate data throughout your program. Unlike many other languages, Python doesn't require you to declare variable types explicitly.

Variable Assignment

In Python, you create a variable by simply assigning a value to a name using the = operator. No type declaration is needed - Python figures out the type automatically.

Output
Click Run to execute your code
Key Point: The type() function returns the data type of any variable. This is useful for debugging and understanding your data.

Variable Naming Rules

Python has specific rules for naming variables:

Rule Valid Invalid
Must start with letter or underscore name, _private 2name
Can contain letters, numbers, underscores user_name, count2 user-name
Case-sensitive Name โ‰  name -
Cannot be reserved keywords my_class class, if

Python Naming Conventions (PEP 8)

  • Variables: snake_case (lowercase with underscores)
  • Constants: SCREAMING_SNAKE_CASE (all uppercase)
  • Classes: PascalCase (each word capitalized)
  • Private: _leading_underscore (convention, not enforced)
Output
Click Run to execute your code

Multiple Assignment

Python allows several elegant ways to assign values to multiple variables at once:

Output
Click Run to execute your code
Pro Tip: The swap trick a, b = b, a is a Pythonic way to swap values without needing a temporary variable. It works because Python evaluates the right side first, then assigns to the left side.

Dynamic Typing

Python is dynamically typed, meaning:

  • You don't declare variable types
  • A variable can hold different types at different times
  • Type checking happens at runtime, not compile time
Output
Click Run to execute your code
Caution: While dynamic typing is flexible, it can lead to bugs if you're not careful. Consider using type hints (covered in advanced topics) for larger projects.

Constants

Python doesn't have true constants (immutable variables). Instead, we use a naming convention - ALL_CAPS indicates a value that shouldn't be changed:

Output
Click Run to execute your code

Variable Scope (Preview)

Variables have different scope - where they can be accessed:

  • Global: Defined outside functions, accessible everywhere
  • Local: Defined inside functions, only accessible within that function
Output
Click Run to execute your code

Common Mistakes

1. Starting variable names with numbers

# Wrong
2nd_place = "Silver"  # SyntaxError

# Correct
second_place = "Silver"

2. Using reserved keywords

# Wrong
class = "Math 101"  # SyntaxError

# Correct
class_name = "Math 101"

3. Forgetting case sensitivity

Name = "Alice"
print(name)  # NameError: 'name' is not defined

Exercise: Fix the Variable Names

Task: The code below has invalid or non-Pythonic variable names. Fix them to make the code run and follow Python conventions.

Requirements:

  • Fix all invalid names so the code runs
  • Use snake_case for regular variables
  • Use ALL_CAPS for the constant
Output
Click Run to execute your code
Show Solution
first_place = "Gold"      # Changed from 1st_place
user_name = "alice"       # Changed from user-name
max_value = 100           # Changed from MaxValue (snake_case)
MY_CONSTANT = 3.14        # Changed from "my constant" (ALL_CAPS)

print(f"1st place: {first_place}")
print(f"Username: {user_name}")
print(f"Max value: {max_value}")
print(f"Constant: {MY_CONSTANT}")

Summary

  • Assignment: Use = to assign values; no type declaration needed
  • Naming: Start with letter/underscore, use snake_case
  • Multiple assignment: a, b, c = 1, 2, 3
  • Dynamic typing: Variables can change type at runtime
  • Constants: Use ALL_CAPS convention
  • Scope: Global vs local - use global keyword to modify globals

What's Next?

Now that you understand variables, let's explore Python's data types in detail. In the next lesson, we'll cover numeric types: integers, floats, and complex numbers.