Top 100 Python Interview Questions
Level 1
Q1. What is Python and why is it so popular?
A1. Python is a high-level, interpreted language known for its simplicity and readability. Its popularity comes from its versatility, used in web development, data analysis, AI, automation, and more.
Q2. What are Python’s key features?
A2. Python is Interpreted, Object-Oriented, Dynamically Typed, has a Large Standard Library, and is Open Source.
Q3. What are Python Data Types?
A3. Numeric (int, float, complex), Text (str), Sequence (list, tuple, range), Set, Dictionary, and Boolean. A list is mutable, whereas a tuple is immutable.
Q4. What is the difference between a “module” and a “package” in Python?
A4. A module is a single file containing Python code (functions, classes, variables) that can be imported and initialized once using the import statement. A package is a collection of related modules organized into a directory structure, often containing an __init__.py file.
Q5. What is the difference between “map” and “filter” functions in Python?
A5. map applies a given function to each item in an iterable and returns an iterator with the results. filter applies a given function and returns an iterator with only the items that meet the given condition.
Q6. What is the difference between “static method” and “class method” in Python?
A6. A static method is bound to the class but does not depend on the instance or class state. A class method is also bound to the class but takes a reference to the class itself (cls) as the first argument, operating on the class itself rather than on the instance.
Q7. What is the purpose of the “yield” keyword in Python?
A7. The yield keyword creates generator functions that can produce a sequence of values on-the-fly. This is a memory-efficient way to generate sequences, as values are generated as iteration occurs.

Q8. What are OOPs in Python?
A8. OOPs (Object-Oriented Programming) is a programming paradigm that uses objects and classes to organize and structure code.
Q9. What is a lambda function in Python?
A9. A lambda function is an anonymous, single-expression function defined using the lambda keyword. It can take any number of arguments but can only have one expression. It is primarily used for short, throwaway functions.
Q10. What is a decorator in Python?
A10. A decorator allows a user to add new functionality or modify the behavior of an existing object (function or class) without modifying its structure. It is a design pattern.
Q11. What is method overloading in Python?
A11. Method overloading allows a class to have multiple methods with the same name but different parameters. In Python, this can be achieved through the use of default arguments.
Q12. What is method overriding in Python?
A12. Method overriding allows a subclass to provide its own implementation of a method already defined in its superclass, by defining a method in the subclass with the same name.
Q13. What is a try/catch block?
A13. The try block is where code that may throw an exception is placed, and the except block (often called ‘catch’) handles the caught exception to deal with runtime errors.
Q14. What is a Finally block?
A14. The finally block is used to specify code that must always be executed before the method is finished, regardless of whether an exception was thrown or caught.
Q15. What is a metaclass in Python?
A15. A metaclass is a class that defines the behavior of other classes. It can be used to customize the creation and behavior of classes.
Q16. What is closure in Python?
A16. A closure is a nested function that has access to the variables of its enclosing function, even after the enclosing function has returned.
Q17. Explain split() and join() functions in Python.
A17. The split() function is used to split a single string into a list of strings using a delimiter. The join() function is used to join a list of strings based on a delimiter to give a single string.
Q18. Explain different types of Literals in Python.
A18. A literal is a fixed value for primitive data types. Types include String, Character, Numeric (int, float, complex), Boolean, and Literal collections (list, tuple, dictionary, set).
Q19. Is indentation required in Python?
A19. Yes. Python uses indentation (specified using four space characters) to define blocks of code (in loops, classes, functions, etc.). If not specified, the code will throw an error.
Q20. What is PEP8?
A20. PEP 8 (Python Enhancement Proposal 8) is Python’s official style guide that helps developers write readable and consistent code. Following it is highly recommended.
Q21. How does Python handle memory management, and what role does garbage collection play?
A21. Python manages memory allocation/deallocation automatically using a private heap space. It primarily uses reference counting and a cyclic garbage collector to detect and collect unused objects, recycling the memory.
Q22. How can you use Python’s collections module to simplify common tasks?
A22. The collections module provides specialized data structures like defaultdict (for default values for non-existent keys), Counter (for counting elements in an iterable), deque, and OrderedDict to simplify various tasks.
Q23. How do you identify and deal with missing values (in Pandas)?
A23. Missing values can be identified in a DataFrame using the isnull() function followed by sum(). isnull() returns boolean values, and sum() gives the count of missing values per column.
Q24. How would you normalize or standardize a dataset in Python?
A24. Normalization scales data to a specific range (e.g., [0, 1]). Standardization transforms data to have a mean of 0 and a standard deviation of 1. Both are essential for preparing data for machine learning models.
Q25. How can you replace string space with a given character in Python (code logic)?
A25. The standard method is to use the built-in str.replace() method. Alternatively, you can iterate over each character in the string, and if it is a space, replace it with the specified character and append it to a result string.
Q26. How is Python interpreted?
A26. Python is an interpreted language, executed line by line at runtime. The source code is first converted into bytecode, which is then executed by the Python Virtual Machine (PVM).
Q27. How do you manage memory in Python?
A27. Python uses automatic memory management and a garbage collector to handle memory by recycling objects that are no longer in use.
Q28. Write a Python code to check if a number is even or odd.
A28. def is_even(num): return num % 2 == 0
Q29. Write a Python code to concatenate two strings.
A29. str1 = “Hello”; str2 = “World”; result = str1 + ” ” + str2
Q30. Write a Python program to find the maximum of three numbers.
A30. def max_of_three(a, b, c): return max(a, b, c)
Q31. Write a Python program to count the number of vowels in a string.
A31. def count_vowels(s): return sum(1 for char in s if char.lower() in ‘aeiou’)
Q32. Write a Python program to calculate the factorial of a number.
A32. def factorial(n): if n == 0: return 1; return n * factorial(n – 1)
Q33. Write a Python code to convert a string to an integer.
A33. str_num = “12345”; int_num = int(str_num)
Q34. Write a Python program to calculate the area of a rectangle.
A34. def area_of_rectangle(length, width): return length * width
Q35. How is a local variable different from a global variable?
A35. Global variables are declared outside a function and have scope throughout the program. Local variables are declared inside a function and have a limited scope only within that function.
Q36. How do you convert all the characters of a string to lowercase?
A36. Use the built-in string method lower(), which returns a new string with all characters converted to lowercase.
Q37. How do you comment on multiple lines at once in Python?
A37. You can use triple quotes (”’ or “””) to enclose text, which is treated as a docstring/comment and can span multiple lines.
Q38. How do you create an empty class in Python?
A38. An empty class can be created using the pass statement as a placeholder: class MyClass: pass. The pass statement is a null operation.
Q39. How will you check if all characters in a string are alphanumeric?
A39. Use the string method isalnum().
Q40. Explain Python’s parameter-passing mechanism.
A40. Python uses pass-by-reference. However, when passing immutable arguments (strings, numbers, tuples), they behave like pass-by-value because they cannot be changed in place.
Q41. How can I make a tuple out of a list?
A41. Use the built-in tuple() method: converting_list = tuple(my_list). Since a tuple is immutable, the converted items cannot be updated.
Q42. In Python, what is a negative index?
A42. A negative index reads items from the end of the array/list. For example, -1 refers to the last element, and -2 refers to the second to last element.

Q43. What is the Python data type SET, and how can I use it?
A43. A set is an unordered collection of distinct (unique) and immutable items. It is used for efficient membership testing and removing duplicates.
Q44. What is the distinction between Python Arrays and Python Lists?
A44. Arrays (from NumPy) are homogeneous (same data type), consume less memory, and are efficient for numerical computation. Lists are heterogeneous (mixed data types) and are part of the core Python language.
Q45. What exactly is __init__?
A45. __init__ is a special method (constructor) that is automatically called when a new object/instance of a class is created. It is used to initialize the object’s attributes and allocate memory.
Q46. How can you randomize the elements of a list while it’s running?
A46. Use the random.shuffle() function from the random module to shuffle the elements of the list in a random order in place.
Q47. What is the difference between pickling and unpickling?
A47. Pickling converts a Python object into a binary representation (byte stream) for storage/transmission. Unpickling is the reverse process, converting the byte stream back into a Python object.
Q48. What is slicing in Python?
A48. Slicing is taking parts of a sequence (string, list, tuple). The syntax is [start : stop : step], where start is inclusive, stop is exclusive, and step is the jump size.
Q49. What are docstrings in Python?
A49. Docstrings (documentation strings) are multiline strings enclosed in triple quotes used to document modules, classes, functions, or methods, accessible via the __doc__ attribute.
Q50. What are unit tests in Python?
A50. Unit testing is a process of testing different components of software separately. Python’s built-in unittest is a testing framework for this purpose.