python questions
python questions
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.
Answer:
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.
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.
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.
Answer:
Python provides several built-in functions and statements for handling exceptions:
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.
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.
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.
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.