Revision of The Basics of Python
Revision of The Basics of Python
syllabus
2020-21
Chapter 1
Revision of the
Basics of Python
Computer Science
Class XII ( As per CBSE
Board)
Introduction
It is used for:
software development,
web development (server-side),
system scripting,
Mathematics.
Data Types
Data Type specifies which type of value a variable
can store. type() function is used to determine a
variable's type in Python.
1. Number In Python
It is used to store numeric values
Output :
- 301.4
121.0
O
utput :-
(5+0j)
Visit School Website for Regular Updates
Data type
2. String In Python continue
A string is a sequence of characters. In python we can create string
using
single (' ') or double quotes (" ").Both are same in python. e.g.
str='computer science'
print('str-', str) # print
string
print('str[0]-', str[0]) # print
first char 'h'
print("str str +'yes')
+'yes'-",str[1:3])
print('str[1:3]-', # concatenated
# print string from postion 1 to 3 'ell'
Output string
print('str[3:]-', str[3:]) # print string staring from 3rd char 'llo
str- computer science
world' print('str *2-', str *2 ) # print string two times
str[0]- c
str[1:3]- om
str[3:]- puter science
str *2- computer sciencecomputer science
str +'yes'- computer scienceyes
m
p
s
Visit School Website for Regular Updates
Data type
continue
3. Boolean In Python
It is used to store two possible values either true or
false
e.g.
str="comp sc"
boo=str.isupper() # test if string contains upper
case print(boo)
Output
False
Visit School Website for Regular Updates
Data type continue
4.List In Python
List are collections of items and each item has its own index value.
5. Tuple In Python
List and tuple, both are same except ,a list is mutable python objects and tuple
is immutable Python objects. Immutable Python objects mean you cannot modify
the contents of a tuple once it is assigned.
e.g. of list e.g. of
list =[6,9] tuple
list[0]=55 tup=(66,99)
print(list[0]) Tup[0]=3 #
print(list[1]) error
message
OUTPUT will be
55 displayed
9 print(tup[0]
)
Visit School Website for Regular Updates
Data type
continue
6. Set In Python
It is an unordered collection of unique and
immutable (which cannot be modified)items.
e.g.
set1={11,22,33,22
}
print(set1)
Output
{33, 11, 22}
Visit School Website for Regular Updates
Data type
7. Dictionary In Python
continue
It is an unordered collection of items and each
item consist of a key and a value.
e.g.
dict = {'Subject': 'comp sc', 'class': '11'}
print(dict)
print ("Subject : ", dict['Subject'])
print ("class : ", dict.get('class'))
Output
{'Subject': 'comp sc', 'class': '11'}
Subject : comp
sc class : 11
Visit School Website for Regular Updates
Operator
Operators are special symbols in Python that carry out arithmetic or logical
computation. The value that the operator operates on is called the
operand. Arithmetic operators
Used for mathematical operation
Operator Meaning Example
% Modulus - remainder of the division of left operand by the right x % y (remainder of x/y)
** Exponent - left operand raised to the power of right x**y (x to the power y)
# driver code
principal = 10000;
rate = 10;
time = 2;
emi = emi_calculator(principal, rate,
time); print("Monthly EMI is= ", emi)
Visit School Website for Regular Updates
Operator
continue
Arithmatic operator continue
How to calculate GST
GST ( Goods and Services Tax ) which is included in netprice of
product for get GST % first need to calculate GST Amount by subtract
original cost from Netprice and then apply
GST % formula = (GST_Amount*100) / original_cost
# Python3 Program to compute GST from original and net
prices. def Calculate_GST(org_cost, N_price):
# return value after calculate GST%
return (((N_price - org_cost) * 100) / org_cost);
# Driver program to test above
functions org_cost = 100
N_price = 120
print("GST =
",end='')
print(round(Calculate
_GST(org_cost,
N_price)),end='')Visit School Website for Regular Updates
Operator continue
Comparison operators -used to compare
values
Operator Meaning Example
> Greater that - True if left operand is greater than the right x>y
< Less that - True if left operand is less than the right x<y
>= Greater than or equal to - True if left operand is greater than or equal x >= y
to the right
<= Less than or equal to - True if left operand is less than or equal to the right x <= y
Output
('x > y is', False)
('x < y is', True)
('x == y is', False)
('x != y is', True)
('x >= y is', False)
('x <= y is', True) Visit School Website for Regular Updates
Operator
Logical continue
operators
Operator Meaning Example
e.g.
a=5
b = 10
list = [1, 2, 3, 4, 5 ]
if ( a in list ):
print ("Line 1 - a is available in the given list")
else:
print ("Line 1 - a is not available in the given list")
if ( b not in list ):
print ("Line 2 - b is not available in the given list") output
else: Line 1 - a is available in the given list
print ("Line 2 - b is available in the given list") Line 2 - b is not available in the given
list
Visit School Website for Regular Updates
Operator
Python Identity Operators continue
Operat Description
or
is Evaluates to true if the variables on either side of the operator point to the same
object and false otherwise.
is not Evaluates to false if the variables on either side of the operator point to the same
object and true otherwise.
e.g.
a = 10
b = 10
print ('Line 1','a=',a,':',id(a), 'b=',b,':',id(b))
if ( a is b ):
print ("Line 2 - a and b have same identity")
else:
print ("Line 2 - a and b do not have same identity")
OUTPUT
('Line 1', 'a=', 10, ':', 20839436, 'b=', 10, ':',
20839436)
Line 2 - a and b have same identity
Visit School Website for Regular Updates
Operator
table
Operator Description
continue
Operators Precedence :highest precedence to lowest precedence
ceil(n) It returns the smallest integer greater than or equal to n. math.ceil(4.2) returns 5
floor(n) It returns the largest integer less than or equal to n math.floor(4.2) returns 4
2. For Loop
x=1
while (x < 3):
print('inside while loop value of x is
',x) x = x + 1
else:
print('inside else value of x is ', x)
Output
inside while loop value of x
is 1 inside while loop value of
x is 2 inside else value of x is 5
*Write a program in python
Visit School Website for Regular Updates
Iteration Statements (Loops)
While Loop continue
Infinite While
Loop
e.g.
x=
5
whil
e
(x
==
5):
pri
nt(‘i
nsid Visit School Website for Regular Updates
Iteration Statements
(Loops)
2. For Loop
It is used to iterate over items of any sequence, such as a
list or a string.
Syntax
for val in sequence:
statements
e.g.
for i in range(3,5):
print(i)
Outp
ut 3
4
Visit School Website for Regular Updates
Iteration Statements
(Loops)
2. For Loop continue
Example
programs for i in
range(5,3,-1):
print(i)
Outp
ut 5
4
rang
e()
Functi
on Visit School Website for Regular Updates
Iteration Statements (Loops)
2. For Loop continue
For Loop With Else
e.g.
for i in range(1, 4):
print(i)
else: # Executed because no break
in for print("No Break")
Output
1
2
3
4
No Break Visit School Website for Regular Updates
Iteration Statements (Loops)
2. For Loop continue
Nested For Loop
e.g.
for i in range(1,3):
for j in
range(1,11):
k=i*j
print (k, end='
') print()
Output
123456789
10
Visit School Website for Regular Updates
Iteration Statements (Loops)
3. Jump Statements
Jump statements are used to
transfer the program's control from one location to
another. Means these are used to alter the flow of a loop
like - to skip a part of a loop or terminate a loop
print("The
end")
Output
s
Visit School Website for Regular Updates
Iteration Statements
2.continue (Loops)
It is used to skip all the remaining statements in the
loop and move controls back to the top of the loop.
e.g.
for val in
"init": if val
== "i":
continue
print(val)
print("The
end")
Output
n
t
The end
OUTP
UT
n
t
a
L
NOTE : continue forces the loop to start at the next
Visit School Website for Regular Updates