Top 25+ Most Asked Python Interview Questions (With Answers & Examples)

Q1. What is Python?
Ans: Python is a general-purpose computer programming language based on object oriented programming. The programs in Python can run equally on every operating system. It is the primary language used in modern technologies like AI, data science, and ML.

Q2.Python an interpreted language. Explain.
Ans: An interpreted language runs its code line by line using an interpreter, instead of converting the whole program into machine code before running it.

Python is an interpreted language because the Python interpreter reads and runs the code one line at a time. This makes it easier to find and fix errors, and to run the same code on different computers.

Q3. What is a function in Python with example?
Ans: A function is a block of reusable code that performs a specific task. Functions help organize code, make it reusable, and improve readability.

Q4. What is the difference between list and tuples in Python?
Ans: The main difference between lists and tuples is that:

Tuples are immutable, which means once a tuple is created, you cannot change its items.

Lists are mutable, which means you can change, add, or remove items after the list is created.

Example

my_list = [1, 2, 3]
my_list[0] = 10 # This works

my_tuple = (1, 2, 3)
my_tuple[0] = 10 # This gives an error

Q5. What is namespace in Python?
Ans
: A namespace is a naming system used to make sure that names are unique to avoid naming conflicts.

Q6.What are local variables and global variables in Python?
Ans: Global Variables: Variables declared outside a function or in global space are called global variables. These variables can be accessed by any function in the program.
Local Variables: Any variable declared inside a function is known as a local variable. This variable is present in the local space and not in the global space.

Example

x = 10 # Global variable

def my_function():
y = 5 # Local variable
print(x) # Can access global variable
print(y) # Can access local variable

my_function()
print(x) # Works
print(y) # Error: y is a local variable

Q7. What is the Python dictionary?
Ans: A dictionary in Python is a collection of items that are written in curly brackets with keys and values. The items are in no particular order and are used to retrieve the value for keys that are known.

Q8 . What is recursion in Python?
Ans: Recursion in Python is a programming technique where a function calls itself within its own definition. It’s a powerful tool for solving problems that can be broken down into smaller, self-similar subproblems.

Q9. What is the usage of help() and dir() function in Python?
Ans:

1. Help() function: The help() function is used to display the documentation string and also facilitates you to see the help related to modules, keywords, attributes, etc.

2. Dir() function: The dir() function is used to display the defined symbols.

Q10. What are negative indexes and why are they used?
Ans: negative indexes allow you to access elements from the end of a list, string, or other sequences.

  • -1 refers to the last element.
  • -2 refers to the second-to-last element
  • Negative indexes provide a convenient way to access elements relative to the end of a sequence without needing to calculate the length of the sequence.

Q11. What are Python packages?
Ans: Python packages are namespaces containing multiple modules.

Q12. Does Python have OOps concepts?
Ans: Python is an object-oriented programming language. This means that any program can be solved in python by creating an object model. Python can be treated as procedural as well as structural language.

Q13. What is the use of frozen set in Python?
Ans: A frozenset is an immutable set-meaning its elements cannot be changed after creation. It is
used when you need a set that cannot be modified, such as using it as a key in a dictionary
or storing it in another set.

Q14. How is Multithreading achieved in Python?
Ans: Multithreading is achieved using the threading module, which allows you to run multiple threads (lightweight subprocesses) concurrently.

Q15. How to import modules in python?
Ans: Modules can be imported using the import keyword. You can import modules in three ways
import module_name
from module_name import function_name
import module_name as alias

Q16. How is OOPS used in Python?
Ans: OOPS is a paradigm in Python that uses classes and objects.
Example – class, data abstraction, polymorphism, inheritance, encapsulation, etc.

Q17. What is encapsulation in Python?
Ans: Encapsulation is the process of hiding the internal state of an object and requiring all interactions to be performed through an object’s methods.

Q18. Does python support multiple inheritance?
Ans: Multiple inheritance means that a class can be derived from more than one parent classes. Python does support multiple inheritance.

Q19. What is inheritance in Python? Types?
Ans: Inheritance is an object-oriented programming (OOP) feature in Python that allows a class (child/derived class) to inherit properties and methods from another class (parent/base class).

Q20. What does an object() do?
Ans: It returns a featureless object that is a base for all classes. It does not take any parameters.

Q21. What is polymorphism in Python?
Ans: Polymorphism in python is something that is used to describe the ability to take various forms.

Q22. What is a lambda function?
Ans: An anonymous function is known as a lambda function. This function can have any number of parameters but, can have just one statement.

Q23. Write a program in Python to check if a sequence is a Palindrome.

Q24. Write a sorting algorithm for a numerical dataset in Python.

Q25. Python code to find factorial of a number

Q26. Python code to reverse a string

Leave a Reply

Your email address will not be published. Required fields are marked *