python Interview Questions

Back

What are the key features of Python?

Python is a high-level, interpreted programming language known for its readability, simplicity, and versatility. Key features include dynamic typing, automatic memory management, an extensive standard library, support for multiple programming paradigms (procedural, object-oriented, functional), and a large ecosystem of third-party packages.

Explain the difference between lists and tuples in Python.

Lists are mutable, allowing modifications like adding, removing, or changing elements. Tuples are immutable, meaning once created, their elements cannot be altered. Lists use square brackets [ ], while tuples use parentheses ( ). Tuples can be used as keys in dictionaries due to their immutability.

What are Python's built-in data types?

Python's built-in data types include numeric types (int, float, complex), sequence types (list, tuple, range), text type (str), mapping type (dict), set types (set, frozenset), boolean type (bool), and binary types (bytes, bytearray, memoryview).

What is the purpose of the `__init__` method in Python classes?

The `__init__` method is the constructor in Python classes. It initializes the instance attributes of a class when a new object is created, setting up the initial state of the object.

What are the four pillars of Object-Oriented Programming in Python?

The four pillars are Encapsulation (bundling data and methods), Abstraction (hiding complex implementation details), Inheritance (deriving new classes from existing ones), and Polymorphism (using a unified interface for different data types).

How is inheritance implemented in Python?

Inheritance is implemented by defining a class that inherits from a parent class. For example, `class ChildClass(ParentClass):` allows `ChildClass` to inherit attributes and methods from `ParentClass`, promoting code reuse and hierarchical relationships.

How do you handle missing data in a pandas DataFrame?

Missing data can be handled using methods like `df.dropna()` to remove missing values, `df.fillna()` to fill them with specified values or strategies (e.g., mean, median), and `df.isnull()` or `df.notnull()` to detect missing values. Additionally, interpolation methods can estimate missing data.

How can you read and write different file formats using pandas?

Pandas provides functions like `pd.read_csv()`, `pd.read_excel()`, `pd.read_json()`, `pd.read_sql()`, and `pd.read_html()` to read various file formats. Similarly, you can write DataFrames using methods like `df.to_csv()`, `df.to_excel()`, `df.to_json()`, and `df.to_sql()` to export data to different formats.

AI Mock Interview

Boost your confidence and skills with our AI-powered mock interviews.

Start Mock Interview