🔍 Quick Lesson
Variables are essential in Python as they store data that can be used and manipulated throughout your program. A variable essentially acts as a container for data. Here's everything you need to know:
What is a Variable?
A variable in Python is a symbolic name that refers to a memory location where a value is stored. You can assign any kind of value to a variable, such as numbers, strings, or even lists.
age = 30
In this example, age
is the variable name, and 30
is the value stored in that variable. Python doesn’t require you to declare the type of a variable before assigning it a value (this is known as dynamic typing).
Variable Assignment
Variables are assigned values using the =
operator:
x = 5
The above example assigns the integer value 5
to the variable x
.
Python Variable Data Types
Python supports several data types for variables. Here are the most common ones:
- int: Whole numbers, e.g.,
age = 25
- float: Decimal numbers, e.g.,
pi = 3.14
- str: Strings, or text, e.g.,
name = "Alice"
- bool: Boolean values, e.g.,
is_active = True
In Python, the type of data is determined by the value assigned to a variable, and Python automatically detects the type. This is why Python is considered a dynamically typed language.
Variable Naming Rules
When naming a variable in Python, there are certain rules and best practices to follow:
- Can start with a letter or underscore (_) but not with a number.
- Can contain letters, numbers, and underscores, but not spaces or special characters.
- Is case-sensitive:
myVar
andmyvar
are different variables. - Should be descriptive: Use meaningful names like
user_name
ortotal_price
.
Examples of valid variable names include total_count
and is_available
. An example of an invalid variable name would be 2ndVar
because it starts with a number.
Reassigning Variables
In Python, variables can be reassigned to new values at any time. The variable’s type can also change dynamically based on the value assigned to it:
number = 5 # Integer
number = "Hello" # String
number = 3.14 # Float
The variable number
is initially assigned an integer, then a string, and finally a float. This shows how Python’s dynamic typing allows for flexible variable handling.
Best Practices for Variable Naming
Here are some tips for effective variable naming in Python:
- Be descriptive: Avoid vague names like
x
orvar1
. Instead, use names that describe the data stored in the variable, such asuser_name
ortotal_cost
. - Avoid reserved keywords: Don't use Python's reserved words (such as
def
,class
, etc.) as variable names. - Use underscores for multi word names (snake_case) for readability, e.g.,
total_price
, instead oftotalPrice
(camelCase).
Variable Scope
In Python, the scope of a variable refers to the area of the program where the variable is accessible:
- Global scope: A variable defined outside of functions can be accessed throughout the program.
- Local scope: A variable defined inside a function is only accessible within that function.
For example:
def my_function():
local_var = 10
print(local_var)
my_function() # Works fine
print(local_var) # This will raise an error because local_var is not accessible outside the function.