Top 25 Python Interview Questions for Data Science Jobs in 2020
Category: Python, Python for Data Science Training Posted:May 04, 2020 By: RobertThere are 25 questions that will help you to crack the Data Scientist interview. Let’s have a look
1. Explain the difference between the lists and tuples.
Both lists and tuples are made up of components, which are values of any Python data type. However, these data types have a number of differences:
- Lists are mutable, while tuples are immutable.
- lists are made with square brackets (e.g., my_list = [a, b, c]), while tuples are enclosed in parentheses brackets (e.g., my_tuple = (a, b, c)).
- Lists are slower than tuples.
2. What type of language is python? Programming or scripting?
Python is fit for scripting, but in a general sense, it is considered as a general-purpose programming language.
3. What is PYTHONPATH?
It is an environment variable that is utilized when a module is imported. At whatever point a module is imported, PYTHONPATH is also looked up to check for the presence of the imported modules in various directories. The interpreter utilizes it to figure out which module to load.
4. What are python modules? Name some commonly used built-in modules in Python?
Python modules are documents/files containing Python code. This code can either be work classes or variables. A Python module is a .py file containing executable code.
A portion of the generally utilized built-in modules are:
- os
- sys
- math
- random
- data time
- JSON
5. What are Python packages?
Ans: Python packages are namespaces containing multiple modules.
6. Name some well-known Python data analysis libraries.
In case you’re doing Data Analysis with Python, you’re likely going to utilize:
- NumPy
- Pandas
- Matplotlib
- Seaborn
- SciKit
These libraries will assist you with working with arrays and DataFrames, build professional-looking plots, and run Machine Learning models.
7. What is type conversion in Python?
Type conversion refers to the conversion of one data type into another.
int() – converts any data type into integer type
float() – converts any data type into float type
ord() – converts characters into integer
hex() – converts integers to hexadecimal
oct() – converts integer to octal
tuple() – This function is used to convert to a tuple.
set() – This function returns the type after converting to set.
list() – This function is used to convert any data type to a list type.
dict() – This function is used to convert a tuple of order (key,value) into a dictionary.
str() – Used to convert integer into a string.
complex(real,imag) – This function converts real numbers to complex(real,imag) number.
8. What is pandas?
Pandas is a Python open-source library that gives high-performance and flexible data structures and data analysis tools that make working with relational or labeled data both simple and intuitive.
9. What is the difference between Python Arrays and lists?
Arrays and lists, in Python, have a similar method of storing data. But, arrays can hold only a single data type element though lists can hold any data type elements.
Example:
- 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)
Output:
array(‘i’, [1, 2, 3, 4]) [1, ‘abc’, 1.2]
10. What is self in Python?
Self is an instance or an object of a class. In Python, this is unequivocally included as the first parameter. However, this isn’t the case in Java where it’s optional. It helps to differentiate between the method and attribute of a class with local variables.
The self variable in the init strategy refers to the newly created object while in other methods, it refers to the object whose method was called.
11. What is a negative index, and how is it used in Python?
A negative index is utilized in Python to index a list, string, or some other container class in reverse order (from the end). Thus, [-1] refers to the last element, [-2] refers to the second-to-last element, and so on.
Here are two examples:
1.list = [2, 5, 4, 7, 5, 6, 9]
print (list[-1])
9
2. text = “I love data science”
print (text[-3])
n
12. What is the default missing value marker in pandas, and how can you detect all missing values in a DataFrame?
In pandas, the default missing value marker is NaN.
You can detect every single missing value in a DataFrame by utilizing the isna() function from the pandas library:
employees.isna()
ID | Department | Age | |
0 | FALSE | FALSE | FALSE |
1 | FALSE | FALSE | TRUE |
2 | FALSE | FALSE | FALSE |
3 | FALSE | TRUE | FALSE |
4 | FALSE | FALSE | TRUE |
To explore the composition of a column, including what number of missing values it has, use the info()function; it shows the number of non-null entries in every column together with the total number of entries in that column (the difference, of course, is the number of missing values!).
13. How does Break, Continue, and Pass work?
BREAK– Allows loop termination when some condition is met and the control is transferred to the next statement. |
CONTINUE- Allows skipping some part of a loop when some a specific condition is met and the control is transferred to the beginning of the loop |
PASS- Used when you need some block of code syntactically, but you want to skip its execution. This is basically a null operation. Nothing happens when this is executed. |
14. What is the distinction between range and xrange?
For the most part, xrange and range are precisely the same regarding functionality. The two of them give an approach to generate a list of integers for you to use, however you please. The only difference is that range returns a Python list object and x range returns an xrange object.
This implies xrange doesn’t really create a static list at run-time as the range does. It creates the values as you need them with a special technique called yielding. This technique is utilized with a kind of object known as generators. That implies that if you have a really gigantic range you’d prefer to generate a list for, say one billion, xrange is the function to use.
This is especially true if you have a real memory sensitive system, for example, a mobile phone that you are working with, as the range will use as much memory as it can to make your array of integers, which can bring about a Memory Error and crash your program. It’s a memory hungry beast.
15. What are the generators in python?
The function that returns an iterable set of the item is called generators.
16. What is the use of help() and dir() work in Python?
Help() and dir() the two functions are available from the Python interpreter and used for viewing a consolidated dump of built-in functions.
- Help() function: The help () function is used to show the documentation string and furthermore encourages you to see the help related to modules, keyword, attributes, and so on.
- Dir()function: The dir() function is used to show the defined symbols.
17. What is a dictionary in Python?
The built-in data types in Python is known as a dictionary. It characterizes a coordinated connection between keys and values. Dictionary contains a pair of keys and their corresponding values. Dictionaries are indexed by keys.
18. What libraries do data scientists use to plot data in Python?
Matplotlib is the main library used for plotting data in Python. However, the plots made with this library need lots of fine-tuning to look shiny and professional. Thus, numerous data scientists favor Seaborn, which enables you to create appealing and meaningful plots with just one line of code.
19. Write Python code to create an employee DataFrame from the “HR.csv”file.
We can create a DataFrame from a CSV file by utilizing the read_csv()function from the pandas library. By convention, pandas are imported as pd. So the code is simply the following:
import pandas as pd |
employees = pd.read_csv(‘HR.csv’) |
20. 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 comprises a capital letter at the beginning, at that point, it returns the original string.
21. In what manner will you convert a string to all lowercase?
To convert a string to lowercase, lower() function can be utilized.
Example:
- stg=’ABCD’
- print(stg.lower())
Output: abcd
22. What are docstrings in Python?
Docstrings are not really commented, be that as it may, they are documentation strings. These docstrings are inside triple quotes. They are not assigned to any variable and therefore, at times serve the purpose of comments as well
Example:
- “””
- Using docstring as a comment.
- This code divides 2 numbers
- “””
- x=8
- y=4
- z=x/y
- print(z)
Output: 2.0
23. Explain list comprehensions and how they’re used in Python.
List Comprehensions give a brief method to make lists.
A list is traditionally created utilizing square brackets. But, with a list comprehension, these brackets contain an expression followed by a for clause and then if clause, when necessary. Evaluating the given expression with regards to these for and if clauses produce a list.
It’s best clarified with an example:
old_list = [1, 0, -2, 4, -3]
new_list = [x**2 for x in old_list if x > 0]
print(new_list)
[1,16]
Here, we’re creating another list by taking the elements of the old lists to the power of 2 but only for the elements that are strictly positive. The list comprehension enables us to solve this task in only one line of code.
24. What are the built-in types of python?
Built-in types in Python are as follows –
- Integers
- Floating-point
- Complex numbers
- Strings
- Boolean
- Built-in functions
25. Whenever Python exits, why isn’t all the memory de-allocated?
- Whenever Python exits, particularly those Python modules which are having circular references to other objects or the objects that are referenced from the global namespaces are not generally de-allocated or freed.
- It is difficult to de-allocate those segments of memory that are reserved by the C library.
- On exit, as a result of having its very own efficient cleanup mechanism, Python would try to de-allocate/destroy every other object/item.
We hope these questions and answers will help you to prepare your coming interviews in the best way with.
If you are planning to boost your skills, choose our best online training platform, and learn from industry experts. So what are you waiting for? Click here, to skyrocket your career with the unique learning needs because Learning Never Exhausts The Mind.