42. What is PIP?
PIP is an acronym for Python Installer Package which provides a seamless
interface to install various Python modules. It is a command-line tool that
can search for packages over the internet and install them without any
user interaction.
43. What is a zip function?
Python zip() function returns a zip object, which maps a similar index of
multiple containers. It takes an iterable, converts it into an iterator and
aggregates the elements based on iterables passed. It returns an iterator
of tuples.
Syntax:
zip(*iterables)
44. What are Pickling and Unpickling?
● Pickling: The pickle module converts any Python object into a
byte stream (not a string representation). This byte stream can
then be stored in a file, sent over a network, or saved for later
use. The function used for pickling is [Link]().
● Unpickling: The process of retrieving the original Python object
from the byte stream (saved during pickling) is called unpickling.
The function used for unpickling is [Link]().
45. What is the difference between @classmethod,
@staticmethod and instance methods in Python?
1. Instance Method operates on an instance of the class and has access to
instance attributes and takes self as the first parameter. Example:
def method(self):
2. Class Method directly operates on the class itself and not on instance, it
takes cls as the first parameter and defined with @classmethod.
Example: @classmethod def method(cls):
3. Static Method does not operate on an instance or the class and takes no
self or cls as an argument and is defined with @staticmethod.
Example: @staticmethod def method(): align it and dont bolod anything
and not bullet points
46. What is __init__() in Python and how does self play a role in
it?
● __init__() is Python's equivalent of constructors in OOP, called
automatically when a new object is created. It initializes the
object's attributes with values but doesn’t handle memory
allocation.
● Memory allocation is handled by the __new__() method, which is
called before __init__().
● The self parameter in __init__() refers to the instance of the class,
allowing access to its attributes and methods.
● self must be the first parameter in all instance methods, including
__init__()
1
class MyClass:
2
def __init__(self, value):
3
[Link] = value # Initialize object attribute
4
5
def display(self):
6
print(f"Value: {[Link]}")
7
8
obj = MyClass(10)
9
[Link]()
Output
Value: 10
47. Write a code to display the current time?
1
import time
2
3
currenttime= [Link]([Link]())
4
print ("Current time is", currenttime)
48. What are Access Specifiers in Python?
Python uses the ‘_’ symbol to determine the access control for a specific
data member or a member function of a class. A Class in Python has three
types of Python access modifiers:
● Public Access Modifier: The members of a class that are
declared public are easily accessible from any part of the
program. All data members and member functions of a class are
public by default.
● Protected Access Modifier: The members of a class that are
declared protected are only accessible to a class derived from it.
All data members of a class are declared protected by adding a
single underscore '_' symbol before the data members of that
class.
● Private Access Modifier: The members of a class that are
declared private are accessible within the class only, the private
access modifier is the most secure access modifier. Data members
of a class are declared private by adding a double underscore ‘__’
symbol before the data member of that class.
49. What are unit tests in Python?
Unit Testing is the first level of software testing where the smallest
testable parts of the software are tested. This is used to validate that each
unit of the software performs as designed. The unit test framework is
Python’s xUnit style framework. The White Box Testing method is used
for Unit testing.
50. Python Global Interpreter Lock (GIL)?
Python Global Interpreter Lock (GIL) is a type of process lock that is used
by Python whenever it deals with processes. Generally, Python only uses
only one thread to execute the set of written statements. The performance
of the single-threaded process and the multi-threaded process will be the
same in Python and this is because of GIL in Python. We can not achieve
multithreading in Python because we have a global interpreter lock that
restricts the threads and works as a single thread.
51. What are Function Annotations in Python?
● Function Annotation is a feature that allows you to add metadata
to function parameters and return values. This way you can
specify the input type of the function parameters and the return
type of the value the function returns.
● Function annotations are arbitrary Python expressions that are
associated with various parts of functions. These expressions are
evaluated at compile time and have no life in Python’s runtime
environment. Python does not attach any meaning to these
annotations. They take life when interpreted by third-party
libraries, for example, mypy.
52. What are Exception Groups in Python?
The latest feature of Python 3.11, Exception Groups. The ExceptionGroup
can be handled using a new except* syntax. The * symbol indicates that
multiple exceptions can be handled by each except* clause.
ExceptionGroup is a collection/group of different kinds of Exception.
Without creating Multiple Exceptions we can group together different
Exceptions which we can later fetch one by one whenever necessary, the
order in which the Exceptions are stored in the Exception Group doesn’t
matter while calling them.
try:
raise ExceptionGroup('Example ExceptionGroup', (
TypeError('Example TypeError'),
ValueError('Example ValueError'),
KeyError('Example KeyError'),
AttributeError('Example AttributeError')
))
except* TypeError:
...
except* ValueError as e:
...
except* (KeyError, AttributeError) as e:
...
53. What is Python Switch Statement?
From version 3.10 upward, Python has implemented a switch case feature
called “structural pattern matching”. You can implement this feature with
the match and case keywords. Note that the underscore symbol is what
you use to define a default case for the switch statement in Python.
Note: Before Python 3.10 Python doesn't support match Statements.
match term:
case pattern-1:
action-1
case pattern-2:
action-2
case pattern-3:
action-3
case _:
action-default
54. What is Walrus Operator?
● Walrus Operator allows you to assign a value to a variable within
an expression. This can be useful when you need to use a value
multiple times in a loop, but don't want to repeat the calculation.
● Walrus Operator is represented by the `:=` syntax and can be
used in a variety of contexts including while loops and if
statements.
Note: Python versions before 3.8 doesn't support Walrus Operator.
1
numbers = [1, 2, 3, 4, 5]
2
3
while (n := len(numbers)) > 0:
4
print([Link]())
Output
5
4
3
2
1