XII-CS-UNIT-I-Part4-PYTHON-LIBRARIES-2020-21

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Computer Science (083)

CLASS-XII

DISTRIBUTION OF MARKS:
UNIT UNIT NAME MARKS

I Computational Thinking and 40


Programming - 2

II Computer Networks 10

III Database Management 20

TOTAL 70

● Revision of the basics of Python covered in Class XI.


● Functions: scope, parameter passing, mutable/immutable properties
of data objects, passing strings, lists, tuples, dictionaries to functions,
default parameters, positional parameters, return values, functions using
libraries: mathematical and string functions.
● File handling: Need for a data file, Types of file: Text files, Binary files
and CSV (Comma separated values) files.
● Text File: Basic operations on a text file: Open (filename – absolute or
relative path, mode) / Close a text file, Reading and Manipulation of
data from a text file, Appending data into a text file, standard input /
output and error streams, relative and absolute paths.
● Binary File: Basic operations on a binary file: Open (filename –
absolute or relative path, mode) / Close a binary file, Pickle Module –
methods load and dump; Read, Write/Create, Search, Append and
Update operations in a binary file.
● CSV File: Import csv module, functions – Open / Close a csv file, Read
from a csv file and Write into a csv file using csv.reader ( ) and
csv.writerow( ).
● Using Python libraries: create and import Python libraries.
● Recursion: simple algorithms with recursion: print a message forever,
sum of first n natural numbers, factorial, Fibonacci numbers; recursion on
arrays: binary search.
● Idea of efficiency: performance measurement in terms of the number
of operations.
●Data-structures: Lists as covered in Class XI, Stacks – Push, Pop using a
list, Queues – Insert, Delete using a list.

A Python program comprises three main components:

1. Module
2. Package
3. Library
Module is a file containing Python definitions, functions, variables,
classes and statements

Python package is simply a directory of Python module(s)

Library is a collection of various packages. Conceptually there is no


difference between package and Python library. In Python a library
is used to loosely describe a collection of core or main modules

Module:
-It is a file which contains python functions/global
variables/clases etc. It is just .py file which has python executable
code / statement.

To import entire module

import <module name>


Example: import math

To import specific function/object from module:

from <module_name> import <function_name>


Example: from math import sqrt

import * : can be used to import all names from module into


current calling module

To use function/constant/variable of imported module we have


to specify module name and function name separated by dot(.).
This format is known as dot notation.

<module_name>.<function_name>
Example: print(math.sqrt(25))

import math
print (math.sqrt(25))
print (math.log10(100))

OR
from math import *
print(sqrt(25))
print(log10(100))
OR
from math import *
print(sqrt(25)) #It will work

from math import sqrt


print(sqrt(25))
print(log10(100)) #It will not work

Create new python file(.py) and type the following code:


import math # this is first module
mynum=100

def area_rect(length,breadth):
return length*breadth

def area_sqr(side):
return side*side

def area_circle(rad):
return math.pi*rad*rad

Save this file as area.py


Execute the following code to import and use your own module

help() is used to get detailed information of any module:


Namesapces

In Python terms, namespace can be thought of as a named


environment holding logical group of related objects.

For every python module(.py), Python creates a namespace having


its name similar to that of module‟s name. That is, namespace of
module AREA is also AREA.

When 2 namespace comes together, to resolve any kind of object


name dispute, Python asks you to qualify the name of object as
<modulename>.<objectname>

Defined functions and variables in the module are now available to


program in new namespace created by the name of module

For example, if the imported module is area, now you want to call
the function area_circle(), it would be called as area.area_circle()

When we issue from module import object command:


The code of imported module is interpreted and executed. Only the
asked function and variables from module are now available in the
current namespace i.e. no new namespace is created that’s why we
can call object of imported module without qualifying the module
name

For example:
from math import sqrt
print(sqrt(25))

However if the same function name is available in current


namespace then local function will hide the imported module’s
function same will be apply for

from math import * method

Defining package
Step 1: Create a new folder which you want to act as package. The
name of folder will be the name of your package (“mypackage”)
Step 2: Create modules (.py) and save it in mypackage folder
(area.py and numcheck.py  mypackahge)
#area.py
import math # my module
mynum=100
def area_rect(length,breadth):
return length*breadth
def area_sqr(side):
return side*side
def area_circle(rad):
return math.pi*rad*rad

# numcheck.py
import math
def even(num):
if num%2==0:
return 1
else:
return 0
def isprime(num):
for i in range (2,int(math.sqrt(num)+1)):
if num%i==0:
return 0
return 1
def palindrome(num):
mynum=num
n=0
while num!=0:
r=num%10
n=n*10+r
num = num//10
if mynum ==n:
return 1
else:
return 0

Importing package and modules in another python program


(xyz.py)

import mypackage.numcheck
n=int(input("Enter number"))
if(mypackage.numcheck.isprime(n)):
print("Number is prime")
else:
print("Number is composite")

OR

import mypackage.numcheck as mpack


n=int(input("Enter number"))
if(mpack.isprime(n)):
print("Number is prime")
else:
print("Number is composite")

Library
It is a collection of various packages. Conceptually, there is no
difference between package and python library. In Python, a library
is used loosely to describe a collection of the core modules.

‘Standard library ‘of Python language comes bundled with the core
Python distribution are collection of exact syntax, token and
semantics of the Python language. The python standard library lists
down approx more than 200 such core modules that form the core
of Python.

“Additional libraries” refer to those optional components that are


commonly included in Python distributions.
The Python installers automatically add the standard library and
some additional libraries.

The additional library is generally provided as a collection of


packages. To use such additional library we have to use packaging
tools like easyinstall or pip to install such additional libraries.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy