Variables & Operators β Python Questions with Answers
Start with the study notes on Variables & Operators (definitions, examples and a quick revision), then work through 30 practice questions, each with the correct answer and a clear explanation of why it is right. When you are ready, take this topic as an interactive quiz to test yourself.
16 easy Β· 10 medium Β· 4 hard Β· Part of Python: Master Python from basics to advanced - 20 topics.
Topic overview
Variables and Operators β storing values and calculating
1. Variables
A variable is a name that refers to a value. You create one with = (assignment). Python is dynamically typed: a name can later refer to a value of a different type.
x = 5
print(x)
x = "hello" # now x refers to a string
print(x)
Output5
hello
Naming rules
Start with a letter or underscore, then letters, digits or underscores.
No spaces or hyphens, and no keywords such as class, if or lambda.
Names are case-sensitive: total and Total are different.
Style: snake_case for variables, ALL_CAPS for constants, e.g. MAX_USERS = 100.
Variable names must start with a letter or underscore, cannot contain hyphens, and cannot be reserved keywords. "_total" is valid. "2count" starts with a digit, "my-var" contains a hyphen, and "class" is a keyword.
By convention, use snake_case: student_name, total_marks.
KeywordsIdentifiers
Q2.easy
What is the output of:
x = 5
y = x
x = 10
print(y)
x = 5
y = x # y refers to 5
x = 10 # only x changes
print(y) # 5
A 10
B 5
C Error
D None
Answer: B β 5
y = x makes y refer to the value 5. Reassigning x to 10 later does not change y - numbers are immutable, and y still points to 5.
AssignmentReferences
Q3.easy
Are total and Total the same variable in Python?
A Yes, capitalization doesn't matter
B No - Python is case-sensitive, they are two different variables
C Only inside functions
D Only if declared with var
Answer: B β No - Python is case-sensitive, they are two different variables
Python is case-sensitive: total, Total and TOTAL are three separate variables. Using the wrong capitalization gives a NameError.
Case sensitivityNameError
Q4.easy
Which of these words CANNOT be used as a variable name?
import keyword
print(keyword.kwlist) # shows all 35+ reserved words
A value
B lambda
C data
D result
Answer: B β lambda
lambda is a reserved keyword (it creates anonymous functions), so it cannot be a variable name. Other keywords include if, for, class, def, return, True, False, None.
Keywordslambda
Q5.easy
What does this multiple assignment do?
a, b = 1, 2
a, b = 1, 2
x, y, z = "do", "re", "mi"
A Error - only one variable per line
B a becomes 1 and b becomes 2
C Both a and b become (1, 2)
D a becomes 2 and b becomes 1
Answer: B β a becomes 1 and b becomes 2
Python unpacks the values left to right: a gets 1, b gets 2. This is called tuple unpacking and works with any matching number of values.
UnpackingTuples
Q8.easy
What does x += 3 mean?
x = 10
x += 3 # x is now 13
x *= 2 # x is now 26
A x = x + 3
B x = 3
C Compare x with 3
D x = x * 3
Answer: A β x = x + 3
+= is an augmented assignment operator: it adds 3 to the current value of x. Similar shortcuts: -=, *=, /=, //=, %=, **=.
Augmented assignmentOperators
Q9.easy
What is the output of: print(7 // 2)
print(7 / 2) # 3.5
print(7 // 2) # 3
A 3.5
B 3
C 4
D 3.0
Answer: B β 3
// is floor division - it divides and rounds DOWN to a whole number: 7 // 2 = 3. Regular division 7 / 2 would give 3.5.
Answer: B β = assigns a value; == compares two values
One equals sign stores a value in a variable (x = 5). Two equals signs ask a question - are these equal? - and give True or False (x == 5).
Writing if x = 5: instead of if x == 5: is a SyntaxError in Python.
AssignmentComparison operators
Q16.easy
Which operator means NOT EQUAL in Python?
print(5 != 3) # True
print(5 != 5) # False
A <>
B !=
C =/=
D not=
Answer: B β !=
!= tests inequality: 5 != 3 is True. (The old <> form existed in Python 2 but was removed in Python 3.)
Comparison operators
Q17.easy
What is the output of: print(True and False)
print(True and True) # True
print(True and False) # False
print(True or False) # True
A True
B False
C 1
D Error
Answer: B β False
and is True only when BOTH sides are True. True and False -> False. Compare: or is True when AT LEAST ONE side is True.
Logical operatorsTruth tables
Q23.easy
By convention, how are constants written in Python?
PI = 3.14159
MAX_USERS = 100
A ALL_CAPS_WITH_UNDERSCORES
B camelCase
C with the const keyword
D prefixed with $
Answer: A β ALL_CAPS_WITH_UNDERSCORES
Python has no true const keyword - instead, ALL_CAPS names like MAX_SPEED = 120 signal 'please don't change this'. It's a convention, not enforced by the language.
Naming conventionsPEP 8
Q29.easy
What is the output of: print(abs(-5))
print(abs(-5)) # 5
print(abs(3.7)) # 3.7
A -5
B 5
C 0
D Error
Answer: B β 5
abs() returns the absolute value - the distance from zero, always positive: abs(-5) = 5, abs(5) = 5.
Built-in functionsNumbers
Medium questions (10)
Q6.medium
What is the output of:
a, b = 5, 10
a, b = b, a
print(a, b)
a, b = 5, 10
a, b = b, a # swap!
print(a, b) # 10 5
A 5 10
B 10 5
C 10 10
D Error
Answer: B β 10 5
a, b = b, a swaps the two variables in one line - the right side (10, 5) is built first, then unpacked. Most languages need a temporary third variable for this.
This one-line swap is a favorite Python interview question.
UnpackingSwapping
Q7.medium
After: x = y = z = 7 - what are the values?
x = y = z = 7
print(x, y, z) # 7 7 7
A Only z is 7; x and y are undefined
B x, y and z are all 7
C Error - chained assignment is not allowed
D x is 7, y and z are None
Answer: B β x, y and z are all 7
Chained assignment gives the same value to every name in the chain: all three variables now refer to 7.
Careful with mutable objects: x = y = [] makes BOTH names share ONE list.
AssignmentReferences
Q12.medium
What is the result type of: 10 / 5
print(10 / 5) # 2.0
print(10 // 5) # 2
A int (2)
B float (2.0)
C str ('2')
D Error
Answer: B β float (2.0)
In Python 3, / ALWAYS returns a float - even when the division is exact: 10 / 5 gives 2.0, not 2. Use // if you need an integer result.
5 > 3 is True, and not flips it to False. not simply reverses a boolean value.
not operatorBooleans
Q19.medium
What is the output of: print(1 < 2 < 3)
age = 25
print(18 <= age < 60) # True - very readable!
A True
B False
C Error
D 3
Answer: A β True
Python allows chained comparisons: 1 < 2 < 3 means (1 < 2) and (2 < 3) - both are True, so the result is True. Most languages don't support this.
Chained comparisonComparison operators
Q22.medium
What does divmod(7, 2) return?
q, r = divmod(7, 2)
print(q, r) # 3 1
A 3.5
B (3, 1) - the quotient and remainder
C [3, 1]
D 1
Answer: B β (3, 1) - the quotient and remainder
divmod(a, b) returns the tuple (a // b, a % b) - the whole-number quotient and the remainder in one call.
Floor divisionModuloTuples
Q24.medium
What happens after:
x = 10
del x
print(x)
x = 10
del x
# print(x) # NameError: name 'x' is not defined
A Prints 10
B Prints None
C NameError - x is not defined
D Prints 0
Answer: C β NameError - x is not defined
del removes the name x entirely. Using it afterwards raises NameError, exactly as if x had never existed.
del statementNameError
Q25.medium
What does this show about Python?
x = 5
x = "hello"
print(x)
x = 5 # int
x = "hello" # now str - perfectly legal
print(type(x)) # <class 'str'>
A Error - x is locked to numbers
B Prints hello - variables can be rebound to any type (dynamic typing)
C Prints 5
D Prints 5hello
Answer: B β Prints hello - variables can be rebound to any type (dynamic typing)
Python is dynamically typed: a variable is just a name and can point to a value of any type at any time. x pointed to an int, now it points to a string.
Convenient, but it means type errors appear at runtime, not before.
Dynamic typingtype()
Q26.medium
What does the global keyword do inside a function?
B Lets the function modify a variable defined at module level
C Makes the variable visible to other files
D Declares a constant
Answer: B β Lets the function modify a variable defined at module level
Assigning inside a function normally creates a LOCAL variable. global tells Python the name refers to the module-level variable, so the function modifies the global one.
Prefer return values over globals - easier to test and debug.
ScopeLEGB rule
Q30.medium
What is the output of: print(min(4, 7, 1), max(4, 7, 1))
min() returns the smallest of its arguments (1) and max() the largest (7). They also work on lists: min([4, 7, 1]).
Built-in functionssum()
Hard questions (4)
Q20.hard
What is the output of:
result = 0 or "hello"
print(result)
print(0 or "hello") # hello
print("hi" or "bye") # hi (first truthy wins)
print(0 and "hello") # 0 (and stops at first falsy)
A 0
B True
C hello
D Error
Answer: C β hello
or returns the FIRST truthy value (not just True/False!). 0 is falsy, so or moves on and returns "hello" itself. This is called short-circuit evaluation.
Common use: name = user_input or "Guest" (default value).
Short-circuit evaluationTruthiness
Q21.hard
What is the output of: print(-7 // 2)
print(7 // 2) # 3
print(-7 // 2) # -4 (not -3!)
A -3
B -3.5
C -4
D 4
Answer: C β -4
Floor division rounds DOWN (toward negative infinity), not toward zero. -7 / 2 = -3.5, and rounding DOWN gives -4. This surprises people from C/Java where -7/2 truncates to -3.
Floor divisionNegative numbers
Q27.hard
What is the difference between == and is?
a = [1, 2]
b = [1, 2]
print(a == b) # True - same values
print(a is b) # False - two separate lists
print(a is None) # False - the correct use of is
A They are identical
B == compares values; is checks if both names point to the SAME object in memory
C is compares values; == checks memory
D is only works with numbers
Answer: B β == compares values; is checks if both names point to the SAME object in memory
== asks 'do these have equal values?'. is asks 'are these literally the same object?'. Two equal lists are == but not is. Use is only for None checks.
Always write: if x is None: (not == None).
Identityid()None
Q28.hard
What is the output of: print(round(2.5), round(3.5))
Python uses banker's rounding: exact halves round to the nearest EVEN number. round(2.5) -> 2, round(3.5) -> 4. This reduces bias when rounding many values.