Top 50+ Python Interview Questions and Answers (Latest 2024)
Top 50+ Python Interview Questions and Answers (Latest 2024)
System Scripting
Web Development
Game Development
Software Development
Complex Mathematics
For Example:
a = lambda x, y : x*y
print(a(7, 19))
5//2 = 2
5/2 = 2.5
The beauty of the final block is to execute the code after trying for an error.
This block gets executed irrespective of whether an error occurred or not.
Finally, block is used to do the required cleanup activities of
objects/variables.
string = "GeeksforGeeks"
string.swapcase() ---> "gEEKSFORgEEKS"
Python Local variable: Local variables are those that are initialized within
a function and are unique to that function. It cannot be accessed outside
of the function.
Python Global variables: Global variables are the ones that are defined
and declared outside any function and are not specified to any function.
Module-level scope: It refers to the global objects of the current module
accessible in the program.
Outermost scope: It refers to any built-in names that the program can
call. The name referenced is located last among the objects in this scope.
We use19. What
cookies to ensureis
youdocstring in Python?
have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Policy
Python documentation strings (or docstrings) provide a convenient way of
associating documentation with Python modules, functions, classes, and
methods.
Continue is also a loop control statement just like the break statement.
continue statement is opposite to that of the break statement, instead of
terminating the loop, it forces to execute the next iteration of the loop.
floor() method in Python returns the floor of x i.e., the largest integer not
greater than x.
Also, The method ceil(x) in Python returns a ceiling value of x i.e., the
smallest integer greater than or equal to x.
range() – This returns a list of numbers created using the range() function.
xrange() – This function returns the generator object that can be used to
display numbers only by looping. The only particular range is displayed
We use cookies to ensure you
on demand andhave the bestcalled
hence browsinglazy
experience on our website.
evaluation . By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Policy
25. What is Dictionary Comprehension? Give an Example
Dictionary Comprehension is a syntax construction to ease the creation of a
dictionary based on the existing iterable.
List
Tuple
A shallow copy has faster program execution whereas a deep copy makes it
We use cookies to ensure you have the best browsing experience on our website. By using
slow.
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Policy
29. Which sorting technique is used by sort() and
sorted() functions of python?
Python uses the Tim Sort algorithm for sorting. It’s a stable sorting whose
worst case is O(N log N). It’s a hybrid sorting algorithm, derived from merge
sort and insertion sort, designed to perform well on many kinds of real-
world data.
We use cookies
35. Whatto ensureis
youPolymorphism
have the best browsing experience on our website. By using
in Python?
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Policy
Polymorphism means the ability to take multiple forms. So, for instance, if
the parent class has a method named ABC then the child class also can
have a method with the same name ABC having its own parameters and
variables. Python allows polymorphism.
os.remove()
os.unlink()
# g.py
class GeeksClass:
def function(self):
print "function()"
import m
def monkey_function(self):
print "monkey_function()"
m.GeeksClass.function = monkey_function
obj = m.GeeksClass()
obj.function()
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.
Python3
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:
...
We use cookies to ensure you have the best browsing experience on our website. By using
53.
our site, you What
acknowledgeisthat
Python
you have readSwitch Statement
and understood our Cookie Policy & Privacy
Policy
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.
Python3
match term:
case pattern-1:
action-1
case pattern-2:
action-2
case pattern-3:
action-3
case _:
action-default
The Walrus Operator is represented by the `:=` syntax and can be used in a
variety of contexts including while loops and if statements.
Python3
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Policy