Loops
Loops
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.
Data type
continue
Data Types In Python
1. Number
2. String
3. Boolean
4. List
5. Tuple
6. Set
7. Dictionary
Data type continue
1. Number In Python
It is used to store numeric values
Output :
- 301.4
121.0
Data type
3. Complex numbers continue
Complex numbers are combination of a
real and imaginary part.Complex numbers are in the
form of X+Yj, where X is a real part and Y is imaginary
part. e.g.
a = complex(5) # convert 5 to a real part val and zero imaginary part
print(a)
b=complex(101,23) #convert 101 with real part and 23 as imaginary
part
print(b)Run Code
O
utput :-
(5+0j)
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
Data type
Iterating through continue
string
e.g.
str='comp sc'
for i in str:
print(i)
Out
put c
o
m
p
s
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
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]
)
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}
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
Operat
Operators are special symbols in Python that carry out arithmetic or logical
or
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)
Operator
continue
Arithmatic operator
continue e.g.
x=5
y=4
print('x + y =',x+y)
print('x - y =',x-y)
print('x * y =',x*y)
print('x / y =',x/y)
print('x // y =',x//y)
print('x ** y
=',x**y) OUTPUT
('x + y =', 9)
('x - y =', 1)
('x * y =', 20) • Write a program in python to calculate the
('x / y =', 1) simple interest based on entered amount ,rate
('x // y =', 1) and time
('x ** y =', 625)
Operator
Arithmatic operator continue continue
# EMI Calculator program in
# driver code
principal = 10000;
rate = 10;
time = 2;
emi = emi_calculator(principal, rate,
time); print("Monthly EMI is= ", emi)
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='')
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
Operator
Comparison operators continue
continue
e.g.
x = 101
y = 121
print('x > y is',x>y)
print('x < y is',x<y)
print('x == y is',x==y)
print('x != y is',x!=y)
print('x >= y is',x>=y)
print('x <= y is',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)
Operator
Logical continue
operators
Operator Meaning Example
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)
Visit School Website for Regular
Line 2 - a and b have same identity
Updates
Operator
table
Operator Description
continue
Operators Precedence :highest precedence to lowest precedence
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 pogram in python
Iteration Statements (Loops)
Outp
ut 3
4
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
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)
Output
123456789
10
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
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
Iteration Statements
(Loops)
3. pass Statement
This statement does nothing. It can be used when a statement
is
required syntactically but the program requires no action.
Use in loop
while True:
pass # Busy-wait for keyboard interrupt (Ctrl+C)
In function
It makes a controller to pass by without executing any
code. e.g.
def myfun():
pass #if we don’t use pass here then error message
will be shown
print(‘my program')
OUTPUT
Iteration Statements
3. pass Statement (Loops)
continue e.g.
for i in 'initial':
if(i ==
'i'):
pass
else:
print(i
)
OUTP
UT
n
t
a
L
NOTE : continue forces the loop to start at the next