KVSPGTCS – Lets make things better



Computer Science (083)CLASS-XII 2020-21DISTRIBUTION OF MARKS:UNITUNIT NAMEMARKSIComputational Thinking and Programming - 240IIComputer Networks10IIIDatabase Management20TOTAL70● 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:ModulePackageLibraryModule is a file containing Python definitions, functions, variables, classes and statementsPython 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 modulesModule:-Itisafilewhichcontainspythonfunctions/global variables/clases etc. It is just .py file which has python executable code / statement. To import entire moduleimport <module name>Example:import mathTo import specific function/object from module:from <module_name> import <function_name>Example: from math import sqrtimport * : can be used to import all names from module into current calling moduleTo use function/constant/variableofimported 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 mathprint (math.sqrt(25))print (math.log10(100))ORfrom math import *print(sqrt(25))print(log10(100))ORfrom math import *print(sqrt(25))#It will workfrom math import sqrtprint(sqrt(25))print(log10(100)) #It will not workCreate new python file(.py) and type the following code:import math# this is first modulemynum=100def area_rect(length,breadth):return length*breadthdef area_sqr(side):return side*sidedef area_circle(rad):return math.pi*rad*radSave this file as area.pyExecute the following code to import and use your own modulehelp() is used to get detailed information of any module: NamesapcesIn 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 moduleFor 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 nameFor 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 * methodDefining packageStep 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.pyimport math# my modulemynum=100def area_rect(length,breadth):return length*breadthdef area_sqr(side):return side*sidedef area_circle(rad):return math.pi*rad*rad# numcheck.pyimport mathdef even(num):if num%2==0:return 1else:return 0def isprime(num):for i in range (2,int(math.sqrt(num)+1)):if num%i==0:return 0 return 1def palindrome(num):mynum=numn=0while num!=0: r=num%10 n=n*10+r num = num//10if mynum ==n:return 1else:return 0Importing package and modules in another python program (xyz.py)import mypackage.numcheckn=int(input("Enter number"))if(mypackage.numcheck.isprime(n)): print("Number is prime")else: print("Number is composite")ORimport mypackage.numcheck as mpackn=int(input("Enter number"))if(mpack.isprime(n)): print("Number is prime")else: print("Number is composite")LibraryIt 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. ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download