Professional Documents
Culture Documents
com
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
TYPES OF SOFTWARE
for more updates visit: www.python4csip.com
Introduction
User
Appn Software
System software
Hardware
Types of Software
Type Of Software
System Software
The functions of all the physical components of a computer
system are guided by some instructions or program collectively
known as System Software. System Software controls all
internal activities inside a computer system and between all
attached components of a computer system.
System Software - OS
Operating system is a set of system programs that controls and
coordinates the operations of a computer system. It provide
interaction between user and computer.
It is stored (installed) on the hard disk or any other external storage
device.
It is the first program to be executed on a computer after the BIOS.
Need of Operating system: it provides a software platform, on top
of which, other programs, called application programs are run.
System Software - OS
Major Functions of Operating System are :-
Device Manager - Communicate with hardware and the attached
devices.
Memory Manager - Manage different types of memories
User Interface
It is the medium by which user interact with any
software. This interface may by text based on
graphical based or it may be touch screen.
Based on this User interface may be categorized
as:
CUI(Character User Interface)
GUI (Graphical User Interface)
TEXT BOX
DROP
DOWN LIST
RADIO BUTTON
BUTTON
Device Driver
A device driver is a system software that acts like an
interface between the Device and the user or the
Operating System. All computer accessories like Printer,
Scanner, Web Camera, etc. come with their own driver
software. These driver software help the operating system
and other application software to communicate with those
devices for optimal use
Language Processor
As discussed computer understand only binary language
so it is very difficult for the programmer to write the entire
program as a bunch of 0 and 1. So need arises to have a
language which is easy to write, learn and understand.
The solution was High Level Languages which used simple
English like statements to write the application.
Common Language processors are :
Compiler
Interpreter
Application Software
Application software runs on Operating system. It uses the
services of operating system to interact with hardware.
Set of software to carry out specific task like creating
documents (word processor ), calculations and analysis
(Spreadsheet), Presentations (Power point), Railway
reservation, library management etc.
It is of 4 type:
Utility software
Customized software
Developer tool
Utility Software
Utility software for efficient working of our computer and other task.
Various utility softwares available like
Text Editor : for storing short notes, message, coding. Examples
are : Notepad, Notepad++, WordPad
To-Do task : to manage the work.
Compression : to compress i.e. reduce the size of file to transfer
file from one computer to another. Examples are : WinZip,
WinRAR, jZIP etc.
Disk Defragmenter : Disk de-fragmentation utility software
speeds up the system by rearranging such fragmented files
stored on a disk in contiguous locations in order to optimize the
system performance.
Anti-Virus : to protect our computer from threats like Virus, worm,
Trojan horse, banking frauds etc.
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Customized Software
These are tailored software i.e. it is created as per
the need of the customer for their specific
requirement like – for maintaining school operation,
Restaurant management, banking, Hotel, Hospital
management etc.
Developer tools
When a programmer starts the process of writing a
program to develop software for any type of application,
he/she requires a series of software developing tools like
code editor, debugger and compiler. A platform where all
these software developing tools are bundled into a
package is known as Integrated Development Environment
(IDE).
Popular development tools are : Net Beans, Eclipse, IDLE,
Visual Studio.
Each IDE provides features of: Editor, Compiler/Interpreter,
Debugger, Automation tool, output window etc.
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Compiler
Compilers are used to convert High Level Language
program to machine language
It convert the entire program in machine language
in one go.
If encounter errors, it report all the error along with
the line number.
It convert HLL code in machine code if all errors are
remove, once converted we can directly execute
program without interacting with the compiler.
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Interpreter
It is also used for converting HLL into machine
language.
It translate the HLL code line by line as well as
execute at the same time.
If any error occurs, it stops the execution and report
the error.
COMPILER INTERPRETER
It convert the HLL code to machine It convert the HLL code to machine
code in one go code line by line
It report all the error after compilation It report error for only that line where
error encountered
It execute program only when all It convert and execute simultaneously
errors are rectified
It is fast It is slow as compare to compiler
It takes less memory, because after It takes more memory because
conversion compiler is not required in Interpreter is required in memory with
memory with execution every execution.
Not suitable for debugging purpose Suitable for debugging purpose
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Digital Systems:
Boolean Algebra and
Logic Gates
DEFINITION:
Logic 0 Logic 1
False True
Off On
Low High
No Yes
Open switch Closed switch
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Truth Tables
Inputs Output
A B x
A
1 1 1
? x
0 1 0
B
1 0 0
0 0 0
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
OR
AND
NOT
OR Operation
AND Operation
NOT Operation
A
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
LOGIC GATES
OR
AND
NOT
NAND
NOR
OR Gate
AND Gate
NOT Gate
The symbol and Truth Table for NOT gate is given below:
A
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Examples 1,2
Examples 3
Evaluating Logic-Circuit
Outputs
x = ABC(A+D)
Examples
NAND Gate
NOR Gate
x* 0 =0 Fallacy
x+1=1 Tautology
x*x=x Indempotence Law
x+x=x Indempotence Law
x*x’=0 Complementarity Law
x+x’=1 Complementarity Law
x” = x Involution Law
x+0=x
x* 1 =x
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
De’Morgan’s Theorems
(Break the line change the sign)
(x+y)’=x’y’
Implications and alternative symbol for NOR function
(xy)’=x’+y’
Implications and alternative symbol for NAND function
Process of Demorganization:
(i) Complement entire function
(ii) Change all AND to OR and all OR to AND
(iii) Complement each of the individual variables
Demorgan’s Theorum
Demorgan’s Theorum
• The binary number system uses 2 symbols, and therefore has a radix
or base of 2.
• Symbols are [ 0 and 1 ], also known as bits or binary digit
• Every bit in Binary number system is identified from its position i.e.
from right to left as (for e.g. 110) :-
Binary Number 1 1 0
POSITIONAL VALUE 22 21 20
• It means : 1 x 22 + 1 x 21 + 0 x 20 will give its decimal equivalent
• 1 x 4 + 1 x 2 + 0 x 1 = 6, So (110)2 = (6)10
• Left most digit will be MSB (most significant bit and right most digit
will be LSB (least significant bit)
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Binary Number 1 4 0
POSITIONAL VALUE 82 81 80
• It means : 1 x 82 + 4 x 81 + 0 x 80 will give its decimal
equivalent
• 1 x 64 + 4 x 8 + 0 x 1 = 96, So (140)8 = (96)10
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Binary Number A 2 B
POSITIONAL VALUE 162 161 160
• It means : Ax162 + 2x161 + Bx160 will give its decimal equivalent
• 10x256 + 2x16 + 11x1 = 2603, So (A2B)16 = (2603)10
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
At times, you need to covert a number from one number system
to another.
Distinct methods have been defined for conversion between
each pair of number systems.
You can convert a decimal number to its binary form by using
the method of successive division by 2, the radix of the binary
number system.
Put the remainder to the right of quotient and repeat this
process till the quotient becomes ZERO or ONE.
Write down the remainders in reverse order to get equivalent
binary number.
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
LSB
MSB
ANSWER: (101101)2
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
• (79)10 to ( ? )2
• (30)10 to ( ? )2
1 0 1 0 1 1 0 1
27 26 25 24 23 22 21 20
128 0 32 0 8 4 0 1
ANSWER: (73)10
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
• ( 11110110) 2 t o ( ? ) 10
• ( 101010110101) 2 t o ( ?) 10
You can convert a decimal number to its octal form by using the
method of successive division by 8, the radix of the octal
number system.
Put the remainder to the right of quotient and repeat this
process till the quotient becomes ZERO.
Write down the remainders in reverse order to get equivalent
octal number.
LSD
MSD
ANSWER: (135)8
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
• ( 173) 10 to ( ? ) 8
• ( 243) 10 to ( ? ) 8
2 3 7
82 81 80
128 24 7
ANSWER: (159)10
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
You can convert a decimal number to its octal form by using the
method of successive division by 16, the radix of the
hexadecimal number system.
Put the remainder to the right of quotient and repeat this
process till the quotient becomes ZERO.
Write down the remainders in reverse order to get equivalent
hexadecimal number.
LSD
A=10
B=11, and so on.
..
MSD
ANSWER: (B2)16
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
• ( 233) 10 to ( ? ) 16
• ( 79) 10 to ( ? ) 16
A 2 B
162 161 160
2560 32 11
ANSWER: (2603)10
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
3 5 2
011 101 010
ANSWER: (011101010)2
• ( 237) 8 to ( ? ) 2
• ( 206) 8 to ( ? ) 2
3 2 6 6
• ( 111000111001) 2 t o ( ? ) 8
• ( 101010101010101) 2 t o ( ? ) 8
6 0110
7 0111
8 1000
9 1001
8 B C 2
1000 1011 1100 0010
ANSWER: (1000101111000010)2
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
• ( CA FE) 16 to ( ? ) 2
• ( 78F9) 16 to ( ? ) 2
6 B 6 6
• ( 10101010101010101) 2 t o ( ? ) 16
• ( 110011110001101) 2 t o ( ? ) 16
CARRY
ADDITION
RESULT
ANSWER : 1100011
CARRY
ADDITION
RESULT
ANSWER : 11110001
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
0 1 0 1 0 0 0 0
CONTROL CODE
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
1 0 1 0 0 1 1 1 OCTET 2
1 0 0 0 0 0 0 0 OCTET 2
1 0 1 1 1 0 1 1 OCTET 3
1 1 1 1 0 0 0 0
OCTET 1
1 0 0 1 0 1 0 0
OCTET 2
1 0 0 0 1 1 0 1
OCTET 3
1 0 0 0 0 1 0 1
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 1 0 1 0 1 0
REQUIREMENTS
ANALYSIS
DESIGN
CODING
TESTING
IMPLEMENTATION
DOCUMENTATION
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHING BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
REQUIREMENTS
What the problem is?
What is needed to solve it?
What the solution should provide
If there are constraints and special
conditions?
ANALYSIS
INPUTS: To the problem, their form
and the input media to be used.
OUTPUTS: Expected from the
problem, their form and the output media
to be used.
Special constraints or (necessity)
conditions (if any).
Formulas or equations to be used
DESIGN
In Design phase, we design the proposed
solution without writing a computer
program by using the techniques:
◦ Algorithm
◦ Pseudocode
◦ Flow Chart
Coding
It is the process of transforming design
(Algorithm, Pseudocode, Flow Chart) in a
computer program using any
programming language.
The output of program must be the
solution of intended problem.
Testing
program testing is the process of executing a
program to demonstrate its correctness.
Program verification is the process of
ensuring that a program meets user
requirement
After the program is compiled we must run
the program and test/verify it with different
inputs before the program can be released
to the public or other users (or to the
instructor of this class)
Implementation
It is the process of installing the software
developed on customer‟s site.
Installation may be done by replace the
entire manual work with the software
developed or in a pilot mode i.e. running
the manual and automated system in
parallel or installing only a part module
with manual work.
It also involves training of software to the
intended users.
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHING BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Documentation
- Contains details produced at all stages of the
program development cycle.
Can be done in 2 ways
* writing comments between your line of
codes.
* Creating a separate text file to explain the
program.
- Important not only for other people to use or
modify your program, but also for you to
understand your own program after along time.
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHING BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Just a minute…
Write an algorithm to accept two numbers, divide the
first number by the second, and display their quotient.
Allocated time to solve the problem is 05 minutes.
Solution - 1
1. Start the algorithm.
2. Get the first number.
3. Get the second number.
4. Divide the first number by the
second.
5. Display the quotient.
6. End the algorithm. 72 9
72 9 8
Solution - 2
1. Start the algorithm.
2. Get the distance in kilometers.
3. Multiply the distance by 1000.
4. Display the result of
multiplication as distance
in meters. 50
5. End the algorithm.
50 1000 50000
Solution - 3
1. Start the algorithm. 3
2. Get the first number. 1
3. Get the second number.
6 20
4. Get the third number.
2
5. Get the fourth number.
6. Get the fifth number. 8
Flowchart
Flowchart is a graph used to depict or
show a step by step solution using
symbols which represent a task.
The symbols used consist of geometrical
shapes that are connected by flow lines.
It is an alternative to pseudo coding,
where as a pseudo code description is
verbal. A flowchart is graphical in nature
ADVANTAGES
It helps in analyzing the problems effectively.
It acts as a guide during the program development phase.
It help easy debugging of logical errors
DISADVANTAGES
A lengthy flowchart may extend over multiple pages, which reduces
readability.
Drawing a flowchart is time consuming.
The changes made to a single step may cause redrawing the entire
flowchart
Flowchart Symbols
Flowchart Symbols
Start
Accept a
number
Display the
number
Stop
Start
Accept a number
Stop
Just a Minute…
Write a flow chart to calculate area of
rectangle
◦ Allotted Time: 05 minutes
Write a flow chart to enter any number
and check it is divisible by 7 or not
◦ Allotted Time: 05 minutes
Solution - 1
Start
Accept length,
breadth
Display Area
Stop
Solution - 2
Start
Accept a number
No
Display
Is the remainder ‘Number not
equal to zero ? divisible by
Yes 7’
Display ‘Number
divisible by 7’
Stop
Pseudocode
It is a formal way of writing the program logic
whereas algorithm is an informal way of writing
program logic
It is detailed yet easy description of what algorithm
must do.
It is written in formally styled-English language
It is used as an initial step in the process of
developing of program
Provide detailed template to programmers.
It is language independent i.e. it can be converted
to program by using any programming language.
Pseudocode
In Pseudocode we can use few special
words for determining the actions like
◦ Accept/input
◦ Display/print
◦ If / else
◦ Repeat
◦ Start
◦ End
◦ Goto
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHING BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Sequential
A series of steps or statements that are executed
in a specific order.
The beginning and end of a block of statements
can be optionally marked with the keywords
begin & end.
Example:
1. Statement1
2. Statement 2
3. Statement 3
4. .
5. Statement n
Here each statement will be executed one by one
Selection
It determine two course of action depending
upon the condition.
A condition is given here, which will result in
either True(1) or False (0)
The keyword either if and else ( almost all
programming language also support this
keyword )
It is also known as branching statement, because
it provide two branch, one for True and another
for False. Branching will be done depending
upon the condition.
Iteration
It specifies block of one or more statement that
are repeatedly executed till condition remains
true.
Keyword used are :
◦ While (supported by almost all programming language
also)
◦ If with go to (in Pseudocode only)
Iteration consists of 3 main parts:
1. Initial value (from where iteration begins)
2. Condition (upto what condition iteration will continues)
3. Stepping (Increment/Decrement from Initial value to
reach to condition)
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHING BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
4. PRINT A
5. A =A + 1 [INCREMENT]
6. END
3. PRINT A
4. A =A + 1
5. IF A<=100 THEN GO TO STEP 3
6. END
Just a Minute….
1. Write a Pseudocode to convert temperature
in Fahrenheit to Celsius
2. Write a Pseudocode to input age and check
age is teenager or not
3. Write a Pseudocode to print all the even
numbers from 2 to 100
4. Write a Pseudocode to print all the odd
numbers from 100 to 1
Decomposition
Large program can be tackled with “divide and
conquer”.
Decomposition happens when we break
down a problem into smaller parts so we can
both understand and manage
the problem better.
For large, complex problems – we can then
analyze, solve and design the smaller parts of
the problem and then put it all together for a
solution.
Advantages
Different people can work on different subprograms.
Parallelization may be possible
Maintenance is easier
Disadvantages
The solution to subprogram might not combine to solve
the original problem
Poorly understood problems are hard to decompose
Need of Decomposition
As studied earlier, first need of decomposition is
to divide the large program into smaller and
manageable sub program, it is easier to write 2
programs of 400 lines than writing single
program of 800 lines.
Each subprogram can be managed
independently
Global changes can be applied very easily
Code can be reused
Functions, modules, class are some example of
decomposition.
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHING BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Decomposition Example - 1
Decomposition Example - 2
LIBRARY MANAGEMENT
DELETE DELETE
Decomposition Example - 3
Think of a company creating a mobile phone.
Mobile phones are made up of lots of different
parts. Companies who make phones might
make a list of everything they need and
decompose the manufacturing process so that
one factory can be making the screens while
another makes batteries, another makes the
phone case and another makes mother board.
In C++ In Python
int a=2, b=3, tmp; a,b=2,3
tmp=a; a,b=b,a
a=b;
b=tmp;
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
• When you install Python, you get everything i.e. you don‟t need
to install additional libraries. All functionality is available with
Python additional library. Features like web-pages, database
connectivity, GUI features are available in Python standard
library. Python follows – “Battery included” philosophy
Interactive Mode
(Immediate
Python working Mode)
Ways
Script Mode
( )
( )
( )
( )
( )
print (“Hello”)
Save the
program
and press
F5
( )
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Note:
1) In Python 2.x print is a statement, not a function
2) Python is case-sensitive i.e. it will treat small letter and
capital letter as two different entities like m & M will be
treated differently in Python.
3) We can use up arrow (↑) and down arrow (↓) to select
previous command and press enter to recall and execute.
4) print statement is required only while working with
script mode otherwise in interactive mode values can be
printed without print statement
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
(c) (d)
Johny Johny Yes Papa This is my first program
No Papa Johny Johny Yes Papa
No PapaLittle Star
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
PYTHON FUNDAMENTALS
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR
& SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
TOKENS
In a passage of text, individual words and punctuation
marks are called tokens or lexical units or lexical
elements. The smallest individual unit in a program is
known as Tokens. Python has following tokens:
Keywords
Identifiers(Name)
Literals
Operators
Punctuators
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
KEYWORDS
Keywords are the reserved words and have special
meaning for python interpreter. Every keyword is
assigned specific work and it can be used only for that
purpose.
A partial list of keywords in Python is
IDENTIFIERS
Are the names given to different parts of program like variables,
objects, classes, functions etc.
Identifier forming rules of Python are :
Literals / Values
Literals are data items that have a fixed value.
Python supports several kinds of literals:
String Literal
Numeric Literals
Boolean Literals
Literal Collections
String Literals
It is a collection of character(s) enclosed in a double or
single quotes
Examples of String literals
“Python”
“Mogambo”
„123456‟
„Hello How are your‟
„$‟, „4‟,”@@”
In Python both single character or multiple characters
enclosed in quotes such as “kv”, „kv‟,‟*‟,”+” are treated
as same
Size of String
Python determines the size of string as the count of characters in the string.
For example size of string “xyz” is 3 and of “welcome” is 7. But if your
string literal has an escape sequence contained in it then make sure to count
the escape sequence as one character. For e.g.
String Size
„\\‟ 1
„abc‟ 3
„\ab‟ 2
“Meera\‟s Toy” 11
“Vicky‟s” 7
You can check these size using len() function of Python. For example
>>>len(„abc‟) and press enter, it will show the size as 3
Size of String
For multiline strings created with triple quotes : while calculating size
the EOL character as the end of line is also counted in the size. For
example, if you have created String Address as:
>>> Address="""Civil lines
Kanpur"""
>>> len(Address)
18
For multiline string created with single/double quotes the EOL is not
counted.
>>> data="ab\
bc\
cd"
>>> len(data)
6
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Numeric Literals
The numeric literals in Python can belong to any of
the following numerical types:
1) Integer Literals: it contain at least one digit and must
not contain decimal point. It may contain (+) or (-) sign.
Types of Integer Literals:
a) Decimal : 1234, -50, +100
b) Octal : it starts from symbol 0o (zero followed by
letter ‘o’)
For e.g. 0o10 represent decimal 8
Numeric Literals
>>> num = 0o10
>>> print(num)
It will print the value 8
Numeric Literals
2) Floating point Literals: also known as real
literals. Real literals are numbers having fractional
parts. It is represented in two forms Fractional Form
or Exponent Form
Fractional Form: it is signed or unsigned with decimal point
For e.g. 12.0, -15.86, 0.5, 10. (will represent 10.0)
Exponent Part: it consists of two parts “Mantissa” and
“Exponent”.
For e.g. 10.5 can be represented as 0.105 x 102 = 0.105E02 where
0.105 is mantissa and 02 (after letter E) is exponent
Points to remember
Numeric values with commas are not considered int or float value, rather Python
treats them as tuple. Tuple in a python is a collection of values or sequence of values.
(will be discussed later on)
You can check the type of literal using type() function. For e.g.
>>> a=100
>>> type(a)
<class 'int'>
>>> b=10.5
>>> type(b)
<class 'float'>
>>> name="hello“
>>> type(name)
<class 'str'>
>>> a=100,50,600
>>> type(a)
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & <class 'tuple'>
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Boolean Literals
A Boolean literals in Python is used to represent one of the two
Boolean values i.e. True or False
These are the only two values supported for Boolean Literals
For e.g.
>>> isMarried=True
>>> type(isMarried)
<class 'bool'>
>>> salary=None
>>> type(salary)
<class 'NoneType'>
Complex Numbers
Complex: Complex number in python is made up of two floating point values,
one each for real and imaginary part. For accessing different parts of
variable (object) x; we will use x.real and x.image. Imaginary part of the
number is represented by “j” instead of “I”, so 1+0j denotes zero imaginary.
part.
Example
>>> x = 1+0j
>>> print x.real,x.imag
1.0 0.0
Example
>>> y = 9-5j
>>> print y.real, y.imag
9.0 -5.0
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Example 2
>>> percentage=float(input("Enter percentage "))
Enter percentage 100 percent
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: „100 percent'
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Program 1
Open a new script file and type the following code:
Operators
are symbol that perform specific operation when
applied on variables. Take a look at the expression:
(Operator)
10 + 25 (Operands)
Above statement is an expression (combination
of operator and operands)
i.e. operator operates on operand. some operator requires two
operand and some requires only one operand to operate
Types of Operators
Unary operators: are those operators that require
one operand to operate upon. Following are some
unary operators:
Operator Purpose
+ Unary plus
- Unary minus
~ Bitwise complement
Not Logical negation
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Types of Operators
Binary Operators: are those operators that require two
operand to operate upon. Following are some Binary
operators:
1. Arithmetic Operators
Operator Action
+ Addition
- Subtraction
* Multiplication
/ Division
% Remainder
** Exponent
// Floor division
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Example
>>> num1=20
>>> num2=7
>>> val = num1 % num2
>>> print(val)
6
>>> val = 2**4
>>> print(val)
16
>>> val = num1 / num2
>>> print(val)
2.857142857142857
>>> val = num1 // num2
>>> print(val)
2
Bitwise operator
Operator Purpose Action
Bitwise operator works
& Bitwise AND Return 1 if both on the binary value of
inputs are 1 number not on the actual
^ Bitwise XOR Return 1, if the value. For example if 5
number of 1 in is passed to these
input is in odd operator it will work on
101 not on 5. Binary of
| Bitwise OR Return 1 if any 5 is 101, and return the
input is 1 result in decimal not in
binary.
Example
Binary of 12 is 1100 and 7 is 0111, so applying &
1100
0111
-------
Guess the output with
0100 Which is equal to decimal value 4
| and ^ ?
Let us see one practical example, To check whether entered number is divisible of 2 (or in
a power of 2 or not) like 2, 4, 8, 16, 32 and so on
To check this, no need of loop, simply find the bitwise & of n and n-1, if the result is 0 it
means it is in power of 2 otherwise not
Operators Purpose
<< Shift left
>> Shift right
Identity Operators
Operators Purpose
is Is the Identity same?
is not Is the identity not same?
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Relational Operators
Operators Purpose
< Less than
> Greater than
<= Less than or Equal to
>= Greater than or Equal to
== Equal to
!= Not equal to
Logical Operators
Operators Purpose
and Logical AND
or Logical OR
not Logical NOT
Assignment Operators
Operators Purpose
= Assignment
/= Assign quotient
+= Assign sum
-= Assign difference
*= Assign product
**= Assign Exponent
//= Assign Floor division
Membership Operators
Operators Purpose
in Whether variable in
sequence
not in Whether variable not in
sequence
Punctuators
Punctuators are symbols that are used in programming
languages to organize sentence structure, and indicate
the rhythm and emphasis of expressions, statements,
and program structure.
Common punctuators are: „ “ # $ @ []{}=:;(),.
C=A+B Expressions
Inline Comment
if(C>=100) #checking condition
print(“Value is equals or more than 100”)
else:
print(“Value is less than 100”) Block
Indentation
Expression
An expression is any legal combination of symbols that
represents a value. An expression is generally a
combination of operators and operands
Example:
expression of values only
20, 3.14
Expression that produce a value when evaluated
A+10
Salary * 10 / 100
Statement
It is a programming instruction that does something i.e.
some action takes place.
Example
print(“Welcome to python”)
The above statement call print function
When an expression is evaluated a statement is executed
i.e. some action takes place.
a=100
b = b + 20
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Comments
Comments are additional information written in a
program which is not executed by interpreter i.e.
ignored by Interpreter. Comment contains information
regarding statements used, program flow, etc.
Comments in Python begins from #
Comments
Full line comment
Example:
#This is program of volume of cylinder
Inline comment
Example
area = length*breadth # calculating area of rectangle
Multiline comment
Example 1 (using #)
# Program name: area of circle
# Date: 20/07/18
#Language : Python
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Comments
Multiline comment (using “ “ “) triple quotes
Example
“““
Program name : swapping of two number
Date : 20/07/18
Logic : by using third variable
”””
Functions
Function is a block of code that has name and can be
reused by specifying its name in the program where
needed. It is created with def keyword.
Example
def drawline():
print(“======================“)
print(“Welcome to Python”)
drawline()
print(“Designed by Class XI”)
drawline()
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
def area():
a = 10
b=5
c=a*b
Indentation means extra space before writing any
statement. Generally four space together marks the next
indent level.
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Variables
Variables are named temporary location used to store
values which can be further used in calculations, printing
result etc. Every variable must have its own Identity, type
and value. Variable in python is created by simply
assigning value of desired type to them.
For e.g
Num = 100
Name=“James”
Variables
Note: Python variables are not storage containers like other
programming language. Let us analyze by example.
In C++, if we declare a variable radius:
radius = 100
[suppose memory address is 41260]
Now we again assign new value to radius
radius = 500
Now the memory address will be still same only value
will change
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Variables
Now let us take example of Python:
radius = 100 [memory address 3568]
Now you can see that In python, each time you assign new
value to variable it will not use the same memory address
and new memory will be assigned to variable. In python the
location they refer to changes every time their value
change.(This rule is not for all types of variables)
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Multiple Assignments
Python is very versatile with assignments. Let‟s see in how different ways
we can use assignment in Python:
1. Assigning same value to multiple variable
a = b = c = 50
2. Assigning multiple values to multiple variable
a,b,c = 11,22,33
Examples:
Multiple Assignments
x,y,z = 10,20,30 #Statement 1
z,y,x = x+1,z+10,y-10 #Statement 2
print(x,y,z)
Output will be
10 40 11
Now guess the output of following code fragment
x,y = 7,9
y,z = x-2, x+10
print(x,y,z)
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Multiple Assignments
Let us take another example
y, y = 10, 20
Variable definition
Variable in python is create when you assign value to it
i.e. a variable is not create in memory until some value is
assigned to it.
Let us take as example(if we execute the following code)
print(x)
Python will show an error „x‟ not defined
So to correct the above code:
x=0
print(x) #now it will show no error
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Dynamic Typing
In Python, a variable declared as numeric type can be
further used to store string type or another.
Dynamic typing means a variable pointing to a value of
certain type can be made to point to value/object of
different type.
Lets us understand with example
x = 100 # numeric type
print(x)
x=“KVians” # now x point to string type
print(x)
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Dynamic Typing
string:KVians
x = 100
y=0
y=x/2
print(y)
x='Exam'
y = x / 2 # Error, you cannot divide string
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Just a minute…
What is the difference between keywords and
identifiers
What are literals in Python? How many types of
literals in python?
What will be the size of following string
Just a minute…
print(“Name is “, name)
Suggest a solution
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Just a minute…
Just a minute…
Just a minute…
Name="James"
Salary=20000
Dept="IT"
print("Name is ",Name,end='@')
print(Dept)
print("Salary is ",Salary)
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Just a minute…
x,y = 7,2
x,y,x = x+4, y+6, x+100
print(x,y)
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR