Python Variables
Python Variables
In Python, a variable acts as a placeholder for storing different types of data. 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.
- In Python, assigning a value to a name automatically creates a variable.
- 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
-
A variable name should start with 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, andAGEare recognized as separate variables.
-
Cannot use reserved keywords: Reserved words like
class,if,else, andTruecannot 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):
TrueorFalsevalues.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 multiple variables in one line to keep your code concise.
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
Use the type() function to identify a variable's data type.
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
Prefer Learning by Watching?
Watch these YouTube tutorials to understand Python Tutorial visually:
What You'll Learn:
- 📌 Master Python Variables in Just 15 Minutes: Quick & Easy Guide
- 📌 Learn Python VARIABLES in 10 minutes!