Python Interview Questions
Python Interview Questions
Here is the list of some areas where Python programming language is used:
● Data Analysis
● Software testing
● Web development
● Game development
● Machine Learning
● Artificial Intelligence
● Desktop GUI
● Blockchain
● Prototyping
● Everyday tasks like downloading audio and video files, updating the lists, converting files,
filling information, renaming and arranging large batches, setting notifications, and many
more.
Most of the big names in the industry are using Python language for their operations. Brands
like IBM, NASA, Pixer, Facebook, Spotify, Intel, YouTube, Instagram, Pinterest, Reddit, etc.,
use Python as one of the primary languages.
● Interpreted language
● Efficiency
● Open-source library
● Rich frameworks
● Testing instruments.
Before writing a program in Python, there should be an interpreter like PyCharm installed on
the system.
You can run a Python code from the run option in the menu in an interpreter. A Python code
can also run in command prompt code. It will run the program after entering the correct
path of the file.
A function in Python is a code block that runs when it is called in a program. Data can be
passed into the function with the help of this, and it is known as a parameter. The function
can also return data as results.
9. How many functions are there in Python?
● Built-in functions: These functions come defined with the Python language. Here are some
of the built-in functions in Python:
➔ print() function
➔ len() function
➔ sum() function
➔ sorted() function
➔ dir() function
➔ max() function
● User-defined functions:
These functions are defined by the user to perform a task in the program.
These are defined with the def keyword at the start. These functions are
written like this:
➔ def add_numbers()
➔ def entry()
● Anonymous functions: These are the functions that are defined without
a name. These are also called lambda functions. These are used when a
user needs a function for a little time in function. It is written as—
lambda().
A function in Python is called with the help of the def keyword. A function block starts with
this keyword, and then function name and parentheses(()) take place. Arguments are placed
between this, and parameters can also be defined inside of this.
Python code is interpreted. A Python code is converted into bytecode that is executed by a
virtual machine in Python. A Python program is not needed to build like code for other
languages where it is compiled, and it works on an interpreter only.
Type checking takes place at execution in a dynamically typed language. The variables are
checked against types only when the code is executing. Programming languages like Python,
JavaScript, PHP, Lisp, etc., come under this type.
The exact same variable can be used multiple times in a program in a dynamically typed
language like Python.
The raw data that is assigned to constants and variables are defined as literals in Python.
Basically, these are used to represent a fixed value in the source code. It can be either a
string type, a numeric type, a boolean type, etc.
When the implementation of the programs is done directly without compilation of the
program into machine language, the language is called interpreted language. These
languages are slower than the compiled languages.
All the debugging occurs at run time in this language. Python, JavaScript, Perl, etc., are some
of the examples of interpreted languages.
The isolated environment for development and debugging Python is called virtualenv. It runs
multiple applications with the help of pip. It is also used to isolate a Python interpreter with
settings and libraries.
It is a tool that is used for storing copies of Python and pip that are installed from the
projects. With the help of this, a user can work on multiple projects on the same machine at
the exact same time.
Most future technologies are mostly dependent on Python, and that is what makes it one of
the most used languages.
Python is very popular, and it is used as it is less complex than other languages. The future is
of Artificial Intelligence (AI) and Machine Learning (ML). Because the majority of
programming in ML and AI is done with Python, the future of the language contains more
opportunities.
The Python map() function processes and transforms the iterable like tuples and lists
without using for loop.
Using the ‘with’ command is the shortest way to open a text file and display the content in
it.
21. Can you write a Python program to add two integers that are greater than zero without
using the plus operator?
A[2]=[1,3,4,5,8,9,11,56]
The indexing starts from zero, and the element at the second index is 4. The output will be 4.
A dictionary in Python is a collection of items that are written in curly brackets with keys and
values. The items are in no particular order and are used to retrieve the value for keys that
are known.
If there are two methods with the exact same name to perform different tasks and provide
different functionality, the concept of overriding is used. The new modifiers hide the original
method, which provides different functionality. The original method can also be accessed by
the base class.
Pass is a statement in Python that is used as a placeholder for upcoming codes. At execution,
it does not show any error, but it is not allowed in loops, class definitions, if statements, or
function definitions.
When the functions call themselves in a program, the process is known as recursion. It has
two parts, the base case and the recursive case.
The base case is used to stop the recursion, and the recursive base is where the function
calls itself.
● Prototyping
● Game development
● Data processing
● OS module
● Random module
● Sys module
● JSON
● Math module
● Collective module.
When a nested function defines a value in an enclosing scope, the closure occurs in the
program. It remembers the values.
All the numerical code is stored in SciPy. It contains a better version of the linear algebra
modules with more features. There are more algebra modules in SciPy as well.
Enumerate() is a function that is used to iterate through a sequence. It keeps track of both
elements and the index. It takes the collection and returns it as an enumerated object.
Python does not perform compile-time code checking except when the code is syntactically
invalid. If the code is following the syntax rules, it will compile without even checking it.
Python run time code checking is done dynamically. All the parts of the program are checked
at runtime.
- Binary type
- Boolean type
- Set type
- Mapping type
- Sequence type
- Numeric types
– Text type.
34. What is the basic difference between .py and .pyc files?
.py files have the source code of a program, while the .pyc files have the bytecode of a
Python program. Python compiles the .py files and saves them as .pyc files.
35. What are comments in Python? What are different types of comments?
Comments are text with the information in Python. These are used when two or more
programmers are working on a project. The use of Python comments is to analyze, debug,
and provide feedback.
- Global variables are defined in the global scope. Global keyword is used for using it in
global scope in a function.
- Private attributes have double underscores as prefixes to their identifiers. They can not be
accessed from outside directly, which results in AttributeError.
- Protected attributes have an underscore as a prefix to their prefix. It can also be accessed
from outside the class.
Duplicate elements can be removed from the list by turning them into the set. The set (list)
function is used for it.
Tkinter in Python is a library and toolkit for GUI development. It has attributes like
dimensions, fonts, colors, etc. It also gives support for various GUI applications.
A Python pyramid is made for larger applications. We can select a database, URL structure,
templating style, database, etc., with the help of a pyramid. It provides flexibility as it is a
heavy configuration program.
It is used to comment out everything that comes after on the line in Python.
A package holds sub-packages and modules, and the file __init__.py is used in the package
for holding user-interpreted codes.
A module is a file that has Python code. It also modifies the code to get executed in run
time. It consists of the unit namespace, which also has extracted variables.
The modules prevent collision between global variable names, and packages do the same
between module names. The packages are also reusable, and that is why they are preferred.
In Python, the location where we can find a variable and can access it when it is required is
called scope. This is of two types: global and local.
Global variables are the ones that can be used by any part of the program and can be
declared outside of any function.
Jython is mostly used for using Python codes on the Java platform. It is an open-source
implementation of Python code integrated with Java.
The code is compiled to Java bytecode and runs on any Java virtual machine. It gives access
to all the Java libraries.
Here are some of the best Python code practices that a developer should follow:
It is used to assign values to a variable in an outer scope. It causes listed identifiers to refer
to the previous variable in the nearest enclosing scopes.
48. If you installed a module with the pip, but it doesn’t import in your IDLE, what are the
possible reasons?
● If the system has Python 2. Since NumPy is installed for Python 3.6 and the latest versions,
this will show an error.
● Check if there is an issue with the configuration of the anaconda with IDLE. If the system is
using default Python in place of anaconda, NumPy can not be installed.
The os.walk() is a function that generates the file names in the directory tree. It is done by
walking the tree either bottom-up or top-down. It yields a 3-tuple for each directory, which
are dirpath, dirnames, and filenames.
Both the methods are used to define a class method that is installed without instantiating
the object in a class. The signature in both methods is different.
51. What is the use of the PYTHONSTARTUP environment variable in the Python program?
The PYTHONSTARTUP environment variable is used to specify the particular location of the
path to a file in Python. It can also be used in setting colors and preloading modules. This
script will run before the interpreter. .
52. What is the use of the PYTHONCASEOK environment variable in the Python program?
It is used to find the first case-sensitive matches in the import statements. It is used to
ignore all import statements while calling the interpreter in Python.
PEP 8 is a document that guides the users with best practices on a Python program. The
document was written by Guido van Rossum, Barry Warsaw, and Nick Coghlan in 2001.
With the primary focus on improving the readability and consistency of Python code, Python
Enhancement Proposal or PEP is also used for improving the design and style of a program.
54. What are decorators in Python?
Decorators in Python are used to add functionality to a class or a function. It can change the
behaviour of a class without permanent modification in it.
55. What is the dogpile effect? What would you do to prevent this effect?
When the cache expires, and the client requests multiple times for the website at the same
time, the dogpile effect occurs. I will use the semaphore lock to prevent this effect. Because
when the value expires, it will acquire the lock and generate a new value.
When several programs are running concurrently by invoking multiple threads, the process is
called multithreading. The Thread class in Python is a predefined class. It is defined in the
threading module. It is a lightweight process. Several threads refer to the data space with
the main thread and share information easily by communicating with each other. Threads
can be used for calculating results while the main part of the program is running.
● Pass by reference: All the arguments are passed by reference in any function in Python.
When the value of the parameter is changed within the function, the change can also be
seen in the calling function.
● Pass by value: When an argument in the program is passed to the function, only values
pass to the function, leaving reference behind. It is not changeable and immutable.
Python is very good for data analysis. Processes like data mining, data processing, and data
visualization are done easily with the help of Python.
Python 2 is faster than Python 3. The print keyword is considered a statement in 2 and a
function in 3. Strings are stored as ASCII as default in version 2, while UNICODE is in version
3 as default.
The xrange() function is used for iterations in Python 2, and the range() function is used to
perform iterations in Python 3. Version 2 is used by the DevOps engineer, and version 3 is
used in fields like data science and software engineering.
The syntax is also easier in Python 3 than in Python 2. Many libraries in Python 2 are not
forward compatible and are strictly used with Python 3.
When the division of integers is performed, version 2 of Python gives an integral value while
version 3 provides floating type values.
61. What is the execution time for else part of a try-except block?
The else part of the try-except block will execute when there is no exception in the program.
62. Is it possible to call the parent class without its instance creation?
Yes, it is possible by creating an object of the child class and calling the function of the
parent class in the Python program with the help of the dot operator.
A namespace in Python is a system that has a unique name for every object. It is used to
implement scope, and it is created when a function, package, or module is evaluated.
Basically, It is used to organize codes into logical groups when there are multiple libraries.
OOPS is a paradigm in Python that uses classes and objects. Coding is done with the help of
real-world elements like class, data abstraction, polymorphism, inheritance, encapsulation,
etc.
It is a concept in OOP that is used to wrap data and the methods that work on data within a
single unit. This helps a Python program to restrict the variable that is used to prevent
unnecessary modifications and changes to the data.
With encapsulation, the data can be changed by the method of the object. These methods
are called private variables. A class is a good example of encapsulation in Python as it is used
to encapsulate the data like variables and member functions.
● Single inheritance
● Multilevel inheritance
● Multiple inheritances
● Hierarchical inheritance
Here are some of the key advantages of using inheritance in a Python code:
● It is transitive in nature.
Data abstraction is used for hiding the actual implementation of the Python program, and it
is done by showing only the functionalities.
If a class has one abstract function, it is called an abstract class. Once the module is
imported from the ABC (Abstraction Base Class) module, abstract methods can be created in
a Python program.
It means to have multiple forms of any object in a Python program. A code can be used to
determine and differentiate the program.
Programs:
1. Python code for printing HelloWorld