Basic Python Interview Questions – Part 1
Q.1 What is Python?
Python is a high-level programming language. Guido van Rossum is the developer of Python. Python was released in 1991.
It is an object-oriented, general-purpose computer programming language that can run on various platforms like Windows, Linux, UNIX, and Macintosh. It has several uses in data science, machine learning, and artificial intelligence.
Q.2 What are the advantages of using Python?
Python is a general-purpose programming language. It has a simple and easy-to-learn syntax. Hence maintains readability and lessens the cost of program maintenance. Not only this, the language can script, totally open-source, and supports third-party. It leads to modularity and code-reusability.
It is a dynamically typed language having high-level data structures. Many developers use Python for application development and deployment.
Q.3 Describe PEP 8 in Python?
PEP stands for Python Enhancement Proposal. It is essentially an official design document that presents information to the Python community. Also, it explains new highlights for Python or its processes. PEP 8 is primarily necessary as it documents the essential guidelines for Python Code. Contributing to the Python open-source community expects you to follow these mentioned guidelines seriously and strictly.
Q.4 Determine the differences between lists and tuples in Python?
|A list is mutable.||A tuple is immutable.|
|A list is unhashable.||A tuple is hashable.|
|We cannot use a list as a key in a dictionary.||We can use a tuple as a key in a dictionary.|
|Syntax: list1 = [10, ‘Saumya’, 20]||Syntax: tup1 = (10, ‘Saumya’ , 20)|
Q5. How does memory management work in Python?
Ans: Memory management takes place in Python in the following manner:
- Python’s private heap space help in memory management in python. All the objects of python and data structures are present in a private heap. A programmer cannot access this private heap. Generally, a python interpreter takes care of this somewhat.
- Python’s memory manager does allocate heap space for Python objects. Also, access to some tools is provided by core API for the programmer to code. Python’s garbage collector recycles all the unused memory. The unused memory is given to the heap space then.
. Q.6 Explain Python namespaces? Also, mention their uses?
A namespace is used in Python to ensure that object names in a program are distinct. So, they can be used without conflict. Python uses a dictionary to implement these namespaces, where ‘name as a key’ mapped to a corresponding ‘object as the value.’ This allows multiple namespaces to use the same name, mapped it to a separate entity. Some examples of namespaces are given below:
- Local Namespace involves local names inside a function. The namespace is built temporarily for a function call and gets freed when the function returns.
- Global Namespace comprises names from various imported packages or modules that are being applied in the prevailing project. This namespace is built when the package is introduced into the script and lasts till its execution.
- Built-in Namespace covers built-in functions of core Python and built-in names for various types of exceptions.
The lifecycle of a namespace depends on the scope of objects they are charted too. If the scope of an object finishes, the lifecycle of this namespace reaches an end. Hence, it’s not probable to obtain inner namespace objects from an outer namespace.
Q7. What are python modules? Name some commonly used built-in modules in Python?
Ans: The files containing Python code are known as python modules. This code can have either function classes or variables. A Python module contains executable code having a .py extension. Here are some commonly used built-in modules :
- data time
Q8. Explain type conversion in Python?
Ans: The conversion of one data type into another is known as type conversion.
Let me tell you some functions that we use for type conversion.
int() – It converts any data type into integer type
float() –It converts any data type into float type
ord() – It converts characters into an integer
hex() – It converts integers to hexadecimal
oct() – It converts an integer to the octal
tuple() – This function converts a data type into a tuple
set() – This function helps in converting any data type to a set type
list() – This function helps in converting any data type to a list type.
dict() – This function helps in converting a tuple of order (key, value) into a dictionary.
str() – This function helps in converting an integer into a string.
complex(real,imag) – This function helps in converting real numbers to complex(real,imag) numbers.
Q9. Why do we need indentation in python?
Ans: Indentation is required for Python. It shows a block of code. We have to put all code within loops, classes, functions, etc., within an indented block. Indentation is implemented using four space characters. If you won’t do proper indentation in your code, it will not execute properly and throw errors.
We will cover the remaining questions in the next article.
Thanks for reading this article !!