You are on page 1of 14

Class_12_Transfer_Statements_and_List

May 13, 2023

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.

[ ]: for loop: # Outer loop


for loop: # Inner loop
code
code

[4]: for i in range(3):


for j in range(2):
print('Hello')

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

[1]: for i in range(3):


print(i)

0
1
2

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


print(j)

1
0
1

[9]: for i in range(3):


for j in range(2):
print('i->',i)
print('j->',j)
print()

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

# Output- Each character should be printed seperately

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.

[22]: # for loop:


# break

[24]: for i in range(3):


print(i)
print('Hello')
break
print('Out of the loop')

0
Hello
Out of the loop

[25]: # for loop:


# for loop:
# break
# break

[28]: for i in range(4):


for j in range(3):
print('Python')
break

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

[31]: for i in range(4):


print(i)
for j in range(3):
print(j)
print('Python')
break

0
0
Python
1
Python
2
Python

[ ]: i=0
j=0- Python
j=1- Python
j=2- Python

[32]: for i in range(4):


for j in range(3):
print('Python')
break
break

Python

[35]: for i in range(4):


break
for j in range(3):
print('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)

File "C:\Users\Dell\AppData\Local\Temp\ipykernel_22888\1782947824.py", line 2


break
^
SyntaxError: 'break' outside loop

[39]: break

File "C:\Users\Dell\AppData\Local\Temp\ipykernel_22888\668683560.py", line 1


break
^
SyntaxError: 'break' outside loop

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]

# Print all the codes except None

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

[51]: for i in range(3):


continue
for j in range(2):
print('Python')
continue

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

[1, 2, 3, 4, 5] <class 'list'>

[55]: lst=['Python',True,None, False, (11,12,15),{1,2,3},10+20j, 2.5, 50]


print(lst,type(lst))

['Python', True, None, False, (11, 12, 15), {1, 2, 3}, (10+20j), 2.5, 50] <class
'list'>

[57]: lst=['Python','Python','Python',50,50, 10,10,10,10, 'Python']


print(lst,type(lst))

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

[60]: [10, 20, 30, 5, 5, 5]

[61]: lst

[61]: [10, 20, 30]

[62]: lst2

[62]: [5, 5, 5]

[63]: lst=[10,20,30]
lst2=[5,5,5]

lst=lst+lst2

[64]: lst

[64]: [10, 20, 30, 5, 5, 5]

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

TypeError: can only concatenate list (not "str") to list

[69]: lst=['This','is','Python']
# Concatenate 'class' to the above list

lst=lst+['class']
print(lst)

['This', 'is', 'Python', 'class']

[68]: lst

[68]: ['This', 'is', 'Python']

8
[66]: list('class')

[66]: ['c', 'l', 'a', 's', 's']

[73]: lst=[10,20,30,40]

# Concatenate 50 to the above list

lst=lst+[50]

[74]: lst

[74]: [10, 20, 30, 40, 50]

[76]: my_str='This is Python class'


# Output- ['class','Python','is','This']

# Approach- Convert to list and reverse the list

lst=[]
t=''

for i in my_str:
if i!=' ':
t+=i
else:
lst=[t]+lst
t=''
lst=[t]+lst
print(lst)

['class', 'Python', 'is', 'This']

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

['Python', 'Python', 'Python', 'Python', 'Python', 'Python', 'Python', 'Python',


'Python', 'Python', 'Python', 'Python', 'Python', 'Python', 'Python', 'Python',
'Python', 'Python', 'Python', 'Python', 'Python', 'Python', 'Python', 'Python',
'Python', 'Python', 'Python', 'Python', 'Python', 'Python', 'Python', 'Python',
'Python', 'Python', 'Python', 'Python', 'Python', 'Python', 'Python', 'Python',
'Python', 'Python', 'Python', 'Python', 'Python', 'Python', 'Python', 'Python',
'Python', 'Python']

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

[89]: lst=['Python', 'Python', 'Python', 'Python', 'Python', 'Python', 'Python',␣


↪'Python', 'Python', 'Python', 'Python', 'Python', 'Python', 'Python',␣

↪'Python', 'Python', 'Python', 'Python', 'Python', 'Python', 'Python',␣

↪'Python', 'Python', 'Python', 'Python', 'Python', 'Python', 'Python',␣

↪'Python', 'Python', 'Python', 'Python', 'Python', 'Python', 'Python',␣

↪'Python', 'Python', 'Python', 'Python', 'Python', 'Python', 'Python',␣

↪'Python', 'Python', 'Python', 'Python', 'Python', 'Python', 'Python',␣

↪'Python']

print(lst)

['Python', 'Python', 'Python', 'Python', 'Python', 'Python', 'Python', 'Python',


'Python', 'Python', 'Python', 'Python', 'Python', 'Python', 'Python', 'Python',
'Python', 'Python', 'Python', 'Python', 'Python', 'Python', 'Python', 'Python',
'Python', 'Python', 'Python', 'Python', 'Python', 'Python', 'Python', 'Python',
'Python', 'Python', 'Python', 'Python', 'Python', 'Python', 'Python', 'Python',
'Python', 'Python', 'Python', 'Python', 'Python', 'Python', 'Python', 'Python',
'Python', 'Python']

[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

[91]: {10, 20, 30, 40, 50}

[92]: s[0]

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_22888\243613605.py in <module>
----> 1 s[0]

TypeError: 'set' object is not subscriptable

[94]: for i in range(len(s)):


print(s[i])

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

TypeError: 'set' object is not subscriptable

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

# Fetch the first element from the above list.

print(lst[0])
print(lst[-4])
print(lst[-len(lst)])

# Fetch the last element from the above list.

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]

# Fetch 200 from the above list

lst[3][1]

[107]: 200

[128]: lst=[10,20,30,[100,[200,300],400],40,50,60]
print(len(lst))

# Fetch 300 from the above list

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

IndexError: list index out of range

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

IndexError: list index out of range

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

[136]: # 2-D list


# 3-D list

13
# or
# Array (Numpy)

[137]: a=10
b=20
c=a/b

[ ]:

14

You might also like