0% found this document useful (0 votes)
4 views

Python Language Questions

Python notes

Uploaded by

Singamala Balaji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Python Language Questions

Python notes

Uploaded by

Singamala Balaji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Python Language Questions

. What is Python?
○ A high-level, interpreted programming language known for its readability and
1
4
flexibility.
. What are Python's built-in data types?
○ Integers, floats, strings, lists, tuples, sets, and dictionaries.
1
3
. Explain list comprehension.
○ A concise way to create lists by iterating over an iterable and applying a condition or
1
2
transformation.
. What is the difference between == and is?
1
1
○ == checks for value equality; is checks for object identity (if they are the same
object).
1
0
. What is a lambda function?
○ An anonymous function defined with the lambda keyword, often for short,
9
throwaway functions.
. What is PEP 8?
○ The style guide for Python code, promoting readable and consistent coding
8
practices.
. What are decorators?
○ Functions that modify the behavior of another function or method, often used for
7
logging or access control.
. What is a generator?
○ A function that returns an iterator, allowing iteration over a sequence of values with
6
yield.
. Explain exception handling.
○ Using try, except, finally, and else to manage and respond to errors.
5
. What does the with statement do?
○ Manages resources, ensuring proper cleanup, commonly used with file operations.
. How do you create a virtual environment?
4
○ Using venv or virtualenv, e.g., python -m venv myenv.
. What is a class in Python?
○ A blueprint for creating objects, encapsulating data and methods.
3
. What is inheritance?
○ A mechanism where a new class derives from an existing class, inheriting attributes
2
and methods.
. What is polymorphism?
1
○ The ability to present the same interface for different data types.
1
5
. Explain the self keyword.
○ A reference to the current instance of a class, used to access instance attributes
and methods.
. What is a context manager?
○ An object that defines methods __enter__() and __exit__() for resource
management.
. What is the purpose of __init__?
○ The constructor method that initializes an object's attributes when it is created.
. What are modules in Python?
○ Files containing Python code that can define functions, classes, and variables,
facilitating code reuse.
. How do you create a list in Python?
○ By using square brackets, e.g., my_list = [1, 2, 3].
. What is the difference between a shallow copy and a deep copy?
○ A shallow copy creates a new object but inserts references to the original objects; a
deep copy creates a new object and recursively copies all objects.
Advanced Python Questions
. What are Python iterators?
○ Objects that implement the iterator protocol, consisting of __iter__() and __next__()
methods.
. How do you implement a singleton pattern in Python?
○ By overriding the __new__() method to ensure only one instance is created.
. What are list slicing and its syntax?
○ A way to access a subset of a list, using the syntax list[start:end:step].
. How do you handle exceptions in Python?
○ By wrapping code in a try block and using except to catch specific exceptions.
. What is the purpose of the map() function?
○ To apply a function to all items in an iterable and return a map object.
. Explain the filter() function.
○ It constructs an iterator from elements of an iterable for which a function returns
true.
. What does zip() do?
○ Combines elements from multiple iterables into tuples, stopping at the shortest
input iterable.
. How do you reverse a list in Python?
○ Using list.reverse() method or slicing list[::-1].
. What is the purpose of the join() method?
○ To concatenate elements of an iterable into a single string, using a specified
separator.
. How do you check if a key exists in a dictionary?
○ Using the in keyword, e.g., key in my_dict.
NumPy Questions
. What is NumPy?
○ A library for numerical computing in Python, providing support for arrays and
mathematical functions.
. How do you create a NumPy array?
○ Using numpy.array().
. What is the difference between a list and a NumPy array?
○ NumPy arrays are more efficient for numerical operations and support vectorized
computations.
. How do you perform element-wise operations in NumPy?
○ By using standard arithmetic operators on arrays.
. What are NumPy's main data types?
○ int, float, bool, str, and object.
. Explain broadcasting in NumPy.
○ The ability to perform operations on arrays of different shapes by automatically
expanding them.
. How do you reshape a NumPy array?
○ Using the reshape() method.
. What is the purpose of numpy.zeros()?
○ To create an array filled with zeros of a specified shape.
. How can you find the mean of a NumPy array?
○ Using numpy.mean().
. What is the difference between ndarray and matrix?
○ ndarray is a general-purpose array type; matrix is strictly 2D and has some different
behaviors.
More NumPy Questions
. How do you stack arrays vertically in NumPy?
○ Using numpy.vstack().
. What function would you use to concatenate arrays?
○ numpy.concatenate().
. How do you select elements from a NumPy array?
○ Using indexing or boolean masks.
. What does numpy.sum(axis=0) do?
○ Computes the sum along the specified axis, aggregating values.
. How can you find the standard deviation in NumPy?
○ Using numpy.std().
. What is the purpose of numpy.arange()?
○ To create an array with evenly spaced values within a specified range.
. How do you get the shape of a NumPy array?
○ Using the shape attribute.
. What is a masked array in NumPy?
○ An array that allows you to mask certain values, effectively ignoring them in
operations.
. How do you sort a NumPy array?
○ Using numpy.sort() or numpy.argsort() for indices.
. How can you transpose a NumPy array?
○ Using the T attribute or numpy.transpose() function.
Pandas Questions
. What is Pandas?
○ A data manipulation and analysis library for Python, featuring data structures like
Series and DataFrame.
. How do you create a Pandas DataFrame?
○ Using pandas.DataFrame().
. What is a Series in Pandas?
○ A one-dimensional labeled array capable of holding any data type.
. How can you read a CSV file in Pandas?
○ Using pandas.read_csv().
. How do you filter rows in a DataFrame?
○ By applying boolean indexing.
. What is the purpose of groupby in Pandas?
○ To group data by one or more columns for aggregation or transformation.
. How can you handle missing data in Pandas?
○ Using methods like fillna(), dropna(), or isnull().
. What is the difference between loc and iloc?
○ loc is label-based indexing, while iloc is position-based indexing.
. How do you merge two DataFrames?
○ Using the merge() function.
. What is pivoting in Pandas?
○ Reshaping data from long to wide format using the pivot() function.
More Pandas Questions
. How do you sort a DataFrame by a column?
○ Using sort_values(by='column_name').
. What is the purpose of apply() in Pandas?
○ To apply a function along a DataFrame axis (rows or columns).
. How do you change the index of a DataFrame?
○ Using set_index().
. What is the purpose of concat() in Pandas?
○ To concatenate two or more DataFrames along a specified axis.
. How can you get descriptive statistics of a DataFrame?
○ Using the describe() method.
. What does drop_duplicates() do?
○ Removes duplicate rows from a DataFrame.
. How can you convert a DataFrame to a NumPy array?
○ Using the to_numpy() method.
. How do you set a specific column as the index?
○ Using set_index('column_name').

Matplotlib Questions

. What is Matplotlib?
○ A plotting library for Python that provides tools for creating static, animated, and
interactive visualizations.
. How do you create a simple line plot in Matplotlib?
○ Using plt.plot(x, y) followed by plt.show().
. What is the purpose of plt.subplot()?
○ To create a grid of subplots within a single figure.
. How can you customize a plot title and axes labels?
○ Using plt.title(), plt.xlabel(), and plt.ylabel().
. What function is used to save a figure in Matplotlib?
○ plt.savefig().
. How do you add a legend to a plot?
○ Using plt.legend().
. What is a scatter plot?
○ A plot that displays values for two variables, using dots to represent the values.
. How do you create a histogram in Matplotlib?
○ Using plt.hist(data).
. What are different styles available in Matplotlib?
○ Various styles can be set using plt.style.use(), such as 'ggplot' or 'seaborn'.
. How can you show multiple plots in the same figure?
○ By using plt.subplots() to create a grid of axes.
. What is the purpose of plt.xticks() and plt.yticks()?

You might also like