Intermediate Python Interview Questions Flashcards
Explain split(), sub(), subn() methods of “re” module in Python?
These methods belong to the Python RegEx or ‘re’ module and are used to modify strings.
split(): This method is used to split a given string into a list.
sub(): This method is used to find a substring where a regex pattern matches, and then it replaces the matched substring with a different string.
subn(): This method is similar to the sub() method, but it returns the new string, along with the number of replacements.
What do you mean by Python literals?
Generally, literals are a notation for representing a fixed value in source code. They can also be defined as raw values or data given in variables or constants. Python has different types of literal such as:
- String literals
- Numeric literals
- Boolean literals
- Literal Collections
- Special literals
Literals supported by python are listed below:
String Literals
These literals are formed by enclosing text in the single or double quotes.
For Example:
“Intellipaat”
‘45879’
Numeric Literals
Python numeric literals support three types of literals
Integer:I=10
Float: i=5.2
Complex:1.73j
Boolean Literals
Boolean literals help to denote boolean values. It contains either True or False.
x=True
What is a map function in Python?
The map() function in Python has two parameters, function and iterable. The map() function takes a function as an argument and then applies that function to all the elements of an iterable, passed to it as another argument. It returns an object list of results.
For example:
def calculateSq(n):
return n*n
numbers = (2, 3, 4, 5)
result = map( calculateSq, numbers)
print(result)
What are the generators in python?
Generator refers to the function that returns an iterable set of items.
What are python iterators?
These are the certain objects that are easily traversed and iterated when needed.
Do we need to declare variables with data types in Python?
No. Python is a dynamically typed language, I.E., Python Interpreter automatically identifies the data type of a variable based on the type of value assigned to the variable.
What are Dict and List comprehensions?
Python comprehensions are like decorators, that help to build altered and filtered lists, dictionaries, or sets from a given list, dictionary, or set. Comprehension saves a lot of time and code that might be considerably more complex and time-consuming.
Comprehensions are beneficial in the following scenarios:
- Performing mathematical operations on the entire list
- Performing conditional filtering operations on the entire list
- Combining multiple lists into one
- Flattening a multi-dimensional list
For example:
my_list = [2, 3, 5, 7, 11]
squared_list = [x**2 for x in my_list] # list comprehension
output => [4 , 9 , 25 , 49 , 121]
squared_dict = {x:x**2 for x in my_list} # dict comprehension
output => {11: 121, 2: 4 , 3: 9 , 5: 25 , 7: 49}
How do you write comments in python?
Comments in Python
Python Comments are the statement used by the programmer to increase the readability of the code. With the help of #, you can define the single comment and the other way to do commenting is to use the docstrings(strings enclosed within triple quotes).
For example:
print(“Comments in Python “)
Is multiple inheritance supported in Python?
Yes, unlike Java, Python provides users with a wide range of support in terms of inheritance and its usage. Multiple inheritance refers to a scenario where a class is instantiated from more than one individual parent class. This provides a lot of functionality and advantages to users.
What is the difference between range & xrange?
Functions in Python, range() and xrange() are used to iterate in a for loop for a fixed number of times. Functionality-wise, both these functions are the same. The difference comes when talking about Python version support for these functions and their return values.
RANGE() Method
- In Python 3, xrange() is not supported; instead, the range() function is used to iterate in for loops
- It returns a list
- It takes more memory as it keeps the entire list of iterating numbers in memory
XRANGE() Method
- The xrange() function is used in Python 2 to iterate in for loops
- It returns a generator object as it doesn’t really generate a static list at the run time
- It takes less memory as it keeps only one number at a time in memory
What is pickling and unpickling?
The Pickle module accepts the Python object and converts it into a string representation and stores it into a file by using the dump function. This process is called pickling. On the other hand, the process of retrieving the original Python objects from the string representation is called unpickling.
What do you understand by Tkinter?
Tkinter is a built-in Python module that is used to create GUI applications. It is Python’s standard toolkit for GUI development. Tkinter comes with Python, so there is no separate installation needed. You can start using it by importing it in your script.
Is Python fully object oriented?
Python does follow an object-oriented programming paradigm and has all the basic OOPs concepts such as inheritance, polymorphism, and more, with the exception of access specifiers. Python doesn’t support strong encapsulation (adding a private keyword before data members). Although, it has a convention that can be used for data hiding, i.e., prefixing a data member with two underscores.
Differentiate between NumPy and SciPy?
NumPY
- NumPy stands for Numerical Python
- It is used for efficient and general numeric computations on numerical data saved in arrays. E.g., sorting, indexing, reshaping, and more
- There are some linear algebraic functions available in this module, but they are not full-fledged
SciPy
- SciPy stands for Scientific Python
- This module is a collection of tools in Python used to perform operations such as integration, differentiation, and more
- Full-fledged algebraic functions are available in SciPy for algebraic computations
Explain all file processing modes supported in Python?
Python has various file processing modes.
For opening files, there are three modes:
- read-only mode (r)
- write-only mode (w)
- read–write mode (rw)
For opening a text file using the above modes, we will have to append ‘t’ with them as follows:
- read-only mode (rt)
- write-only mode (wt)
- read–write mode (rwt)
Similarly, a binary file can be opened by appending ‘b’ with them as follows:
- read-only mode (rb)
- write-only mode (wb)
- read–write mode (rwb)
To append the content in the files, we can use the append mode (a):
- For text files, the mode would be ‘at’
- For binary files, it would be ‘ab’
What do file-related modules in Python do? Can you name some file-related modules in Python?
Python comes with some file-related modules that have functions to manipulate text files and binary files in a file system. These modules can be used to create text or binary files, update their content, copy, delete, and more.
Some file-related modules are os, os.path, and shutil.os. The os.path module has functions to access the file system, while the shutil.os module can be used to copy or delete files.