Professional Documents
Culture Documents
BA11 Notebook
BA11 Notebook
https://pythontutor.com/visualize.html (https://pythontutor.com/visualize.html)
break
The break statement ends the current loop and jumps to the statement immediately following the loop
It is like a loop test that can happen anywhere in the body of the loop.
In [2]:
for i in range(10):
if i > 5:
break
print(i)
0
1
2
3
4
5
In [15]:
for i in range(10):
if i == 5:
continue
print(i)
0
1
2
3
4
6
7
8
9
In [ ]:
n = int(input("input number"))
flag = 0
for i in range(2,n):
if n % i == 0:
flag = 1
break
if flag == 0:
print('prime number')
else:
print('not prime')
localhost:8889/notebooks/Term4S/scripts/2A/BA11-Notebook.ipynb 1/7
2/20/23, 1:47 PM BA11-Notebook - Jupyter Notebook
In [ ]:
Q. Write a program using while loop that terminate with specific number/passwor
d input.
Q. Write a program using while loop that terminate with valid pincode
In [4]:
while True:
n = int(input("Enter Passcode"))
if n == 12345:
print("logged in")
break
Enter Passcode12345
logged in
In [7]:
n = int(input("Enter Passcode"))
while True:
if n == 12345:
print("logged in")
break
n = int(input("Enter Correct Passcode"))
Enter Passcode123
Enter Correct Passcode12345
logged in
In [12]:
len('123456')
Out[12]:
In [14]:
n = input("Enter Pincode")
while True:
if len(n) == 6:
print("Correct Pincode")
break
n = int(input("Enter Correct Pincode"))
Enter Pincode123456
Correct Pincode
localhost:8889/notebooks/Term4S/scripts/2A/BA11-Notebook.ipynb 2/7
2/20/23, 1:47 PM BA11-Notebook - Jupyter Notebook
In [37]:
n=1
while n<=10:
print(n)
if n == 5:
break
n+=1
1
2
3
4
5
In [38]:
while True:
password = int(input("enter password"))
if password == 12345:
print("successfully login")
break
enter password123
enter password12345
successfully login
In [39]:
while True:
pincode = input('enter pincode')
if 6 == len(pincode) :
break
print('Please enter valid pincode')
print('Success!')
enter pincode123456
Success!
continue
With the continue statement we can stop the current iteration of the loop, and continue with the next:
localhost:8889/notebooks/Term4S/scripts/2A/BA11-Notebook.ipynb 3/7
2/20/23, 1:47 PM BA11-Notebook - Jupyter Notebook
In [40]:
n=0
while n<=10:
n+=1
if n==5:
continue
print(n)
1
2
3
4
6
7
8
9
10
11
In [41]:
n=0
while n<=10:
n+=1
if (n==5) | (n==8) | (n==2) :
continue
print(n)
1
3
4
6
7
9
10
11
Nested Loop
In [ ]:
localhost:8889/notebooks/Term4S/scripts/2A/BA11-Notebook.ipynb 4/7
2/20/23, 1:47 PM BA11-Notebook - Jupyter Notebook
In [16]:
for i in range(11):
print('i = ', i)
for j in range(6):
print('j = ',j)
localhost:8889/notebooks/Term4S/scripts/2A/BA11-Notebook.ipynb 5/7
2/20/23, 1:47 PM BA11-Notebook - Jupyter Notebook
i = 0
j = 0
j = 1
j = 2
j = 3
j = 4
j = 5
i = 1
j = 0
j = 1
j = 2
j = 3
j = 4
j = 5
i = 2
j = 0
j = 1
j = 2
j = 3
j = 4
j = 5
i = 3
j = 0
j = 1
j = 2
j = 3
j = 4
j = 5
i = 4
j = 0
j = 1
j = 2
j = 3
j = 4
j = 5
i = 5
j = 0
j = 1
j = 2
j = 3
j = 4
j = 5
i = 6
j = 0
j = 1
j = 2
j = 3
j = 4
j = 5
i = 7
j = 0
j = 1
j = 2
j = 3
j = 4
j = 5
i = 8
j = 0
j = 1
j = 2
j = 3
localhost:8889/notebooks/Term4S/scripts/2A/BA11-Notebook.ipynb 6/7
2/20/23, 1:47 PM BA11-Notebook - Jupyter Notebook
j = 4
j = 5
i = 9
j = 0
j = 1
j = 2
j = 3
j = 4
j = 5
i = 10
j = 0
j = 1
j = 2
j = 3
j = 4
j = 5
In [19]:
for i in range(5):
for j in range(5):
print("({}, {} )".format(i,j), end=' ')
print(" ")
In [ ]:
In [ ]:
localhost:8889/notebooks/Term4S/scripts/2A/BA11-Notebook.ipynb 7/7