Python Variables
Python Variables
A variable in Python is a container for storing data values. When you create a variable, you are essentially reserving a portion of memory to hold a value that you can reference, use, or modify later in your program.
In Python:
- Variables do not require an explicit declaration.
- A variable is created the moment you assign a value to it.
- Python is dynamically typed, meaning you don't need to specify the type of the variable; the interpreter infers the type automatically.
Rules for Naming Variables
-
Start with a letter or an underscore (_):
- Valid:
name
,_age
- Invalid:
1name
,@value
- Valid:
-
Followed by letters, numbers, or underscores:
- Valid:
value1
,user_name
- Invalid:
user-name
,user@name
- Valid:
-
Case-sensitive:
age
,Age
, andAGE
are treated as three distinct variables.
-
Cannot use reserved keywords: Reserved words like
class
,if
,else
, andTrue
cannot be used as variable names.
Assigning Values to Variables
In Python, assignment is done using the =
operator.
# Assigning integer value x = 10 print(x) # Output: 10 # Assigning string value name = "Alice" print(name) # Output: Alice # Assigning a float value pi = 3.14159 print(pi) # Output: 3.14159
Variable Types
Python supports various data types, and variables can hold any of them. Some common types include:
-
Integers (int): Whole numbers.
age = 25
-
Floating-Point Numbers (float): Decimal numbers.
price = 19.99
-
Strings (str): A sequence of characters enclosed in quotes.
greeting = "Hello, World!"
-
Booleans (bool):
True
orFalse
values.is_active = True
-
Lists: Ordered, mutable collections.
colors = ["red", "blue", "green"]
-
Dictionaries (dict): Key-value pairs.
user = {"name": "Alice", "age": 25}
Multiple Assignments
You can assign values to multiple variables in one line.
x, y, z = 5, 10, 15 print(x, y, z) # Output: 5 10 15 # Assigning the same value to multiple variables a = b = c = 100 print(a, b, c) # Output: 100 100 100
Changing Variable Values
You can change the value of a variable at any time.
counter = 1 print(counter) # Output: 1 # Reassigning a new value counter = counter + 1 print(counter) # Output: 2
Using type()
to Check Variable Type
You can check the data type of a variable using the type()
function.
x = 42 print(type(x)) # Output:y = "Hello" print(type(y)) # Output:
Dynamic Typing
Python allows a variable to change its type during program execution.
var = 10 # Initially an integer print(type(var)) # Output:var = "Python" # Now a string print(type(var)) # Output:
Best Practices for Variables
-
Use meaningful names:
temperature = 25 # Good t = 25 # Not descriptive
-
Stick to snake_case for variable names:
user_name = "Alice"
-
Avoid using single-letter names except in short loops or temporary variables:
for i in range(5): print(i)
-
Use constants (all uppercase) for values that should not change:
MAX_USERS = 100
Example Program
Here's an example of variables in action:
# Variables for user information name = "Alice" age = 30 is_subscribed = True # Display user information print("User Information:") print("Name:", name) print("Age:", age) print("Subscribed:", is_subscribed) # Changing values age = age + 1 is_subscribed = False print("\nUpdated Information:") print("Name:", name) print("Age:", age) print("Subscribed:", is_subscribed)
Output:
- User Information:
- Name: Alice
- Age: 30
- Subscribed: True
- Updated Information:
- Name: Alice
- Age: 31
- Subscribed: False