You are on page 1of 6

2/1/2021 Python for O&G Lecture 25 - Break, Continue keywords - Colaboratory

Python for Oil and Gas

Website - https://petroleumfromscratchin.wordpress.com/

LinkedIn - https://www.linkedin.com/company/petroleum-from-scratch

YouTube - https://www.youtube.com/channel/UC_lT10npISN5V32HDLAklsw

What exactly are keywords?

Python has a set of keywords that are reserved words that cannot be used as variable names, function names, or any other identi ers

# and, or, if, else, None, True, False, break, continue

# examples of keywords being used as variable name

if = 'scratch'

/
2/1/2021 Python for O&G Lecture 25 - Break, Continue keywords - Colaboratory

File "<ipython-input-6-53bbc456ec0f>", line 3


if = 'scratch'
^
SyntaxError: invalid syntax
break = 76

SEARCH STACK OVERFLOW


File "<ipython-input-7-b8e123c59438>", line 1
break = 76
^
SyntaxError: invalid syntax

SEARCH STACK OVERFLOW

continue = 45

File "<ipython-input-8-a46ee9151869>", line 1


continue = 45
^
SyntaxError: invalid syntax

SEARCH STACK OVERFLOW

and = 'String'

File "<ipython-input-9-6d8a0013eacd>", line 1


and = 'String'
^
SyntaxError: invalid syntax

SEARCH STACK OVERFLOW

And = 'string'

print(And)

string

break and coninue keyword is used when we want to stop at a certain point or want to skip something particular in a loop /
2/1/2021 Python for O&G Lecture 25 - Break, Continue keywords - Colaboratory

break keyword

for i in range(3, 10):


print(i)

3
4
5
6
7
8
9

# introduce break keyword

for i in range(3, 10):

print(i)
break

for i in range(3, 10):


print(i)

print('This is the random line')

3
4
5
6
7
8
9
This is the random line

for i in range(3, 10):

print(i)
break /
2/1/2021 Python for O&G Lecture 25 - Break, Continue keywords - Colaboratory

print('This is the random line')

3
This is the random line

# use of break keyword at specific value only

for i in range(3, 14):


if i == 3:
continue
print(i)

print('This is end of the loop')

4
5
6
7
8
9
10
11
12
13
This is end of the loop

Generally we use these break and continue statemnents when there is need of speci c conditions

# # break keyword in while loop

# while True:
# porosity = float(input('Enter a porosity value: '))
# if porosity < 0.6:
# print('Thanks for the input!')
# break
# else:
# print('You entered an unrealistic porosity value. Enter the value again!')

while True:
porosity = float(input('Enter a porosity value: ')) /
2/1/2021 Python for O&G Lecture 25 - Break, Continue keywords - Colaboratory

if porosity <= 0.4:


print('You have entered a realistic porosity value')
break
else:
print('Value of porosity is unrealistic')

Enter a porosity value: 31516513


Value of porosity is unrealistic
Enter a porosity value: 68465132
Value of porosity is unrealistic
Enter a porosity value: 0.9
Value of porosity is unrealistic
Enter a porosity value: 0.2
You have entered a realistic porosity value

/
2/1/2021 Python for O&G Lecture 25 - Break, Continue keywords - Colaboratory

You might also like