XI Computer Science Gist-01
XI Computer Science Gist-01
XI Computer Science Gist-01
Computer Science
Gist-1 of Lessons covered between 16/04/2024 to 28/06/2024
Introduction to Programming:
Program:
• Programs are a set of codes that generally take some input, process the input and produce the desired
output. This is called the IPO Cycle.
• A Programming Language is used to write a program code
• Though codes generated depend upon type of computer language used, but the procedure should be a
set of general rules to solve the given problem and should be independent of the language
• This general rule is meant to solve a given problem stepwise, based on some procedure or logic
Definition of Algorithm:
An Algorithm is:
• A sequence of precise and unambiguous instructions
• Designed in such a way that if the instructions are executed in the specified sequence
• The desired result is obtained within a finite number of steps
Example-1: Algorithm to find the average of three numbers:
1. Input the three numbers A, B, C
2. Add the numbers to get sum: Sum = (A+B+C)
3. Divide the Sum by 3, to get average:
Avg = Sum/3
4. Display the average Avg
5. End
Example-2: Algorithm to find the Cost of fencing a garden of length L and breadth B, with rate R taken per unit
length of fencing:
1. Input the length : L
2. Input the breadth : B
3. Input rate of fencing: R
4. Get perimeter of the garden: Peri = 2 * (L + B)
5. Get the total price: Price = Peri * R
6. Display Price
7. End
Example-3: Algorithm for finding the absolute value of any number:
1. Input the number to check: Num
2. If Num < 0, then
a. AValue = (–1) * Num
3. Else
a. AValue = Num
4. Print the result Avalue
5. Stop
Example-4: Branching
Example-5: Branching
Example-6: Looping
Example-7: Looping
In Python, a VARIABLE represents a NAMED MEMORY LOCATION in the RAM, where a value is stored.
Python internally creates the following LABELS referring to these values as shown below:
Data Types in Python:
• Not every object we see around us behaves in the same manner.
• Each material has its own properties and needs to be stored and used accordingly
• In a similar manner, depending upon the nature of the value used in a program, Python categorises data
into the following categories or types, for proper and efficient handling of data
• Also, the operations possible on each category of data may be different based on the nature of the data
The type function can be used to see the data type of an object:
Arithmetic Operators: Modulo operation with floating point and negative numbers
The modulo operator works on BOTH integer and float numbers
Examples on Floats: i. 8 % 4.2 = 3.8 ii. 9.5 % 4 = 1.5 iii. 8.5 % 2.5 = 1.0
To get the above results remember the following points:
• remember that while doing the division do not put a point after the quotient and continue with the division
• Stop the division when you reach the last digit of the whole number portion
• After carrying out the final subtraction, whatever is left behind, will give you the remainder
To get the result of modulo division when either or both the numbers involved are negative, REMEMBER that
the quotient part for such an operation is similar to floor division. The remainder can then be calculated using
this quotient as shown below:
The following examples and their explanations in the following slides will make things clear:
E.g. -8 % 5 = 2 8 % (-5) = -2 -8 % (-5) = -3
Some Definitions:
ATOM
o In programming, an ATOM means something that has a value
o E.g. Identifiers, Literals, Strings, Lists, Tuples, Sets, Dictionaries
EXPRESSION
o Any valid combinations of Operators and Atoms and composed of one or more Operations
o Expressions can be of the following types:
▪ Arithmetic E.g. 12 * 6.5
▪ String E.g. ‘Multi’ + ’level’
▪ Relational E.g. 5<8
▪ Logical E.g. 5<8 or 6<8
▪ Compound E.g. a+b > c and c*b-d != b-c
Lvalue and Rvalue:
o Lvalue: It means Locator Value. These represent some objects in memory. Generally, they occur on the
LEFT side of the assignment operator (=) and represent a variable. They can also occur on the RIGHT of
assignment (=)
o Rvalue: Expressions that are not L-Values and do not occupy some memory location. They come on RIGHT
hand side of an assignment (=) operator and represents an expression generally.
▪ Vol = side*side*side : Vol is LValue, side*side*side is RValue
▪ Peri = 2 * (length + width) : Peri is LValue, 2*(length+width) is RValue
▪ Y=Z : Y and Z both LValue
▪ 3.5 = A + B : Incorret use of LValue
TYPE CASTING
• The explicit conversion of an operand to a specific type
• It is a user defined conversion that forces an expression to be of a different type
▪ It is performed by using an expression like: New_Value = <New_Data_Type> ( Expression )
▪ Examples: a = 8.5 → Indicates a to be a float type data
b = int (a) → Will convert 8.5 to integer type 8
x=6 → Indicates x to be an int type data
y = float (x) → Will convert 6 to float type 6.0
Examples of typecasting between float and integers.
Comparing Strings
• Python compares strings using the dictionary order
• Strings that will occur lower in the dictionary are given a lower value compared to strings that occur
higher in the dictionary. Thus, the string ‘camera’ is considered less than the string ‘eye’
• CAPITAL letters are considered lesser than small letters. Thus ‘Great’ is considered less than ‘great’
• Actually, the ASCII value of the characters in the string are taken while deciding upon the order
Examples:
>>> print ( 'abc' < 'pqr' ) → True # As ‘abc’ will occur in a dictionary before ‘pqr’
>>> print ( 'bat' < 'mat' ) → True # As ‘bat’ will occur in a dictionary before ‘mat’
>>> print ( 'Bat' < 'bat' ) → True # As Capital Letters are considered lower in dictionary
>>> print ( 'bat' < 'battery' ) → True # As 'bat' will occur lower in the dictionary than 'battery'
>>> print ( 'Bat' < 'battery' ) → True # As 'B' occurs lower in the order than 'b'
Comparing Lists / Tuples
• Two lists or tuples are considered identical if these have the same values in the same order
• For inequality, a list / tuple with the first larger value from the left is considered larger in value
• However, when considering the values during comparison, the pair of values that decide the result
during comparison, should be of the same data type
>>> print( [2, 4, 6] == [2, 4, 6] ) → True #As both lists are identical
>>> print( [2, 4, 8] > [2, 4, 6] ) → True #As first two values are same, but for third value 8>6
>>> print( [5, 3] > [2, 8, 9] ) → True #As first value 5>2; number of elements is NOT considered
>>> print ( [2, 4, 6] < [2, 4, 6, 8] ) → True #First 3 elements same. But second list has one more value
>>> print ( [7, 4, 6] < [2, 4, 6, 8] ) → False #As first value 7>2, the remaining values are not considered
>>> print ( ['bat', 'mat', 'rat'] == ['bat', 'mat', 'rat'] ) → True #As both the lists are identical
>>> print ( ['bat', 'mat', 'rat'] < ['bat', 'pat', 'rat'] ) → True #As 'mat' < 'pat' in dictionary order
>>> print ( [ 'abc', 'pqr', 'zyk‘ ] < [ 2, 5, 3 ] ) → ERROR #As str and int can’t be compared
>>> print ( [ 2, 'ab', 4 ] < [ 2, 5, 'pq‘ ] ) → ERROR
#As first values are identical, hence second values are compared and 'ab' (str) and 5 (int) are different types
>>> print ( ( 1, 'ab', 4 ) < ( 2, 5, 'pq‘ ) ) → True #As first values in the tuple are of same data
type and different, hence 'ab' and 5 not checked
>>> print( ( [ 4, 7, 3 ] , ‘pqr’, 45 ) < ( [ 6, 8, 2 ] , ‘abc’, 66 ) ) → True
#The first element in each tuple is a list, and 4 in [4, 7, 3] is less than 6 in [6, 8, 2]
>>> print( [ [5, 8] , [2, 7] , 4 ] > [ [5, 8] , [9, 3] , 4 ] ) → False
#As first values are identical, hence second values are compared and 2 in [2, 7] is less than 9 in [9, 3]
>>> print( [ [‘abc’, 8] , [2, 7] , ‘mno’ ] > [ [5, 8.2] , [9, 3] , 4 ] ) → ERROR
#First values compared ‘abc’ and 5 are of different data types. Hence error
Comparing Dictionaries
• As dictionaries are unordered sequence, hence these are compared based on the key:value pairs
• The order in which the key:value pairs occur is not considered
• Only equality (==) and non-equality (!=) operators are used with dictionaries
• Other type of comparisons cannot be done on dictionaries
>>> print ( { 'a':2, 'b':7, 'c':5 } == { 'a':2, 'b':7, 'c':5 } ) → True #As both dictionaries are identical
>>> print ( { 'a':2, 'b':7, 'c':5 } == { 'a':2, 'c':5, 'b':7 } ) → True # Order of elements does not matter in
dictionaries
>>> print ( { 'a':2, 'b':7, 'c':5 } == { 'a':2, 'c':3, 'b':7 } ) → False #As different key:value pairs
>>> print ( { 'a':2, 'b':7, 'c':5 } != { 'a':2, 'c':3, 'b':7 } ) → True #As different key:value pairs
Computer Organisation
Computer System: It consists of:
• Central Processing Unit (CPU)
✓ Control Unit (CU)
✓ Arithmetic & Logic Unit (ALU)
• Input Devices: Taking data into the computer
✓ Keyboard, Mouse, Scanner, Mic, Tablet, OMR, OCR, MICR
• Output Devices: Taking data out of the computer
✓ VDU, Printer (DMP, Inkjet, Laser), Plotter, Speaker
• Primary Memory: For storing the working data
✓ Random Access Memory (RAM) & Read Only Memory (ROM)
• Cache Memory: Fast memory between CPU and RAM
• Secondary Memory: For storing data permanently. E.g. HDD, CD, DVD, BD
• Bus: For communication
✓ Data Bus, Address Bus, Control Bus
Some Definitions
Scanner: A scanner is an input device which can be used to input or scan images and documents and store
them in a digital format in the computer
Barcode Reader: A barcode reader is used to scan ready product information from product labels. It is used in
supermarkets, shops, libraries, post offices and other places
Touch Screen: A touch screen is a special display screen where the user can enter data by touching the screen
at specific locations. It is a display, which can detect the presence and location of a touch within the display area
OMR: Optical Mark Reader is an input device used to read and process data from pre-printed document-forms
marked by humans.
OCR: Optical Character Reader is a technology used to optically scan pages containing text and then to identify
and save the scanned data as a text file instead of an image file
MICR: Magnetic Ink Character Recognition technology is used mainly by banking systems for faster processing
of large volumes of cheques. The bank’s data are pre-printed using a special magnetic ink.
VDU: A visual display unit (VDU), or commonly a video monitor is the most widely used output device. Two
types of VDU are Cathode Ray Tube or CRT displays, Liquid Crystal Displays (LCDs), Light Emitting Diode (LED)
displays
Dot Matrix Printer (DMP): These printers are impact printers and are similar to mechanical typewriters. They
use a set of pins of very small radius to print dots on paper
Inkjet Printer: An inkjet printer is a non-impact printer. It prints by ejecting microscopic drops of ink through
tiny ink nozzles in the print head to produce a particular character or image.
Laser Printer: These non-impact type printers use dry powder ink (called toner) to print text or images on
paper, similar to a photocopy machine.
Plotter: A plotter is similar to an inkjet printer, but is used for printing on larger papers or continuous paper
rolls.
CPU: Central Processing Unit is the brain of any computer system and is made up of two basic components,
namely the Control Unit (CU) and the Arithmetic and Logic Unit (ALU)
ALU: Arithmetic and Logic Unit is the place where the actual execution of the instructions takes place during
data processing. It executes arithmetic calculations and comparisons of data
CU: Control Unit acts like a supervisory unit in the CPU and is responsible for issuing control instructions to
carry out different functions of the CPU
Primary Memory: The primary memory is the main memory of the computer and a computer cannot run
without it. Two types of primary memory are Random Access Memory (RAM) and Read Only Memory (ROM).
ROM: Read Only Memory is a non-volatile permanent memory, the contents of which can be only read but
cannot be altered. It is also called firmware, which permanently stores the basic information needed by the
computer during start-up. Different types of ROM are:
▪ ROM: These are the first semiconductor ROMs that contained a pre-programmed set of data or
instructions.
▪ PROM (Programmable ROM): It is purchased in an un-programmed state. Special equipment like a device
programmer is used to write data into the PROM by applying an electrical charge to the input pins of the
chip.
▪ EPROM (Erasable and Programmable ROM): It is programmed in exactly the same manner as a PROM.
Unlike a PROM, EPROMs can be erased using strong ultraviolet (UV) light and reprogrammed repeatedly.
▪ EEPROM (Electrically Erasable and Programmable ROM): Internally, they are similar to EPROMs, but
instead of using UV light, the erase operation is done electrically (it is also pronounced as E2 PROM)
RAM: Random Access Memory is treated as the main working memory of the computer. However, RAM is
volatile in nature and it’s content are lost when power is switched off. The different types of RAM are:
▪ Dynamic RAM (DRAM): Dynamic RAM is a volatile memory with a very short data lifetime. The
electrical charge stored in each memory cell needs to be periodically refreshed to retain its data.
Hence, it is called dynamic RAM. DRAM is available in the form of IC chips of 2GB, 4GB, 8GB capacity
▪ Static RAM (SRAM): Static RAM is also a volatile memory, but unlike DRAM, it does not require any
refreshing of charges to retain its data. Hence these are called static RAM. It can retain its data as long
as its electrical power is on. These are costlier than DRAM and are used in smaller quantities like 512
KB, 1 MB, or 2 MB
Secondary Memory: To store large volumes of data permanently we need a secondary storage device. The
secondary memory is a permanent memory, where the results of data processing are stored from the RAM for
future use.
Memory Unit:
Main unit of memory is byte consisting of 8 binary digits or bits. Several bytes can be taken together to form
the following units:
✓ Kilo Byte or KB: 1 KB = 1024 Bytes = 210 Bytes
✓ Mega Byte or MB: 1 MB = 1024 KB = 210 Kilo Bytes
✓ Giga Byte or GB: 1 GB = 1024 MB = 210 Mega Bytes
✓ Tera Byte or TB: 1 TB = 1024 GB = 210 Giga Bytes
✓ Peta Byte or PB: 1 PB = 1024 TB = 210 Tera Bytes