Professional Documents
Culture Documents
Class 12 Transfer Statements and List
Class 12 Transfer Statements and List
1 Nested loop
• A loop within another loop is known as nested loop.
• Inner loop completes all its iterations for iteration of th outer loop.
Hello
Hello
Hello
Hello
Hello
Hello
[5]: # i=0
# j=0, j=1
# i=1
# j=0, j=1
# i=2
# j=0, j=1
0
1
2
1
0
1
i-> 0
j-> 0
i-> 0
j-> 1
i-> 1
j-> 0
i-> 1
j-> 1
i-> 2
j-> 0
i-> 2
j-> 1
[14]: lst=['This','is','Python','class']
for i in lst:
for j in i:
print(j)
T
h
i
s
i
s
P
y
t
h
o
n
c
2
l
a
s
s
[19]: i='This'
for j in i:
print(j)
i='is'
for j in i:
print(j)
i='Python'
for j in i:
print(j)
i='class'
for j in i:
print(j)
T
h
i
s
i
s
P
y
t
h
o
n
c
l
a
s
s
[21]: i_c=0
j_c=0
for i in range(4):
i_c+=1
for j in range(2):
j_c+=1
print(i_c)
print(j_c)
3
8
2 Transfer Statements
3 break, continue
4 break
• It is used to break or terminate a loop (for or while).
• As soon as break statement gets encountered the control comes out of the loop.
0
Hello
Out of the loop
Python
Python
Python
Python
[27]: # i=0
# j=0- Python
# i=1
# j=0- Python
# i=2
# j=0- Python
4
# i=3
# j=0- Python
0
0
Python
1
Python
2
Python
[ ]: i=0
j=0- Python
j=1- Python
j=2- Python
Python
[ ]: i=0
j=0- Python
[37]: lst=[101,102,103,104,None,105,106,107,108]
for i in lst:
if i==None:
break
else:
print(i)
101
102
5
103
104
[38]: if i==None:
break
else:
print(i)
[39]: break
5 continue
• It is used to skip the current iteration of the loop.
• It does not terminate the loop. It only skips the current iteration.
• As soon as continue statement get exeuted the control goes to the next iteration.
[1]: lst=[101,102,None,105,106,107,108]
for i in lst:
if i==None:
print('Skip the iteration')
continue
print('Hello')
print('Thanks')
else:
print(i)
101
102
Skip the iteration
105
106
6
107
108
6 List
[56]: # Anything inside the [] is a list.
# A list is a container type that can contain multiple elements.
# A list can contain heterogenous (different type) elements.
# A list can contain duplicate elements too.
[53]: lst=[1,2,3,4,5]
print(lst,type(lst))
['Python', True, None, False, (11, 12, 15), {1, 2, 3}, (10+20j), 2.5, 50] <class
'list'>
['Python', 'Python', 'Python', 50, 50, 10, 10, 10, 10, 'Python'] <class 'list'>
7 Operations on a list
[58]: # Concatenation
# Repetition
# Length
# Indexing and slicing
8 Concatenation
[59]: # - Joining two or more lists together is known as concatenation.
# - + operator is used to concatenate two lists.
# - **Note-** Only a list can be concatenated with another list.
[60]: lst=[10,20,30]
lst2=[5,5,5]
7
lst+lst2
[61]: lst
[62]: lst2
[62]: [5, 5, 5]
[63]: lst=[10,20,30]
lst2=[5,5,5]
lst=lst+lst2
[64]: lst
[65]: lst=['This','is','Python']
# Concatenate 'class' to the above list
lst+'class'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_22888\3933868034.py in <module>
2 # Concatenate 'class' to the above list
3
----> 4 lst+'class'
[69]: lst=['This','is','Python']
# Concatenate 'class' to the above list
lst=lst+['class']
print(lst)
[68]: lst
8
[66]: list('class')
[73]: lst=[10,20,30,40]
lst=lst+[50]
[74]: lst
lst=[]
t=''
for i in my_str:
if i!=' ':
t+=i
else:
lst=[t]+lst
t=''
lst=[t]+lst
print(lst)
[78]: # lst=[]
# t='This'
# lst=[t]+lst
# lst=['This']
# t='is'
# lst=[t]+lst
# ['is']+['This']
9 Repetition
[79]: # * operator is used to perform the repetition operation on a list.
9
[80]: # lst*number of repetitions
[85]: lst=['Python']
print(lst*50)
10 Lngth
• The number of elements in a list is the length of the list.
[86]: # len(lst)
[88]: lst=[10,10,30,40,'Python','Hello',78]
print(len(lst))
↪'Python']
print(lst)
[90]: print(len(lst))
50
10
11 Indexing
• A list is an ordered collection of data because each element in the list is stored at a particular
index.
• A list is also known as sequence type because it follows indexing method.
• It follows +ive and -ive indexing both.
[91]: s={10,20,30,40,50}
s
[92]: s[0]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_22888\243613605.py in <module>
----> 1 s[0]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_22888\2542452953.py in <module>
1 for i in range(len(s)):
----> 2 print(s[i])
12 +ive indexing
• It starts from left to right.
• It starts from 0 by default.
• The index of the last element len(lst)-1
13 -ive indexing
• It is from right to left.
• Iy starts from -1 by default.
• The index of the last element is -len(lst)
11
[104]: lst=['This','is','Python','class']
print(lst[0])
print(lst[-4])
print(lst[-len(lst)])
print(lst[-1])
print(lst[3])
print(lst[len(lst)-1])
This
This
This
class
class
class
[98]: -len(lst)
[98]: -4
[103]: len(lst)-1
[103]: 3
[107]: lst=[10,20,30,[100,200,300,400],40,50,60]
lst[3][1]
[107]: 200
[128]: lst=[10,20,30,[100,[200,300],400],40,50,60]
print(len(lst))
print(lst[3][1][1])
7
300
12
[133]: lst[-4][-2][-1]
[133]: 300
[119]: lst=[10,20,30,40,50]
print(lst[len(lst)])
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_22888\1629920518.py in <module>
1 lst=[10,20,30,40,50]
2
----> 3 print(lst[len(lst)])
[120]: len(lst)
[120]: 5
[121]: lst[5]
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_22888\370352303.py in <module>
----> 1 lst[5]
[122]: len(lst)-1
[122]: 4
[123]: lst[4]
[123]: 50
[126]: lst[len(lst)-1]
[126]: 50
[134]: # [[[[[1215]5,1,5,1,5,5],5,5,6,7,8,9],6,4,7,9]]
13
# or
# Array (Numpy)
[137]: a=10
b=20
c=a/b
[ ]:
14