Class 10 Loop
Class 10 Loop
[ ]: if condition:
code_1
if condition:
code_2
if condition:
code_3
else:
code_4
else:
code_5
else:
code_6
[1]: # If a student scores 90 or more than 90 marks in all the three subjects␣
↪(Maths, English and Science) then it should say well done.
# If the students scores < 90 marks in any subject then it should say try again.
if math>=90:
if eng>=90:
if sci>=90:
print('Well done')
else:
print('Try again in Science')
else:
print('Try again in English')
else:
print('Try again in Maths')
1
Enter the marks in Science- 50
Try again in Maths
# mm=100
# # Calculate the percentage
# First division if the percentage is 60 or more than 60
# Second division if the percentage is between 45 and 59
# Third division if the percentage is between 33 and 44
# Fail if the percentage is less than 33
[14]: # Take an alphabet from the user and check whether it is a vowel or consonant
vowels='AEIOUaeiou'
a='z'
if a.isalpha():
if a in vowels:
print('Vowel')
else:
print('Consonant')
else:
print('The character is not an alphabet')
Consonant
[12]: 'i'.isalpha()
[12]: True
2 Number System
[17]: # Decimal- 0-9 - Base=10
# Binary- 0,1 - Base=2
# Octal- 0-7 - Base=8
# Hexadecimal- 0-15 - Base= 16
# Hexadecimal- 0-9
# 10- A
# 11- B
# 12- C
# 13- D
# 14- E
# 15- F
2
# 0b- Binary representation
# 0o- Octal representation
# 0x- Hexadecimal representation
int(0o50)
[18]: 40
[25]: 341
[26]: '0o525'
[27]: hex(a)
[27]: '0x155'
[33]: int(0xF)
[33]: 15
[38]: int(0xabcadfe1516326154)
[38]: 198063242953717080404
[36]: ord('A')
[36]: 65
3 Operator Precedence
[39]: 50 and 60+20*9/2**3
3
[39]: 82.5
[ ]: () Parentheses
** Exponent
+x, -x, ~x Unary plus, Unary minus, Bitwise NOT
*, /, //, % Multiplication, Division, Floor division, Modulus
+, - Addition, Subtraction
<<, >> Bitwise shift operators
& Bitwise AND
^ Bitwise XOR
| Bitwise OR
==, !=, >, >=, <, <=, is, is not, in, not in Comparisons, Identity,␣
↪Membership operators
[53]: 10+(10*32)//2**5&20+(~(-10))<<2
[53]: 20
[47]: (10+320//2**5&20+(9)<<2)
(10+320//32&20+9<<2)
(10+10&20+9<<2)
20&29<<2
20&116
[47]: 20
[45]: 9
[46]: 29<<2
[46]: 116
[48]: 20&116
[48]: 20
[50]: bin(20),bin(116)
4
[51]: # 0010100
# 1110100
# 0010100
[52]: int(0b10100)
[52]: 20
4 Loop
• Loop is a concept that we use to execute a of code repeteadly.
Hello
Hello
Hello
Hello
Hello
[58]: print('Hello\n'*5)
Hello
Hello
Hello
Hello
Hello
[65]: my_str='Python'
print(my_str[0])
print(my_str[1])
print(my_str[2])
print(my_str[3])
print(my_str[4])
print(my_str[5])
P
y
t
h
o
n
5
5 Types
• for loop
• while loop
6 for loop
• It is always applied on an iterable.
for i in my_str:
print(i)
P
y
t
h
o
n
c
l
a
s
s
[74]: lst=[10,20,30,40,50,60]
for x in lst:
print(x)
10
20
30
40
50
60
6
7 Iteration
• Traversing upon the elements of an iterable from the beginning to the end is known as
iteration.
• The number of iterations are equal to the number of elements in the iterable.
for i in t:
print(i)
True
Python
{1, 2, 3}
50
(10+20j)
[76]: d={1:100,2:200,3:300,4:400}
print(d,type(d))
[77]: for i in d:
print(i)
1
2
3
4
100
200
300
400
[81]: emp={'name':'Raghvendra','age':26,'city':'Mumbai','company':'TCS'}
print(emp,type(emp))
name
age
city
company
7
[83]: for i in emp.values():
print(i)
Raghvendra
26
Mumbai
TCS
('name', 'Raghvendra')
('age', 26)
('city', 'Mumbai')
('company', 'TCS')
[86]: s={10,20,30,40,50}
print(s)
for i in s:
print(i)
[89]: f=frozenset([1,2,3,4,5,6])
print(f)
for i in f:
print(i)
frozenset({1, 2, 3, 4, 5, 6})
1
2
3
4
5
6
1
2
3
4
8
[92]: for i in range(10):
print(i)
0
1
2
3
4
5
6
7
8
9
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_580\472501201.py in <module>
----> 1 for i in 10.5+2j:
2 print(i)
Hello
Hello
Hello
Hello
Hello
Thanks
Hello
I am from HBD
Hello
I am from HBD
9
[108]: # Add all the elements from the following list
lst=[10,20,30,40,50,60,70,80,90,100]
s=0
for i in lst:
s=s+i
print(s)
10
30
60
100
150
210
280
360
450
550
[109]: # s=0
# i=10
# s=0+10=10
# s=10
# i=20
# s=10+20=30
# s=30
# i=30
# s=30+30=60
# s=60
# i=40
# s=60+40=100
lst=[10,20,30,40,50,60,70,80,90,100]
s=0
for i in lst:
s=s+i
print(s)
550
10
[114]: for i in range(6):
print(i)
0
1
2
3
4
5
[113]: len(my_str)
[113]: 6
0 P
1 y
2 t
3 h
4 o
5 n
[116]: my_str[0]
[116]: 'P'
for i in range(5,-1,-1):
print(i,my_str[i])
5 n
4 o
3 h
2 t
1 y
0 P
5 n
4 o
3 h
2 t
11
1 y
0 P
-1 n
-2 o
-3 h
-4 t
-5 y
-6 P
-1 n
-2 o
-3 h
-4 t
-5 y
-6 P
[144]: -(len(my_str)+1)
[144]: -7
[151]: lst=[10,20,30,40,50,60,70,80]
for i in range(0,len(lst),2):
print(lst[i])
10
30
50
70
[154]: lst=[10,20,30,40,50,60,70,80]
# Print every alternate element in reverse order of the list using loop
# Output- [80,60,40,20]
for i in range(len(lst)-1,-1,-2):
print(lst[i])
12
80
60
40
20
for i in my_str:
if i=='P':
c+=1 # c=c+1
print(c)
c=0
for i in my_str:
if i=='p':
c+=1
print(c)
for i in range(len(my_str)):
if my_str[i]=='P':
print(i)
13
0
6
for i in range(len(my_str)):
print(i)
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
14
38
39
40
41
42
43
44
0
6
8
12
21
29
38
40
41
[177]: len(my_str)
[177]: 45
[180]: my_str[44]
[180]: 's'
[188]: # Loop
# While loop
[189]: # w3schools.com
# if Maths>=90:
# if Eng>=90:
# if Sci>=90:
# print('Well Done')
# else:
# print('Try again in Science')
# else:
# print('Try again in English')
# else:
15
# print('Try again in Maths')
[ ]:
16