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

Python From Scratch: Website - Linkedin - Youtube - Logical Conditions

This document discusses Python logical conditions and control flow statements like if/else. It provides examples of using logical operators (> ,<, ==, etc.), if/else statements, pass statements, and in/or operators to check conditions and execute code blocks conditionally. It also demonstrates getting user input and checking for empty strings.

Uploaded by

LIC Helper
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Python From Scratch: Website - Linkedin - Youtube - Logical Conditions

This document discusses Python logical conditions and control flow statements like if/else. It provides examples of using logical operators (> ,<, ==, etc.), if/else statements, pass statements, and in/or operators to check conditions and execute code blocks conditionally. It also demonstrates getting user input and checking for empty strings.

Uploaded by

LIC Helper
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1/30/2021 Python for O&G Lecture 14th and15th - If else statements, in, and/or operators - Colaboratory

Python From Scratch

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

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

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

Logical Conditions

# a > b
# a < b
# a == b
# a != b
# a <= b
# a >= b

# First let us see the difference between = and ==

# = is used to assign the values


# == is used to check equality. Hence, it will alwayas return the boolean value True or False /
1/30/2021 Python for O&G Lecture 14th and15th - If else statements, in, and/or operators - Colaboratory

# examples of values assigned

a = 5
b= 5
c =8

print(a)
print(c)

5
8

print(a == b) # This is asking a question whether a and b has same value or not

True

print(a != b)

False

# more examples of logical conditions

print(a>=c)

False

print(c>=a)

True

If statements

exp_res_press = 3000 # psi

act_res_press = input('What is the actual reservoir pressure? ')


/
1/30/2021 Python for O&G Lecture 14th and15th - If else statements, in, and/or operators - Colaboratory

if act_res_press > exp_res_press: # If statement


print('This is abnormal pressure.') # If block

What is the actual reservoir pressure? 5000


---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-f01abb0a001d> in <module>()
1 act_res_press = input('What is the actual reservoir pressure? ')
2
----> 3 if act_res_press > exp_res_press: # If statement
4 print('This is abnormal pressure.') # If block

TypeError: '>' not supported between instances of 'str' and 'int'

SEARCH STACK OVERFLOW

# method 1

act_res_press = int(input('What is the actual reservoir pressure? '))

if act_res_press > exp_res_press: # If statement


print('This is abnormal pressure.') # If block

What is the actual reservoir pressure? 5000


This is abnormal pressure.

# Method 2

act_res_press = input('What is the actual reservoir pressure? ')

if int(act_res_press) > exp_res_press: # If statement


print('This is abnormal pressure.') # If block

What is the actual reservoir pressure? 3450


This is abnormal pressure.

# You can add multiple lines in if block

act_res_press = int(input('What is the actual reservoir pressure? '))

if act_res_press > exp_res_press: /


i t('R i i t th th t d i ')
1/30/2021 Python for O&G Lecture 14th and15th - If else statements, in, and/or operators - Colaboratory
print('Reservoir pressure is greater than the expected resevoir pressure')
print('This is abnormal pressure.') # If block

What is the actual reservoir pressure? 3658


Reservoir pressure is greater than the expected resevoir pressure
This is abnormal pressure.

Pass statement

x = 56
if x>56:

# let's say that i know that I want to use this conditon but I'm not sure what I want to print right now.
# If you leave it empty. It will give an error
# Hence, we use pass statement

File "<ipython-input-15-4002fef93e49>", line 6


# Hence, we use pass statement
^
SyntaxError: unexpected EOF while parsing

SEARCH STACK OVERFLOW

x = 56
if x>56:
pass

Else Statement

act_res_press = int(input('What is the actual reservoir pressure? '))

if act_res_press > exp_res_press:


print('Reservoir pressure is greater than the expected resevoir pressure')
print('This is abnormal pressure.')

else:
print('This is a normal pressure')

What is the actual reservoir pressure? 2800


This is a normal pressure /
1/30/2021 Python for O&G Lecture 14th and15th - If else statements, in, and/or operators - Colaboratory

var = 25

if var>21:
print('Var is greater than 20')
else:
print('kjcsbiibv')

Var is greater than 20

in keyword

xyz = 'Python is easy'

if 'z' in xyz:
print('Character z is present in variable xyz')
else:
print('Character z is not present in variable xyz')

Character z is not present in variable xyz

if ' ' in xyz:


print('Space is present ')
else:
print('Space is not present')

Space is present

/
1/30/2021 Python for O&G Lecture 14th and15th - If else statements, in, and/or operators - Colaboratory

use of and/or operators in if else statement

# Let's say we know that pressure in KG Basin is 5000 psi

basin = 'KG Basin'


pressure = 5000 #psi

if basin == 'KG Basin' and pressure == 5100:


print('anything')
else:
print('nothing')

nothing

if basin == 'KG basin' or pressure == 5100:


print('anything')
else:
print('nothing')

nothing

Check wether a string is empty or not

drill_rate = input('What is the drill bit ROP in ft/hr? ')

if drill_rate:
print(f'Drill rate of well is {drill_rate} ft/hr') # string formatting
else:
/
print('No Input Given')
1/30/2021 Python for O&G Lecture 14th and15th - If else statements, in, and/or operators - Colaboratory
print( No Input Given )

What is the drill bit ROP in ft/hr? 45


Drill rate of well is 45 ft/hr

drill_rate = input('What is the drill bit ROP in ft/hr? ')

if drill_rate:
print(f'Drill rate of well is {drill_rate} ft/hr') # string formatting
else:
print('No Input Given')

What is the drill bit ROP in ft/hr?


No Input Given

You might also like