You are on page 1of 9

QS 5

A) Explain following functions.


(i) math.exp()
(ii) math.floor()
(iii) math.pow()
Ans-
(i) math.exp(x):
The math.exp(x) function is part of the math module
in Python and is used to compute the exponential
value of a number x. The exp() function returns the
exponential value of x, which is e raised to the
power of x

(ii) math.floor(x):
The math.floor(x) function is used to return the
largest integer value less than or equal to x. It
rounds down a floating-point number x to the nearest
integer towards negative infinity.
(iii) math.pow(x, y):
The math.pow(x, y) function is used to compute the
value of x raised to the power of y. It returns the
result as a floating-point number.
B) Write a program to plot sine wave using
matplotlib. matplotlib.
Ans-

Output-
C)List out various file accessing modes and explain
each of them.
Ans-
the various file accessing modes in Python and their
explanations:

=>r (read):
Opens the file for reading only. The file pointer is
positioned at the beginning of the file. If the file
does not exist, a FileNotFoundError is raised. This
is the default mode for opening files.

=>r+ (read and write):


Opens the file for both reading and writing. The file
pointer is positioned at the beginning of the file.
If the file does not exist, it is created.

=>w (write):
Opens the file for writing only. The file pointer is
positioned at the beginning of the file. If the file
exists, its contents are overwritten. If the file
does not exist, it is created.

=>w+ (write and read):


Opens the file for both writing and reading. The file
pointer is positioned at the beginning of the file.
If the file exists, its contents are overwritten. If
the file does not exist, it is created.
=>a (append):
Opens the file for appending only. The file pointer
is positioned at the end of the file. If the file
does not exist, it is created.

=>a+ (append and read):


Opens the file for both appending and reading. The
file pointer is positioned at the end of the file. If
the file does not exist, it is created.

Mode Description
r Opens the file for reading only.
r+ Opens the file for both reading and writing.
w Opens the file for writing only.
w+ Opens the file for both writing and reading.
a Opens the file for appending only.
a+ Opens the file for both appending and reading.

OR
A) List out functions for writing to file operation
and explain each.
Ans-
some of the functions for writing to file operation
in Python:

=>open(): This function is used to open a file in


read, write, or append mode. The mode parameter can
be "r" for read, "w" for write, "a" for append, or
"r+" for read and write.

=>write(): This function is used to write data to a


file. The data to be written can be a string, a list
of strings, or a binary object.

=>writelines(): This function is used to write a list


of strings to a file. The strings in the list are
written one after the other, without any spaces in
between.

=>flush(): This function is used to flush the output


buffer of a file. This means that any data that has
been written to the file but not yet saved to disk
will be written to disk immediately.
B) Explain the following string function with
example-
(I) endswith,
(ii)find
Ans-
->endswith:

The endswith function is used to check if a string


ends with a specified suffix. It returns True if the
string ends with the given suffix, and False
otherwise.
The general syntax of endswith is as follows:

Example

-> find:
The find function is used to locate the first
occurrence of a substring within a string. It returns
the index of the first character of the substring if
found, and -1 if the substring is not present in the
string.
The general syntax of find is as follows:

Example-
C) Write a program to read a date in the format
DD/MM/YYYY and print the same date in MM-DD-YYYY
format.
Ans-

Output-

You might also like