0% found this document useful (0 votes)
2 views

Complete Python Interview QA

The document is a comprehensive guide to Python interview questions and answers, covering topics such as basic Python features, data structures, control flow, functions, object-oriented programming, error handling, modules, file handling, Pythonic concepts, advanced concepts, libraries, testing, multithreading, and data science. It provides concise explanations and examples for each question, making it a useful resource for interview preparation. Key distinctions between Python versions, data types, and programming paradigms are highlighted throughout.

Uploaded by

abhijinwoo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Complete Python Interview QA

The document is a comprehensive guide to Python interview questions and answers, covering topics such as basic Python features, data structures, control flow, functions, object-oriented programming, error handling, modules, file handling, Pythonic concepts, advanced concepts, libraries, testing, multithreading, and data science. It provides concise explanations and examples for each question, making it a useful resource for interview preparation. Key distinctions between Python versions, data types, and programming paradigms are highlighted throughout.

Uploaded by

abhijinwoo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Complete Python Interview Questions & Answers

Basic Python
Q: What are Python's key features?

A: Python is interpreted, dynamically typed, high-level, supports object-oriented and functional

programming, and has a large standard library.

Q: Explain the difference between Python 2 and Python 3.

A: Python 3 is the future and has better Unicode support, print is a function, and integer division

returns float. Python 2 is legacy and no longer maintained.

Q: How is Python an interpreted language?

A: Python code is executed line by line by the interpreter rather than being compiled into machine

language beforehand.

Q: What are variables and how do you declare them?

A: Variables are containers for storing data. You declare them simply by assigning a value: `x = 5`.

Q: What are Python data types?

A: Common types include int, float, str, bool, list, tuple, set, dict, and NoneType.

Q: What is type casting in Python?

A: Type casting converts data from one type to another using functions like int(), float(), str().

Q: What are *args and **kwargs?

A: *args allows variable number of positional arguments; **kwargs allows variable number of

keyword arguments.

Q: How does indentation affect Python code?

A: Indentation defines blocks of code and is syntactically required in Python.

Q: What is the difference between `is` and `==`?

A: `is` checks for object identity, `==` checks for value equality.

Data Structures
Q: What are the different built-in data structures in Python?

A: List, Tuple, Set, and Dictionary.


Q: What is the difference between a list and a tuple?

A: Lists are mutable, tuples are immutable.

Q: How is a set different from a list?

A: Sets are unordered and do not allow duplicate values.

Q: What is a dictionary and how do you use it?

A: Dictionaries store key-value pairs. Use curly braces or dict() to define.

Q: How do you iterate over a dictionary?

A: Using a loop: `for key, value in dict.items()`.

Q: How do you merge two dictionaries in Python?

A: Use the unpacking operator `{**d1, **d2}` or `d1.update(d2)`.

Q: What are list comprehensions? Give examples.

A: A concise way to create lists: `[x for x in range(5)]`.

Control Flow
Q: How do `if`, `elif`, and `else` statements work?

A: They control the flow of execution based on conditions.

Q: How do `for` and `while` loops differ?

A: `for` is used with iterables; `while` runs based on a condition.

Q: What are Pythons loop control statements?

A: `break`, `continue`, and `pass`.

Q: How do `break`, `continue`, and `pass` work?

A: `break` exits loop, `continue` skips iteration, `pass` does nothing.

Functions
Q: How do you define a function in Python?

A: Use the `def` keyword followed by the function name and parentheses.

Q: What is the difference between a function and a method?

A: Functions are independent; methods are associated with object instances.

Q: What is recursion? Give an example.


A: Recursion is a function calling itself. Example: calculating factorial.

Q: What are lambda functions?

A: Anonymous functions defined using the `lambda` keyword.

Q: Explain the difference between `global` and `nonlocal`.

A: `global` accesses variables in the global scope; `nonlocal` accesses variables in the enclosing

function scope.

Object-Oriented Programming (OOP)


Q: What are classes and objects?

A: Classes are blueprints; objects are instances of classes.

Q: What is `self` in a class?

A: `self` refers to the instance of the class.

Q: What are inheritance, encapsulation, and polymorphism?

A: Inheritance: reuse code, Encapsulation: restrict access, Polymorphism: multiple forms.

Q: What is the difference between `@staticmethod`, `@classmethod`, and instance methods?

A: `staticmethod` doesnt use class or instance, `classmethod` takes cls, instance methods take self.

Q: How do you implement multiple inheritance in Python?

A: By specifying multiple base classes in the class definition.

Error Handling
Q: How does exception handling work in Python?

A: Using try-except blocks.

Q: What is the difference between `try/except`, `try/finally`, and `try/except/finally`?

A: `except` handles errors, `finally` always runs.

Q: How do you create a custom exception?

A: Inherit from `Exception` class.

Modules and Packages


Q: What is the difference between a module and a package?

A: A module is a .py file, a package is a folder with `__init__.py`.


Q: How do you import a module?

A: Using `import module_name`.

Q: What is the use of `__init__.py`?

A: Marks a directory as a Python package.

Q: What are some commonly used Python modules?

A: `os`, `sys`, `math`, `random`, `datetime`.

File Handling
Q: How do you read and write files in Python?

A: Using `open()`, with modes like `r`, `w`, `a`.

Q: What is the difference between `open()`, `with open()`, and `file.close()`?

A: `with` handles file closing automatically.

Q: How do you handle CSV or JSON files in Python?

A: Using `csv` and `json` modules.

Pythonic Concepts
Q: What is slicing in Python?

A: Extracting parts of a sequence using `[start:stop:step]`.

Q: What are generators and how do they differ from iterators?

A: Generators yield values lazily using `yield`; iterators use `__next__()`.

Q: What is a decorator in Python?

A: A function that modifies another functions behavior.

Q: What is a context manager?

A: Manages resources using `with` statement.

Q: What is list/dictionary/set comprehension?

A: Concise ways to create data structures in one line.

Advanced Concepts
Q: Explain GIL (Global Interpreter Lock).

A: GIL allows only one thread to execute at a time in CPython.


Q: How is memory managed in Python?

A: Using reference counting and garbage collection.

Q: What are closures in Python?

A: Functions retaining access to enclosing variables.

Q: What is monkey patching?

A: Modifying code at runtime.

Q: Explain the use of `@property`.

A: Allows method to be accessed like an attribute.

Libraries & Frameworks


Q: What are the differences between NumPy and Pandas?

A: NumPy is for numerical arrays; Pandas is for tabular data.

Q: What is the use of `requests` library?

A: To make HTTP requests easily.

Q: How is `Flask` different from `Django`?

A: Flask is lightweight and flexible; Django is feature-rich and opinionated.

Q: What is the role of virtual environments (`venv`, `pipenv`)?

A: To isolate dependencies for different projects.

Testing & Debugging


Q: What are the different testing frameworks in Python?

A: `unittest`, `pytest`, `nose`.

Q: How do you write unit tests using `unittest` or `pytest`?

A: Define test functions with assertions.

Q: How can you debug Python code?

A: Using `pdb` or IDE built-in debuggers.

Multithreading & Multiprocessing


Q: What is the difference between multithreading and multiprocessing in Python?

A: Threads share memory, processes have separate memory.


Q: How do you use the `threading` module?

A: By creating Thread objects and starting them.

Q: What is the `multiprocessing` module used for?

A: To run code in parallel using separate memory space.

Data Science/Machine Learning


Q: What are key Python libraries used in ML?

A: Scikit-learn, Pandas, NumPy, Matplotlib, TensorFlow, etc.

Q: How do you read a dataset using Pandas?

A: Using `pd.read_csv()` or similar functions.

Q: What is the difference between `.loc` and `.iloc`?

A: `.loc` is label-based, `.iloc` is index-based.

Q: How do you handle missing values in a DataFrame?

A: Using `dropna()`, `fillna()` or imputing with statistics.

You might also like