1. What is the output of the following code?
x = [1, 2, 3]
y = x
y.append(4)
print(x)
Output: [1, 2, 3, 4]
The whole question comes down to one sentence: in Python, variables hold references, not copies.
x = [1, 2, 3] creates a list object in memory and makes x point to it. y = x does not copy the list — it makes y point to the same list object. Both names reference one and the same thing.
Now y.append(4) mutates that shared list. Since x and y are just two names for the same object, the change is visible through both. Printing x shows [1, 2, 3, 4].
If you actually wanted an independent copy, you’d need y = x.copy() (or list(x), or a slice x[:]). Then appending to y would leave x untouched.
The interview point: assignment never copies. It binds a new name to the existing object, and mutable objects are shared through all their names.
Answer:
[1, 2, 3, 4]
The whole question comes down to one sentence: in Python, variables hold references, not copies.
x = [1, 2, 3] creates a list object in memory and makes x point to it. y = x does not copy the list — it makes y point to the same list object. Both names reference one and the same thing.
Now y.append(4) mutates that shared list. Since x and y are just two names for the same object, the change is visible through both. Printing x shows [1, 2, 3, 4].
If you actually wanted an independent copy, you’d need y = x.copy() (or list(x), or a slice x[:]). Then appending to y would leave x untouched.
The interview point: assignment never copies. It binds a new name to the existing object, and mutable objects are shared through all their names.
2. What is the difference between the == operator and the is operator in Python?
Answer: == compares values; is compares identity (whether two references point to the exact same object in memory).
==asks: “do these two objects hold equal values?” It dispatches to the__eq__method, which can be customized per type. Two distinct lists with the same contents compare equal with==.isasks: “are these the same object?” It’s reference equality — equivalent toid(a) == id(b). Two objects with identical contents can still fail anischeck if they live at different addresses.
The classic gotcha is with small integers and short strings, which Python interns — it reuses the same object for common values. That’s why x = 1000; y = 1000; x is y is often False, while small values like x = 5; y = 5; x is y is True. The interning makes is look like value comparison for some literals, but that’s an implementation detail — is is only for comparing identity.
The practical rules: use is for singleton checks like x is None and x is True; use == for everything that’s actually about values.
Answer:
== compares values; is compares identity (whether two references point to the exact same object in memory).
==asks: “do these two objects hold equal values?” It dispatches to the__eq__method, which can be customized per type. Two distinct lists with the same contents compare equal with==.isasks: “are these the same object?” It’s reference equality — equivalent toid(a) == id(b). Two objects with identical contents can still fail anischeck if they live at different addresses.
The classic gotcha is with small integers and short strings, which Python interns — it reuses the same object for common values. That’s why x = 1000; y = 1000; x is y is often False, while small values like x = 5; y = 5; x is y is True. The interning makes is look like value comparison for some literals, but that’s an implementation detail — is is only for comparing identity.
The practical rules: use is for singleton checks like x is None and x is True; use == for everything that’s actually about values.
3. What is the output of the following slice operation?
nums = [10, 20, 30, 40, 50]
print(nums[::-2])
Output: [50, 30, 10]
A slice is written nums[start:stop:step], and the rules are: start is where you begin (inclusive), stop is where you end (exclusive), and step is how you move. All three can be omitted.
With [::-2], both boundaries are empty, and the step is -2. An empty boundary means “use the natural end of the sequence” — and a negative step means the natural direction is reversed, so the slice starts from the last element.
So it begins at 50, steps backward by 2: 50 → 30 → 10. It stops when the negative step runs past the start of the list. The result is [50, 30, 10].
The general trick to remember: [::-1] reverses a sequence, and [::-2] is “every second element, going backward.”
Answer:
[50, 30, 10]
A slice is written nums[start:stop:step], and the rules are: start is where you begin (inclusive), stop is where you end (exclusive), and step is how you move. All three can be omitted.
With [::-2], both boundaries are empty, and the step is -2. An empty boundary means “use the natural end of the sequence” — and a negative step means the natural direction is reversed, so the slice starts from the last element.
So it begins at 50, steps backward by 2: 50 → 30 → 10. It stops when the negative step runs past the start of the list. The result is [50, 30, 10].
The general trick to remember: [::-1] reverses a sequence, and [::-2] is “every second element, going backward.”
4. What is the output of the following tuple unpacking statement?
a, *b, c = (1, 2, 3, 4, 5)
print(b)
Output: [2, 3, 4]
This is extended iterable unpacking, also called star unpacking. The * captures “everything in the middle.”
The tuple has five elements: 1, 2, 3, 4, 5. a takes the first (1), c takes the last (5), and the starred variable *b gobbles up everything between them — 2, 3, 4.
One detail that catches people: a starred variable always collects its items into a list, not a tuple. So b is [2, 3, 4], not (2, 3, 4).
The pattern is extremely common — splitting a sequence into a head, a tail, or a middle: first, *rest = data, *head, last = data. The star name can only appear once per unpacking (on the left side of an assignment), and it can be empty when there’s nothing in the middle.
Answer:
[2, 3, 4]
This is extended iterable unpacking, also called star unpacking. The * captures “everything in the middle.”
The tuple has five elements: 1, 2, 3, 4, 5. a takes the first (1), c takes the last (5), and the starred variable *b gobbles up everything between them — 2, 3, 4.
One detail that catches people: a starred variable always collects its items into a list, not a tuple. So b is [2, 3, 4], not (2, 3, 4).
The pattern is extremely common — splitting a sequence into a head, a tail, or a middle: first, *rest = data, *head, last = data. The star name can only appear once per unpacking (on the left side of an assignment), and it can be empty when there’s nothing in the middle.
5. What will be the output of this code snippet?
print(bool([])), print(bool("False")), print(bool(0.0))
Output: False True False
This tests Python’s truthiness rules — which values are treated as True and which as False in a boolean context.
Three values are being tested:
bool([])— an empty list. All empty collections ([],(),{},set(),"") are falsy. Result:False.bool("False")— a non-empty string. The contents don’t matter; only whether the string has any characters."False"has five characters, so it’s truthy. Result:True. This is the one that trips people up — the string saying “False” is still a truthy value.bool(0.0)— numeric zero. Zero of any numeric type (0,0.0,0j) is falsy. Result:False.
Output: False True False.
The rule of thumb: None, False, zero, and empty collections are falsy; everything else is truthy.
Answer:
False True False
This tests Python’s truthiness rules — which values are treated as True and which as False in a boolean context.
Three values are being tested:
bool([])— an empty list. All empty collections ([],(),{},set(),"") are falsy. Result:False.bool("False")— a non-empty string. The contents don’t matter; only whether the string has any characters."False"has five characters, so it’s truthy. Result:True. This is the one that trips people up — the string saying “False” is still a truthy value.bool(0.0)— numeric zero. Zero of any numeric type (0,0.0,0j) is falsy. Result:False.
Output: False True False.
The rule of thumb: None, False, zero, and empty collections are falsy; everything else is truthy.
6. What will print(type((1))) and print(type((1,))) output respectively?
Answer: int and tuple.
The difference is the trailing comma.
(1) — parentheses with a single value and no comma — are just grouping parentheses, like in arithmetic. (1) is the integer 1 wrapped in brackets that do nothing. So type((1)) is int.
(1,) — the same value with a comma — is the syntax for a one-element tuple. The comma is what makes it a tuple, not the parentheses (the parentheses are optional in most contexts: 1, is also a tuple). So type((1,)) is tuple.
This is a classic interview trap because a single-element tuple has a special syntax that everyone forgets. An empty tuple is () (no comma needed), a one-element tuple is (x,) (comma essential), and two or more elements are (x, y, z).
Answer:
int and tuple.
The difference is the trailing comma.
(1) — parentheses with a single value and no comma — are just grouping parentheses, like in arithmetic. (1) is the integer 1 wrapped in brackets that do nothing. So type((1)) is int.
(1,) — the same value with a comma — is the syntax for a one-element tuple. The comma is what makes it a tuple, not the parentheses (the parentheses are optional in most contexts: 1, is also a tuple). So type((1,)) is tuple.
This is a classic interview trap because a single-element tuple has a special syntax that everyone forgets. An empty tuple is () (no comma needed), a one-element tuple is (x,) (comma essential), and two or more elements are (x, y, z).
7. What is the result of evaluating 0.1 + 0.2 == 0.3 in standard Python?
Answer: False.
This isn’t a Python bug — it’s a property of how computers represent floating-point numbers.
Binary floating-point (IEEE 754) can’t represent every decimal exactly. Just as 1/3 has an infinite decimal expansion, 0.1 and 0.2 have infinite binary expansions, and the machine stores a rounded approximation of each. When you add those two approximations, you get something slightly off from 0.3: 0.30000000000000004.
So 0.1 + 0.2 produces 0.30000000000000004, and comparing it to 0.3 fails. The result is False.
The practical rules: never compare floats with ==. Use a tolerance — abs(a - b) < 1e-9 — or, for money and anything needing exact decimal arithmetic, use Python’s decimal.Decimal module. The interview answer: False, because of floating-point rounding.
Answer:
False.
This isn’t a Python bug — it’s a property of how computers represent floating-point numbers.
Binary floating-point (IEEE 754) can’t represent every decimal exactly. Just as 1/3 has an infinite decimal expansion, 0.1 and 0.2 have infinite binary expansions, and the machine stores a rounded approximation of each. When you add those two approximations, you get something slightly off from 0.3: 0.30000000000000004.
So 0.1 + 0.2 produces 0.30000000000000004, and comparing it to 0.3 fails. The result is False.
The practical rules: never compare floats with ==. Use a tolerance — abs(a - b) < 1e-9 — or, for money and anything needing exact decimal arithmetic, use Python’s decimal.Decimal module. The interview answer: False, because of floating-point rounding.
8. What will be the output of print(“python”[::-1][::1])?
Output: "nohtyp"
This is two slices chained, evaluated left to right.
First, "python"[::-1]. The negative step reverses the string: "nohtyp".
Then the result, "nohtyp"[::1]. A step of 1 moves forward through every character in order — the identity slice. It changes nothing.
So the chain reduces to just the reversal: "nohtyp".
The lesson: [::-1] reverses; [::1] is a no-op. Chaining them in that order leaves you with a reversed string. If the order were flipped ([::1][::-1]), you’d also get "nohtyp" — because reversal is self-inverse and the identity slice is harmless.
Answer:
"nohtyp"
This is two slices chained, evaluated left to right.
First, "python"[::-1]. The negative step reverses the string: "nohtyp".
Then the result, "nohtyp"[::1]. A step of 1 moves forward through every character in order — the identity slice. It changes nothing.
So the chain reduces to just the reversal: "nohtyp".
The lesson: [::-1] reverses; [::1] is a no-op. Chaining them in that order leaves you with a reversed string. If the order were flipped ([::1][::-1]), you’d also get "nohtyp" — because reversal is self-inverse and the identity slice is harmless.
9. What will print(1 or 2) and print(1 and 2) display?
Output: 1 and 2
Python’s or and and don’t return booleans — they return one of their operands, using short-circuit evaluation.
1 or 2:orreturns the first truthy operand.1is truthy, so evaluation stops right there and1is returned. Result:1.1 and 2:andreturns the first falsy operand, or the last operand if all are truthy.1is truthy, so it continues to2.2is the last operand, so it’s returned. Result:2.
The general rules:
a or b→aifais truthy, elseb.a and b→aifais falsy, elseb.
This is why or is used for defaults — name = user_input or "default" — and and for guarding. The values come back untouched, not coerced to True/False. That’s the distinction this question tests.
Answer:
1 and 2
Python’s or and and don’t return booleans — they return one of their operands, using short-circuit evaluation.
1 or 2:orreturns the first truthy operand.1is truthy, so evaluation stops right there and1is returned. Result:1.1 and 2:andreturns the first falsy operand, or the last operand if all are truthy.1is truthy, so it continues to2.2is the last operand, so it’s returned. Result:2.
The general rules:
a or b→aifais truthy, elseb.a and b→aifais falsy, elseb.
This is why or is used for defaults — name = user_input or "default" — and and for guarding. The values come back untouched, not coerced to True/False. That’s the distinction this question tests.
10. How does isinstance(True, int) evaluate in Python?
Answer: True.
In Python, bool is a subclass of int. The two boolean values are just special integers: True == 1 and False == 0.
isinstance(obj, type) checks whether the object’s type is the given type or any of its subclasses. Since bool subclasses int, isinstance(True, int) is True.
This has real, occasionally surprising consequences:
True + Truegives2.["a", "b"][True]gives"b"(index 1).sum([True, False, True])gives2— a handy trick for counting.
Note the asymmetry: isinstance(True, int) is True, but the reverse direction — isinstance(1, bool) — is False. An int is not necessarily a bool; bools are a narrower type on top of ints. The interview answer is simply True, because bool inherits from int.
Answer:
True.
In Python, bool is a subclass of int. The two boolean values are just special integers: True == 1 and False == 0.
isinstance(obj, type) checks whether the object’s type is the given type or any of its subclasses. Since bool subclasses int, isinstance(True, int) is True.
This has real, occasionally surprising consequences:
True + Truegives2.["a", "b"][True]gives"b"(index 1).sum([True, False, True])gives2— a handy trick for counting.
Note the asymmetry: isinstance(True, int) is True, but the reverse direction — isinstance(1, bool) — is False. An int is not necessarily a bool; bools are a narrower type on top of ints. The interview answer is simply True, because bool inherits from int.
11. What will print(“a” “b” “c”) output?
Output: abc
This is string literal concatenation: when two string literals sit next to each other (separated only by whitespace), the Python parser joins them into a single string — at compile time, before the program even runs.
"a" "b" "c" is parsed as the single literal "abc". There are no operators, no commas, no function calls — just adjacency.
The result prints abc.
This feature is used in real code to split long strings across lines for readability, especially in SQL or docstrings. The important caveat: it only works with literals. Variables don’t combine this way — a b is a syntax error, and concatenating variables requires + or f-strings.
Answer:
abc
This is string literal concatenation: when two string literals sit next to each other (separated only by whitespace), the Python parser joins them into a single string — at compile time, before the program even runs.
"a" "b" "c" is parsed as the single literal "abc". There are no operators, no commas, no function calls — just adjacency.
The result prints abc.
This feature is used in real code to split long strings across lines for readability, especially in SQL or docstrings. The important caveat: it only works with literals. Variables don’t combine this way — a b is a syntax error, and concatenating variables requires + or f-strings.
12. What is the output of print(round(2.5)) and print(round(3.5)) in Python 3?
Output: 2 and 4
Python 3 uses banker’s rounding — “round half to even.” When a number is exactly halfway between two integers, it rounds to the nearest even integer, rather than always rounding up.
2.5is halfway between2and3. The even neighbor is2. Result:2.3.5is halfway between3and4. The even neighbor is4. Result:4.
This differs from the common “round half up” convention most people expect from arithmetic. It’s not an accident: rounding half to even avoids the systematic upward bias that plain “round half up” introduces when you sum many rounded numbers — the errors tend to cancel instead of accumulate.
Note this applies to the halfway case. 2.6 rounds to 3 normally, and 2.4 rounds to 2. Only exact halves follow the banker’s rule.
The interview answer: 2 and 4, because Python 3 rounds halves to the nearest even number.
Answer:
2 and 4
Python 3 uses banker’s rounding — “round half to even.” When a number is exactly halfway between two integers, it rounds to the nearest even integer, rather than always rounding up.
2.5is halfway between2and3. The even neighbor is2. Result:2.3.5is halfway between3and4. The even neighbor is4. Result:4.
This differs from the common “round half up” convention most people expect from arithmetic. It’s not an accident: rounding half to even avoids the systematic upward bias that plain “round half up” introduces when you sum many rounded numbers — the errors tend to cancel instead of accumulate.
Note this applies to the halfway case. 2.6 rounds to 3 normally, and 2.4 rounds to 2. Only exact halves follow the banker’s rule.
The interview answer: 2 and 4, because Python 3 rounds halves to the nearest even number.
13. What will print(3 * ‘2’) display?
Output: 222
The * operator on a string repeats it. '2' * 3 takes the string '2' and concatenates it with itself three times: '222'.
There is no numeric conversion happening. '2' is a string, and string repetition doesn’t turn it into the number 2. The multiplication produces '222' (a string), not 6 (a number).
The symmetry with lists is worth noting: [0] * 3 gives [0, 0, 0], the same repetition idea. The interview answer: '2' * 3 is '222'.
Answer:
222
The * operator on a string repeats it. '2' * 3 takes the string '2' and concatenates it with itself three times: '222'.
There is no numeric conversion happening. '2' is a string, and string repetition doesn’t turn it into the number 2. The multiplication produces '222' (a string), not 6 (a number).
The symmetry with lists is worth noting: [0] * 3 gives [0, 0, 0], the same repetition idea. The interview answer: '2' * 3 is '222'.
14. What will bool(datetime.time(0, 0, 0)) evaluate to in Python 3.5+?
Answer: True.
The trap is historical. Before Python 3.5, a datetime.time object was falsy when its time was midnight — time(0, 0, 0) evaluated to False. This matched the (bad) convention that “zero time” means “no time.”
Python 3.5 fixed it. Since then, every time object — including midnight — is truthy. The rationale: the only clearly-falsy values should be things representing “nothing,” and midnight is a perfectly valid moment in time, not an absence of time.
So bool(datetime.time(0, 0, 0)) is True in Python 3.5+.
The interview point is that this one changed between versions — a reminder that truthiness rules are just behavior Python defines, and they can (and occasionally do) evolve.
Answer:
True.
The trap is historical. Before Python 3.5, a datetime.time object was falsy when its time was midnight — time(0, 0, 0) evaluated to False. This matched the (bad) convention that “zero time” means “no time.”
Python 3.5 fixed it. Since then, every time object — including midnight — is truthy. The rationale: the only clearly-falsy values should be things representing “nothing,” and midnight is a perfectly valid moment in time, not an absence of time.
So bool(datetime.time(0, 0, 0)) is True in Python 3.5+.
The interview point is that this one changed between versions — a reminder that truthiness rules are just behavior Python defines, and they can (and occasionally do) evolve.
15. What is the output of print(1 < 2 < 3) and print(1 < 2 > 3)?
Output: True and False
Python supports chained comparisons — a single expression can string together several comparisons, and they’re evaluated as a conjunction.
1 < 2 < 3is(1 < 2) and (2 < 3). Both are true →True.1 < 2 > 3is(1 < 2) and (2 > 3). First true, second false →False.
Two details make chaining correct and efficient:
- The middle value is evaluated only once (no duplicated side effects).
- Evaluation short-circuits: if the first comparison fails, the rest aren’t evaluated.
Chaining is more than a curiosity — it’s the natural way to write range checks like 0 <= score <= 100, and it reads clearly. The interview answer: chained comparisons desugar into and-joined comparisons, giving True and False.
Answer:
True and False
Python supports chained comparisons — a single expression can string together several comparisons, and they’re evaluated as a conjunction.
1 < 2 < 3is(1 < 2) and (2 < 3). Both are true →True.1 < 2 > 3is(1 < 2) and (2 > 3). First true, second false →False.
Two details make chaining correct and efficient:
- The middle value is evaluated only once (no duplicated side effects).
- Evaluation short-circuits: if the first comparison fails, the rest aren’t evaluated.
Chaining is more than a curiosity — it’s the natural way to write range checks like 0 <= score <= 100, and it reads clearly. The interview answer: chained comparisons desugar into and-joined comparisons, giving True and False.
16. What will print(type(type)) output?
Output: <class 'type'>
This is a question about metaclasses — the classes that classes themselves belong to.
Every object in Python is an instance of some class. An integer is an instance of int; a string is an instance of str. But what is type itself an instance of? The answer is its own metaclass:
typeis the metaclass for all built-in classes —int,str,list, and so on.type(type)asks “what is the class oftype?” The answer:typeis itself an instance oftype. It’s the root of the metaclass hierarchy.
So type(type) prints <class 'type'>.
This is the bootstrapping at the heart of Python’s object model: type is an instance of itself. When you define a class with class Foo:, Python calls type (or a custom metaclass) to create the class object. The interview answer is simply <class 'type'>.
Answer:
<class 'type'>
This is a question about metaclasses — the classes that classes themselves belong to.
Every object in Python is an instance of some class. An integer is an instance of int; a string is an instance of str. But what is type itself an instance of? The answer is its own metaclass:
typeis the metaclass for all built-in classes —int,str,list, and so on.type(type)asks “what is the class oftype?” The answer:typeis itself an instance oftype. It’s the root of the metaclass hierarchy.
So type(type) prints <class 'type'>.
This is the bootstrapping at the heart of Python’s object model: type is an instance of itself. When you define a class with class Foo:, Python calls type (or a custom metaclass) to create the class object. The interview answer is simply <class 'type'>.
17. What is the output of print(3 and 0 or 5)?
Answer: 5
Evaluate left to right, respecting that and binds tighter than or, and that both return operand values.
Step 1: 3 and 0. and returns the first falsy operand, or the last operand if all are truthy. 3 is truthy, so it proceeds to 0, which is falsy → the result is 0.
Step 2: 0 or 5. or returns the first truthy operand. 0 is falsy, so it moves to 5, which is truthy → the result is 5.
So the expression reduces to 5. The interview point is the short-circuit return-value semantics: these operators hand back operands, not booleans, and you must evaluate in stages.
Answer:
5
Evaluate left to right, respecting that and binds tighter than or, and that both return operand values.
Step 1: 3 and 0. and returns the first falsy operand, or the last operand if all are truthy. 3 is truthy, so it proceeds to 0, which is falsy → the result is 0.
Step 2: 0 or 5. or returns the first truthy operand. 0 is falsy, so it moves to 5, which is truthy → the result is 5.
So the expression reduces to 5. The interview point is the short-circuit return-value semantics: these operators hand back operands, not booleans, and you must evaluate in stages.
18. What will print({1, 2} < {1, 2, 3}) return?
Output: True
On sets, the comparison operators take set-theoretic meanings. < tests proper subset: A < B is true when every element of A is in B and A is strictly smaller.
Here {1, 2} is a proper subset of {1, 2, 3} — both 1 and 2 are present in the larger set, and the sets are not equal. So the result is True.
The family of operators:
A < B— proper subset (A strictly contained).A <= B— subset (contained or equal).A > B— proper superset.A >= B— superset.
Note these aren’t element-wise comparisons like on lists — they’re pure subset relations. The interview answer: True, because {1, 2} is a proper subset of {1, 2, 3}.
Answer:
True
On sets, the comparison operators take set-theoretic meanings. < tests proper subset: A < B is true when every element of A is in B and A is strictly smaller.
Here {1, 2} is a proper subset of {1, 2, 3} — both 1 and 2 are present in the larger set, and the sets are not equal. So the result is True.
The family of operators:
A < B— proper subset (A strictly contained).A <= B— subset (contained or equal).A > B— proper superset.A >= B— superset.
Note these aren’t element-wise comparisons like on lists — they’re pure subset relations. The interview answer: True, because {1, 2} is a proper subset of {1, 2, 3}.
19. What will print(type(lambda: None)) output?
Output: <class 'function'>
A lambda is just a compact way to define a function. It creates the exact same underlying type as a def — the built-in function type. There is no separate lambda type.
lambda: None defines an anonymous function taking no arguments and returning None. Its type is function.
There’s no class 'lambda' — that’s a common misconception. The only difference between lambda and def is syntax: lambda is restricted to a single expression and has no name. Both produce function objects. The interview answer: <class 'function'>.
Answer:
<class 'function'>
A lambda is just a compact way to define a function. It creates the exact same underlying type as a def — the built-in function type. There is no separate lambda type.
lambda: None defines an anonymous function taking no arguments and returning None. Its type is function.
There’s no class 'lambda' — that’s a common misconception. The only difference between lambda and def is syntax: lambda is restricted to a single expression and has no name. Both produce function objects. The interview answer: <class 'function'>.
20. What is the output of print(5 // 2) and print(-5 // 2)?
Output: 2 and -3
Floor division (//) rounds down toward negative infinity, not toward zero — that distinction is the entire question.
5 // 2:5 / 2 = 2.5, floored →2.-5 // 2:-5 / 2 = -2.5, floored → the next integer below-2.5, which is-3.
The result for the negative case surprises people who expect truncation (which would give -2). Floor division never rounds up: it always goes to the greatest integer less than or equal to the exact quotient.
This is why // pairs with the % modulo operator such that a == (a // b) * b + (a % b) holds even for negatives. The interview answer: 2 and -3.
Answer:
2 and -3
Floor division (//) rounds down toward negative infinity, not toward zero — that distinction is the entire question.
5 // 2:5 / 2 = 2.5, floored →2.-5 // 2:-5 / 2 = -2.5, floored → the next integer below-2.5, which is-3.
The result for the negative case surprises people who expect truncation (which would give -2). Floor division never rounds up: it always goes to the greatest integer less than or equal to the exact quotient.
This is why // pairs with the % modulo operator such that a == (a // b) * b + (a % b) holds even for negatives. The interview answer: 2 and -3.
21. What will print(type(10 / 2)) output in Python 3?
Output: <class 'float'>
The / operator is true division in Python 3: it always produces a float, even when the division comes out exactly.
10 / 2 is 5.0 — a float — so type reports <class 'float'>.
This was a deliberate change from Python 2, where / performed integer division on integers (10 / 2 was 2, an int). In Python 3:
/— always float (true division).//— floor division; returns anintwhen both operands are ints.
The interview answer: <class 'float'> — Python 3’s / always yields a float.
Answer:
<class 'float'>
The / operator is true division in Python 3: it always produces a float, even when the division comes out exactly.
10 / 2 is 5.0 — a float — so type reports <class 'float'>.
This was a deliberate change from Python 2, where / performed integer division on integers (10 / 2 was 2, an int). In Python 3:
/— always float (true division).//— floor division; returns anintwhen both operands are ints.
The interview answer: <class 'float'> — Python 3’s / always yields a float.
Premium Content
Unlock Data Types & Operators and all premium lessons with a subscription.
From ₹199.99/year — See plans