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

python interview questions

The document provides a comprehensive overview of Python, covering its definition, key features, and various programming concepts such as data types, functions, and error handling. It includes explanations of lists, tuples, dictionaries, sets, and advanced topics like decorators, recursion, and multithreading. Additionally, the document addresses common questions about Python's syntax and behavior, making it a valuable resource for learners and developers.

Uploaded by

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

python interview questions

The document provides a comprehensive overview of Python, covering its definition, key features, and various programming concepts such as data types, functions, and error handling. It includes explanations of lists, tuples, dictionaries, sets, and advanced topics like decorators, recursion, and multithreading. Additionally, the document addresses common questions about Python's syntax and behavior, making it a valuable resource for learners and developers.

Uploaded by

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

1. What is Python?

 Python is a high-level, interpreted programming


language known for its simplicity and readability. It
supports multiple programming paradigms,
including procedural, object-oriented, and
functional programming.

2. What are the key features of Python?

 Python is known for its easy-to-read syntax,


dynamic typing, automatic memory management, a
large standard library, and support for both small
and large-scale projects.

3. How do you create a list in Python?

 Lists are created using square brackets. For


example: my_list = [1, 2, 3].

4. How can you convert a string to a number in


Python?

 Use the int(), float(), or complex() functions to


convert a string to an integer, float, or complex
number, respectively.
5. What is the difference between append() and
extend() methods for lists?

 append() adds a single element to the end of a list,


while extend() adds multiple elements by
appending elements from an iterable like another
list.

6. Explain the difference between remove() and pop()


methods for lists.

 remove() deletes the first occurrence of a specified


value, whereas pop() removes and returns the
element at a specified index.

7. What is a tuple in Python?

 A tuple is an immutable ordered collection of


elements, defined using parentheses. For
example: my_tuple = (1, 2, 3).

8. How do you create a dictionary in Python?

 Dictionaries are created using curly braces with


key-value pairs. For example: my_dict = {‘key’:
‘value’}.

9. What is a set in Python?


 A set is an unordered collection of unique
elements. Sets are defined using curly
braces: my_set = {1, 2, 3}.

10. What is the purpose of the pass statement in


Python?

 pass is a null statement used as a placeholder in


functions, loops, or classes where code is
syntactically required but no action is needed.

11. What are Python keywords?

 Keywords are reserved words that have special


meanings in Python, such as if, else, for, while,
return, True, False, and None.

12. What are Python literals?

 Literals represent fixed values in Python. Examples


include string literals (“Hello”), numeric literals
(123), boolean literals (True, False), and special
literal (None).

13. What is the difference between .py and .pyc files?

 .py files contain Python source code, while .pyc


files contain the bytecode that the Python
interpreter generates from the .py files for faster
execution.

14. Explain slicing in Python.

 Slicing is used to access a range of elements in


sequences like lists, tuples, and strings.
Syntax: sequence[start:end:step].

15. What is a Python module?

 A module is a file containing Python definitions and


statements. Modules allow for code reuse and
organization.

Medium Questions
1. What is a lambda function in Python?

 A lambda function is a small anonymous function


defined with the lambda keyword. It can have
multiple arguments but only one expression.
Example: lambda x: x + 1.

2. Explain the difference between map() and filter()


functions.
 map() applies a function to all items in an iterable
and returns a list of results. filter() returns a list of
items for which a function returns True.

3. What is a decorator in Python?

 A decorator is a function that modifies another


function, adding functionality without changing its
structure. Decorators are denoted with the @
symbol.

4. Explain the concept of *args and **kwargs.

 *args allows a function to accept any number of


positional arguments, which are passed as a tuple.
**kwargs allows a function to accept keyword
arguments, passed as a dictionary.

5. What is recursion in Python?

 Recursion is a function calling itself to solve a


smaller instance of the same problem. Example:
calculating the factorial of a number.

6. Explain the purpose of the global keyword in


Python.
 The global keyword is used to declare that a
variable inside a function is global and should not
be treated as a local variable.

7. What is dictionary comprehension?

 Dictionary comprehension is a concise way to


create dictionaries. Syntax: {key: value for item
in iterable}.

8. What is the nonlocal keyword in Python?

 The nonlocal keyword is used to indicate that a


variable inside a nested function refers to a
variable in the enclosing scope.

9. How can you handle exceptions in Python?

 Use try, except, else, and finally blocks to handle


exceptions. This structure allows for graceful error
handling.

10. What is the difference between shallow copy and


deep copy?

 A shallow copy creates a new object but inserts


references into it. A deep copy creates a new object
and recursively copies all objects found in the
original.

11. What is the Global Interpreter Lock (GIL)?

 The GIL is a mutex that protects access to Python


objects, preventing multiple threads from
executing Python bytecodes simultaneously.

12. Explain the concept of generators in Python.

 Generators are iterators that yield items instead of


returning them all at once. They are defined using
the yield keyword.

13. What are metaclasses in Python?

 Metaclasses define the behavior of classes and are


themselves classes of classes. They allow for the
customization of class creation.

14. How does memory management work in Python?

 Memory management in Python involves private


heap space, managed by Python’s memory
manager and supported by garbage collection.

15. What is monkey patching in Python?


 Monkey patching refers to dynamic modifications
of a class or module at runtime.

Hard Questions
1. Explain the difference between __init__ and
__new__ methods.

 __init__ initializes an instance after it is created.


__new__ is responsible for creating a new instance
of a class.

2. How do you implement multithreading in Python?

 Multithreading can be implemented using the


threading module. Python threads are not truly
parallel due to the GIL, but they can be useful for
I/O-bound tasks.

3. What are the built-in data types in Python?

 Python’s built-in data types include numeric types


(int, float, complex), sequence types (str, list, tuple,
range), mapping types (dict), set types (set), and
boolean types (bool).

4. What is the difference between == and is


operators?
 == checks for value equality, while is checks for
object identity, i.e., whether two variables
reference the same object.

5. What is the purpose of the if __name__ ==


“__main__”: statement?

 This statement ensures that code block runs only


when the script is executed directly, not when
imported as a module.

6. What is the difference between a shallow copy and


a deep copy?

 A shallow copy copies an object but not the objects


it references. A deep copy recursively copies the
object and all objects it references.

7. Explain the use of decorators in Python.

 Decorators modify the behavior of a function or


method. They are often used to extend
functionalities without changing the original
function’s code.

8. What is the purpose of self in class methods?


 self refers to the instance of the class, allowing
access to the attributes and methods of the class
within its methods.

9. How do you handle exceptions in Python?

 Exceptions are handled using try, except, else, and


finally blocks. This allows for graceful error
handling and resource cleanup.

10. What is polymorphism in Python?

 Polymorphism allows methods to do different


things based on the object it is acting upon,
enabling different classes to be treated through the
same interface.

You might also like