XII Computer Science Gist-01
XII Computer Science Gist-01
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
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.
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.
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.
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
• 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
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
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
(FOC)
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