Using Python Libraries Class 12 Notes

Teachers and Examiners (CBSESkillEduction) collaborated to create the Using Python Libraries Class 12 Notes. All the important Information are taken from the NCERT Textbook Computer Science (083) class 12.

Using Python Libraries Class 12 Notes

Python Library

Python library is a collection of codes or modules of codes that may be used by a programme to do particular activities. We use libraries to avoid having to rewrite code that is already present in our programme.

Python standard library

  • Pandas
  • Numpy
  • SciPy
  • Scrapy
  • Matplotlib
  • TensorFlow

Example
# Importing math library
import math

num = 32
print(math.sqrt(num))

Using Python Libraries Class 12 Notes

Python Modules

A file containing definitions and statements in Python is known as a module. Functions, classes, and variables can all be defined using modules. Runnable code might also be a part of a module. The code is simpler to comprehend and utilise when similar code is gathered into a module. It also organises the code logically.

Example
# Creating simple code of Module in python
def addition(a, b):
return (a+b)

def subtraction(a, b):
return (a-b)

Importing Module in Python

Example
import calc

print(calc.add(22, 2))

Advantage of module

  • It reduces the complexity of the program
  • You can create well defined program in python
  • You can use same program many times

Using Python Libraries Class 12 Notes

Importing Modules

A programme can import modules using the import statement in Python. There are two ways to use the import statement.

  • Use the import <module> command is used to import a full module.
  • The from “module” import “object” command can be used to import specific objects from a module.
Importing Entire Module in Python

The appropriate module name must be used along with the import keyword. The import statement causes the interpreter to add the module to the current programme when it encounters one. By adding the dot (.) operator to the module name, you can call the functions contained within a module.

Example
import math
n = 25
print(math.sqrt(n))

Importing Selected Objects in Python

You can also import single or multiple objects in python using the following syntax –

from <module name> import <object name1>, <object name2>, <object name3>

Using Python Libraries Class 12 Notes

Mathematical and String Function

1. oct(<integer>) – Returns octal string for given number.

Example
x = oct(32)
print(x)

Output
0o40

2. hex(<integer>) – Returns hex string for given number.

Example
x = hex(32)
print(x)

Output
0x20

3. int(<number) – Truncates the fractional part of given number and returns only the integer or whole part.

Example
x = int(32.6)
print(x)

Output
32

4. round(<number>, [<ndigits>]) – Returns number rounded to ndigits after the decimal points. If ndigits is not given, it returns nearest integer to its input.

Example
x = round(7.3554, 2)
print(x)

Output
7.36

5. <Str>.join(<String iterable>) – Joins a string or character after each memeber of the string iterator.

Example
myTuple = (“Amit”, “Rakesh”, “Rajesh”)
x = “#”.join(myTuple)
print(x)

Output
Amit#Rakesh#Rajesh

6. <Str>.split(<string / char>) – Splits a string based on given string or character and returns a list containing split strings are members.

Example
str = “welcome to my School”
x = str.split()
print(x)

Output
[‘welcome’, ‘to’, ‘my’, ‘School’]

Using Python Libraries Class 12 Notes

7. <Str>.replace(<word to be replaced>, <replace word>) – Replaces a word or part of the string with another in the given string <str>.

Example
str = “welcome to my School”
x = str.replace(“School”, “Home”)
print(x)

Output
welcome to my Home

8. random() – It returns a random floating point number N in the range [0.0, 1.0]

Example
import random
myList = [1, 2, 3, 4, 5]
print(random.choice(myList))

9. randint(a,b) – It returns a random integer N in the range (a,b).

Example
import random
print(random.randint(3, 9))

Disclaimer – 100% of the information are taken from the Computer Science textbook written by Sumita Arora, and our team has tried to collect all the correct Notes from the textbook. If you found any suggestion or any error please contact us anuraganand2017@gmail.com.

error: Content is protected !!