Module: Python Basics

Variables

Python Variables: A Beginner's Guide

Variables are fundamental to any programming language, and Python is no exception. They act as named storage locations in the computer's memory, allowing you to store and manipulate data.

What is a Variable?

Think of a variable as a labeled box. You can put something inside the box (the data), and then refer to that data by the label on the box (the variable name).

Declaring and Assigning Variables

In Python, you don't need to declare variables explicitly like you do in some other languages (e.g., C++ or Java). You simply assign a value to a variable name using the assignment operator (=).

# Assigning an integer value
age = 30

# Assigning a string value
name = "Alice"

# Assigning a floating-point value
price = 99.99

# Assigning a boolean value
is_active = True

Explanation:

  • age = 30: Creates a variable named age and stores the integer value 30 in it.
  • name = "Alice": Creates a variable named name and stores the string "Alice" in it. Strings are enclosed in either single quotes (') or double quotes (").
  • price = 99.99: Creates a variable named price and stores the floating-point value 99.99 in it.
  • is_active = True: Creates a variable named is_active and stores the boolean value True in it. Boolean values are either True or False (case-sensitive).

Variable Naming Rules

Python has specific rules for naming variables:

  • Must start with a letter (a-z, A-Z) or an underscore (_).
  • Can contain letters, numbers, and underscores.
  • Case-sensitive: myVariable and myvariable are treated as different variables.
  • Cannot be a Python keyword: You can't use words like if, else, for, while, def, class, etc., as variable names.

Examples of valid variable names:

  • my_variable
  • x
  • count
  • _private_variable
  • user_age

Examples of invalid variable names:

  • 1st_variable (starts with a number)
  • my-variable (contains a hyphen)
  • if (is a keyword)
  • class (is a keyword)

Data Types

Python is dynamically typed. This means you don't need to explicitly specify the data type of a variable when you create it. Python infers the data type based on the value you assign. Common data types include:

  • Integer (int): Whole numbers (e.g., 10, -5, 0)
  • Floating-point (float): Numbers with decimal points (e.g., 3.14, -2.5)
  • String (str): Text enclosed in single or double quotes (e.g., "Hello", 'Python')
  • Boolean (bool): True or False
  • List (list): An ordered collection of items (e.g., [1, 2, 3], ["apple", "banana"])
  • Tuple (tuple): An ordered, immutable collection of items (e.g., (1, 2, 3))
  • Dictionary (dict): A collection of key-value pairs (e.g., {"name": "Alice", "age": 30})

You can check the data type of a variable using the type() function:

age = 30
print(type(age))  # Output: <class 'int'>

name = "Alice"
print(type(name)) # Output: <class 'str'>

Reassigning Variables

You can change the value of a variable at any time. The variable will then hold the new value, and the old value is discarded.

age = 30
print(age)  # Output: 30

age = 31
print(age)  # Output: 31

Multiple Assignment

Python allows you to assign values to multiple variables in a single line:

x, y, z = 1, 2, 3
print(x)  # Output: 1
print(y)  # Output: 2
print(z)  # Output: 3

You can also assign the same value to multiple variables:

a = b = c = 10
print(a)  # Output: 10
print(b)  # Output: 10
print(c)  # Output: 10

Best Practices

  • Use descriptive variable names: Choose names that clearly indicate the purpose of the variable. For example, user_name is better than x.
  • Follow a consistent naming convention: Common conventions include snake_case (e.g., user_age) and camelCase (e.g., userName). snake_case is generally preferred in Python.
  • Avoid using single-character variable names (except for simple loop counters like i or j).
  • Keep variable names concise but meaningful.

This provides a solid foundation for understanding variables in Python. As you progress, you'll encounter more advanced concepts related to variables, such as scope and mutability, but this is a great starting point.