B1_0801IT231121_LAB1
B1_0801IT231121_LAB1
B1_0801IT231121_LAB1
0801IT231121
LAB ASSIGNMENT -1
No. Question
1 What is Python, and what are its key features?
Ans Python is a high-level, interpreted programming language known for its
readability, simplicity, and versatility. It was created by Guido van Rossum and
was first released in 1991. Python is widely used in various fields such as web
development, data analysis, machine learning, automation, and scientific
computing, among others.
0801IT231121
Compilation: The entire source code is translated into machine code (a sequence
of instructions the computer's CPU can directly understand) before
execution.This process is performed by a compiler.
Execution: The generated machine code is then executed directly by the CPU.
Cons:Slower development cycle due to the need for compilation after each code
change.
Interpreted Languages
0801IT231121
Python's Place
Ans PEP 8 is a style guide for Python code. It provides a set of recommendations for
writing clean, readable, and consistent Python code.
Key aspects of PEP 8:
Indentation: Use 4 spaces for indentation, not tabs.
Line Length: Limit all lines to a maximum of 79 characters.
Blank Lines: Use blank lines to separate functions and classes, and to group
related blocks of code within a function.
Naming Conventions: Use lowercase with underscores for variable and function
names (e.g., my_variable, my_function). Use CamelCase for class names (e.g.,
MyClass).
Comments: Use dorings to document functions and classes. Use inline
comments sparingly to explain complex logic.
Readability: Consistent code style makes it easier for others (and your future
self) to read and understand your code.
Maintainability: Following a common style guide makes it easier to maintain
and modify code over time.
Collaboration: When working on projects with others, adhering to PEP 8
ensures that everyone is writing code in a consistent manner, which improves
team productivity.
Shivam Jhawar
0801IT231121
0801IT231121
Parses the code: It analyzes the code to check for syntax errors and to
understand the structure of the program. This involves breaking down the code
into smaller, more manageable units.
Executes the bytecode: The PVM executes the bytecode instructions. This is
where the actual work of the program happens.
Manages memory: The interpreter also manages the memory used by the
program, allocating and deallocating memory as needed.
7 What is the significance of indentation in Python?
Ans Indentation is crucial in Python. It's not just for readability; it defines the
structure and execution flow of the code.
0801IT231121
Syntax Errors: Incorrect indentation will lead to syntax errors, preventing the
code from running.
Logical Errors: Even if the code runs, incorrect indentation can cause
unexpected behavior, leading to incorrect results.
0801IT231121
Key Points:
Python 3 is the present and future of Python.
Python 2 is no longer officially supported.
It's generally recommended to learn and use Python 3 for new project
9 What is the purpose of Python's `pip` tool?
Ans Python's pip is a crucial command-line tool that acts as the package installer for
the Python ecosystem. It simplifies the process of acquiring and managing
third-party libraries, which are collections of pre-written code that extend
Python's functionality. With pip, developers can easily install packages from the
Python Package Index (PyPI) and other repositories, ensuring access to a vast
array of tools for various domains like data science, machine learning, web
development, and more. Furthermore, pip enables the uninstallation of packages
when they are no longer needed, updates installed packages to the latest
versions, lists currently installed packages, and effectively manages
dependencies between packages, streamlining the development process and
ensuring a smooth and efficient project workflow.
0801IT231121
System Integrity: Changes made within a virtual environment won't affect your
system-wide Python installation, ensuring stability and preventing accidental
modifications.
Reproducibility: Virtual environments make it easy to recreate the exact
environment for a specific project, ensuring consistent behavior across different
machines.
Dependency Management: Managing dependencies becomes more
straightforward, as you can easily install, update, and uninstall packages within
the isolated environment.
11 What are Python's advantages compared to other programming
languages?
Ans Python boasts several advantages over other programming languages:
0801IT231121
Module: A module is essentially a single Python file (with the .py extension)
containing Python code. This code can include functions, classes, variables, and
more. Modules help break down large programs into smaller, more manageable
units, improving code organization and reusability.
Package: A package is a collection of related modules organized within a
directory hierarchy. To signify a directory as a package, it must contain a special
file named __init__.py (which can be empty). Packages allow for hierarchical
structuring of modules, creating a more organized and modular namespace,
especially for larger projects.
Shivam Jhawar
0801IT231121
Mutable Objects:
Can be changed after creation.
Modifications are made in-place, altering the original object.
Examples:
Lists (list)
Dictionaries (dict)
Sets (set)
15 What are keywords in Python? Name a few examples.
Ans In Python, keywords are reserved words with specific meanings and purposes.
They cannot be used as variable names, function names, or any other identifiers.
Here are a few examples:
0801IT231121
Controlling Imports:
The __init__.py file can be used to control which modules or subpackages are
imported when you use the from ... import * syntax.You can explicitly list the
names of modules or subpackages that should be imported by default in the
__init__.py file.
Namespace Management:
It helps to manage the namespace of the package, defining which names are
accessible when importing from the package.
# In my_package/__init__.py
from .module1 import function1
from .module2 import class1
17 Explain the concept of Python's dynamic typing.
Shivam Jhawar
0801IT231121
Ans In Python, dynamic typing refers to the ability of variables to hold values of
different data types throughout the program's execution. This means you don't
need to explicitly declare the data type of a variable before assigning a value to
it.
Key Characteristics:
No explicit type declarations: You can directly assign values to variables without
specifying their type.
Type inference: The Python interpreter automatically determines the data type of
a variable based on the assigned value.
Flexibility: Allows for more flexible code and rapid prototyping.
18 What are Python's control flow statements? Name them.
Ans Control flow statements in Python are used to alter the normal sequential
execution of code. They allow you to control which parts of the code are
executed and in what order. Here are some of the main control flow statements
in Python:
if, elif, else: These statements are used to conditionally execute blocks of code
based on whether a certain condition is true or false.
for loop: This statement is used to iterate over a sequence (like a list, tuple, or
string) or any other iterable object.
while loop: This statement is used to repeatedly execute a block of code as long
as a given condition is true.
0801IT231121
try, except, and finally: These statements are used for exception handling,
allowing you to gracefully handle errors that might occur during the execution
of your code.
19 what is the Global Interpreter Lock (GIL) in Python?
Ans The Global Interpreter Lock (GIL) is a mutex (or lock) used in CPython, the
default implementation of Python, to synchronize access to Python objects. It
ensures that only one thread executes Python bytecode at a time, even on
multi-core systems. This simplifies memory management in CPython but limits
the ability of Python programs to achieve true parallelism in multi-threaded
code.
20 How does Python handle memory management?
Ans
Python handles memory management automatically through a combination of
mechanisms that manage the allocation and deallocation of memory for Python
objects. This is primarily achieved using a private heap and a garbage collection
system. Here's an overview of how Python handles memory management:
1. Private Heap:
● All Python objects and data structures are stored in a private heap,
which is managed by the Python memory manager.
● Programmers do not have direct access to this heap; instead, Python
provides constructs like variables, lists, and dictionaries to interact with
objects in memory.
2. Memory Allocation:
0801IT231121
3. Reference Counting:
Python uses reference counting as the primary mechanism for memory
management:
● Each object in memory has a reference count, which tracks the number
of references to that object.
● When an object's reference count drops to zero (i.e., no references to it
exist), the memory is deallocated immediately.