Once you have a good understanding of the different concepts of Python, you can do an interview. To increase your chances of clearing them, here is a list of top Python interview questions you need to know the answer to:
1. What is Python?
At first, Python was developed by Guido van Rossum and released in 1991.
Then, this is a general-purpose computer programming language. It is a high-level object-oriented language that runs on various platforms such as Windows, Linux, UNIX, and Macintosh. A built-in high-level data structure that combines dynamic typing and dynamic binding. Widely used in data science, machine learning, and artificial intelligence.
Finally, it is easy to learn and requires less code to develop your application.
Uses of python are:
- Web development (server-side).
- Software development.
- System scripting.
2. What type of language is python? Programming or scripting?
Generally Python considered a general-purpose programming language but it can write scripts.
3. What are the supported standard data types in Python?
The standard data types supported by Python include:
4. What is an Interpreted language?
5. What are the key features of Python?
The main features of Python are:
- Interpreter language: Python is the interpreter language used to execute code line by line. This makes debugging easier.
- Portable: Python can run on a variety of platforms such as Unix, Macintosh, Linux, and Windows. In other words, it is a highly portable language.
- Extendable: Allows Python code to be compiled in various other languages such as C and C ++.
- GUI programming support: This means that Python supports the development of graphical user interfaces.
6. What is the purpose of the PYTHONHOME environment variable?
PYTHONHOME-Alternate module search path. It is usually embedded in the PYTHONSTARTUP or PYTHONPATH directory to facilitate switching module libraries.
7. Define tuples in Python?
Tuples are a Python sequence data type. The number of tuple values is separated by commas.
8. What are the global and local variables in Python?
Python Global Variables:
Variables declared outside a function are called Global variables. These variables can be accessed or called from any function in your program.
g = "welcome to eMexo Technologies" def v() : print (g) v()
welcome to eMexo Technologies
Python local variables:
Local variables refer to variables defined within a function. These variable types can only be accessed within the function.
def v() : g = "welcome to eMexo Technologies" v() print (g)
NameError: name 'g' is not defined
9. What is pass in Python?
The pass keyword represents a null operation in Python. This is typically used to fill an empty block of code that can be run at runtime but has not yet been written. If you do not have a pass statement in the following code, you may get an error while executing the code.
def myEmptyFunc(): # do nothing pass myEmptyFunc() # nothing happens ## Without the pass keyword # File "<stdin>", line 3 # IndentationError: expected an indented block
10. What are Python’s dictionaries?
A Python dictionary is a type of hash table. They act like Perl associative arrays or hashes and consist of key/value pairs. The dictionary key can be almost any Python type, but it is usually a number or string. On the other hand, the value can be any Python object.
11. What are the limitations of Python?
Python has certain restrictions, such as:
- There are design limitations.
- Slow compared to C and C ++ or Java.
- It is inefficient in mobile computing.
- It consists of an undeveloped database access layer.
12. Define PYTHON PATH?
PYTHONPATH is an environment variable used when importing modules. Assuming you are importing modules at any time, use PYTHONPATH to check for the existence of modules being imported into another directory. The loading of the module is determined by the interpreter.
13. What are decorators in Python?
Decorators are used to adding some design patterns to a function without changing the structure of the function. You usually define decorators before extending functionality. To apply a decorator, first, define a decorator function. Then just write a function that applies it and add a decorator function on top of the function that applies it. For this, use the @ sign before the decorator.
14. What is the use of self in Python?
Self is used to represent an instance of a class. You can use this keyword to access class attributes and methods in Python. Binds the attribute with the specified arguments. In many places, Self is used and is often considered a keyword. However, unlike C ++, self is not a keyword in Python.
15. Can we reverse a list in Python?
Yes, you can reserve a list in Python using the reverse () method. The following code expressed as:
16. What is PEP 8?
PEP8 stands for Python Enhancement Proposal and can be defined as a document that helps provide guidelines for writing Python code. Python code should follow these rules to make it easier to read. Guido van Rossum, Barry Warsaw, and Nick Coghlan wrote it in 2001.
17. What is docstring in Python?
- A documentation string or docstring is a multi-line string used to document a particular segment of code.
- The docstring should describe what the function or method does.
18. What is the Purpose of the Python Break statement?
Break statement – Ends a loop statement and transfers execution to the statement immediately following the loop.
19. In Python, how does memory management work?
- Python manages memory over a private heap. The private heap is the storage area for all data structures and objects. The interpreter can access the private heap, and the programmer cannot access this private heap.
- Python’s memory manager allocates memory for data structures and objects. Access to some tools is provided by the core API for programmers to code.
- To make heap space available, Python’s garbage collector recycles all unused memory.
20. Can we use a break and continue together in Python? How?
In Python, we can use both break and continue together at a time. The break stops the execution of the current loop and the jump leads to another loop.
21. What are generators in Python?
A generator is a function that returns a repeatable collection of items in an individually specified way. There are a variety of approaches to creating iterators using generators. To return a generator object, use the yield keyword instead of return.
Let’s build a Fibonacci number generator-
## generate fibonacci numbers upto n def fib(n): p, q = 0, 1 while(p < n): yield p p, q = q, p + q x = fib(10) # create generator object ## iterating using __next__(), for Python2, use next() x.__next__() # output => 0 x.__next__() # output => 1 x.__next__() # output => 1 x.__next__() # output => 2 x.__next__() # output => 3 x.__next__() # output => 5 x.__next__() # output => 8 x.__next__() # error ## iterating using loop for i in fib(10): print(i) # output => 0 1 1 2 3 5 8
22. What is the difference between .py and .pyc files?
At First, the .py file is a Python source code file. Then, the .pyc file contains the bytecode of the Python file. Importing code from another source creates the .pyc file. The interpreter converts .py source files to .pyc files, which helps to save time.
23. Define modules in Python?
This module is defined as a file that contains various functions and Python statements that you add to your application.
Example of creating a module:
At First, To create the module for the first time, you need to save the required code in a file with a .py extension. Save the module with module .py
def wishes(name): Print("Hi, " + name)
24. Does Python pass arguments by value or by reference?
- Pass by value: A copy of the actual object is passed. Changing the value of a copy of an object does not change the value of the original object.
- Pass by Reference: A reference to the actual object is passed. Changing the value of the new object changes the value of the original object.
Python passes arguments by reference. That is a reference to the actual object passed.
def appendNumber(arr): arr.append(4) arr = [1, 2, 3] print(arr) #Output: => [1, 2, 3] appendNumber(arr) print(arr) #Output: => [1, 2, 3, 4]
25. What are python modules? Name some commonly used built-in modules in Python?
A Python module is a file that contains Python code. Using a function class or variable can configure this code. A Python module is a .py file that contains executable code.
Some of the most common built-in modules are:
- data time
26. How to overload constructors or methods in Python?
Python constructor: init _ () is the first method of the class. When you try to instantiate an object, Python automatically calls init () to initialize the members of the object. Finally, Python does not allow the overloading of constructors and methods. I get an error when I try to overload.
class student: def __init__(self, name): self.name = name def __init__(self, name, email): self.name = name self.email = email # This line will generate an error #st = student("eMexo") # This line will call the second constructor st = student("eMexo", "email@example.com") print("Name: ", st.name) print("Email id: ", st.email)
Name: eMexo Email id: firstname.lastname@example.org
27. What are the built-in types available in Python?
The built-in types in Python are as follows:
- Complex numbers
- Floating-point numbers
- Built-in functions
28. Explain split() and join() functions in Python?
- You can use the split () function to split a string into a list of strings based on the delimiter.
- You can use the join () function to join a list of strings into a single string based on the delimiter.
string = "This is a string." string_list = string.split(' ') #delimiter is ‘space’ character or ‘ ‘ print(string_list) #output: ['This', 'is', 'a', 'string.'] print(' '.join(string_list)) #output: This is a string.
29. What is the difference between Python Arrays and lists?
Arrays and lists are the same way you store data in Python. However, an array can contain only elements of a single data type, but a list can contain elements of any data type.
import array as arr My_Array=arr.array('i',[1,2,3,4]) My_list=[1,'abc',1.20] print(My_Array) print(My_list)
array(‘i’, [1, 2, 3, 4]) [1, ‘abc’, 1.2]
30. What is swapcase() function in Python?
This is a string function that converts all uppercase letters to lowercase and vice versa. Used to change the existing case of a string. This method makes a copy of the string containing all the characters in case of replacement. Lowercase strings will generate lowercase strings and vice versa. There is an automatic disregard for all non-alphabetic characters. See the example below.
string = "IT IS IN LOWERCASE." print(string.swapcase()) string = "it is in uppercase." print(string.swapcase())
it is in lowercase. IT IS IN UPPERCASE.