You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
So far we have only written small programs that are a sequence of instructions. Sometimes you have to alter the sequential flow of a program to suit the needs of a particular situation.
When strings are compared, they are compared lexicographic, meaning strings are put into alphabetical order and uppercase comes before lowercase.
The conditional operator converts the conditional into a boolean, which is a basic Python data type.
bool datatype only has two values: True or False
There are only three Boolean operators: and, or, and not.
Truthiness and Falsiness
Things that are false on their own
None (basic data type)
False (basic data type)
Any empty sequence: '', [], ()
Any zero value: 0, 0.0
Anything whose len() returns 0
Empty objects
Everything else is True
Boolean/Logical Operators
Operator
What it does?
Example
and
True if both a AND b are true (logical conjunction)
if is_teacher and is_active: print('You can access')
or
True if either a OR b are true (logical disjunction)
if is_superuser or (is_teacher and is active): print('You can access')
not
True if the opposite of a is true (logical negation)
x=3
(1<x) and (x<=5)
x=7
(1<x) and (x<=5)
x=31<x<=5# You can chain comparisons3<5!=True
(3<5) and (5!=True)
3<5!=False
(3<5) and (5!=False)
String Comparisons
'A'<'a''A'>'Z''abc'<'abd''abc'<'abcd'# shorter is less
'Jan'in'01 Jan 1838''Feb'in'01 Jan 1838''a'in'abc''A'in'abc'
Choosing which statements to execute
ifsomeconditionisTrue:
dosomethingelifsomeotherconditionisTrue: # else if dosomethingelifsomeotherconditionisTrue: # else if dosomethingelifsomeotherconditionisTrue: # else if dosomethingelse:
dosomething
colons are important and indentation matters
can have many elif tests
do not need else
conditions can be nested
Example
pH Level
Solution Category
0-4
Strong acid
5-6
Weak acid
7
Neutral
8-9
Weak base
10-14
Strong base
ph=float(input('Enter the pH level: '))
ifph<7.0:
print(ph, " is acidic.") #indentation important
ph=float(input('Enter the pH level: '))
ifph<7.0:
print(ph, " is acidic.")
print('You should be careful with that!') #indentation is important
ph=float(input('Enter the pH level: '))
ifph<7.0:
print(ph, " is acidic.") #indentation importantifph>7.0:
print(ph, " is basic.")
ph=float(input('Enter the pH level: '))
ifph<7.0:
print(ph, " is acidic.") #indentation importantelifph>7.0:
print(ph, " is basic.")
ph=float(input('Enter the pH level: '))
ifph<7.0:
ph=8.0#indentation importantifph>7.0:
print(ph, " is basic.")
ph=float(input('Enter the pH level: '))
ifph<7.0:
ph=8.0#indentation importantelifph>7.0:
print(ph, " is basic.")
ph=float(input('Enter the pH level: '))
ifph<7.0:
ph=8.0#indentation importantelifph>7.0:
print(ph, " is basic.")
value=input('Enter the pH level: ')
iflen(value) >0:
ph=float(value)
ifph<7.0:
print(ph, " is acidic.")
elifph>7.0:
print(ph, " is basic.")
else:
print(ph, " is neutral.")
else:
print('No pH value was given!')
ph=2ifph<7.0:
print(ph, ' is acidic')
elifph<3.0:
print(ph, ' is VERY acidic! Be careful.')
# Implement the min() function for three inputsnumber1=int(input('Enter first integer: '))
number2=int(input('Enter second integer: '))
number3=int(input('Enter third integer: '))
minimum=number1ifnumber2<minimum:
minimum=number2ifnumber3<minimum:
minimum=number3print('Minimum value is', minimum)