0% found this document useful (0 votes)
27 views

Python For Oil and Gas: Website - Linkedin - Youtube

The document discusses for loops in Python. It shows how to use for loops with the range() function to iterate over a sequence of numbers. Some key examples include summing the digits of a number input by the user using both a while loop and for loop. It also demonstrates using a for loop to iterate over each character in a string. Nested if/else statements are used within a for loop to check water saturation percentages.

Uploaded by

LIC Helper
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Python For Oil and Gas: Website - Linkedin - Youtube

The document discusses for loops in Python. It shows how to use for loops with the range() function to iterate over a sequence of numbers. Some key examples include summing the digits of a number input by the user using both a while loop and for loop. It also demonstrates using a for loop to iterate over each character in a string. Nested if/else statements are used within a for loop to check water saturation percentages.

Uploaded by

LIC Helper
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

1/30/2021 Python for O&G Lecture 20, 21, 22, 23, 24 - For loops - 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

# printing 1 to 5 using while loop

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

for loop using range function

Range Function

# printing 1 to 5 using for loop with the help of range function

for i in range(0, 5): # range(0, 5) - 0, 1, 2, 3, 4 , range(1, 5) - 1, 2,3, 4


print(i)

0
1
2
3
4

for i in range(0, 7):


print(i)

0
1
2
3
4
5
6

for i in range(1, 6):


print(i)

1
2
3 /
1/30/2021 Python for O&G Lecture 20, 21, 22, 23, 24 - For loops - Colaboratory
4
5

# another way to write range(0, 5)

# range(0, 5) - range(5)

for i in range(6):
print(i)

0
1
2
3
4
5

# You can print anything, not only values of i

for i in range(0, 5):


print('Petroleum From Scratch')

Petroleum From Scratch


Petroleum From Scratch
Petroleum From Scratch
Petroleum From Scratch
Petroleum From Scratch

# 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

for i in range(0, 5):


print(f'Petroleum From Scratch {i}')

Petroleum From Scratch 0


Petroleum From Scratch 1
Petroleum From Scratch 2
Petroleum From Scratch 3
Petroleum From Scratch 4

# range(1, 11) and range(0, 10) will give us same result

for i in range(1, 11):


print('Python')

Python
Python
Python
Python
Python
Python
Python
Python
Python
Python

for i in range(0, 10):


print('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

for i in range(1, 11):


sum = sum + i # 0 + 1, 1 + 2, 3 + 4....
# print(sum)

print(sum)
/
1/30/2021 Python for O&G Lecture 20, 21, 22, 23, 24 - For loops - Colaboratory

55

# WHILE LOOP: sum of n natural numbers

num = int(input('Enter any natural number: '))

total = 0
i = 0

while i <= num:


total = total + i
i = i + 1

print(total)

Enter any natural number: 100


5050

# FOR LOOP: Sum of n natural numbers

num = int(input('Enter any natural number: '))

addition = 0 # 0, 1

for i in range(1, num + 1 ): # 1, 2


addition = addition + i # 0+1, 1 + 2,
/
1/30/2021 Python for O&G Lecture 20, 21, 22, 23, 24 - For loops - Colaboratory

print(addition)

Enter any natural number: 100


5050

Assignment 8

# You have to do this problem 2 times.

# Once with a while loop and then another time with a for loop

# ask user any 4 digit pressure value in psi. Example: 2679

# What you have to do is, you have to sum the all digits of this pressure value. 2 + 6 + 7 + 9

# # 2679 -> string

# 2 + 6 + 7 + 9

# pressure[0] = '2'

# int(pressure[0]) = int('2') = 2 is an integer

# 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

# Assignment 8 using while loop

pressure = input('Please enter any pressure value in psi: ') # 1234 - string

x = 0 # index , 1
sum = 0

while x < len(pressure): # 4 -----> while x < 4:


sum = sum + int(pressure[x]) 0 + 1, 1 + 2,
x = x + 1

print(sum)

Please enter any pressure value in psi: 1234


10

# Assignment 8 using for loop

pressure = input('Please enter any pressure value in psi: ')

addition = 0

for i in range(0, len(pressure)): # i is representing index value


addition = addition + int(pressure[i])

print(addition)

Please enter any pressure value in psi: 111111


6

/
1/30/2021 Python for O&G Lecture 20, 21, 22, 23, 24 - For loops - Colaboratory

Nested If else inside for loop

sat_w = 10 # initial saturation of water in percentage

while sat_w<= 100:


if sat_w <= 50:
print(f'{sat_w} % water saturation is bearable')
else:
print(f'{sat_w} % of water saturation is unbearable')
sat_w = sat_w + 10

10 % water saturation is bearable


20 % water saturation is bearable
30 % water saturation is bearable
40 % water saturation is bearable
50 % water saturation is bearable
60 % of water saturation is unbearable
70 % of water saturation is unbearable
80 % of water saturation is unbearable
90 % of water saturation is unbearable
100 % of water saturation is unbearable

sat_w = 10

for i in range(sat_w, 101, 10):


if i<=50:
print(f'{i} % is bearable')
else:
print(f'{i} % becomes unbearable')

/
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!

We can also for loop with strings

# 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

Easier version of previous problem

/
# 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

pressure = input('Please enter any pressure value in psi: ')

addition = 0

for i in range(0, len(pressure)): # i is representing index value


addition = addition + int(pressure[i])

print(addition)

# easy method

pres = input('Pressure value is psi: ') # 1234 - string

add = 0

for i in pres: # 1234 '1', '2', '3', '4'


add = add + int(i)

print(add)

Pressure value is psi: 1234


10

/
1/30/2021 Python for O&G Lecture 20, 21, 22, 23, 24 - For loops - Colaboratory

You might also like