Variables :- A Programmer's Perspective
Understanding variables from a programmer’s perspective is fundamental to learning Python. Think of variables as named storage locations in a computer’s memory that hold values. They are like containers or baskets where you can put different kinds of “stuff”.
Here’s an easy-to-understand breakdown:
What are Variables? 🧺
- Containers for Values: Variables are names that refer to a value. For example, when you write
a = 10
,a
is the variable, and10
is the value it holds. - Dynamic Nature: In Python, you don’t need to explicitly declare a variable’s type before using it. Python automatically determines the data type of the value you assign to it.
- For instance, if you assign
n = 10
, Python recognisesn
as an integer (int
). - If you then assign
n = "Sudarshan"
,n
becomes a string (str
). It’s like using the same jar in your kitchen to store rice or water – the jar (variable) remains, but its content (value and type) changes.
- For instance, if you assign
- Mutability: The values stored in variables can be changed throughout your program. This is why they’re called “variables” – their values can vary.
Why Use Variables? (A Programmer’s Perspective) 🤔
Programmers use variables for several key reasons that make code more flexible, readable, and maintainable:
- Self-Explanatory Names 🏷️: Instead of using vague names like
A
,B
,C
, andD
for different values, it’s a good practice to use names that clearly describe what the variable represents, likeram_bank_balance
orlakshman_loan
. This makes your code much easier to understand, especially when you revisit it later or when others read it. - Flexibility and Interaction 💬: Variables allow your programs to be interactive and dynamic. Instead of hardcoding values directly into your operations (literals), you can store user inputs or changing data in variables. For example, a program asking for a user’s name can store it in a
name
variable and use it to personalise greetings. - Reusability ♻️: Once a value is assigned to a variable, you can use that variable multiple times in different parts of your code without having to retype the actual value.
- Clarity in Calculations 🧮: For values that are constant (like Pi,
3.14
), you might use them as literals. However, for values that might change (like the radius of a circle or a person’s age), using variables is essential.
How Variables Work with Code: Operations and Concepts 💡
- Assignment Operator (
=
): This is how you give a value to a variable. The value on the right of the=
sign is assigned to the variable on the left.- Example:
age = 30
- Example:
- Using Variables in Expressions: Variables can be used in mathematical or other expressions. Python replaces the variable with its current value when the expression is evaluated.
- Example:
total = price + tax
- Example:
- Incrementing and Decrementing (Updating Variables) 📈📉: A very common operation is to update a variable’s value based on its existing value.
- You can write
count = count + 1
. - Python offers a shorthand operator for this:
count += 1
. This is equivalent and makes your code more concise. This also applies to other arithmetic operations like multiplication (count *= 2
) or division (count /= 2
).
- You can write
- Multiple Assignment 👯: Python allows you to assign values to multiple variables in a single line.
- Assigning different values:
x, y = 1, 2
assigns1
tox
and2
toy
. The order of values matters here. - Assigning the same value:
x = y = z = 10
assigns the value10
to all three variablesx
,y
, andz
.
- Assigning different values:
- Swapping Values 🔄: A neat trick in Python is to swap the values of two variables without needing a temporary variable, using multiple assignment:
- Example:
nudge, wink = wink, nudge
. This internally creates a temporary tuple to hold the values, then assigns them.
- Example:
- Deleting Variables: You can remove a variable from memory using the
del
keyword. Once deleted, trying to use it will result in an error.- Example:
del x
- Example:
Important Concept: Shared References (A “Gotcha” to Watch Out For!) ⚠️
When you assign one variable to another, like L2 = L1
, both variables end up referencing the exact same object in memory. This is called a shared reference.
- For immutable objects (like numbers, strings, and tuples), this usually isn’t an issue because you can’t change them in place. If you reassign
L1
to a new object,L2
will still point to the original object. - However, for mutable objects (like lists and dictionaries), if you modify the object through one variable, the changes will be reflected when you access it through the other variable, because they both point to the same object.
- Example:
L1 = L2 = L1 L1.append(4) print(L2) # Output: -- L2 also changed!
- To create a true copy of a mutable object, you often need to use slicing (
L2 = L1[:]
) or specific copy methods.
- Example:
Common Mistakes with Variables 😅
- Using Python Keywords: You cannot use Python’s reserved words (like
if
,for
,print
,True
,False
,None
) as variable names. These words have special meaning to the Python interpreter. - Invalid Naming Rules: Variable names must start with an alphabet (A-Z, a-z) or an underscore (
_
). They cannot start with a number (1a
is invalid). They can contain alphanumeric characters and underscores. - Case Sensitivity: Python variable names are case-sensitive.
roll
,Roll
, andROLL
are treated as three distinct variables. - Using Unassigned Variables: You must assign a value to a variable before you can use it in an expression. Trying to use an unassigned variable will result in a
NameError
. - Literals on the Left: You cannot assign a value to a literal (e.g.,
30 = 30 + 1
or42 = n
). The left side of an assignment must be a variable name or something that can be assigned to. - Confusing Assignment and Equality: Remember that
=
is for assignment, while==
is for checking if two values are equal.
Practice Questions 🧠
- What is a variable in Python, in simple terms?
- Which of the following are valid Python variable names, and why?
_my_variable
1st_number
totalAmount
if
price$
- Explain the difference between
x = 5
andy = x
. If you then runx = 10
, what will be the value ofy
? - You have a list
my_list =
. If you then sayanother_list = my_list
, and lateranother_list.append(4)
, what willmy_list
contain? Why? - How would you swap the values of two variables,
a
andb
, in a single line of Python code?
Answers 📜
- A variable in Python is like a named container or label in the computer’s memory that holds a value. Its value can be changed, and Python automatically figures out the type of data it holds.
_my_variable
: Valid ✅. Starts with an underscore and uses alphanumeric characters.1st_number
: Invalid ❌. Variable names cannot start with a number.totalAmount
: Valid ✅. Uses only alphanumeric characters and starts with an alphabet. Python variable names are case-sensitive, sototalAmount
is different fromtotalamount
.if
: Invalid ❌.if
is a reserved keyword in Python.price$
: Invalid ❌. Variable names can only contain alphanumeric characters and underscores. The$
symbol is not allowed.
x = 5
: This assigns the integer value5
to the variable namedx
.y = x
: This assigns the value currently held byx
(which is5
) to the variable namedy
. At this point, bothx
andy
hold the value5
.- If you then run
x = 10
,x
is reassigned to a new value,10
. The value ofy
will remain5
. This is because numbers (integers) are immutable; changingx
to refer to a new object10
does not affecty
which still refers to the original object5
.
- If you say
my_list =
, and thenanother_list = my_list
, bothmy_list
andanother_list
are pointing to (referencing) the same list object in memory.- When you then execute
another_list.append(4)
, you are modifying the shared list object in place. - Therefore,
my_list
will contain ``.
- When you then execute
- To swap the values of two variables,
a
andb
, in a single line, you can use Python’s multiple assignment feature:a, b = b, a
- This is a concise and common Pythonic way to swap variables.