0% found this document useful (0 votes)
4 views6 pages

? Python Interview Questions With Detailed Answers

The document provides a comprehensive list of Python interview questions and detailed answers covering various topics such as Python basics, data types, loops, functions, object-oriented programming, file handling, exception handling, advanced concepts, and data analysis with Pandas. It includes explanations of key features, differences between Python versions, and practical coding examples. Additionally, it offers coding practice questions to reinforce understanding of Python concepts.

Uploaded by

abhipanda2004
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)
4 views6 pages

? Python Interview Questions With Detailed Answers

The document provides a comprehensive list of Python interview questions and detailed answers covering various topics such as Python basics, data types, loops, functions, object-oriented programming, file handling, exception handling, advanced concepts, and data analysis with Pandas. It includes explanations of key features, differences between Python versions, and practical coding examples. Additionally, it offers coding practice questions to reinforce understanding of Python concepts.

Uploaded by

abhipanda2004
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

🐍 Python Interview Questions with Detailed Answers

1. Python Basics

Q1. What are the key features of Python?

 Easy to learn and use

 Interpreted and high-level language

 Dynamically typed

 Object-Oriented Programming (OOP) supported

 Large standard libraries

 Platform-independent

 Community support

Q2. Difference between Python 2 and Python 3?

 print is a statement in Python 2 and a function in Python 3

 Integer division behavior differs (5/2 is 2 in Python 2 and 2.5 in Python 3)

 Unicode support is better in Python 3

 Python 2 is no longer officially supported (end-of-life in 2020)

Q3. How is Python an interpreted language?


Python code is executed line by line by the interpreter at runtime, not compiled beforehand.

Q4. What are Python’s data types?

 Numeric: int, float, complex

 Sequence: str, list, tuple

 Set: set, frozenset

 Mapping: dict

 Boolean: bool

 NoneType

2. Data Types and Structures

Q1. Difference between a list and a tuple?

 List: Mutable, defined using [ ]

 Tuple: Immutable, defined using ( )

Q2. How is a set different from a dictionary?

 Set: Collection of unique elements (no keys)

 Dict: Key-value pairs, keys must be unique

Q3. What are mutable and immutable types?

 Mutable: Can be changed (e.g., list, dict, set)


 Immutable: Cannot be changed (e.g., int, str, tuple)

Q4. Type conversion in Python?

python

int("10") → 10

str(100) → "100"

list("abc") → ['a', 'b', 'c']

3. Loops and Conditionals

Q1. For vs While loop?

 for: Iterates over a sequence

 while: Loops until a condition is false

Q2. Use of break, continue, and pass?

 break: Exit loop early

 continue: Skip current iteration

 pass: Placeholder (does nothing)

Q3. Fibonacci Series Code

a, b = 0, 1

for _ in range(10):

print(a, end=' ')

a, b = b, a + b

4. Functions

Q1. Arguments vs Keyword Arguments?

 Positional: func(1, 2)

 Keyword: func(x=1, y=2)

Q2. What is a lambda function?


Anonymous one-line function:

square = lambda x: x * x

**Q3. *args vs kwargs?

 *args: Tuple of positional arguments

 **kwargs: Dictionary of keyword arguments


Q4. Recursive Factorial Function:

def factorial(n):

if n == 1: return 1

return n * factorial(n - 1)

5. Object-Oriented Programming (OOP)

Q1. What is a class and object?

 Class: Blueprint

 Object: Instance of a class

Q2. What is __init__() method?


Constructor called automatically when an object is created.

Q3. Inheritance, Encapsulation, Polymorphism?

 Inheritance: Reusing code from parent classes

 Encapsulation: Restricting access using private variables

 Polymorphism: Same function behaves differently

Q4. Class vs Instance variable?

 Class: Shared by all objects

 Instance: Unique to each object

6. File Handling

Q1. Reading/Writing Files in Python?

f = open("file.txt", "r")

data = f.read()

f.close()

Q2. File Modes?

 'r': Read

 'w': Write

 'a': Append

Q3. with open() Statement?


Auto-closes file after use:

with open("file.txt", "r") as f:

data = f.read()
7. Exception Handling

Q1. What are exceptions?


Errors during execution, e.g., ZeroDivisionError, IndexError

Q2. try-except-finally Example:

try:

x = 10 / 0

except ZeroDivisionError:

print("Cannot divide by zero")

finally:

print("Done")

Q3. raise vs assert?

 raise: Manually throw exception

 assert: Check condition (good for debugging)

8. Advanced Concepts

Q1. List Comprehension Example:

squares = [x*x for x in range(5)]


Q2. What is a decorator?
Function that modifies another function’s behavior.

def decorator(func):

def wrapper():

print("Before")

func()

print("After")

return wrapper

Q3. What is a generator?


Function that uses yield instead of return. Generates values one at a time.

Q4. if __name__ == "__main__" usage?


Used to ensure code runs only when file is executed directly, not when imported.
9. Data Analyst with Pandas

Q1. Read a CSV file?

import pandas as pd

df = pd.read_csv("data.csv")

Q2. loc[] vs iloc[]?

 loc[]: label-based

 iloc[]: integer-based indexing

Q3. apply() vs map()?

 map(): Series only

 apply(): Series or DataFrame rows/columns

Q4. Handling missing data?

df.dropna()

df.fillna(0)

10. Coding Practice Questions

Q1. Reverse a string:

"hello"[::-1]

Q2. Prime number check:

def is_prime(n):

if n < 2: return False

for i in range(2, int(n**0.5)+1):

if n % i == 0: return False

return True

Q3. Find largest in list:

max([1, 2, 3])

Q4. Count word frequency:

from collections import Counter

Counter("hello world hello".split())


Q5. Find duplicates in a list:

lst = [1, 2, 2, 3]

duplicates = [x for x in lst if lst.count(x) > 1]

Q6. Merge dictionaries:

d1 = {"a": 1}

d2 = {"b": 2}

merged = {**d1, **d2}

You might also like