In programming, a variable is a named storage location that holds a value. Think of it as a label or a box where you can store different kinds of information. Python variables are dynamic; you don't need to declare their type before using them. Python automatically infers the type based on the value assigned.
Declaring and Assigning Variables:
To create a variable, you simply give it a name and assign a value using the assignment operator (=):
python# Assigning an integer to a variable age = 30 # Assigning a string to a variable name = "Alice" # Assigning a boolean to a variable is_student = True # You can also assign multiple variables in one line (though use sparingly for readability) x, y, z = 10, 20, 30 # Or assign the same value to multiple variables a = b = c = "Python" print(age) print(name) print(is_student) print(x, y, z) print(a, b, c)
Variable Naming Conventions (PEP 8):
Python has specific rules and widely accepted conventions for naming variables to ensure code readability and consistency (outlined in PEP 8, Python Enhancement Proposal 8).
Rules:
-
Must start with a letter or an underscore (
_). They cannot start with a number.python# Valid: my_variable = 10 _internal_var = 20 # Invalid (will raise a SyntaxError): # 1st_variable = 30 -
Can only contain alphanumeric characters (A-Z, a-z, 0-9) and underscores. Special characters like
$, #, @are not allowed.python# Valid: user_name = "Bob" product_id_123 = 456 # Invalid: # user-name = "Bob" # Hyphens are used for subtraction # total$ = 100 -
Are case-sensitive:
age,Age, andAGEare three different variables. -
Cannot be Python keywords: Words that have special meaning in Python (e.g.,
if,else,while,for,print,True,False,None).python# Invalid (will raise a SyntaxError or be confusing): # print = "hello" # Don't redefine built-in functions or keywords # class = 5
Conventions (PEP 8 Recommendations):
snake_casefor variables and functions: All lowercase letters, with words separated by underscores. This is the most common convention.pythonfirst_name = "John" total_price = 150.75 calculate_area() # For functionsCamelCasefor class names: (You'll learn about classes later).pythonclass MyClass: passUPPER_SNAKE_CASEfor constants: Variables whose values are not expected to change throughout the program.pythonMAX_SIZE = 1000 PI = 3.14159- Descriptive names: Choose names that clearly indicate the purpose of the variable. Avoid single-letter variable names unless their context is absolutely clear (e.g.,
x, yfor coordinates in a mathematical context).python# Good: customer_age = 25 student_grade = 'A' # Less descriptive: c = 25 s = 'A'
Adhering to these conventions makes your code more readable, maintainable, and understandable to others (and your future self).
Key Takeaways:
- Variables are named containers for storing data.
- Assign values using the
=operator. - Follow naming rules (start with letter/underscore, alphanumeric/underscore content, case-sensitive, no keywords).
- Adhere to PEP 8 conventions, especially
snake_casefor variable names.