Python, known for its readability and versatility, relies heavily on variables to store and manipulate data. This post provides a introduction to Python variables, covering their declaration, types, naming conventions, and best practices.
What are Variables?
In simple terms, a variable is a named storage location in your computer’s memory that holds a value. Think of it like a labeled container that you can fill with different types of information. This information can be anything from numbers and text to more complex data structures.
Declaring Variables in Python
Unlike some programming languages, Python doesn’t require you to explicitly declare the type of a variable. The type is inferred based on the value assigned to it. This is called dynamic typing.
name = "Alice" # String variable
age = 30 # Integer variable
height = 5.8 # Float variable
is_student = True # Boolean variableIn this example:
namestores a string value.agestores an integer value.heightstores a floating-point value.is_studentstores a boolean value (True or False).
Variable Naming Conventions
Choosing meaningful names for your variables improves code readability and maintainability. Here are some key guidelines:
- Use descriptive names: Instead of
x, usecustomer_ageorproduct_price. - Use lowercase letters:
my_variableis preferred overMyVariable. - Separate words with underscores:
first_nameis better thanfirstName. - Avoid reserved keywords: Don’t use words like
if,else,for,while, etc., as variable names.
Variable Types
Python supports many built-in data types:
- Integers (int): Whole numbers (e.g., 10, -5, 0).
- Floating-point numbers (float): Numbers with decimal points (e.g., 3.14, -2.5).
- Strings (str): Sequences of characters (e.g., “Hello”, ‘Python’).
- Booleans (bool): Represent truth values (True or False).
- Lists (list): Ordered, mutable (changeable) sequences of items.
- Tuples (tuple): Ordered, immutable (unchangeable) sequences of items.
- Dictionaries (dict): Collections of key-value pairs.
my_list = [1, 2, 3, "apple", "banana"]
my_tuple = (10, 20, 30)
my_dict = {"name": "Bob", "age": 25}Assigning Values to Variables
You can assign values to variables using the = operator. You can also reassign a variable to a different value later in your code.
x = 10
x = 20 # x now holds the value 20Multiple Assignments
Python allows you to assign values to multiple variables in a single line:
a, b, c = 1, 2, 3Variable Scope
The scope of a variable refers to the part of your code where the variable is accessible. Variables declared inside a function are only accessible within that function (local scope). Variables declared outside functions have global scope and are accessible from anywhere in your program.
global_var = 100
def my_function():
local_var = 50
print(global_var) # Accessing global variable
print(local_var) # Accessing local variable
my_function()
print(global_var) # Accessing global variable
#print(local_var) # This would cause an error because local_var is not in global scopeData Type Conversion
You can convert variables from one type to another using type casting functions like int(), float(), str(), and bool().
num_str = "10"
num_int = int(num_str) # Convert string to integerThis introduction covers the fundamentals of Python variables. Further exploration into more advanced topics like data structures and object-oriented programming will build upon this foundation.