Python Variable Names

Objectives: Python Variable Names

Python Variable Names - Complete Notes

1. What is a Variable?

A variable in Python is a name given to a piece of data or value stored in the computer's memory. It works like a label for a container that holds information.

Example:


x = 5
name = "Alice"

Here, x stores the integer 5 and name stores the string "Alice".

2. Rules for Naming Variables

When naming variables in Python, you must follow these rules:

  • Variable names must start with a letter (a-z, A-Z) or an underscore (_).
  • Variable names cannot start with a number.
  • Variable names can only contain letters, numbers, and underscores (A-Z, a-z, 0-9, _).
  • Variable names are case-sensitive. For example, age, Age, and AGE are different variables.
  • Variable names cannot be Python keywords like if, for, while, class, etc.

Examples of Legal Variable Names


myvar = "John"      # simple name
my_var = "John"     # with underscore
_my_var = "John"    # starts with underscore
myVar = "John"      # camel case
MYVAR = "John"      # uppercase
myvar2 = "John"     # contains number

Examples of Illegal Variable Names


2myvar = "John"     # starts with number
my-var = "John"     # contains hyphen
my var = "John"     # contains space

3. Understanding Case Sensitivity

Python treats variable names with different cases as different variables. The compiler/interpreter checks the exact letters and their case.


age = 25
Age = 30
AGE = 35

print(age)  # prints 25
print(Age)  # prints 30
print(AGE)  # prints 35

The Python interpreter stores each variable separately in memory.

4. Multi-word Variable Names

When a variable name has multiple words, readability is important. There are three popular naming conventions:

a) Camel Case

First word lowercase, each following word starts with a capital letter.


myVariableName = "Python"

b) Pascal Case

Each word starts with a capital letter.


MyVariableName = "Python"

c) Snake Case

Words are separated by underscores.


my_variable_name = "Python"

5. How Python Interpreter Handles Variables

When you run a Python script, the interpreter:

  1. Reads each line of code.
  2. Checks the syntax (compiler-like behavior, known as parsing).
  3. Stores variables in memory with their assigned values.
  4. References these names when you use them in expressions or functions.

Example:


x = 10
y = 20
sum_value = x + y

print(sum_value)  # The interpreter looks for x and y in memory, adds them, and prints 30

6. Real-life Examples


# Storing student information
student_name = "Alice"
student_age = 16
student_grade = "A"

# Calculating total cost
item_price = 50
quantity = 3
total_cost = item_price * quantity
print("Total Cost:", total_cost)

# Using multi-word variable names
current_temperature_celsius = 25
number_of_apples = 10

7. Summary

  • Always start variable names with a letter or underscore.
  • Do not use spaces, hyphens, or Python keywords.
  • Use readable multi-word naming conventions like camelCase, PascalCase, or snake_case.
  • Remember that Python variable names are case-sensitive.
  • The interpreter stores variables in memory and uses them whenever they are referenced.

Understanding variable naming well is key to writing clear, maintainable Python code.

Reference Book: N/A

Author name: MWALA_LEARN Work email: biasharabora12@gmail.com
#MWALA_LEARN Powered by MwalaJS #https://mwalajs.biasharabora.com
#https://educenter.biasharabora.com

:: 1::