Python Basics Interview Questions
Table of Contents + −
Python Basics Interview Questions
1. What is Python?
Python is a high-level, interpreted programming language known for its simplicity and readability. It supports multiple programming paradigms, including procedural, object-oriented, and functional programming.
2. What are the key features of Python?
- Easy to learn and read
- Interpreted language
- Dynamically typed
- Object-oriented
- Extensive standard library
- Cross-platform compatibility
- Large community support
3. What is the difference between Python 2 and Python 3?
Key differences include:
- Print function syntax
- Integer division
- String handling
- Unicode support
- Exception handling
- Input function
- Range function
4. What are Python’s data types?
Basic data types:
- Numbers (int, float, complex)
- Strings
- Booleans
- None
Compound data types:
- Lists
- Tuples
- Sets
- Dictionaries
5. What is the difference between a list and a tuple?
Lists:
- Mutable
- Created using []
- Slower than tuples
- More memory intensive
Tuples:
- Immutable
- Created using ()
- Faster than lists
- Less memory intensive
6. What is a dictionary in Python?
A dictionary is a collection of key-value pairs. It’s unordered, mutable, and indexed by keys.
# Creating a dictionarymy_dict = {'name': 'John', 'age': 30}
# Accessing valuesprint(my_dict['name']) # Output: John
# Adding new key-value pairsmy_dict['city'] = 'New York'
7. What are Python decorators?
Decorators are functions that modify the behavior of other functions. They use the @decorator syntax.
def my_decorator(func): def wrapper(): print("Before function call") func() print("After function call") return wrapper
@my_decoratordef say_hello(): print("Hello!")
8. What is the difference between is and == in Python?
is
checks if two objects have the same identity (same memory location)==
checks if two objects have the same value
a = [1, 2, 3]b = [1, 2, 3]
print(a == b) # Trueprint(a is b) # False
9. What are Python generators?
Generators are functions that return an iterator using yield. They generate values on-the-fly instead of storing them in memory.
def count_up_to(n): i = 0 while i <= n: yield i i += 1
# Using the generatorfor num in count_up_to(5): print(num)
10. What is the Global Interpreter Lock (GIL)?
The GIL is a mutex that protects access to Python objects, preventing multiple threads from executing Python bytecodes at once. This means Python’s threading is limited for CPU-bound tasks but works well for I/O-bound tasks.
11. What are Python’s built-in functions?
Common built-in functions include:
- print()
- len()
- type()
- int()
- str()
- list()
- dict()
- set()
- tuple()
- range()
12. What is the difference between shallow copy and deep copy?
Shallow copy:
- Creates a new object but references the same nested objects
- Use
copy()
method or[:]
slicing
Deep copy:
- Creates a new object and recursively copies all nested objects
- Use
deepcopy()
from the copy module
import copy
# Shallow copylist1 = [1, [2, 3], 4]list2 = list1.copy()
# Deep copylist3 = copy.deepcopy(list1)
13. What are Python’s naming conventions?
- Use lowercase for variables and functions
- Use uppercase for constants
- Use CamelCase for classes
- Use underscore_case for function and variable names
- Prefix private attributes with underscore
- Use double underscore for name mangling
14. What is the difference between append() and extend() in lists?
append()
adds a single item to the end of a listextend()
adds all items from an iterable to the end of a list
list1 = [1, 2, 3]list2 = [4, 5]
list1.append(list2)print(list1) # [1, 2, 3, [4, 5]]
list1.extend(list2)print(list1) # [1, 2, 3, 4, 5]
15. What is the difference between pass, continue, and break?
pass
: Does nothing, used as a placeholdercontinue
: Skips the rest of the current iterationbreak
: Exits the loop entirely
# pass exampledef my_function(): pass # To be implemented later
# continue examplefor i in range(5): if i == 2: continue print(i) # Prints: 0, 1, 3, 4
# break examplefor i in range(5): if i == 2: break print(i) # Prints: 0, 1
What is the difference between is
and ==
?
==
compares the values of two objects.is
compares the identity (memory address) of two objects.
Example:
a = [1, 2, 3]b = [1, 2, 3]c = a
print(a == b) # True (same values)print(a is b) # False (different objects)print(a is c) # True (same object)
What is the difference between list
, tuple
, and set
?
- List: Ordered, mutable, allows duplicates.
- Tuple: Ordered, immutable, allows duplicates.
- Set: Unordered, mutable, does not allow duplicates.
What is the difference between append()
and extend()
in lists?
append()
adds a single item to the end of a list.extend()
adds all items from an iterable to the end of a list.
Example:
list1 = [1, 2, 3]list2 = [4, 5, 6]
list1.append(list2)print(list1) # [1, 2, 3, [4, 5, 6]]
list1 = [1, 2, 3] # Reset list1list1.extend(list2)print(list1) # [1, 2, 3, 4, 5, 6]
What is a lambda function?
A lambda function is a small anonymous function defined with the lambda
keyword. It can have any number of arguments but can only have one expression.
Example:
add = lambda x, y: x + yprint(add(2, 3)) # 5
What is the difference between *args
and **kwargs
?
*args
allows a function to accept any number of positional arguments.**kwargs
allows a function to accept any number of keyword arguments.
Example:
def example_function(*args, **kwargs): print("Positional arguments:", args) print("Keyword arguments:", kwargs)
example_function(1, 2, 3, a=4, b=5)# Positional arguments: (1, 2, 3)# Keyword arguments: {'a': 4, 'b': 5}