Python Interview Questions
Python Interview Questions
Python Interview Questions
Interview Question
Q 3. What is PEP 8?
©Topperworld
Q 4. Is Python a compiled language or an interpreted
language?
Ans: A function is a piece of code that is only written once and can
be executed whenever the program calls for it. A function is a self-
contained block of statements with a valid name, list of parameters,
and body. Capabilities make programming more practical and particular
to perform measured assignments. There are a number of built-in
functions for completing tasks in Python, and the user can also create
new functions.
Functions fall into three categories:
Built-In Functions: duplicate(), len(), count() are the a few
implicit capabilities.
User-defined Functions: User-defined functions are functions
that are defined by a user.
Anonymous functions: Because they are not declared using the
standard def keyword, these functions are also referred to as
lambda functions.
Ans: Mutable data types can be edited i.e., they can change at
runtime. Example - List, Dictionary, etc.
Immutable data types can not be edited i.e., they can not change at
runtime. Example – String, Tuple, etc.
©Topperworld
Q 7. What is zip() capability in Python?
Ans: The zip () function in Python returns a zip object that maps an
identical index across multiple containers. It takes an iterable, transforms it
into an iterator, and then uses the passed iterables to combine the elements.
It returns a tuple iterator.
Signature zip(iterator1, iterator2, iterator3, etc.) Parameters iterator1,
iterator2, and iterator3: These are joined-together iterator objects.
Return It returns a iterator that is the product of two or more iterators.
Ans: There are 3 main keywords i.e. try, except, and finally which are
used to catch exceptions and handle the recovering mechanism accordingly.
Try is the block of a code that is monitored for errors. Except block gets
executed when an error occurs.
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.
©Topperworld
Q 10. Can we Pass a function as an argument in
Python?
Example:
OUTPUT:
©Topperworld
Q 12. Give an example of shuffle() method?
Ans: The given string or array is shuffled using this technique. The items
in the array become random as a result. The random module includes this
method. Therefore, we must import it before calling the function. It
rearranges components each time when the capability calls and creates
different result.
Example:
OUTPUT:
©Topperworld
Q 13. What are *args and *kwargs?
Ans: The location where we can find a variable and access it if required is
called the scope of a variable.
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.
©Topperworld
Q 16. What is tuple in Python?
Example:
OUTPUT:
©Topperworld
Q 17. What are the different file processing modes
supported by Python?
Ans: There are four ways to open files in Python. The read-write (rw),
write-only (w), append (a), and read-only (r) modes. 'r' is used to open a file
in read-only mode; 'w' is used to open a file in write-only mode; 'rw' is used
to open in both read-only and write-only modes; and 'a' is used to open a file
in append mode. In the event that the mode isn't determined, of course
document opens in read-just mode.
Read-only (r) mode: Read a file by opening it. It's the default setting.
Only write mode (w): Open a document for composing. On the off
chance that the record contains information, information would be lost.
A brand-new file is also created.
Read-Write (rw) mode: In write mode, open a file for reading. It
implies refreshing mode.
Addition mode (a): If the file exists, open it for writing and append it
to the end.
Ans: Numerous operations can be carried out with Python's extensive set
of operators. A few individual administrators like membership and identity
operators are not all that recognizable yet permit to perform operations.
Arithmetic Operators - perform basic arithmetic operations. For
example, "+" is used to add.
Relational Operators - are used to comparing the values. These
operators test the conditions and then returns a Boolean value.
Assignment Operators - are used to assigning values to the variables.
Logical Operators - are used to performing logical operations like And,
Or, and Not.
Membership Operators - Participation administrators are accustomed
to checking whether a component is an individual from the grouping or
not.
Identity Operators - are used to check two variables that are in the
same memory area.
Bitwise Operators - are used to performing operations over the bits.
The binary operators (&, |, OR) work on bits. See the example below.
©Topperworld
Q 19. What is a break, continue, and pass in Python?
©Topperworld
Q 22. What is the difference between xrange and range
functions?
Ans: range() and xrange() are two functions that could be used to iterate
a certain number of times in for loops in Python. In Python 3, there is no
xrange, but the range function behaves like xrange in Python 2.
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.
Ans: In Python 3, the old Unicode type has replaced by "str" type, and
the string is treated as Unicode of course. Using the art.title.encode("utf-8")
function, we can create a Unicode string.
Example:
OUTPUT:
©Topperworld
Q 25. is Python interpreted language?
Ans: Python is a language that is interpreted. From the source code, the
Python program runs directly. It turns the source code into intermediate
language code, which is then again translated into machine language that
needs to be run.
Python does not require compilation before running, unlike Java and C.
Ans: 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.
Ans: 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.
Ans: Python uses its private heap space to manage the memory. Basically,
all the objects and data structures are stored in the private heap space. Even
the programmer can't access this private space as the interpreter takes care
of this space. Python also has an inbuilt garbage collector, which recycles all
the unused memory and makes it available to the heap space.
©Topperworld
Q 29. What is slicing in Python?
Ans: Python Slicing is a string operation for extracting a part of the string,
or some part of a list. With this operator, one can specify where to start the
slicing, where to end, and specify the step. List slicing returns a new list from
the existing list.
Ans: The Pickle module accepts any Python object and converts it into a
string representation and dumps it into a file by using the dump function, this
process is called pickling. While the process of retrieving original Python
objects from the stored string representation is called unpickling.
©Topperworld
Q 32. What is the Python decorator?
Example:
OUTPUT:
©Topperworld
Q 33. What are Access Specifiers in Python?
Ans: 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.
Example:
©Topperworld
Q 35. What is a negative index in Python and why are
they used?
Ans: Python's sequences are indexed and contain both positive and
negative numbers. The numbers that are positive purposes '0' that is
utilizes as first record and '1' as the subsequent file and the cycle go on
that way.
The negative number's index begins with '-1,' which denotes the
sequence's final index, and ends with '-2,' which denotes the sequence's
penultimate index.
Ans: Help() and dir() the two capabilities are open from the Python.
Function "help()": The help() function enables us to view module,
keyword, and attribute-related help in addition to displaying the
documentation string.
The Dir() method: The defined symbols are displayed using the dir()
function.
©Topperworld
Q 39. Which programming language is a good choice
between Java and Python?
Ans: Most of the checking for things like type, name, and so on is done
at compile time in Python. are deferred until code execution. As a result,
the Python code will compile successfully if it refers to a user-defined
function that does not exist. With one exception, the Python code will
fail when the execution path is missing.
Ans: The shortest way to open a text file is by using "with" command
in the following manner:
Example:
©Topperworld
Q 42. What is the usage of enumerate () function in
Python?
Example:
OUTPUT:
Ans: Self is a class's instance or object. This is explicitly set as the first
parameter in Python. Be that as it may, this isn't true in Java where it's
discretionary. It assists with separating between the techniques and
properties of a class with neighborhood factors.
The newly created object is referred to as the self-variable in the init
method, whereas the object whose method was called is referred to in
other methods.
©Topperworld
Q 44. How to send an email in Python Language?
Ans: Python has the smtplib and email modules for sending emails.
Import these modules into the made mail script and send letters by
confirming a client.
It has a strategy SMTP(smtp-server, port). It requires two boundaries
to lay out SMTP connection.
A simple example to send an email is given below.
Example:
Ans: In Python, lists and arrays both store data in the same way.
However, lists can hold any data type of elements, whereas arrays can
only hold one data type of elements.
Example:
©Topperworld
OUTPUT:
Example:
OUTPUT:
©Topperworld
Q 47. What benefits do NumPy exhibits offer over
(nested) Python records?
Ans: A simple text file serves as the template. Any text-based format,
including XML, CSV, and HTML, can be created by it. A layout contains
factors that get supplanted with values when the format is assessed and
labels (% tag %) that control the rationale of the layout.
©Topperworld
Q 49. Explain the use of session in Django framework?
Ans: Django gives a meeting that allows the client to store and
recover information on a for each site-guest premise. By placing a
session ID cookie on the client side and storing all relevant data on the
server side, Django abstracts the sending and receiving of cookies.
So, the data itself is not stored client side. This is good from a security
perspective.
Ans: Modules in Python are files that contain Python code. This code
can either be capabilities classes or factors. A Python module is a.py file
with code that can be run.
os
sys
math
random
data time
JSON
©Topperworld
“Unlock Your
Potential”
With- Topper World
Explore More
topperworld.in
Follow Us On
E-mail
topperworld.in@gmail.com