Variables are fundamental building blocks in Python programming. They allow you to store and manipulate data in your programs. In this tutorial, you'll learn how to create, use, and manage variables in Python, including their naming conventions, data types, and scope.
Python Variables Tutorial
By the end of this tutorial, you'll understand how to work with variables effectively in Python and use them to build dynamic and flexible programs.
What is a Variable?
A variable is a named location in memory used to store data. In Python, you don't need to declare a variable's type explicitly; it is dynamically inferred based on the value assigned to it.
- Key Features:
- Variables store data that can be used and modified throughout the program.
- Python variables are dynamically typed, meaning their type can change during runtime.
- Example:
# Assigning values to variables x = 10 name = "Alice" is_active = True
Variable Naming Rules
Python has specific rules for naming variables:
- Variable names must start with a letter (a-z, A-Z) or an underscore (
_
). - Variable names can only contain letters, numbers, and underscores.
- Variable names are case-sensitive (
myVar
andmyvar
are different). - Avoid using Python keywords (e.g.,
if
,for
,while
) as variable names.
# Valid variable names
my_var = 10
user_name = "Alice"
total_amount = 100.50
# Invalid variable names
2var = 5 # Cannot start with a number
my-var = 10 # Cannot contain hyphens
Data Types in Python
Python supports various data types, and variables can hold values of any type. Here are some common data types: Read More About
- Integer: Whole numbers (e.g.,
10
,-5
). - Float: Decimal numbers (e.g.,
3.14
,-0.001
). - String: Text data (e.g.,
"Hello"
,'Python'
). - Boolean: True or False values (e.g.,
True
,False
). - List: Ordered, mutable collection of items (e.g.,
[1, 2, 3]
). - Tuple: Ordered, immutable collection of items (e.g.,
(1, 2, 3)
). - Dictionary: Key-value pairs (e.g.,
{"name": "Alice", "age": 25}
).
# Examples of different data types
age = 25 # Integer
price = 19.99 # Float
name = "Alice" # String
is_student = True # Boolean
fruits = ["apple", "banana", "cherry"] # List
coordinates = (10.0, 20.0) # Tuple
person = {"name": "Alice", "age": 25} # Dictionary
Variable Scope
Variable scope determines where a variable can be accessed in a program. Python has two main types of scope:
- Global Scope: Variables defined outside of any function or block. They can be accessed anywhere in the program.
- Local Scope: Variables defined inside a function or block. They can only be accessed within that function or block.
# Global variable
x = 10
def my_function():
# Local variable
y = 20
print("Local y:", y)
print("Global x:", x)
my_function()
# print(y) # This will cause an error because y is local to my_function
Dynamic Typing
Python is dynamically typed, meaning you can change the type of a variable by assigning a new value of a different type.
# Dynamic typing example
x = 10 # x is an integer
print(type(x)) # Output: <class 'int'>
x = "Hello" # x is now a string
print(type(x)) # Output: <class 'str'>
Best Practices for Using Variables
Follow these best practices to write clean and maintainable code:
- Use descriptive variable names (e.g.,
user_age
instead ofu
). - Avoid using single-letter variable names unless they are used in short loops or mathematical contexts.
- Use constants (variables that don't change) in uppercase (e.g.,
PI = 3.14159
). - Keep variable scope in mind to avoid unintended side effects.
This tutorial covered the basics of variables in Python, including naming conventions, data types, scope, and dynamic typing. Practice using variables to store and manipulate data in your Python programs.