# PYTHON CONDITIONALS CHAPTER 5 FROM ```PYTHON
CONDITIONALS
CHAPTER 5
FROM
THINK PYTHON
HOW TO THINK LIKE A COMPUTER SCIENTIST
BOOLEAN
EXPRESSION
A Boolean expression is an expression that results in either a
True or False. We can test these in easily. For example
&gt;&gt;&gt; 5 == 2 #note the double == is used to test for equality
False
&gt;&gt;&gt; 5 == 5 # Note that 5=5(or x=5) is really bad!!
True
# Other relational operators include
x != y
x&lt;y
x &lt;= y x&gt;y
x&gt;=y
All of these return a True or False
Note that True and False are NOT strings
LOGICAL OPERATORS
There are three logical operators, and, or and not
These are used as we do in English
&gt;&gt;&gt; True and True
True
&gt;&gt;&gt; True and False if both true return true else false
# False
&gt;&gt;&gt; True or False # if either is true return true
True
&gt;&gt;&gt; not True
True
&gt;&gt;&gt; True and not False
True
FURTHER EXAMPLES
&gt;&gt;&gt; x=10
&gt;&gt;&gt; y = 0
&gt;&gt;&gt; x&gt;5 and y == 0
True
&gt;&gt;&gt; x != 10 or not y == 3
True
&gt;&gt;&gt; x&gt;5 and x&lt;=20 # returns True if x is between 5 and 20
True
&gt;&gt;&gt; x%2==0 or x%3==0 # divisible by 2 or by 3 return True
True
CONDITIONAL EXECUTION
The first instruction that we will look at that uses conditions
is the if statement. Here is the syntax
if &lt;condition&gt;:
statement
statement
more_statements
Execute these instruction only
if &lt;condition&gt; Is True. Four space
Indention is required!
# an example
if x!=0:
y = 20.0/x
print y
#makes sure the we are not dividing by 0
ALTERNATIVE
EXAMPLE
if &lt;condition&gt;:
Instruction to execute if &lt;condition&gt; is True
else:
Instructions to execute if &lt;condition&gt; is False
# Example
if (x&gt;0):
print ‘x is positive’
else:
print ‘x is less than or equal to zero’
CHAINED CONDITIONALS
If x&lt;y:
print ‘x is less than y’
elif x&gt;y:
print ‘x is greater than y’
else:
print ‘x is equal to y’
#You can chain as many of these elif’s as you want
# Lets do an example with grades
DETERMINE A LETTER
#Assign some value here
#Note: As soon as a true is found
# the remainder is skipped!!
else:
print ‘I better get to studying’
WE CAN ALSO PUT AN IF
WITHIN AN IF (NESTED)
Suppose we want to see if a is greater than b and c
if a&gt;b :
if b&gt;c:
print a, ‘is the largest’
else:
Note: This is a contrived
example. There is a much
easier way to do this!
if a&gt;c:
print a, ‘is the largest’
else:
print a, ‘is not the largest’
else:
print a, ‘ is not the largest’
and operator!!
KEYBOARD INPUT
HTTP://DOCS.PYTHON.ORG/2/LIBRARY/FUNCTIONS.HTML
Before we look at more if-else examples we need to see how
to get input (keyboard) from the person running the python
program.
raw_input([prompt]) If the prompt argument is present, it is written to
screen without a trailing newline. The function then reads a line from input,
converts it to a string (stripping a trailing newline), and returns that.
When EOF is read, EOFError is raised.
Example:
x = raw_input(‘Enter a number:’)
Prompt
A string
OK, LOOK AT THIS
&gt;&gt;&gt; x = raw_input('Enter a number:')
Enter a number:23
&gt;&gt;&gt; x+3
#What will be printed?
Traceback (most recent call last):
File &quot;&lt;pyshell#3&gt;&quot;, line 1, in &lt;module&gt;
x+3
TypeError: cannot concatenate 'str' and 'int' objects
WHY?
So what can we do to fix it?
CONVERT IT TO AN
INTEGER OR FLOAT
#method 1
&gt;&gt;&gt; x = raw_input('Enter a number:')
Enter a number:12
&gt;&gt;&gt; x=int(x)
#convert x to an integer
&gt;&gt;&gt; x+4
16
#method 2
&gt;&gt;&gt; x = int(raw_input('Enter a number:'))+4
Enter a number:23
27
What if I typed in 3.45
HERE IS WHAT
HAPPENS
&gt;&gt;&gt; x = raw_input('Enter a number:')
Enter a number:3.45
&gt;&gt;&gt; int(x)
Traceback (most recent call last):
File &quot;&lt;pyshell#12&gt;&quot;, line 1, in &lt;module&gt;
int(x)
ValueError: invalid literal for int() with base 10: '3.45'
Note:
float(x) will work!
SOME PROBLEMS TO
CONTEMPLATE
1. Evaluate a polynomial 𝒚 = 𝒂𝒙𝟐 + 𝒃𝒙 + 𝒄 where a,b and c
are entered from the keyboard. Here is a script
a=float(raw_input(‘Enter a:’))
b=float(raw_input(‘Enter b:’))
c=float(raw_input(‘Enter c:’))
x=float(raw_input(‘Enter x:’))
y = a*x**2+b*x+c
Enter a:2
Enter b:3
Enter c:4
Enter x:5
&gt;&gt;&gt;
FIND THE LARGEST INTEGER
( USE MAX SUPPORT VARIABLE )
x = raw_input('Enter an integer :')
max=int(x)
x = raw_input('Enter an integer :')
x = int(x)
if x&gt;max:
max = x
x = raw_input('Enter an integer :')
x=int(x)
if x&gt;max:
max = x
print 'The largest is',max
DIVISIBILITY TESTS
A SEQUENCE OF IF’S
x = raw_input('Enter an integer :')
x=int(x)
# don’t forget this one.
if x%2==0:
print &quot;divisible by 2&quot;
if x%3==0:
print &quot;divisible by 3&quot;
if x%5==0:
print &quot;divisible by 5&quot;
if x%7==0:
print &quot;divisible by 7&quot;
USING STRINGS
x = raw_input('Enter your first name :')
# note x is a string already so no conversion is required!
if x=='Richard':
print x,'is an awesome teacher'
else:
print x, 'must be an awesome student'
```