Python For Oil and Gas: Website - Linkedin - Youtube
Python For Oil and Gas: Website - Linkedin - Youtube
Website - https://petroleumfromscratchin.wordpress.com/
LinkedIn - https://www.linkedin.com/company/petroleum-from-scratch
YouTube - https://www.youtube.com/channel/UC_lT10npISN5V32HDLAklsw
c = 1
while c<= 5:
print(c)
c = c + 1
1
2
3
4
5
/
1/30/2021 Python for O&G Lecture 20, 21, 22, 23, 24 - For loops - Colaboratory
Range Function
0
1
2
3
4
0
1
2
3
4
5
6
1
2
3 /
1/30/2021 Python for O&G Lecture 20, 21, 22, 23, 24 - For loops - Colaboratory
4
5
# range(0, 5) - range(5)
for i in range(6):
print(i)
0
1
2
3
4
5
# just include the values of i in previous code. It will give you a much better idea how for loop is working
/
1/30/2021 Python for O&G Lecture 20, 21, 22, 23, 24 - For loops - Colaboratory
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Examples
/
1/30/2021 Python for O&G Lecture 20, 21, 22, 23, 24 - For loops - Colaboratory
# While loop
total = 0
c = 0
while c<=10:
total = total + c
# print(total)
c = c + 1
print(total)
55
# for loop
sum = 0 # 1, 3
print(sum)
/
1/30/2021 Python for O&G Lecture 20, 21, 22, 23, 24 - For loops - Colaboratory
55
total = 0
i = 0
print(total)
addition = 0 # 0, 1
print(addition)
Assignment 8
# Once with a while loop and then another time with a for loop
# What you have to do is, you have to sum the all digits of this pressure value. 2 + 6 + 7 + 9
# 2 + 6 + 7 + 9
# pressure[0] = '2'
# pressure[1] = '6'
/
i t( [ ]) i t(' ')
1/30/2021 Python for O&G Lecture 20, 21, 22, 23, 24 - For loops - Colaboratory
# int(pressure[1]) = int('6') 6
pressure = input('Please enter any pressure value in psi: ') # 1234 - string
x = 0 # index , 1
sum = 0
print(sum)
addition = 0
print(addition)
/
1/30/2021 Python for O&G Lecture 20, 21, 22, 23, 24 - For loops - Colaboratory
sat_w = 10
/
1/30/2021 Python for O&G Lecture 20, 21, 22, 23, 24 - For loops - Colaboratory
10 % is bearable
20 % is bearable
30 % is bearable
40 % is bearable
50 % is bearable
60 % becomes unbearable
70 % becomes unbearable
80 % becomes unbearable
90 % becomes unbearable
100 % becomes unbearable
Til now we have seen how to use for loops with range functions!
# Example:
var = 'Divyansh'
for i in 'Divyansh':
print(i)
D
i
v
/
1/30/2021 Python for O&G Lecture 20, 21, 22, 23, 24 - For loops - Colaboratory
y
a
n
s
h
# easy method
for i in var:
print(f'Python {i}')
Python D
Python i
Python v
Python y
Python a
Python n
Python s
Python h
/
# Assignment 8 using for loop
1/30/2021 Python for O&G Lecture 20, 21, 22, 23, 24 - For loops - Colaboratory
# Assignment 8 using for loop
addition = 0
print(addition)
# easy method
add = 0
print(add)
/
1/30/2021 Python for O&G Lecture 20, 21, 22, 23, 24 - For loops - Colaboratory