Variables :- A Programmer's Perspective

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, and 10 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 recognises n 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.
  • 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, and D for different values, it’s a good practice to use names that clearly describe what the variable represents, like ram_bank_balance or lakshman_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
  • 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
  • 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).
  • Multiple Assignment 👯: Python allows you to assign values to multiple variables in a single line.
    • Assigning different values: x, y = 1, 2 assigns 1 to x and 2 to y. The order of values matters here.
    • Assigning the same value: x = y = z = 10 assigns the value 10 to all three variables x, y, and z.
  • 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.
  • 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

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.

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, and ROLL 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 or 42 = 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 🧠

  1. What is a variable in Python, in simple terms?
  2. Which of the following are valid Python variable names, and why?
    • _my_variable
    • 1st_number
    • totalAmount
    • if
    • price$
  3. Explain the difference between x = 5 and y = x. If you then run x = 10, what will be the value of y?
  4. You have a list my_list =. If you then say another_list = my_list, and later another_list.append(4), what will my_list contain? Why?
  5. How would you swap the values of two variables, a and b, in a single line of Python code?

Answers 📜

  1. 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, so totalAmount is different from totalamount.
    • 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 value 5 to the variable named x.
    • y = x: This assigns the value currently held by x (which is 5) to the variable named y. At this point, both x and y hold the value 5.
    • If you then run x = 10, x is reassigned to a new value, 10. The value of y will remain 5. This is because numbers (integers) are immutable; changing x to refer to a new object 10 does not affect y which still refers to the original object 5.
  2. If you say my_list =, and then another_list = my_list, both my_list and another_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 ``.
  3. To swap the values of two variables, a and b, 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.