0% found this document useful (0 votes)
79 views16 pages

Class 10 Loop

The document discusses nested if-else statements and examples of using nested if-else statements to check student marks. It also discusses loops in Python including for loops and iterating over different data types like lists, tuples, dictionaries, sets and ranges. Examples are provided to add elements in a list using a for loop, iterate over dictionary keys and values, and reverse a string using a for loop.

Uploaded by

TheAncient01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views16 pages

Class 10 Loop

The document discusses nested if-else statements and examples of using nested if-else statements to check student marks. It also discusses loops in Python including for loops and iterating over different data types like lists, tuples, dictionaries, sets and ranges. Examples are provided to add elements in a list using a for loop, iterate over dictionary keys and values, and reverse a string using a for loop.

Uploaded by

TheAncient01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Class_10_Loop

May 13, 2023

1 Nested if….else statement


When we have an if statement with in another if statement then it is known as nested if statement.

[ ]: 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.

[5]: math=int(input('Enter the marks in Maths- '))


eng=int(input('Enter the marks in English- '))
sci=int(input('Enter the marks in Science- '))

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')

Enter the marks in Maths- 50


Enter the marks in English- 50

1
Enter the marks in Science- 50
Try again in Maths

[6]: # math=int(input('Enter the marks in Maths- '))


# eng=int(input('Enter the marks in English- '))
# sci=int(input('Enter the marks in Science- '))

# 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

[16]: # decimal- int()


# Binary- bin()
# Octal- oct()
# hexadecimal- hex()

[18]: a=50 #(Octal value)


# Convert it into int

int(0o50)

[18]: 40

[25]: a=0b101010101 # binary


# Convert to decimal
int(a)

[25]: 341

[26]: # Convert the above binary to octal


oct(a)

[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

not Logical NOT


and Logical AND
or Logical OR

[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]: ~-10 # ~num=-(num+1)

[45]: 9

[46]: 29<<2

[46]: 116

[48]: 20&116

[48]: 20

[50]: bin(20),bin(116)

[50]: ('0b10100', '0b1110100')

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.

[55]: # Print Hello 5 times


print('Hello')
print('Hello')
print('Hello')
print('Hello')
print('Hello')

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 variable in iterable:


code_1
code_2
code_3

[66]: # Iterables- String, List, Tuple, Dictionary, Set, Frozenset, Range

[68]: for i in iterable:


code
# # i will point out each element of the iterable one by one.

[70]: my_str='Python class'

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.

[75]: t=(True, 'Python', {1,2,3}, 50, 10+20j)

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))

{1: 100, 2: 200, 3: 300, 4: 400} <class 'dict'>

[77]: for i in d:
print(i)

1
2
3
4

[79]: for i in d.values():


print(i)

100
200
300
400

[81]: emp={'name':'Raghvendra','age':26,'city':'Mumbai','company':'TCS'}
print(emp,type(emp))

{'name': 'Raghvendra', 'age': 26, 'city': 'Mumbai', 'company': 'TCS'} <class


'dict'>

[82]: for i in emp:


print(i)

name
age
city
company

7
[83]: for i in emp.values():
print(i)

Raghvendra
26
Mumbai
TCS

[87]: for i in emp.items():


print(i)

('name', 'Raghvendra')
('age', 26)
('city', 'Mumbai')
('company', 'TCS')

[86]: s={10,20,30,40,50}
print(s)
for i in s:
print(i)

{50, 20, 40, 10, 30}


50
20
40
10
30

[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

[91]: for i in range(1,5):


print(i)

1
2
3
4

8
[92]: for i in range(10):
print(i)

0
1
2
3
4
5
6
7
8
9

[95]: for i in 10.5+2j:


print(i)

---------------------------------------------------------------------------
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)

TypeError: 'complex' object is not iterable

[98]: for i in range(5):


print('Hello')
print('Thanks')

Hello
Hello
Hello
Hello
Hello
Thanks

[2]: for i in range(2):


print('Hello')
print('I am from HBD')
print()

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

[110]: # 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)

550

[111]: my_str='Python' # [0,1,2,3,4,5]

# Print all the indexes of the above string

10
[114]: for i in range(6):
print(i)

0
1
2
3
4
5

[113]: len(my_str)

[113]: 6

[118]: for i in range(len(my_str)):


print(i,my_str[i])

0 P
1 y
2 t
3 h
4 o
5 n

[116]: my_str[0]

[116]: 'P'

[146]: my_str='Python' # [5,4,3,2,1,0]

# Reverse the above string using loop

for i in range(5,-1,-1):
print(i,my_str[i])

5 n
4 o
3 h
2 t
1 y
0 P

[132]: for i in range(len(my_str)-1,-1,-1):


print(i,my_str[i])

5 n
4 o
3 h
2 t

11
1 y
0 P

[135]: my_str='Python' # [-1,-2,-3,-4,-5,-6]

# Reverse the above string using loop


for i in range(-1,-7,-1):
print(i,my_str[i])

-1 n
-2 o
-3 h
-4 t
-5 y
-6 P

[143]: for i in range(-1,-len(my_str)-1,-1):


print(i,my_str[i])

-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]

# Print every alternate element of the list using loop

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

[157]: my_str='Peter Piper picked a pack of pickeled peppers'

# Count all the P


c=0

for i in my_str:
if i=='P':
c+=1 # c=c+1
print(c)

[161]: my_str='Peter Piper picked a pack of pickeled peppers'

# Count all the p

c=0

for i in my_str:
if i=='p':
c+=1
print(c)

[162]: my_str='Peter Piper picked a pack of pickeled peppers'

# Count all the p and P


c=0
for i in my_str:
if i=='p' or i=='P':
c+=1
print(c)

[168]: my_str='Peter Piper picked a pack of pickeled peppers'

# Get the indexes of all the P's

for i in range(len(my_str)):
if my_str[i]=='P':
print(i)

13
0
6

[175]: my_str='Peter Piper picked a pack of pickeled peppers'

# Get the indexes of all the P and p

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

[187]: for i in range(len(my_str)):


if my_str[i]=='P' or my_str[i]=='p':
print(i)

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

[1]: # Maths=int(input('Enter the marks in Maths- '))


# Eng=int(input('Enter the marks in English- '))
# Sci=int(input('Enter the marks in Science - '))

# 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

You might also like