[go: up one dir, main page]

0% found this document useful (0 votes)
17 views14 pages

XII Computer Science Gist-01

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views14 pages

XII Computer Science Gist-01

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Class XII

Computer Science
Gist of Lessons covered between 22/4/2024 and 28/6/2024
Class XI Revision:
Token: The building blocks of a programming language
• Keywords, Identifier, Literal, Operators, Punctuators
Operators:
• Arithmetic Operators and their use (+, -, *, /, //, **)
• Priority, Associativity Rules [ (); **; *, /, //, %; +, - ]
• Normal Division & Floor Division: For both Positive and Negative numbers
• Remainder Operator and its use: For both Positive and Negative numbers
Relational, Logical, Membership Operators:
• >, <, >=, <=, ==, !=
• Comparing numbers, strings, lists, tuples, dictionaries using relational optr.: Ex. [5,8] > [2,7,6] → True
• Use of Logical operators not, and, or and their priority: Ex. 7>2 or 9!=5 and 7==16//2 → True
• Membership operators: in, not in. Ex. 'en' in 'student' → True
Using Logical Operators directly on values without Relational Operators:
• Concept of FALSE values like False, 0, 0.0, 0+0j, '', ( ), [ ], { }, None
• Applying logical or directly on values: Ex. 'abc' or [1,2,3] → 'abc'
• Applying logical and directly on values: Ex. 'abc' and [1,2,3] → [1,2,3]
Bitwise Operators:
• Bitwise and (&), or (|), xor (^), not(~) and their priority and use
Using Identity Operators:
• Identity Operators: is, is not. Used to check if two objects have same address or not
• Difference between equality (==) and identity (is) operators
Cases when Python creates two different objects that are storing the same value:
o When strings are input from the console/keyboard
o Writing very large integers
o Writing Floating Point and Complex Literals in a program
Typecasting:
• int() applied on float and string data
• float(): applied on int and string data
• complex(): applied on int or float data
• str(): applied on any type of data
• bool(): applied on any type of data
math module functions:
• import math
• from math import *
• ceil(n), floor(n), sqrt(n), exp(n), pow(x, y),
fabs(n), log(n, [base]), log10(num), sin(n), cos(n),
tan(n), degrees(n), radians(n)
random module functions:
• import random
• random.random()
• random.randint(a, b)
• random.randrange(end),
random.randrange(start, end),
random.randrange(start, end, step)
Class XII Function:
Introduction to Functions:
• Functions are used to write a given code
only once and then to use it at multiple
places
• Properties of Functions:
o Each function does a specific job
o It is like a sub-program in a program
o Usually, one or more values are
passed to the function as parameters with which the function works
o Any additional variable that is required by the function for carrying out the calculations are the internal
variables that are internally defined by the function (and not passed as parameters)
o A function usually returns the result calculated, using the return keyword
• The def keyword is used to define a function
• When defining a function, the first line of definition contains the def keyword, followed by the function
name (that is written following the identifier naming rules), followed by the function parameter list
• In the example shown above, after the keyword def, getReci is the function name and (x) is the function
parameter list. The return keyword is returning or giving back the calculated reciprocal value of x.
Function Header & Body:
• The first line of the function definition is
called the function header. The three parts
of the header are:
o def keyword
o Function name
o Function parameter list, followed by a
colon
• The part of the function definition below the
function header is called the function Body. The body is to be indented from the header and contains the
code for calculating the result as per the function’s use
Examples of some other function definitions:
Counting letter ‘e’ in a word

Finding maximum from list

Some more examples:


Process of Calling a Function:
• The process of using a function is known as a function call
• When a function is called, the code under the function definition executes
• In the example shown below, the line r = getReci(5) is the line that calls the function getReci( )
• The value passed to the function when calling it is called the function argument. In the example, the
value 5 in getReci(5) is the function argument
• The argument is copied to the parameter variable in the function parameter list. In the example, the
value 5 will get copied to the variable x in the function parameter list. Hence x becomes equal to 5
• The function body then calculates the value of 1/x (i.e. 1/5 = 0.2) and stores it in the internal variable
reci
• The value in reci is finally returned using the return reci statement at the end of the function.
• The program control comes out of the function and the returned value gets stored in the variable r in
the main section of the code.
• The value in r is printed finally

The next example below shows how the factorial( ) function is called with three different arguments to calculate
the value of nCr = n! / ( r! * (n-r)! ). First it is called with the argument as n, next with the argument as r, and
finally with the argument as the result of the calculation (n-r). each of the arguments get copied to the
parameter num in the function header and calculates the factorial of num and finally returns the result stored in
the variable fact.

Calling Functions with Multiple Parameters:


Examples of functions with multiple parameters
• Function to find Volume of Cylinder (with height and radius as parameters)
• Function to find Simple Interest (with P, R, T as parameters)
• Function to find the time taken to cover a distance (with Speed and Distance as parameters)
• Function to find the cost to lay grass on 80% of a park (len, br and cost/area as parameters)
The function below receives three parameters, the principal amount P, the rate of interest R, and the time of
deposit T and calculates the simple interest and returns it.

In the next example, the function receives two parameters, the length and width of a rectangle and calculates
its area and returns it.

In the next example, the function receives two parameters, the height and radius of a cylinder and calculates its
volume and returns it. Note that the order of the arguments passed is important here. The argument H goes to
the parameter height, and the argument R, goes to the parameter radius in that order.

Different types of Functions w.r.t. return statements:


There are three kinds of functions with respect to return statements. These are:
• Functions with only one return statement: Similar to all the problems discussed so far. The return
statement is at the end of the function body and executes at the end to return the calculated value.
• Functions with multiple return statements: A function can have more than one return statements.
However only ONE of those return statements will get executed at any given time. These are used with
if-elif-else blocks or with a loop.
• Functions with no return statements. These are called VOID functions and do not return anything
The following example shows how a function can have multiple return
statements. Based on the sign of the value passed to the function either
the if block return, or the elif block return or the else block return
statement will execute. Hence only one return statement actually
executes.

The next two examples are that of void functions.


• The getVol( ) function body inputs the side of a cube and then calculates the volume of the cube and
prints the volume within the function itself without returning it.
• The getArea( ) function receives the side of a square as its parameter and uses it to calculate the area
and prints the area within the function, without returning it.

Different types of function Arguments/Parameters:


Function call can be made using three types of arguments. These are:
o Positional Arguments: During a function call, all such arguments must be provided i.e. they are
mandatory. The values of the arguments are matched with the parameters position-wise. Hence these
arguments are ordered arguments and need to be provided in the same order they are appearing in the
function header parameter list. The following example shows the working.

o Keyword Arguments: This feature allows a user to provide the arguments in any order. Here the
arguments need to be named while calling the function. Hence these are also called named arguments.
The following program shows that during the function call, the arguments speed and dist are named and
then placed in any order. The names are matched and then the corresponding values are transferred to
the correct parameters in the function header.
o Default Arguments: Python allows default values to be assigned to variables in a function’s parameter list.
This helps in case matching arguments are not passed during a function call. All default parameters should
be written on the right of the parameter list, after the positional parameters.

Rules for combining all types of arguments during function call:


• The Argument List must FIRST contain the Positional Arguments
• SECOND it should contain the Keyword Arguments. It is suggested that Keyword Arguments are taken
from Positional Arguments preferably
• The value for a given argument CANNOT be specified more than once during a function call
• Remember that Default Arguments will always match the rightmost unspecified values from the
parameter list

Examples of valid Function Arguments for Interest() function:


S1 = Interest ( 40000, 6.2, 5 )
S2 = Interest ( 50000, Rate = 5, Tm = 10 )
S3 = Interest ( Rate = 4.2, Prin = 50000 )
S4 = Interest ( Tm = 5, Rate = 6, Prin = 30000 )
S5 = Interest ( Prin = 4500, Tm = 8, Rate = 5 )
S6 = Interest ( 60000, Rate = 7 )
Multiple values Returned by a Function:
A Function in Python can return more than one values. Python returns multiple values as a TUPLE. To return
multiple values from a function, the following things are to be taken care of:
o The values (or expressions) to be returned should be written separated by commas after return keyword
o When receiving the returned values from the function, either of the following things can be done:
▪ Use a single variable to receive the returned values. In that case this variable if of TUPLE data type
▪ Use the concept of Tuple unpacking to directly unpack the received values and store them in as many
numbers of variables as the values
Example-1: To return the perimeter and area of a circle
Function receives the radius of a circle and returns its perimeter & area together.
Example-2: To calculate and return the absolute value, ceiling, and floor of a number

Example-3: Function receives coefficients of a quadratic equation ax2+bx+c = 0, and returns the two real
roots, which are then accepted as a tuple

Scope of Variables in a Program:


• The SCOPE rules in a program determine in which part of a program a particular code or variable is
available for use
• In a program, a code or a variable has the following two scopes. These are:
o GLOBAL Scope: A code or value having a global scope is usable inside the whole program and all blocks.
Names declared in the top level segment ( __main__ ) of a program have such scope
o LOCAL Scope: A code or value having a local scope is usable only within a function and other blocks
contained within it. Names and code declared within a function body of a program have such scope
Example-1 of Global and Local Scope:

• When def is encountered, the lines for the function definition are ignored
• When __main__ code is executed, the variable N is added to Global Environment
• When function call is made, num, fact, x are added to Local Environment
• When function returns fact, Local Environment variables are removed
• When returned value is assigned to F, variable F is added to Global Environment
• When program ends after printing F, the Global Environment is also removed
Memory Namespace:
• Python stores values in memory in a hierarchical manner based on where the variable is defined in a
program
• This memory space where the variable’s value is placed is called the Namespace of the variable
• When Python comes across a variable name in a program, it looks for the variable’s value from the
memory following the same hierarchical path
• According to the hierarchical approach, the Namespace is divided into the following sections:
o Local Namespace
o Enclosing Namespace
o Global Namespace
o Built-In Namespace
• A higher order Namespace encloses a lower order Namespace
• The rule laid down by Python to use these namespaces is called the LEGB Rule

Working of the LEGB Rule:


For every variable name referred within a program, Python follows the name resolution rule i.e. the LEGB rule
to access it. It works like:
1. First, Python checks for a variable name within its Local Environment. If the required name is not found
there, it moves to the next level
2. Second, Python checks the variable name within its Enclosing Environment. If the name is not found there,
this process is repeated with its higher-level Enclosing Environment, if any. Else it moves to the next level
3. Third, Python checks for the variable name in the Global Environment. If not found there, it moves to the
next level
4. Fourth, it checks for the variable name in its Built-In Environment that contains all the built-in variable
names and functions of Python
Example: Searching a value in memory following the LEGB Namespace Hierarchy:

Working of Global and Local Variables with the SAME NAME


• The program shown below has a variable num in global scope
• Function also has a variable with the same variable name num within it
• Within the function Python automatically HIDEs the global num, and uses local num
• The global num is not accessed within the function and remains intact outside in the global scope
• Outside function the global num is unhidden
When using global Keyword
The global keyword can UNHIDE the global variable num within the function. It tells Python not to create a local
variable with the same name, but to use the global variable num instead

Variables used within a function are not accessible outside:


Being Local, variables x, y, z are NOT available outside
• When you try to print value a inside function, there is no error as a is globally available
• When you try to print x, y, x outside the function ERROR is shown, as x, y, z, are local and not available
outside function

Categories of Functions:
Based on how functions are defined,
stored and used, functions can be
categorised as the ones shown on the
right:
Passing Immutable Objects to a Function:
When an immutable data type, like an integer is passed to a function:
• When program runs, first Global Namespace is created and variable N1 points to the data space for value 5
• When function called, Local Name-space and variable N2 created. It points to data space for value 5
• Inside function, when N2 changes, it points to data space for value 10
• When control returns to main code, Local Namespace and N2 gets removed
• N1 still preserves the value of 5

Passing Mutable Objects to a Function:


When a mutable data type, like a list is passed to a function (value appended):
• First Global Namespace is created and mutable variable L1 points to list index space of the different values
in data space
• When function is called, the Local Name-space for modifyNumber & variable L2 are created. L2 points to
same memory index space as pointed by L1
• Inside function, when value is appended to L2, the original list pointed by L1 gets modified
• When control returns to main code, Local Namespace and L2 gets removed
• L1 still points to old memory space, and displays the modified list under main

Passing Mutable Objects to a Function and then REDIFINING them:


When a mutable data type, like a list is passed to a function (name reassigned):
• First Global Namespace is created and mutable variable L1 points to list index of the different values in
data space
• When function called, Local Name-space and variable L2 created. Initially it points to same memory
pointed by L1
• Inside function when L2 changes a new list is created and named L2. Thereby, L2 becomes different than L1
• When control returns to main code, Local Namespace and L2 gets removed
• L1 still points to old memory space, and displays the old list

REMEMBER
• The address of the actual argument passed is lost, the moment a parameter is made to refer to another
address by assigning a different variable name or a different value
• Hence whatever changes are made to the variable AFTER this, will not affect the original argument, even
though it was mutable
• For a mutable data type as a List, when the following methods are used to modify the List inside a
function call, it will affect both the actual and formal parameters:
o append( ), sort( ), extend( ), reverse( ), remove( ), pop( ), insert( )

NETWORKING
A Computer Network is a collection interconnected autonomous computing device so as to exchange
information or share resources
▪ Advantage of Networks
• Share Resources – Printer, Scanner
• Share Storage – Share Files
• Share Software – Centrally Installed
• Improve Communication - Email
▪ Disadvantages of Networks
• Complex System requiring
• If Central Server Fails, System Fails
• File Security can be a Problem
Types of Networks:
Based on the Geographical Spread of the computers in the network we have the following categories:
▪ PAN (Personal Area Network)
Computer Network spread across the personal space of a person, like Bluetooth
▪ LAN (Local Area Network)
Small Computer Network confined to a localised area like a room, floor or building
▪ CAN (Campus Area Network)
Computer Network spread across a college campus
▪ MAN (Metropolitan Area Network)
Computer Network spread across a metropolitan city like the Cable TV Network
▪ WAN (Wide Area Network)
Large Computer Network spread across countries and continents like the Internet
Difference between LAN and WAN

Types of Networks based on Component Roles


• Peer-to-Peer Networks (P2P Networks): Used for small computer networks typically with up to 10
computers. Each computer can play the role of both a server and a client
• Client Server Network: Used for large computer networks with many computers. A dedicated server
operates solely as a server that serves the client computers
Network Topologies
Topology means geographical arrangement of computers in a network. The different LAN topologies are:
BUS Topology:
• Oldest network topology
• Computers connected to backbone cable (usually co-axial cable)
Advantage:
• Minimum cable length
• Easy to set-up
Disadvantage:
• Backbone cable-fault stops network operations
STAR Topology:
• Most common network topology
• Nodes connected to central node (server)
Advantage:
• Easy to set-up nodes
• Easy to detect fault & remove parts
Disadvantage:
• If central node fails, the network stops
RING Topology:
• Each node connected to form a closed loop
• Data flows through the ring using token
Advantage:
• Can handle high traffic
• Cheaper to expand
Disadvantage:
• A break in the main cable shuts down the network
• Addition/deletion of node disturbs network activity
MESH Topology
• Each node connected to more than one node
• This provides alternative route for data
Advantage:
• Robust network
• Alternative path in case a given path fails
Disadvantage:
• More cabling cost due to multiple routes
• Complexity increases with more computers
TREE Topology
• Combines technology for linear bus and star topology
• It is basically a group of star networks connected by a linear bus backbone
Advantage:
• Supported by various hardware and software vendors
Disadvantage:
• If backbone line breaks, network stops

Types of Cable or Wired Media used to connect Computers:

(FOC)

Types of Wireless Media:

Network Operating System


• A network operating system (NOS) is an operating system that manages computer network resources
• It is essentially, an operating system that includes special functions for connecting multiple computers
Modes of Communications
• Simplex: When data travels in one direction only. Like Television broadcast communication
• Half Duplex Communication: When network communication can take place in both directions, but only in
one direction at any given time. Example walkie-talkie
• Full Duplex Communication: When network communication takes place in both directions, simultaneously.
Example phone communication
Some definitions related to Data Transmission
• Bandwidth: The maximum number of data bits that can be transmitted through a communication system
per sec. It is measured in bits per second or bps.
• Channel Capacity: The actual number of data bits that can be transmitted through a communication
channel per second
• Units of Data Transmission: The unit of data transmission in computer networking are bps, Kbps, Mbps,
Gbps
Switching Techniques:
Switching techniques are used for transmitting data across networks. There are 3 such techniques. These are:

Switching Description

Circuit • A physical connection or end-to-end path is established between two computers that are
Switching going to connect and transmit data
• After the connection is made the entire data is transmitted through the connection
• Example landline telephone network

Message • The source computer sends the message to the switching office first. This message has no
Switching upper-limit for its size
• The switching office stores data in a buffer memory and forwards it to next switching
office
• The process continues till the data is received by the destination computer

Packet • This type of switching takes place using fixed size message packets unlike message
Switching switching. The Internet functions using Packet Switching mechanism
• These data packets reside in the main memory of the computer
• Different packets can move through different network paths to reach destination

You might also like