Python Interview Qts
Python Interview Qts
Facebook, JP Morgan Chase, Spotify, and many more because of its performance and its powerful
libraries. To get into these companies and organizations as a Python developer, you need to master
some important Python Interview Questions to crack their Python Online Assessment Round and
Python Interview Round. We have prepared a list of the Top 50 Python Interview Questions along
with their answers to ace in interviews.
Python Interview Questions
Table of Content
Basic Python Interview Questions for Freshers
What is Python?
Python is one of the most widely-used and popular programming languages, was developed by
Guido van Rossum and released first on February 20, 1991. Python is a free and open-source
language with a very simple and clean syntax which makes it easy for developers to learn Python. It
supports object-oriented programming and is most commonly used to perform general-purpose
programming. Python is used in several domains like Data Science, Machine Learning, Deep Learning,
Artificial Intelligence, Scientific Computing Scripting, Networking, Game Development Web
Development, Web Scraping, and various other domains.
Python grows exponentially due to its simplistic nature and the ability to perform several functions in
just a few lines of code. Due to its ability to support powerful computations using powerful libraries,
it is used in various domains and it become the reason for the huge demand for Python developers
across the world. Companies are willing to offer amazing Packages and benefits to these developers.
System Scripting
Web Development
Game Development
Software Development
Complex Mathematics
2. What are the benefits of using Python language as a tool in the present
scenario?
The following are the benefits of using Python language:
Object-Oriented Language
High-Level Language
Dynamically Typed language
For Example:
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.
Declaring Docstrings: The docstrings are declared using ”’triple single quotes”’ or “””triple double
quotes””” just below the class, method, or function declaration. All functions should have a
docstring.
Accessing Docstrings: The docstrings can be accessed using the __doc__ method of the object or
using the help function.
Statically typed languages: In this type of language, the data type of a variable is known at the
compile time which means the programmer has to specify the data type of a variable at the time of
its declaration.
Dynamically typed languages: These are the languages that do not require any pre-defined data
type for any variable as it is interpreted at runtime by the machine itself. In these languages,
interpreters assign the data type to a variable at runtime depending on its value.
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.
Numeric: The numeric data type in Python represents the data that has a numeric value. A numeric
value can be an integer, a floating number, a Boolean, or even a complex number.
Sequence Type: The sequence Data Type in Python is the ordered collection of similar or different
data types. There are several sequence types in Python:
Python String
Python List
Python Tuple
Python range
Mapping Types: In Python, hashable data can be mapped to random objects using a mapping object.
There is currently only one common mapping type, the dictionary, and mapping objects are mutable.
Python Dictionary
Set Types: In Python, a Set is an unordered collection of data types that is iterable, mutable, and has
no duplicate elements. The order of elements in a set is undefined though it may consist of various
elements.
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 on demand and hence called lazy evaluation.
Tuple comprehension is not possible in Python because it will end up in a generator, not a tuple
comprehension.
List
The list is better for performing operations, such as insertion and deletion.
Tuple
28. What is the difference between a shallow copy and a deep copy?
Shallow copy is used when a new instance type gets created and it keeps values that are copied
whereas deep copy stores values that are already copied.
A shallow copy has faster program execution whereas a deep coy makes it slow.
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.
If a function contains at least a yield statement, it becomes a generator. The yield keyword pauses
the current execution by saving its states and then resumes from the same when required.
os.remove()
os.unlink()
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.
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.
Python3
try:
TypeError('Example TypeError'),
ValueError('Example ValueError'),
KeyError('Example KeyError'),
AttributeError('Example AttributeError')
))
except* TypeError:
...
except* ValueError as e:
...
...
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
print(f"Hello, {name}!")
else: