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.
= "Alice" # String variable
name = 30 # Integer variable
age = 5.8 # Float variable
height = True # Boolean variable is_student
In this example:
name
stores a string value.age
stores an integer value.height
stores a floating-point value.is_student
stores 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_age
orproduct_price
. - Use lowercase letters:
my_variable
is preferred overMyVariable
. - Separate words with underscores:
first_name
is 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.
= [1, 2, 3, "apple", "banana"]
my_list = (10, 20, 30)
my_tuple = {"name": "Bob", "age": 25} my_dict
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.
= 10
x = 20 # x now holds the value 20 x
Multiple Assignments
Python allows you to assign values to multiple variables in a single line:
= 1, 2, 3 a, b, c
Variable 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.
= 100
global_var
def my_function():
= 50
local_var 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 scope
Data Type Conversion
You can convert variables from one type to another using type casting functions like int()
, float()
, str()
, and bool()
.
= "10"
num_str = int(num_str) # Convert string to integer num_int
This 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.