Python Programming Language
Python Programming Language
43(iv)
Important Instructions
Assignment – 01
Q1. Discuss the key features of Python that distinguish it from other
programming languages. Why is Python considered both beginner-friendly
and powerful for professionals?
Ans.
2. Interpreted Language:
3. Dynamically Typed:
o Python does not require explicit declaration of variable types.
The interpreter automatically determines the data type at
runtime.
o This reduces the need for writing code from scratch for
common tasks.
5. High-Level Language:
✅ Conclusion:
Ans.
A. Numeric Types
B. Sequence Types
C. Set Types
D. Mapping Type
E. Boolean Type
F. Binary Types
G. None Type
Example:
a = 10 # int
b = 3.5 # float
Example:
a = "100"
You don’t need to declare the type of a variable when creating it.
Example:
x = 10 # int
x = "hello" # now it's a string
Conclusion
Python's data types provide the foundation for managing data effectively.
Its automatic type conversion, support for explicit casting, and
dynamic typing model make it powerful yet flexible. However, this
flexibility comes at the cost of potential type-related errors, which
programmers must guard against. Mastering these concepts is key to
writing efficient and error-free Python code.
Q3. What are functions in Python? Discuss the importance of functions in
code reusability and modular programming. Also, explain the concept of
scope and lifetime of variables in Python functions.
Ans.
Functions in Python
Definition:
1. Code Reusability:
2. Modular Programming:
4. Promotes Collaboration:
1. Scope of Variables:
The scope of a variable refers to the region or part of the program where
the variable is accessible.
Global Scope: Variables defined outside all functions are global and
can be accessed from anywhere in the program (unless shadowed
by local variables).
2. Lifetime of Variables:
The lifetime of a variable refers to the duration for which the variable
exists in memory.
Conclusion:
Ans.
Python uses a private heap space for storing all objects and data
structures. The management of this heap is handled by the Python
Memory Manager.
2. Reference Counting
Limitations:
How it Works:
Significance:
Python uses its own memory allocator called PyMalloc, which optimizes
memory usage for small objects by managing memory in blocks and
pools.
Interning:
For performance, Python may intern certain immutable objects like small
integers and strings. This means they are stored in a global table to avoid
multiple copies of the same object.
Conclusion
Ans.
Definition:
Real-Life Analogy:
Real-Life Analogy:
3. Polymorphism
Real-Life Analogy:
A bike drives differently than a car, and a car drives differently than
a truck.
Conclusion
Ans.
Exceptions in Python
Definition:
Example (conceptual):
Trying to divide a number by zero or accessing a file that doesn't exist will
raise an exception.
Key Components:
finally block: Code that runs no matter what (usually for cleanup).
Anticipating and handling edge cases and invalid conditions makes code
robust, especially in real-world applications involving user input, file
operations, or network communication.
The finally block ensures that resources like files and network connections
are properly closed, even if an error occurs.
Exception
Description
Name
ZeroDivisionErr
Raised when dividing by zero
or
Exception
Description
Name
1. Raising Exceptions
Conclusion
Exceptions are an essential part of reliable software development.
Python’s structured and flexible exception handling system allows
developers to write robust applications that can deal with unexpected
situations without breaking. Built-in exceptions cover most common
errors, while custom exceptions offer precision and control in
specialized use-cases. Mastering exception handling in Python leads to
cleaner, more professional, and fault-tolerant code.