50 Basic Python Interview Questions with Answers
1. What is Python?
Python is a high-level, interpreted programming language known for its simplicity and readability. It's
widely used in web development, data analysis, automation, and machine learning.
2. What are the key features of Python?
Python is easy to learn, supports object-oriented and functional programming, has a large standard
library, and works across different platforms.
3. What are Python data types?
Common data types include int, float, str, bool, list, tuple, dict, and set.
4. What is the difference between a list and a tuple?
Lists are mutable (can be changed), while tuples are immutable (cannot be changed).
5. What is a dictionary in Python?
A dictionary stores key-value pairs. Its unordered and mutable. Example: {'name': 'Manoj', 'age': 22}
6. What are variables?
Variables are used to store data. Python variables are dynamically typed, meaning their type is
inferred.
7. What is type casting?
Type casting converts one data type to another. Example: int('5') converts string to integer.
8. What is indentation in Python?
Indentation defines blocks of code. It replaces curly braces used in other languages. Incorrect
indentation causes errors.
9. What is a function?
A function is a reusable block of code. Defined using 'def'. Example: def greet(): print('Hello')
10. What is the difference between return and print?
return sends a result back to the caller, while print displays it on the screen.
11. What is a lambda function?
Lambda is an anonymous one-line function. Example: lambda x: x + 1
12. What are conditional statements?
They allow branching logic: if, elif, else. Example: if x > 0: print('Positive')
13. What are loops?
Loops repeat code. for and while are the two types. 'break' exits the loop, 'continue' skips to the next
iteration.
14. What is a list comprehension?
A shorter way to create lists. Example: [x for x in range(5)]
15. How do you handle exceptions?
Use try-except blocks. Example: try: x = 1/0 except ZeroDivisionError: print('Error')
16. What is a module?
A file containing Python code (functions, variables). Use import to include it.
17. What is a package?
A collection of modules in a directory with an __init__.py file.
18. How do you install external packages?
Use pip. Example: pip install pandas
19. What is the difference between is and ==?
== compares values. is checks identity (same memory location).
20. What are Pythons logical operators?
and, or, not used to combine conditional statements.
21. What are Pythons bitwise operators?
&, |, ^, ~, <<, >> used for binary operations.
22. What is the use of pass?
pass is a placeholder when no code is to be executed. Example: if x > 0: pass
23. What are *args and **kwargs?
*args allows variable number of positional arguments. **kwargs allows variable number of keyword
arguments.
24. What is the difference between local and global variables?
Local variables are defined inside functions. Global variables are accessible throughout the
program.
25. What is recursion?
A function that calls itself. Must have a base case to stop recursion.
26. What is a docstring?
A special comment used to describe a function/module. Written with triple quotes.
27. What is slicing?
Accessing a subset of sequences using [start:stop:step].
28. What is the use of enumerate()?
It adds a counter to an iterable. Example: for i, val in enumerate(list)
29. What is zip() used for?
Combines multiple iterables into a tuple of pairs. Example: zip(list1, list2)
30. What is map() used for?
Applies a function to all items in an iterable. Example: map(str.upper, ['a', 'b'])
31. What is filter() used for?
Filters elements based on a condition. Example: filter(lambda x: x > 0, numbers)
32. What is reduce() used for?
Applies a rolling computation. From functools import reduce.
33. What are generators?
Functions that yield values one by one using 'yield'. Efficient for large data.
34. What is a decorator?
A function that modifies another function. Used for logging, access control, etc.
35. What are Python files and how do you read them?
Use open('file.txt') to read. Methods: read(), readline(), readlines().
36. How to write to a file in Python?
Use open('file.txt', 'w') and write(). Always close files or use with statement.
37. What is object-oriented programming (OOP)?
OOP involves classes and objects. Python supports inheritance, polymorphism, encapsulation.
38. How do you create a class?
Use 'class' keyword. Example: class Car: def __init__(self): self.speed = 0
39. What is __init__?
A constructor method automatically called when an object is created.
40. What is self?
Represents the instance of a class. Used to access variables that belong to the class.
41. What is inheritance?
One class can inherit features from another class. class B(A):
42. What is polymorphism?
Ability to use a common interface for multiple data types.
43. What is encapsulation?
Hiding internal object details and only exposing whats necessary.
44. What is abstraction?
Hiding complex implementation details and showing only functionality.
45. What is isinstance()?
Checks if an object is an instance of a class. Example: isinstance(x, int)
46. What is the difference between class and instance variables?
Class variables are shared. Instance variables are unique to each object.
47. What are magic methods?
Special methods with double underscores. Example: __str__, __len__, __init__.
48. What is the difference between shallow copy and deep copy?
Shallow copy copies references. Deep copy creates independent copies.
49. What is a virtual environment?
A self-contained directory for Python projects with separate dependencies.
50. How do you debug in Python?
Use print statements, IDE debuggers, or import pdb for step-by-step debugging.