20+ Important Python Interview question and answer for Freshers

Post a Comment

 20 IMPORTANT PYTHON QUESTION AND ANSWER EVERYONE SHOULD KNOW BEFORE INTERVIEW

1.What is Python and what are its features?

Python is a interpreted, high-level and general-purpose programming language that was first released in 1991. It has a clear syntax and dynamic typing, making it easy to read and write. Some of its notable features include automatic memory management, support for multiple programming paradigms (e.g., procedural, object-oriented, functional), and a large standard library that includes modules for web services, string processing, and more.

2.What is the difference between a tuple and a list in Python?

A tuple is an immutable (i.e., cannot be changed) ordered collection of elements, while a list is a mutable ordered collection of elements. In other words, once you create a tuple, you cannot add, remove, or change elements, but you can do so with a list. Tuples are usually used to represent values that shouldn't be changed throughout a program's execution, such as coordinates or dates.

3.What is the use of the "pass" statement in Python?

The "pass" statement is a null statement that does nothing. It is used as a placeholder in situations where the code will be added later. For example, when defining a class, you may want to define the methods later, so you can use the "pass" statement to create the class structure first.

4.In Python, How do you check if a number is odd or even?

You can check if a number is odd or even by using the modulo operator (%). If the result of dividing the number by 2 is 0, then the number is even, otherwise, it's odd. 

Here's an example:

def is_even(number):
    if number % 2 == 0:
        return True
    else:
        return False

5.What is a Python decorator?

A decorator is a special type of function that modifies the behavior of another function. Decorators are used to add additional functionality to functions, methods, or classes, without changing their source code. They are defined using the @ symbol, followed by the decorator name, and are applied to a function using the () operator.

6.What is the difference between deep and shallow copy in Python?

A deep copy is a copy of an object that includes a copy of all its sub-objects, while a shallow copy is a copy of an object that includes a reference to its sub-objects. In other words, changes made to a sub-object in a deep copy won't affect the original object or other sub-objects, while changes made to a sub-object in a shallow copy will affect the original object and other sub-objects.

7.How do you convert a string to a list in Python?

You can convert a string to a list in Python using the split() method. This method splits a string into a list of substrings based on a specified delimiter. 

Here's an example:

string = "apple,banana,cherry"
list = string.split(",")

8.Difference between range and xrange in Python 2?

range and xrange are functions that generate a sequence of numbers. The main difference is that range returns a list of numbers, while xrange returns an object that generates numbers on the fly. xrange is more memory-efficient when dealing with large ranges, since it only generates the numbers as they are needed, rather than creating a list with all the numbers up front. In Python 3, xrange has been replaced by range, which behaves like xrange in Python 2.

9.How do you check if a given key exists in a dictionary in Python?

You can check if a key exists in a dictionary using the in operator. Here's an example:

d = {'key1': 1, 'key2': 2, 'key3': 3}
if 'key2' in d:
    print("Key exists.")
else:
    print("Key does not exist.")

10.What is the difference between a class and an object in Python?

A class is a blueprint for creating objects, while an object is an instance of a class. Classes define what attributes and methods an object of that class will have, while objects are instances of a class that have their own unique values for those attributes and can access the methods defined in the class.

11.What is a Python generator?

A Python generator is a special type of function that can be used to generate a sequence of values. Unlike normal functions, generators do not return a value, instead, they return a generator object that can be used to generate the values one at a time. Generators are often used to generate sequences of values that are too large to fit into memory all at once.

12.Difference between a local variable and a global variable in Python?

A local variable is a variable that is declared within a function and can only be accessed within that function. A global variable is a variable that is declared outside of a function and can be accessed from anywhere in the code. If a local variable and a global variable have the same name, the local variable will take precedence within the function, but changes made to it will not affect the global variable.

13.What is a Python module?

A Python module is a file that contains definitions and statements that can be executed or imported into other Python code. Modules can contain variables, functions, classes, and other types of objects, and can be used to organize code into reusable components. Modules are imported using the import statement, and their contents can be accessed using the dot notation (e.g., module_name.object_name).

14.How do you convert a string to a list in Python?

You can convert a string to a list in Python using the split method. The split method splits a string into a list based on a specified delimiter. If no delimiter is specified, the string is split into a list of individual characters. For example:

string = "hello, world"
list = string.split(',')
print(list)
# Output: ['hello', ' world']

15.What is the difference between a tuple and a list in Python?

Tuples and lists are both data structures in Python that can be used to store collections of values. The main difference between the two is that tuples are immutable, meaning their contents cannot be changed after they are created, while lists are mutable and their contents can be changed. Tuples use parentheses to define their contents, while lists use square brackets. For example:

t = (1, 2, 3)
l = [1, 2, 3]

16.How do you define a function in Python?

You can define a function in Python using the def keyword, followed by the function name, a set of parentheses for any arguments, and a colon. The contents of the function must be indented beneath the definition line. 

For example:

def add(a, b):
    return a + b

17.What is a decorator in Python?

A decorator is a special type of function in Python that is used to modify the behavior of another function. Decorators can be used to add, remove, or change the behavior of functions, methods, or classes without having to modify the code of the original functions. Decorators are defined using the @ symbol followed by the decorator name. 

For example:

def my_decorator(func):
    def wrapper():
        print("Something is happening before the function is called.")
        func()
        print("Something is happening after the function is called.")
    return wrapper

@my_decorator
def say_hello():
    print("Hello!")

18.What is a lambda function in Python?

A lambda function is a small anonymous function in Python that can take any number of arguments but can only have one expression. Lambda functions are defined using the lambda keyword, followed by the arguments, a colon, and the expression. Lambda functions are often used as shorthand for defining short, simple functions, especially when the function will only be used once. 

For example:

sum = lambda x, y: x + y
print(sum(1, 2))
# Output: 3

19.What is the difference between a list and an array in Python?

In Python, a list is a built-in data structure for storing a collection of values, while an array is a more specialized data structure that can store values of a single type, such as integers or floating-point numbers. Lists are more flexible than arrays, but arrays are more efficient in terms of memory and performance. To use arrays in Python, you need to import the array module. For example:

import array
a = array.array("i", [1, 2, 3, 4])
print(a)
# Output: array('i', [1, 2, 3, 4])

20.How do you remove duplicates from a list in Python?

There are several ways to remove duplicates from a list in Python. One way is to use a set, which is an unordered collection of unique elements. To remove duplicates from a list, you can simply convert the list to a set and then convert it back to a list. For example:

list = [1, 2, 3, 4, 1, 2, 3]
unique_list = list(set(list))
print(unique_list)
# Output: [1, 2, 3, 4]

21.How do you reverse a list in Python?

You can reverse a list in Python using the reverse method, which is a built-in method of the list object. The reverse method modifies the list in place and does not return a new list. For example:

list = [1, 2, 3, 4, 5]
list.reverse()
print(list)
# Output: [5, 4, 3, 2, 1]

22.What is a generator in Python?

A generator is a type of iterator in Python that allows you to generate values one at a time. Generators are defined using the yield keyword, which returns a value and suspends the execution of the generator function until the next value is requested. Generators are often used for large sequences of values where it's not feasible to store all the values in memory. For example:

def my_generator():
    yield 1
    yield 2
    yield 3

gen = my_generator()
print(next(gen))
# Output: 1
print(next(gen))
# Output: 2
print(next(gen))
# Output: 3

Related Posts

Post a Comment