Python Interview Questions Set 1
Q.1. What is Python? What are the benefits of using Python?
Answer:- Python is a programming language with objects, modules, threads, exceptions and automatic memory management. The benefits of pythons are that it is simple and easy, portable, extensible, build-in data structure and it is an open source.
Q.2. What are the key features of Python?
Answer:- The key features of Python are as follows:-
Interpreted Language: Python is an interpreted language that is used to execute the code line by line at a time. It doesn’t need to be compiled before execution, unlike languages such as C.
Dynamically Typed: Python is a dynamic-typed language. Typing refers to type-checking in programming languages. Dynamic type checking refers to check Data Types during execution. It means that you don’t need to mention the data type of variables during their declaration. Python Interpreter will identify the data type on the basis of the value of the variable.
For Example: below code will run without any error
A=10 // it will automatically assign integer datatype
A= “ Goeduhub” // it will automatically assign string datatype
Object-oriented programming : Python supports an object-oriented programming paradigm. Python has classes, inheritance, and all other usual OOPs concepts other than access specifiers. Python does not have access specifiers like public, private.
Highly Portable: Python is a cross-platform language, i.e., a Python program written on a Windows system will also run on a Linux system or Mac system with little or no modifications at all.
Quick and simple: Developing using Python is quick but running it is often slower than compiled languages. Luckily, Python enables to include the “C” language extensions so you can optimize your scripts. Its syntax very much similar to English Language.
Usage in Several Domain- Python can be used in different domain like web-based applications, Data Science, Artificial Intelligence, Machine Learning, big data analytics, Automation , Data modeling and many more. It could be used effectively along with other languages like Java, COBRA, C, C++, ActiveX, and COM.
Open Source and has a large community.
Q.3. Difference between Python and Java?
Answer:-
Parameter |
Python |
Java |
Code |
Less lines of code |
Less lines of code |
Syntax |
Syntax is easy to remember almost similar to english language |
Syntax is complex as it throws error if you miss semicolon or curly braces. |
Compilation |
Python is an Interpreted Language |
Java is a Compiled Language |
Data types |
Dynamically typed |
Statically typed |
Speed |
Python programs run faster than Java. |
Java program runs slowly compared to Python. |
Machine Learning Libraries |
Tensorflow, Pytorch |
Weka, Mallet, Deeplearning4j, MOA |
Q.4. What are local variables and global variables in Python?
Answer:- Global Variables: Variables declared outside a function or in global space are called global variables. These variables can be accessed by any function in the program.
Local Variables: Any variable declared inside a function is known as a local variable. This variable is present in the local space and not in the global space.
For Example-
x=10
def sum():
y=20
z=x+y
print(z)
sum()
Output-30
When we try to access local variable y outside the sum() function , program will throw error.
Q.5. How Python is interpreted?
Answer:- Python language is an interpreted language. An Interpreted language executes its statements line by line. Programs written in an interpreted language runs directly from the source code, with no intermediary compilation step. Some Interpreted languages such as Python, Javascript, R, PHP and Ruby etc.
Q.6. What is pep 8?
Answer:- PEP stands for Python Enhancement Proposal. A PEP is a design document providing information to the Python community, or describing a new feature for Python or its processes or environment. The PEP should provide a concise technical specification of the feature and a rationale for the feature.
PEP 8 is especially important since it documents the style guidelines for Python Code.
Q.7. What is the purpose of PYTHONPATH environment variable in Python?
Answer:- It is an environment variable which is used when a module is imported. It is also responsible for handling the default search path for Python Modules. The PYTHONPATH variable holds a string with the name of various directories that need to be added to the sys.path directory list by Python.
Whenever a module is imported, PYTHONPATH is also looked up to check for the presence of the imported modules in various directories. The interpreter uses it to determine which module to load.
We can set PYTHONPATH on a machine just like environment variables. After setting PYTHONPATH when we import a module which is not installed yet , it will search in PYTHONPATH directory.
For Example- we have created a .py file named my_module.py. Now we just want to import this file into another .py file like a module.
import my_module
So interpreter will search this file inside PYTHONPATH directory and use it. Here PYTHONPATH directory is location of my_module.py file.
Q.8. What is the purpose of PYTHONSTARTUP, PYTHONCASEOK, and PYTHONHOME environment variables?
Answer:-
PYTHONSTARTUP:
- This variable contains path of an initialization file containing Python source code.
- When we start the Python interpreter, initialization is executed each time.
- In the Unix system, it is named as pythonrc.py
- The commands are included to load utilities or change PYTHONPATH
PYTHONCASEOK:
- It is used in the Windows operating system and OS X.
- It is used to find the first case-insensitive match in the import statement.
- To activate this variable, we can set it to any value.
PYTHONHOME:
- It is normally used within PYTHONSTARTUP or PYTHONPATH directories.
- It is used to make easines in switching module libraries.
Q.9. What are the applications of Python?
Answer:-
1.Web Applications
We can use Python to develop web applications.It also provides Frameworks such as Django, Pyramid to design and develop web applications.
2.Machine Learning
Different applications can be implemented in machine learning using python.
3.Scientific computing
Some libraries like Scipy , Pandas are full package of engineering and mathematics
4.Business applications
OpenErp, Tryton, Picalo are good example of python business applications.
5.Games and 3D graphics
PyGame library for games and GUI library to develop user interface in python based application.
6.Desktop applications
You tube, drop box, bit torrent, bazaar, and cinema 4D are the desktop applications which are developed in python
Q.10. How memory management is done in Python?
Answer:-
- Memory management in Python involves a private heap containing all Python objects and data structures. The management of this private heap is ensured internally by the Python memory manager. The programmer does not have access to this private heap.
- Python memory manager takes care of the allocation of Python private heap space.
- Python also has an inbuilt garbage collector, which recycles all the unused memory and so that it can be made available to the heap space.
Q.11. What type of language is python? Programming or scripting?
Answer:- Python is general purpose programming language. But python is also capable of scripting. The scripting language which works on the basis of automating a repeated task which involves a same or similar type of steps while executing the procedure or program. Scripting Languages are interpreted.
Q.12. What is namespace in Python?
Answer:- A namespace in Python ensures that object names in a program are unique and can be used without any conflict. Python implements these namespaces as dictionaries with 'name as key' mapped to a corresponding 'object as value'. This allows for multiple namespaces to use the same name and map it to a separate object.
A few examples of namespaces are as follows:
-
Local Namespace includes local names inside a function. the namespace is temporarily created for a function call and gets cleared when the function returns.
-
Global Namespace includes names from various imported packages/ modules that is being used in the current project. This namespace is created when the package is imported in the script and lasts until the execution of the script.
-
Built-in Namespace includes built-in functions of core Python and built-in names for various types of exceptions.
Lifecycle of a namespace depends upon the scope of objects they are mapped to. If the scope of an object ends, the lifecycle of that namespace comes to an end. Hence, it isn't possible to access inner namespace objects from an outer namespace.
Q.13. What are python modules? Name some commonly used built-in modules in Python?
Answer:- Module is defined as a file that includes a set of various functions and Python statements that we want to add in our application. A Python module is a .py file containing executable code.
Some of the commonly used built-in modules are:
- os
- sys
- math
- random
- data time
- JSON
How to create a module in Python?
Example of creating a module:
In order to create a module first, we need to save the code that we want in a file with .py extension.
Save the module with module.py
def hello(name):
print("Hey, " + name)
Q.14. Is python case sensitive?
Answer:- Yes. Python is a case sensitive language.
Q.15. How are arguments passed by value or by reference?
Answer:- Everything in Python is an object and all variables hold references to the objects. The references values are according to the functions; as a result you cannot change the value of the references. However, you can change the objects if it is mutable.
For any queries or doubts refer to comment section mentioning question number with doubt
For more Technical MCQ's and Interview Questions Click here