0% found this document useful (0 votes)
6 views3 pages

python questions

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
6 views3 pages

python questions

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 3

1. What is Python, and what are its key features?

Answer:

Python is a high-level, interpreted programming language that emphasizes code readability and
simplicity. Key features of Python include:

Easy to Learn: Simple and clean syntax makes it ideal for beginners.
Interpreted Language: Python is executed line-by-line, which makes debugging easier.
Dynamically Typed: No need to declare data types explicitly.
Object-Oriented: Supports object-oriented programming (OOP) concepts like inheritance and
polymorphism.
Extensive Standard Library: Python comes with a vast standard library for file handling, web
development, etc.
Cross-platform: Python is platform-independent, meaning it can run on various operating
systems without modification.

2. What are Python’s data types?

Answer:

Python supports several built-in data types, including:

Numeric types: int (integers), float (floating-point numbers), complex (complex numbers).
Sequence types: list, tuple, range.
Text type: str (string).
Set types: set, frozenset.
Mapping type: dict (dictionary).
Boolean type: bool (True or False).
Binary types: bytes, bytearray, memoryview.

3. What is the difference between a list and a tuple in Python?

Answer:

List:
Mutable (can be modified after creation).
Defined with square brackets: [].
Elements can be added, removed, or changed.
Tuple:
Immutable (cannot be modified once created).
Defined with parentheses: ().
Ideal for fixed collections of data.
4. What are Python decorators, and how do they work?

Answer:

A decorator in Python is a function that takes another function as input and extends or modifies
its behavior without changing its source code. Decorators are often used in scenarios like
logging, access control, memoization, etc.

5. Explain the difference between deepcopy() and copy() in Python.

Answer:

copy(): Performs a shallow copy, meaning it copies the references of the objects in the original
list. If you modify nested objects, changes will affect both the original and the copied object.
deepcopy(): Performs a deep copy, meaning it recursively copies all objects, including nested
ones, creating independent copies of everything.

6. What are Python's built-in functions for handling exceptions?

Answer:

Python provides several built-in functions and statements for handling exceptions:

try and except: Used for handling exceptions.


else: Executes if no exception occurs.
finally: Executes regardless of whether an exception occurred or not.
raise: Used to throw an exception manually.

7. What are Python generators?

Answer:

A generator in Python is a special type of iterator that is defined using a function with the yield
keyword. Unlike a regular function that returns a single value, a generator returns an iterator that
yields multiple values one at a time as you iterate over it. It is memory efficient, especially for
large data sets, since it generates values on-the-fly.

8. What is list comprehension in Python?


Answer:

List comprehension is a concise way to create lists in Python. It allows you to generate a new list
by applying an expression to each item in an existing iterable.

9. What is the purpose of the self keyword in Python?

Answer:

The self keyword in Python is used to represent the instance of the class. It allows access to the
attributes and methods of the class from within the class methods. It is the first parameter of
instance methods in class definitions and refers to the current object.

10. Explain how Python handles memory management.

Answer:

Python uses automatic memory management and garbage collection. Key points include:

Reference Counting: Python keeps track of the number of references to each object in memory.
When the reference count drops to zero, the object is deallocated.
Garbage Collection: Python uses an additional garbage collector to reclaim memory from objects
that are no longer in use, particularly those that are involved in circular references (where objects
refer to each other).
Memory Pools: Python’s memory manager uses small memory pools for frequently used objects
(e.g., integers, strings) to reduce memory fragmentation.

You might also like