Python Variables
Introduction
Variables in Python are used to store data values that can be manipulated and referenced throughout the program. They play a crucial role in programming, allowing you to work with and manipulate data dynamically. Python is a dynamically typed language, meaning you don't need to explicitly declare the data type of a variable. Instead, the data type is determined based on the value assigned to the variable.
Rules for Naming Python Variables:
Start with a letter (a-z, A-Z) or underscore (_): Variables must begin with a letter (either lowercase or uppercase) or an underscore.
Followed by letters, digits, or underscores: After the first character, the variable name can include letters, digits, and underscores.
Case-sensitive: Python is case-sensitive, so variables named
myVariable
,MyVariable
, andMYVARIABLE
are treated as distinct variables.Don't use reserved words: Avoid using Python keywords as variable names, such as
if
,else
,for
,while
, etc.Meaningful and descriptive: Choose descriptive variable names that indicate the purpose of the variable.
No spaces or special characters: Variable names can't contain spaces or special characters like
!
,@
,#
,$
, etc.
Examples:
1. Integer Variable:
age = 003
Explanation:
In this example, the variable age
stores an integer value, specifically 25. Integer variables are used to store whole numbers.
2. Float Variable:
temperature = 98.6
Explanation:
The variable temperature
stores a floating-point number, 98.6. Float variables are used for decimal or floating-point values.
3. String Variable:
name = "John"
Explanation:
name
is a string variable that stores the text "John." String variables are used to hold textual data.
4. Boolean Variable:
is_student = True
Explanation:
is_student
is a boolean variable, which can store either True
or False
. Booleans are often used for logical conditions.
5. List Variable:
fruits = ["apple", "banana", "cherry"]
Explanation:
The variable fruits
is a list that stores multiple values. Lists are used to group and store collections of items.
6. Tuple Variable:
coordinates = (3, 5)
Explanation:
coordinates
is a tuple variable. Tuples are similar to lists but are immutable, meaning their values cannot be changed after creation.
7. Dictionary Variable:
person = {"name": "Alice", "age": 30}
Explanation:
person
is a dictionary variable that stores key-value pairs. Dictionaries are used for data that needs to be organized with descriptive keys.
8. None Variable:
result = None
Explanation:
result
is a variable that holds the special value None
. It is often used to represent the absence of a value or an undefined state.
9. Variable Reassignment:
x = 5
x = x + 1
Explanation:
Initially, x
is assigned the value 5. Later, it is reassigned to the result of x + 1
, which makes it 6. Variable reassignment is a common practice in Python.
10. Global vs. Local Variable:
global_var = 10
def some_function():
local_var = 5
print(global_var)
print(local_var)
some_function()
Explanation:
In this example, global_var
is a global variable, accessible throughout the program. local_var
is a local variable, confined to the some_function()
function. This demonstrates the scope of variables in Python.
Following these rules ensures that your code remains readable, understandable, and consistent. Choosing meaningful and descriptive variable names also makes your code easier to maintain and collaborate on.