Basic Python Interview Questions - Complete Guide
Fundamentals & Syntax
- What is Python and why is it popular?
- What are the differences between Python 2 and Python 3?
- What is PEP 8 and why is it important?
- Explain mutable vs immutable data types in Python with examples.
- What is the difference between
== and is?
- How do you comment in Python?
- What are Python's naming conventions?
- Explain the concept of indentation in Python.
- What is the GIL (Global Interpreter Lock)?
- How does Python handle memory allocation?
Data Types & Variables
- What are the main data types in Python?
- How do you check the type of a variable?
- What is type casting and how do you do it?
- Explain the difference between lists, tuples, and sets.
- What are dictionaries and how do you access elements?
- Can you have duplicate values in a set?
- What is the difference between append() and extend() on lists?
- How do you create an empty list, dictionary, and set?
- Explain string slicing with examples.
- What are f-strings and how do you use them?
- What is a frozenset?
- How do you check if a key exists in a dictionary?
- What is the difference between dict.get() and dict[]?
- How do you sort a dictionary by keys or values?
- What are bytes and bytearray in Python?
Operators & Control Flow
- What are the different types of operators in Python?
- Explain the difference between
/ and // operators.
- What are comparison operators and provide examples.
- What is short-circuit evaluation in Python?
- Explain if, elif, and else statements with an example.
- What is the ternary operator in Python?
- What is the purpose of
pass statement?
- Explain
break and continue statements.
- What is the
in operator?
- What is the
not in operator?
- How does operator precedence work in Python?
- What is the
and, or, and not operators?
Loops & Iteration
- What are the different types of loops in Python?
- Explain for and while loops with examples.
- What is the difference between a for and while loop?
- How do you use enumerate() in a loop?
- What is a list comprehension and provide an example?
- What is a dictionary comprehension?
- What is a set comprehension?
- What is a generator expression?
- How do you iterate through a dictionary?
- What is the
else clause in loops?
- How do you create an infinite loop and how do you exit it?
- What is the difference between range() and xrange()?
Functions
- What is a function and why are they useful?
- What is the difference between parameters and arguments?
- What are default arguments in a function?
- What are keyword arguments?
- Explain
*args and **kwargs.
- What is the difference between
return and print()?
- What is a lambda function and when should you use it?
- What are higher-order functions?
- Explain the
map(), filter(), and reduce() functions.
- What is the scope of variables in Python (LEGB rule)?
- What is the
global keyword used for?
- What is the
nonlocal keyword used for?
- How do type hints work in Python?
- What is a generator function?
- What is the
yield keyword?
- What is the difference between a generator and a list?
- How do you use
next() with generators?
- What are *args order and **kwargs order in function parameters?
Object-Oriented Programming
- What is a class and an object?
- What is the difference between a class variable and instance variable?
- What is
self in Python classes?
- What is the
__init__ method?
- What are special methods (dunder methods) in Python?
- Explain inheritance in Python.
- What is method overriding?
- What is method overloading in Python?
- Explain polymorphism in Python.
- What is encapsulation in Python?
- What are properties and how do you create them?
- What is the
super() function?
- What is the difference between composition and inheritance?
- What are abstract classes and abstract methods?
- What are class methods and static methods?
- What is multiple inheritance?
- What is the Method Resolution Order (MRO)?
- What is the
__str__ and __repr__ method?
- What is the
__eq__ method?
- What is the
__hash__ method?
- What is the
__call__ method?
- What are descriptors in Python?
- What is a metaclass?
- How do private variables work in Python?
- What are name mangling?
- What is the
@property decorator?
- How do you create a singleton class?
Exceptions & Error Handling
- What is an exception in Python?
- What is the difference between syntax errors and exceptions?
- How do you handle exceptions using try-except?
- What is the difference between
except and finally?
- Can you have multiple except blocks?
- How do you raise an exception?
- What is a custom exception?
- What is the
else clause in try-except?
- What is the
with statement used for?
- What are common built-in exceptions?
- What is exception chaining?
- How do you access exception information?
- What is the difference between
raise and raise from?
File Handling
- How do you open and read a file in Python?
- What are the different file opening modes?
- How do you write to a file?
- What is the difference between read(), readline(), and readlines()?
- How do you close a file safely?
- What is the context manager (with statement) for files?
- How do you append to a file?
- How do you delete a file?
- How do you check if a file exists?
- How do you get file size?
- How do you work with file paths in Python?
- What is the difference between absolute and relative paths?
- How do you read/write binary files?
String Operations
- How do you concatenate strings in Python?
- What are string methods like upper(), lower(), strip()?
- How do you check if a substring exists in a string?
- How do you split and join strings?
- What is string formatting and what are the different methods?
- How do you replace characters in a string?
- What are escape sequences in Python?
- How do you find the length of a string?
- What is the
startswith() and endswith() method?
- How do you reverse a string?
- What is string interpolation?
- How do you convert string to list and vice versa?
- What is the
find() and index() method?
- What is the difference between
find() and index()?
Built-in Functions & Modules
- What are some commonly used built-in functions?
- What is the difference between
len(), max(), and min()?
- What does
zip() do?
- What is the
range() function?
- What is the
sorted() function?
- What is the
all() and any() function?
- What is the
sum() and abs() function?
- What is a module and how do you import it?
- What is the difference between
import and from...import?
- What is the
__name__ variable?
- What are some commonly used modules (os, sys, datetime, random)?
- How do you install external modules using pip?
- What is a virtual environment and why is it important?
- What is the
dir() function?
- What is the
help() function?
- What is the
id() function?
- What is the
hash() function?
- What is the
isinstance() and issubclass() function?
Collections & Data Structures
- What is the difference between list.sort() and sorted()?
- What is the
collections module?
- What are namedtuples?
- What is
defaultdict?
- What is
Counter and how is it used?
- What is
deque and when should you use it?
- What is
OrderedDict?
- What are the time complexities of list, tuple, set, and dict operations?
- What is a heap and how do you use the heapq module?
- What is a priority queue?
Debugging & Best Practices
- What is debugging and what are common techniques?
- How do you use print statements for debugging?
- What is the
pdb module?
- What are unit tests and why are they important?
- What is docstring and how do you write it?
- What are assertions and how do you use them?
- What is logging in Python?
- What are the different logging levels?
- What is PEP 257 (docstring conventions)?
- What are code reviews?
Functional Programming
- What is functional programming?
- What is recursion and what are its limitations?
- What is a closure?
- What are decorators in Python?
- How do you create and use decorators?
- What are function decorators vs class decorators?
- How do you create a decorator with arguments?
- What is
functools.wraps?
- What are built-in decorators like
@staticmethod, @classmethod, @property?
Performance & Optimization
- What is list comprehension vs loops (performance)?
- What is the difference between deep copy and shallow copy?
- How do you create a copy of a list?
- What is memory management in Python?
- What is garbage collection?
- How do you measure code performance?
- What is Big O notation?
- What are common time complexities?
- How do you optimize Python code?
Coding Scenarios & Algorithms
- How do you swap two variables without using a temporary variable?
- How do you find the second largest number in a list?
- How do you remove duplicates from a list?
- How do you reverse a string or list?
- How do you check if a number is prime?
- How do you flatten a nested list?
- How do you find common elements in two lists?
- How do you count occurrences of an element in a list?
- How do you check if a string is a palindrome?
- How do you implement binary search?
- How do you implement linear search?
- How do you implement bubble sort?
- How do you implement merge sort?
- How do you implement quick sort?
- How do you find the factorial of a number?
- How do you find Fibonacci series?
- How do you check if a number is palindrome?
- How do you find the GCD of two numbers?
- How do you find the LCM of two numbers?
- How do you create a matrix and perform operations?
Regular Expressions
- What is regular expressions (regex)?
- How do you import the regex module?
- What are common regex patterns?
- What does the
re.match() function do?
- What does the
re.search() function do?
- What does the
re.findall() function do?
- What does the
re.sub() function do?
- What is the difference between match and search?
- What are character classes in regex?
- What are quantifiers in regex?
Web Scraping & APIs
- What is web scraping and is it legal?
- What libraries are used for web scraping?
- What is BeautifulSoup?
- What is Selenium?
- How do you make HTTP requests in Python?
- What is REST API?
- How do you parse JSON in Python?
- What is the requests library?
Working with Data
- What is JSON and how do you work with it?
- How do you read and write JSON files?
- What is CSV and how do you work with it?
- What is the CSV module?
- What is pandas library used for?
- What are DataFrames in pandas?
- What is NumPy?
Advanced Concepts
- What is threading in Python?
- What is the difference between threading and multiprocessing?
- What is a context manager?
- How do you create a custom context manager?
- What is the
__enter__ and __exit__ method?
- What is pickle in Python?
- What is serialization and deserialization?
- What are iterables and iterators?
- What is the
__iter__ and __next__ method?
- What is the difference between iterable and iterator?
- What are asynchronous functions?
- What is async and await?
- What is the
asyncio module?
- What is type checking in Python?
- What are union types and optional types?