Hey there! In this guide, we'll explore the concept of variables in Python. Variables are essential in programming, allowing you to store and manipulate data dynamically. Let's dive in!
Variables in Python
Introductionâ
- Variables in Python are containers that hold data.
- They are used to store values that can be referenced and manipulated throughout a program.
- Python is dynamically typed, which means you do not need to declare the type of a variable when creating one.
Variable Declarationâ
In Python, variables are created when you assign a value to them. No explicit declaration is required.
x = 7 # Integer variable
y = 3.14 # Float variable
name = "Ben" # String variable
is_active = True # Boolean variable
Naming Conventionsâ
- Variable names can contain letters, digits, and underscores.
- They must start with a letter or an underscore (_).
- They cannot start with a number.
- Python is case-sensitive, so age and Age are different variables.
Valid variable namesâ
my_var = 25
_name = "Mike"
age2 = 22
Invalid variable namesâ
2name = "Rohan" # Cannot start with a number
my-var = 21 # Hyphen (-) is not allowed
Variable Typesâ
Python supports several data types that can be assigned to variables:
- Integers (int): Whole numbers
- Floating-Point Numbers (float): Numbers with decimals
- Strings (str): Text enclosed in single or double quotes
- Booleans (bool): True or False
- Lists (list): Ordered collections of items
- Tuples (tuple): Immutable ordered collections
- Dictionaries (dict): Key-value pairs
Example:
age = 50 # Integer
height = 5.11 # Float
name = "John Doe" # String
is_student = False # Boolean
fruits = ["cherry", "orange"] # List
coordinates = (10.0, 20.0) # Tuple
person = {"name": "John", "age": 30} # Dictionary
Dynamic Typingâ
Python is dynamically typed, meaning the same variable can hold values of different types at different points in the program.
x = 5 # Initially an integer
x = "Hello" # Now a string
Multiple Variable Assignmentâ
You can assign multiple variables at once in a single line
a, b, c = 5, 10, 15
You can also assign same value to multiple variables
x = y = z = 0
Variable Scopeâ
- Local Variables: Defined inside a function and can only be used within that function.
- Global Variables: Defined outside all functions and can be accessed throughout the program.
x = "global variable"
def my_function():
x = "local variable"
print(x) # Output: local variable
my_function()
print(x) # Output: global variable
Swapping Variablesâ
In Python you can easily swap the values of two variables without needing any temporary variable
a = 5
b = 10
a, b = b, a
print(a) # Output: 10
print(b) # Output: 5
Constants in Pythonâ
By convention, variables that should not change are written in uppercase. However, Python does not have built-in support for constants, so it's up to the programmer to treat such variables as constant.
PI = 3.14159
MAX_SPEED = 120
Deleting Variablesâ
You can delete a variable using del statement
x = 12
del x # Deletes the variable 'x'
Best Practicesâ
- Use descriptive names that make it clear what the variable represents.
- Follow standard naming conventions (snake_case for variable names).
- Keep variable scope as limited as possible to avoid conflicts.