Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
10 views

Python Interview Questions

The document provides a compilation of common Python interview questions and answers, covering key features, data types, and fundamental concepts such as decorators, list comprehension, and exceptions. It also explains the differences between various Python constructs like lists, tuples, sets, and the distinction between shallow and deep copies. Additionally, it addresses advanced topics like Python's Global Interpreter Lock (GIL) and the use of *args and **kwargs.

Uploaded by

hhi545343
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)
10 views

Python Interview Questions

The document provides a compilation of common Python interview questions and answers, covering key features, data types, and fundamental concepts such as decorators, list comprehension, and exceptions. It also explains the differences between various Python constructs like lists, tuples, sets, and the distinction between shallow and deep copies. Additionally, it addresses advanced topics like Python's Global Interpreter Lock (GIL) and the use of *args and **kwargs.

Uploaded by

hhi545343
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/ 4

Python Interview Questions and Answers

1. What are Python's key features?

- Easy to learn & use

- Interpreted language

- Dynamically typed

- High-level language

- Extensive libraries

- Object-oriented and functional

- Portable and platform-independent

2. What are Python's data types?

- Numeric: int, float, complex

- Text: str

- Sequence: list, tuple, range

- Set: set, frozenset

- Mapping: dict

- Boolean: bool

- NoneType: None

3. What is the difference between list, tuple, and set?

List: Mutable, ordered, allows duplicates

Tuple: Immutable, ordered, allows duplicates

Set: Mutable, unordered, no duplicates

4. What are *args and **kwargs?

*args: Variable positional arguments

**kwargs: Variable keyword arguments

Example:

def demo(*args, **kwargs):

print(args)
Python Interview Questions and Answers

print(kwargs)

5. What is a Python decorator?

A decorator is a function that modifies another function.

Example:

def decorator(func):

def wrapper():

print('Before')

func()

print('After')

return wrapper

@decorator

def greet():

print('Hello')

6. What is list comprehension?

A concise way to create lists.

Example:

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

7. What is the difference between 'is' and '=='?

'==' compares values, 'is' compares identities.

Example:

a = [1]; b = a; c = [1]

print(a == c) # True

print(a is c) # False

8. What are Python generators?

Generators yield items one at a time, saving memory.


Python Interview Questions and Answers

Example:

def gen():

yield 1

yield 2

9. What is the difference between deep copy and shallow copy?

Shallow copy: One level deep, references nested objects.

Deep copy: Copies everything recursively.

Example:

import copy

copy.deepcopy(obj)

10. What is a lambda function?

A small anonymous function.

Example:

add = lambda x, y: x + y

11. What is Python's Global Interpreter Lock (GIL)?

GIL ensures only one thread executes Python bytecode at a time. It affects multi-threading.

12. What is the difference between @staticmethod and @classmethod?

@staticmethod: no self or cls

@classmethod: receives cls

Example:

@staticmethod

def stat(): pass

@classmethod

def clsm(cls): pass


Python Interview Questions and Answers

13. Common Python exceptions?

- ZeroDivisionError

- TypeError

- ValueError

- IndexError

- KeyError

14. How to handle exceptions?

Use try-except-finally blocks.

Example:

try:

1/0

except ZeroDivisionError:

print('Error')

15. Modules vs Packages?

Module: .py file

Package: folder with __init__.py

You might also like