MCS-201 · Unit 10

Data Structures and Control Statements in Python

Section 2 of 12

Block 3 · Unit 10Data Structures and Control Statements in Python8 min · Section 2 of 12

10.2Variables and Assignment

Covers IGNOU MCS-201 §10.5

Variables in Python look like variables in C, but they work differently underneath. Understanding this difference now will prevent many surprises when you work with lists and dictionaries later.

Names and objects

x = 5, then x = 'five'
x ──► 5            x ──► 'five'
                         5   (no longer referenced: freed)

Syntax:

Assignment
variable_name = expression

The expression on the right is evaluated first, then the name on the left is bound to the result. The = sign means “make this name refer to”, not mathematical equality; comparison uses ==.

Forms of assignment

FormExampleEffect
Simplenumber = 100Binds number to 100
Reassignment, possibly to another typeyear = 2020, then year = "twenty twenty"year now refers to a string
Same value to several namesx = y = z = 5All three names refer to the same object 5
Several values at oncename, age = "Nav", 31name gets “Nav”, age gets 31
Augmentedcount += 1Same as count = count + 1

Using a name that has never been assigned raises NameError.

Key takeaways

  • name = expression evaluates the right side, then binds the name.
  • Names have no fixed type; values do.
  • x = y = 5 and a, b = 1, 2 assign several names in one statement.
  • An unknown name raises NameError.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

What is the value of a after a = 25, b = 35, a = b?

Show answer

Answer: B. 35

a = b makes a refer to the value of b, which is 35.

Question 2 · multiple choice

Which error does total = subtotal + 5 raise if subtotal was never assigned?

Show answer

Answer: C. NameError

Python cannot find the name subtotal. The line’s syntax is correct, so it is not a SyntaxError.

Question 3 · multiple choice

What does this program print?

Python · quiz_swap.py
p, q = 3, 8
p, q = q, p + q
print(p, q)
Show answer

Answer: B. 8 11

The right side is evaluated first using the old values (8 and 3 + 8 = 11), and then both names are assigned.

Question 4 · multiple choice

Which is a valid assignment?

Show answer

Answer: D. x = y = 2

Chained assignment binds both names to 2. The left side of = must be a name (or a target such as a[0]), and y += 2 is a statement, not an expression, so it cannot appear on the right.

Question 5 · multiple choice

After count = 10 and count = "ten", what is type(count)?

Show answer

Answer: B. str

The name now refers to a string object; its earlier binding to 10 no longer matters.

Question 6 · short answer

Explain the difference between x = 5 in C and in Python.

Show worked answer

In C, int x = 5; declares a fixed-size memory location of type int named x and stores 5 in it; x can only ever hold an int. In Python, x = 5 creates (or reuses) an integer object 5 and binds the name x to it; there is no declaration, and later x = "five" simply rebinds the name to a string object. In Python the type belongs to the object, not to the name.

↑ ↓ move↵ openTab switch type