Python Variables


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:

  1. Start with a letter (a-z, A-Z) or underscore (_): Variables must begin with a letter (either lowercase or uppercase) or an underscore.

  2. Followed by letters, digits, or underscores: After the first character, the variable name can include letters, digits, and underscores.

  3. Case-sensitive: Python is case-sensitive, so variables named myVariable, MyVariable, and MYVARIABLE are treated as distinct variables.

  4. Don't use reserved words: Avoid using Python keywords as variable names, such as if, else, for, while, etc.

  5. Meaningful and descriptive: Choose descriptive variable names that indicate the purpose of the variable.

  6. No spaces or special characters: Variable names can't contain spaces or special characters like !, @, #, $, etc.


1. Integer Variable:

age = 003


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


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"


name is a string variable that stores the text "John." String variables are used to hold textual data.

4. Boolean Variable:

is_student = True


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"]


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)


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}


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


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


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



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.