Top 30 Python language Interview Questions & Answers
1. What is Python?
Python is a high-level and object-oriented programming language with unified semantics designed primarily for developing apps and the web. It is the core language in the field of Rapid Application Development (RAD) as it offers options such as dynamic binding and dynamic typing.
2. What are the key features of Python?
The following are the significant features of Python, and they are:
- Interpreted Language: Python is an interpreted language that is used to execute the code line by line at a time. This makes debugging easy.
- Highly Portable: Python can run on different platforms such as Unix, Macintosh, Linux, Windows, and so on. So, we can say that it is a highly portable language.
- Extensible: It ensures that the Python code can be compiled on various other languages such as C, C++, and so on.
- GUI programming Support: It implies that Python provides support to develop graphical user interfaces.
- 3. What type of language is Python? Programming or Scripting?
Python is suitable for scripting, but in general, it is considered a general-purpose programming language.
4. What are the global and local variables in Python?
Global Variables in Python: The variables that are declared outside the function are called global variables. These variables can be accessed or invoked by any function in the program.
Local Variables in Python: The variables that are declared inside a function are called local variables. These types of variables can be accessed only inside the function.
5. Define PYTHON PATH?
PYTHONPATH is an environmental variable that is used when we import a module. Suppose at any time we import a module, PYTHONPATH is used to check the presence of the modules that are imported in different directories. Loading of the module will be determined by interpreters.
6. What are the two major loop statements?
For and while
7. What do you understand by the term PEP 8?
PEP 8 is the Python latest coding convention and it is abbreviated as Python Enhancement Proposal. It is all about how to format your Python code for maximum readability.
8. What is the Difference between “is” and “==”?
- The
==
operator compares the equality of two values. - The
is
operator checks if two variables point to the same memory address.
9. What is the Difference Between Shallow Copy and a Deep Copy?
- In Python, a shallow copy is a “one-level-deep” copy. This means the copied object contains references to the children of the original object.
- A deep copy is an independent copy of the original object.
10. What are Pickling and Unpickling Conceptually?
Pickling and unpickling serialize Python objects.
- Pickling is the process of converting Python objects into a byte stream
- Unpickling is the opposite of pickling
11. What is a Module in Python?
A module is a Python file. It contains functions and global variables that can be used in your program. If you want to use a module, you need to import it to your
program
- Why Python?
- Python is an interpreted, object-oriented, high-level programming language with dynamic semantics.
- Python is compatible with different platforms like Windows, Mac, Linux, Raspberry Pi, etc.
- Python has a simple syntax as compared to other languages.
- Python allows a developer to write programs with fewer lines than some other programming languages.
- 12. What is PEP 8?
- PEP 8 stands for Python Enhancement Proposal, it can be defined as a document that helps us to provide the guidelines on how to write the Python code. It is basically a set of rules that specify how to format Python code for maximum readability. It was written by Guido van Rossum, Barry Warsaw and Nick Coghlan in 2001.
13. What is Python’s parameter passing mechanism?
There are two parameters passing mechanism in Python:
- Pass by references
- Pass by value
14. What is swapcase () function in the Python?
It is a string’s function which converts all uppercase characters into lowercase and vice versa. It is used to alter the existing case of the string. This method creates a copy of the string which contains all the characters in the swap case. If the string is in lowercase, it generates a small case string and vice versa. It automatically ignores all the non-alphabetic characters.
15. What are Python namespaces?
A namespace in python refers to the name which is assigned to each object in python. The objects are variables and functions. As each object is created, its name along with space (the address of the outer function in which the object is), gets created. The namespaces are maintained in python like a dictionary where the key is the namespace and value is the address of the object.
16. What is the difference between .py and .pyc files?
The .py files are the python source code files. While the .pyc files contain the bytecode of the python files. .pyc files are created when the code is imported from some other source. The interpreter converts the source .py files to .pyc files which helps by saving time.
17. What is slicing in Python?
Slicing is used to access parts of sequences like lists, tuples, and strings. The syntax of slicing is-[start:end:step]
. The step can be omitted as well. When we write [start:end]
this returns all the elements of the sequence from the start (inclusive) till the end-1 element. If the start or end element is negative i, it means the ith element from the end. The step indicates the jump or how many elements have to be skipped. E.g. if there is a list- [1,2,3,4,5,6,7,8]
. Then [-1:2:2]
will return elements starting from the last element till the third element by printing every second element i.e. [8,6,4]
.
18. Is python case sensitive?
Yes. Python is a case sensitive language.
19. Is indentation required in python?
Indentation is necessary for Python. It specifies a block of code. All code within loops, classes, functions, etc is specified within an indented block. It is usually done using four space characters. If your code is not indented necessarily, it will not execute accurately and will throw errors as well.
20. What is the difference between Python Arrays and lists?
Arrays and lists, in Python, have the same way of storing data. But, arrays can hold only a single data type elements whereas lists can hold any data type elements.
21. What is pickling and unpickling?
Pickle module accepts any Python object and converts it into a string representation and dumps it into a file by using dump function, this process is called pickling. While the process of retrieving original Python objects from the stored string representation is called unpickling.
22. What are the generators in python?
Functions that return an iterable set of items are called generators.
23. How will you capitalize the first letter of string?
In Python, the capitalize () method capitalizes the first letter of a string. If the string already consists of a capital letter at the beginning, then, it returns the original string.
24. How will you convert a string to all lowercase?
To convert a string to lowercase, lower () function can be used.
Example:
1 2 | stg=’ABCD’ print(stg.lower()) |
Output: abcd
25. Are Dictionaries Faster to Lookup than Lists?
- A list lookup time is
O(n)
. This is because in the worst case the whole list needs to be iterated through before the value is found. - A dictionary lookup time is
O (1)
. This is because a dictionary is a hash table.
In other words, dictionary-lookup is quicker than a list lookup.
26. What is a Module in Python?
A module is a Python file. It contains functions and global variables that can be used in your program. If you want to use a module, you need to import it to your progra
27. What Is the Itertools Module?
Itertools is a popular Python module. It has useful methods to perform tasks related to looping over iterables.
You can use Itertools for example to obtain the permutations of a group of values. Or you can create an infinite group of evenly spaced numbers.
28. What is the Difference between Pass, Continue, and Break Statements?
- pass means do nothing. This is used because you cannot create empty classes, methods, etc. in Python.
- continue stops the execution of the iteration of a loop and jumps to the next one.
- break stops the iteration of a loop and jumps out of it.
29. What is the Difference Between Dictionary and JSON?
- Dictionary is a data structure.
- JSON is a data format (a string).
30. Python Lists vs. Numpy Arrays? Briefly explain the differences between a regular Python list and a NumPy array.
List
A list is a resizable collection. It may contain elements of different types. Python lists have limitations:
NumPy Array
NumPy array is an efficient and more convenient way to store elements than a built-in list. It gives you vector and matrix operations. Also, the implementation of a NumPy array makes it more efficient. NumPy array: