Presentation is loading. Please wait.

Presentation is loading. Please wait.

Python Conditionals chapter 5

Similar presentations


Presentation on theme: "Python Conditionals chapter 5"— Presentation transcript:

1 Python Conditionals chapter 5
From Think Python How to Think Like a Computer Scientist

2 Boolean Expression A Boolean expression is an expression that results in either a True or False. We can test these in easily. For example >>> 5 == 2 #note the double == is used to test for equality False >>> 5 == 5 # Note that 5=5(or x=5) is really bad!! True # Other relational operators include x != y x < y x <= y x>y x>=y All of these return a True or False Note that True and False are NOT strings

3 Logical Operators There are three logical operators, and, or and not These are used as we do in English >>> True and True True >>> True and False if both true return true else false # False >>> True or False # if either is true return true >>> not True >>> True and not False

4 Further examples >>> x=10 >>> y = 0 >>> x>5 and y == 0 True >>> x != 10 or not y == 3 >>> x>5 and x<=20 # returns True if x is between 5 and 20 >>> x%2==0 or x%3==0 # divisible by 2 or by 3 return True

5 Conditional execution
The first instruction that we will look at that uses conditions is the if statement. Here is the syntax if <condition>: statement more_statements # an example if x!=0: y = 20.0/x #makes sure the we are not dividing by 0 print y Execute these instruction only if <condition> Is True. Four space Indention is required!

6 Alternative example if <condition>: Instruction to execute if <condition> is True else: Instructions to execute if <condition> is False # Example if (x>0): print ‘x is positive’ print ‘x is less than or equal to zero’

7 Chained Conditionals If x<y: print ‘x is less than y’ elif x>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

8 Determine a Letter Grade from a number
grade= ? #Assign some value here if grade>=90: print ‘I made an A’ #Note: As soon as a true is found elif grade>=80: # the remainder is skipped!! print ‘I made a B’ elif grade>=70: print ‘I made a C’ else: print ‘I better get to studying’

9 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>b : if b>c: print a, ‘is the largest’ else: if a>c: print a, ‘is not the largest’ print a, ‘ is not the largest’ Note: This is a contrived example. There is a much easier way to do this! Think about using the and operator!!

10 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

11 Ok, Look at this So what can we do to fix it?
>>> x = raw_input('Enter a number:') Enter a number:23 >>> x+3 #What will be printed? Traceback (most recent call last): File "<pyshell#3>", line 1, in <module> x+3 TypeError: cannot concatenate 'str' and 'int' objects WHY? So what can we do to fix it?

12 Convert it to an integer or float
#method 1 >>> x = raw_input('Enter a number:') Enter a number:12 >>> x=int(x) #convert x to an integer >>> x+4 16 #method 2 >>> x = int(raw_input('Enter a number:'))+4 Enter a number:23 27 What if I typed in 3.45 instead of 12

13 Here is what happens >>> x = raw_input('Enter a number:') Enter a number:3.45 >>> int(x) Traceback (most recent call last): File "<pyshell#12>", line 1, in <module> int(x) ValueError: invalid literal for int() with base 10: '3.45' Note: float(x) will work!

14 Some problems to contemplate
Evaluate a polynomial 𝒚=𝒂 𝒙 𝟐 +𝒃𝒙+𝒄 where a,b and c are entered from the keyboard. Here is a script Enter a:2 Enter b:3 Enter c:4 Enter x:5 The answer is 69.0 >>> 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 print ‘The answer is’,y

15 Find the largest integer ( use max support variable )
x = raw_input('Enter an integer :') max=int(x) x = int(x) if x>max: max = x x=int(x) print 'The largest is',max

16 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 "divisible by 2" if x%3==0: print "divisible by 3" if x%5==0: print "divisible by 5" if x%7==0: print "divisible by 7"

17 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'


Download ppt "Python Conditionals chapter 5"

Similar presentations


Ads by Google