🎯 07 – Interview Questions: Python Basics¶
These are real questions asked in Data Science and Python developer interviews.
🔢 Variables & Data Types¶
Q1: What is the difference between is and == in Python?
Answer:
==compares values;iscompares identity (same object in memory).Rule: Usea = [1, 2, 3] b = [1, 2, 3] print(a == b) # True (same values) print(a is b) # False (different objects) c = a # c points to the same object print(a is c) # Trueisonly forNone,True,Falsecomparisons. Use==for value comparisons.
Q2: What is the output of 0.1 + 0.2 == 0.3? Why?
Answer:
False. Floating point numbers can't be represented exactly in binary. Useabs(0.1 + 0.2 - 0.3) < 1e-9to compare floats.
Q3: What is the difference between int and float? When does division return a float?
Answer:
intstores whole numbers;floatstores decimals. In Python 3,/always returns a float (even4 / 2 = 2.0). Use//for integer division.
🔀 Control Flow¶
Q4: What is the difference between break and continue?
Answer:
breakexits the loop entirely.continueskips the rest of the current iteration and jumps to the next one.
Q5: What does this code print?
Answer:2— the loop variableiretains its last value after the loop ends, even withpass.
Q6: What is a list comprehension? Write one that filters and transforms.
Answer: A compact syntax for building lists. Example: get squares of even numbers from 1–20:
🔧 Functions¶
Q7: What is the difference between *args and **kwargs?
Answer: -
*argscollects extra positional arguments into a tuple -**kwargscollects extra keyword arguments into a dict
Q8: What is a lambda function? Write one that sorts a list of tuples by the second element.
Answer: An anonymous one-line function.
Q9: What is a mutable default argument trap?
Answer: Using a mutable object (list, dict) as a default parameter is a classic bug:
📚 Data Structures¶
Q10: What is the difference between a list and a tuple?
Answer: | | List | Tuple | |---|------|-------| | Mutable | ✅ | ❌ | | Syntax |
[1,2,3]|(1,2,3)| | Use case | Dynamic data | Fixed data, dict keys | | Speed | Slightly slower | Slightly faster |
Q11: How do you safely get a value from a dict that might not have the key?
Answer: Use
.get()with a default:
Q12: What is the time complexity of in for a list vs a set?
Answer: List: O(n) — must check every element. Set: O(1) — uses a hash table. For large membership tests, always use a set.
Q13: How do you remove duplicates from a list while preserving order?
Answer:
Q14: What is the difference between append() and extend()?
Answer: