PWP Notes4
PWP Notes4
4.1 Use of Python built-in functions (e.g type/ data conversion functions, math
functions etc.)
Q.1.Explain four built-in list functions. - 4 mks
Python provides a wide range of built-in functions that simplify programming tasks. These
functions can be categorized based on their purposes, such as type conversion, mathematical
operations, etc.
int(x)
c): Converts x to an integer.
int("42") # Output: 42
2. Math Functions
The open() function in Python is used to open a file and return a file object, which allows you to
interact with the file (reading, writing, etc.). It takes at least one argument, the file path, and
optionally a mode (to specify how the file should be opened).
ii) The write() Function
The write() function is used to write data to a file. It is available after opening a file in a write or
append mode ('w', 'a')
3. Function Arguments and Parameter Passing
● The return statement is used to exit a function and optionally pass an expression or value
back to the caller. This allows functions to produce output.
5. Scope of Variables
Q.1. Explain Local and Global variable. - 2 mks
Local Scope: A variable defined inside a function is local to that function and cannot be accessed
outside.
def f():
x = 10 # Local variable
print(x)
f()
Global Scope: A variable defined outside any function is global and can be accessed inside
functions, but to modify it inside a function, you need to use the global keyword.
x = 10 # Global variable
def f():
global x
f()
print(x) # Output: 20
The math module provides mathematical functions like square roots, logarithms, trigonometry,
and constants
Similar to the math module, but for complex numbers. It includes functions for complex math,
such as computing square roots and logarithms of complex numbers.
1.3 random Module
The random module is used to generate random numbers. It supports various randomization
functions like generating random integers, floats, and random selections from a sequence.
The statistics module provides functions to calculate mathematical statistics of data, like
mean, median, mode, and standard deviation.
The datetime module provides classes for manipulating dates and times.
Q.1. Explain Module and its use in Python. - 4 mks
Q.2.Write a program for importing module for addition and
substraction of two numbers. - 4 mks
Q.3.Write a python program to create a user defined module that will ask
your program name and display the name of the program. - 4 mks
Q.4.Write python program using module, show how to write and
use module by importing it. - 4mks
Namespace and Scoping.
Namespace
A namespace is a space where Python stores names (like variables or functions). It keeps track of
where each name belongs.
Scoping
Scoping defines where you can access a variable. It tells Python where a variable can be used.
Writing your own Python packages helps organize and reuse code effectively. A Python
package is a collection of modules that are organized in directories. Each package can contain
multiple modules (Python files) and sub-packages.Writing Python packages
Create an __init__.py file to mark the directory as a package. This can be empty or contain
code to initialize the package.
Use Your Package by importing it in your Python script.
Q.2.Write use of matplotlib package in python - 2 mks
Matplotlib is a powerful library in Python used for creating various types of plots and charts. It's
widely used for data visualization.
Python comes with many pre-installed packages (known as standard libraries) that provide a
wide range of functionalities like mathematical operations, data manipulation, visualization, etc.
NumPy is a key package in Python. It helps you work with arrays and matrices, which are
collections of numbers, and provides many tools to perform mathematical operations on
them efficiently.
1. NumPy Arrays
import numpy as np
● You can perform mathematical operations on entire arrays at once, which makes
your code shorter and faster.
arr = np.array([1, 2, 3, 4])
print(arr * 2) # Output: [2 4 6 8]
● You can easily get parts of an array, like selecting a row from a table or a slice of a
list.