1.
Difference between Python and Java:
Python is an interpreted, dynamically typed language, while Java is compiled and statically
typed. Python focuses on simplicity; Java emphasizes structure and performance.
2. What are generators?
Generators are special functions that return items one by one using the yield keyword, saving
memory by not storing the entire sequence in memory.
3. What is the usage of the with statement?
The with statement simplifies resource management, ensuring that files or resources are
automatically closed after use.
4. Explain exception handling in Python:
Exception handling manages runtime errors using try, except, else, and finally blocks to
prevent program crashes.
5. What are the key features of Python:
Python is simple, interpreted, object-oriented, portable, and supports large standard libraries
and dynamic typing.
6. What is the difference between list and tuple:
Lists are mutable and can be modified, while tuples are immutable and cannot be changed
after creation.
7. What are *args and kwargs:
*args is used to pass a variable number of positional arguments, while **kwargs passes
keyword arguments to a function.
8. Explain the difference between is and ==:
is checks whether two variables refer to the same object in memory, while == compares their
values.
9. What is a lambda function:
A lambda function is an anonymous, single-line function defined using the lambda keyword,
often used for short operations.
10. What are the different data types in Python:
The main data types are int, float, str, bool, list, tuple, set, and dict.
11. What is a dictionary in Python:
A dictionary stores data as key-value pairs, allowing fast access using unique keys.
12. What is the difference between append(), extend(), and insert():
append() adds one element, extend() adds multiple elements, and insert() adds an element at a
specified index.
13. What are Python’s built-in data structures:
Python has four main structures — List, Tuple, Set, and Dictionary — each used for different
data storage needs.
14. What is list comprehension:
List comprehension is a compact way to create lists using expressions inside square brackets.
Example: [x*x for x in range(5)].
15. What is the difference between break, continue, and pass:
break exits the loop, continue skips to the next iteration, and pass does nothing (used as a
placeholder).
16. What is the difference between local and global variables:
Local variables are declared inside a function and used only there, while global variables are
declared outside and accessible everywhere.
17. What are Python modules and packages:
A module is a Python file containing code, while a package is a collection of modules
organized in directories.
18. What is the purpose of the init method:
The __init__() method is a constructor used to initialize object attributes automatically when
a class instance is created.
19. What is inheritance in Python:
Inheritance allows one class (child) to acquire the attributes and methods of another class
(parent), promoting code reuse.
20. What is the difference between deep copy and shallow copy:
A shallow copy copies only references of objects, while a deep copy creates an independent
copy of the entire object structure.
21. What is exception handling in Python:
It’s a process to handle errors safely using try-except blocks, ensuring the program continues
running smoothly.
22. What is the use of with statement in file handling:
It automatically opens and closes files, ensuring no resource leakage even if an error occurs.
23. What is recursion, give an example:
Recursion is when a function calls itself repeatedly until a base condition is met.
Example: factorial function.
24. What is the difference between sort() and sorted():
sort() modifies the original list in place, while sorted() returns a new sorted list without
changing the original.
25. What are Python’s advantages and disadvantages:
Advantages: Easy syntax, portable, and extensive libraries.
Disadvantages: Slower speed and high memory usage.
26. What are Python identifiers and rules for naming them:
Identifiers are names for variables or functions. They can start with a letter or underscore but
not a number or keyword.
27. What are Python keywords:
Keywords are reserved words with predefined meanings like if, for, while, class, and return.
28. What is the difference between mutable and immutable objects:
Mutable objects (like lists) can change their values, while immutable ones (like strings and
tuples) cannot.
29. What is type casting in Python with examples:
Type casting converts data from one type to another.
Example: int("5"), float(10), str(15).
30. What is the difference between Python 2 and Python 3:
Python 3 uses Unicode by default and print() as a function, while Python 2 used ASCII and
print as a statement.
31. What is indentation in Python and why is it important:
Indentation defines code blocks in Python. It replaces braces {} and ensures proper code
structure.
32. What is the use of the id() function in Python:
The id() function returns the unique identity (memory address) of an object.
33. What are Python comments and how to write them:
Comments describe code for readability.
Single-line: # comment, Multi-line: ''' comment ''' or """ comment """.
34. What is slicing in Python with examples:
Slicing extracts parts of a sequence.
Example: a[1:4] returns elements from index 1 to 3.
35. What is the difference between del, remove(), and pop():
del deletes by index or entire list, remove() deletes by value, pop() removes and returns an
element.
36. What are Python strings and some common string functions:
Strings are sequences of characters enclosed in quotes.
Common functions: upper(), lower(), split(), replace(), strip().