class 12 computer science solution python using library functions Image by Free-Photos from Pixabay |

# Python functions using libraries: mathematical and string functions

**Q – 1 What do you mean by Function Library in Python?**

**Ans.:**Click to read the answer

**Q – 2 Enlist some commonly used libraries of python and explain in short.**

**Ans.:**View the answer

**Q – 3 Explain the structure of python module.**

**Ans.:**Answer

**Q – 4 How to import a python module? or What are the ways to insert python modules in a program?**

**Ans.:**To insert a python module you can use the import statement in the following two ways:

- import command: Ex. import math
- from import: Ex. from math import *

**Q – 5 Explain some commonly used mathematical functions in detail with example.**

**Ans.:**Click here

**Q – 6**

**Explain some commonly used string functions in detail with example.**

**Ans.:**View the answer

**Q – 7 Write related library function name to do the following:**

- Display the negative number into positive:
**abs()** - Display nearest integer for the given number:
**ceil()** - Get the remainder:
**fmod(), remainder()** - Get the compute cube of a given number:
**pow()** - Get the square root of the given number:
**sqrt()** - Convert the first letter of the text into capital:
**capitalise()** - Join two words:
**join()** - Convert the text into the lower case:
**lower()**

**Q – 8 Name the module required to import for the following functions:**

- pow() –
**math** - fabs() –
**math**

**Q – 9 Find the output for the following:**

**1.**

def str_exp():

str1= 'COVID-19,Sanitizer,Mask,LockDown'

str1=str1.lower()

str2 =str1.split(',')

for i in str2:

if i<'s':

print(str.lower(i))

else:

print(str.upper(i))

str_exp()

**Answer:**

covid-19

SANITIZER

mask

lockdown

In the above example, the text starts with the alphabet ‘s’ and letter coming after ‘s’ will be converted into uppercase and rest text will be in lower case.

**2.**

def str_exp():

str1='Covid - 19 forces the entire worl to be lockdown'

str1=str1.replace('e','i')

print(str1)

str_exp()

**Answer:**

Covid – 19 forcis thi intiri worl to bi lockdown

All e replace with the letter i in the text.

**3.**

def str_exp():

str="Lockdown to Unlock 1.0"

d=str.split()

print(d)

str_exp()

**Answer:**

[‘Lockdown’, ‘to’, ‘Unlock’, ‘1.0’]

The words will be separated in a string after space

**4.**

import math

def mth_exp():

a=5.5

b=3.75

c=2.25

print(round(math.fsum([a,b,c]),0))

mth_exp()

**Answer:**

12.0

Function fsum() returns sum of specified variables a,b,c. The values are 5.5 + 3.75 + 2.25 = 11.5. Then round function returns next integer if the adjecent digit is more than 5. So the final output will be 12.0

**Programs:**

**1. Write a program to accept the marks of 5 subjects and do the following:**

**Display the total using fsum() function****Display total marks in round integers**

**Ans.:**

import math

def compute_result():

eng=float(input("Enter marks of English:"))

phy=float(input("Enter marks of Physics:"))

che=float(input("Enter marks of Chemistry:"))

mat=float(input("Enter marks of Maths:"))

cs=float(input("Enter marks of Computer Science:"))

tot=round(math.fsum([eng,phy,che,mat,cs]),0)

print("Total:",tot)

compute_result()

**2. Write a program to display the computation of power using math module function.**

**Ans.:**

import math

def compute_power():

no=int(input("Enter the number:"))

p=int(input("Enter the power to be raised:"))

ans=math.pow(no,p)

print("The",p,"power of ",no," is:", ans)

compute_power()

**3. Write a program to convert the first letter of the sentence into a capital letter.**

**Ans.:**

def first_upper():

s=input("Enter the sentences:(in lower case:)")

print(s.capitalize())

first_upper()

**4.**

**Write a program to convert the first letter of each word of the sentence into a capital letter.**

def first_upper():

s=input("Enter the sentences:(in lower case:)")

print(s.title())first_upper()

More programs will be posted in the practical section.