if statements and boolean expressions cse 1310 – introduction to computers and programming...

27
If Statements and Boolean Expressions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington a significant part of this material has been created by Dr. Darin B and Dr. Gian Luca Mariottini 1

Upload: eleanor-logan

Post on 18-Jan-2016

235 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: If Statements and Boolean Expressions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits:

1

If Statements and Boolean Expressions

CSE 1310 – Introduction to Computers and ProgrammingVassilis Athitsos

University of Texas at Arlington

Credits: a significant part of this material has been created by Dr. Darin Brezealeand Dr. Gian Luca Mariottini

Page 2: If Statements and Boolean Expressions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits:

2

The Boolean Type

• Expressions of type boolean can only have two values: True, or False.– True and False are reserved keywords in Python.

>>> 3 > 2True>>> type(True)<type 'bool'>

>>> a = 3 == 2>>> aFalse>>> a = (3 == 2)>>> aFalsePreferred style

(parenthesize)

>>> b = 4;>>> a = (b != 5)>>> aTrue

Page 3: If Statements and Boolean Expressions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits:

3

Comparisons Generating Booleans

• The following operators compare numerical values, or strings, and generate boolean results:

== equality!= not equal< less than> greater than<= less than or equal to>= greater than or equal to

Page 4: If Statements and Boolean Expressions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits:

4

Doing Numerical Comparisons

>>> a = 3>>> b = 5>>> c = 3

>>> a == bFalse>>> a == cTrue>>> a >= cTrue>>> a != cFalse

Page 5: If Statements and Boolean Expressions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits:

5

Doing Comparisons on Strings

>>> a = "hello">>> b = "goodbye">>> c = "hello"

>>> a == bFalse>>> a == cTrue>>> a < bFalse>>> a >= "world"False

>>> a != "hello"False>>> a == "hello"True>>> a != bTrue>>> a < "sky"True>>> "apple" < "car"True>>> "apple" < "Car"False

Page 6: If Statements and Boolean Expressions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits:

6

Doing Comparisons on Strings

• Note how "greater than" and "less than" behave on strings:– Not exactly based on alphabetical order.– Capital letters come before (are "less than") lower

case letters.

>>> "apple" < "car"True>>> "apple" < "Car"False

Page 7: If Statements and Boolean Expressions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits:

7

Logical Operators

• The following logical operators can be used to produce boolean results:

notandor

Page 8: If Statements and Boolean Expressions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits:

8

Truth Tables for or, and, not

A B A or B A and B

True True True True

True False True False

False True True False

False False False False

A not A

True False

False True

Page 9: If Statements and Boolean Expressions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits:

9

Using Logical Operators

>>> a = 3>>> b = 4

>>> (a == b) or (a+b == 7)True

>>> (a == b) and (a+b == 7)False

>>> not(a == b)True

Page 10: If Statements and Boolean Expressions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits:

10

The in operator

• The in operator checks if a value is included in a set of values.

• For now, we will use in to check if a letter appears in a string.– We will see more uses when we do lists.

>>> var1 = 'a'>>> vowels = 'aeiouAEIOU'>>> var1 in vowelsTrue>>> 'c' in vowelsFalse

Page 11: If Statements and Boolean Expressions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits:

11

Combining operators

• What does this line do?>>> (3 == 5) and (2 < 3) or (3 >= 0)

Page 12: If Statements and Boolean Expressions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits:

12

Combining operators

• What does this line do?>>> (3 == 5) and (2 < 3) or (3 >= 0)

• I don't know, and I don't want to know.– Use parentheses to make the meaning of these

statements clear.>>> ((3 == 5) and (2 < 3)) or (3 >= 0) True>>> (3 == 5) and ((2 < 3) or (3 >= 0)) False

Page 13: If Statements and Boolean Expressions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits:

13

Conditionals - if statements

• An if statement is defined as follows:

if (expression): line 1 line 2 … line n

• Line 1, line 2, …, line n are called the body of the if statement.

Page 14: If Statements and Boolean Expressions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits:

14

An example of an if statementnumber_text = input("enter a number: ")number = float(number_text)

if (number > 2): print(number, "is greater than 2")

print("good bye")

Page 15: If Statements and Boolean Expressions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits:

15

Another example of an if statement

number_text = input("enter a number: ")number = float(number_text)

if (number > 2) and (number < 34): print(number, "is greater than 2") print(number, "is less than 34")

print("good bye")

Page 16: If Statements and Boolean Expressions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits:

16

Conditionals - if-else

• An if-else statement is defined as follows:

if (expression): one or more lineselse: one or more lines

Page 17: If Statements and Boolean Expressions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits:

17

An example of an if-else statement

number_text = input("enter a number: ")number = float(number_text)

if (number > 2): print(number, "is greater than 2")else: print(number, "is less than or equal to 2")

print("good bye")

Page 18: If Statements and Boolean Expressions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits:

18

Another example of an if-elsenumber_text = input("enter a number: ")number = float(number_text)

if (number > 2) and (number < 34): print(number, "is greater than 2") print(number, "is less than 34")else: print(number, "is less than or equal to 2") print("or greater than or equal to 34")

print("good bye")

Page 19: If Statements and Boolean Expressions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits:

19

Conditionals - elif

• An if-else can include additional conditions, using elif:

if (expression1): one or more lineselif (expression2): one or more lines.elif (expression2): one or more lines…

Page 20: If Statements and Boolean Expressions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits:

20

Conditionals - elif

• You can use elif zero, one, or more times.

if (expression1): one or more lineselif (expression2): one or more lines.elif (expression2): one or more lines…

Page 21: If Statements and Boolean Expressions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits:

21

An example of using elifnumber_text = input("enter a number: ")number = float(number_text)

if (number > 2) and (number < 34): print(number, "is greater than 2") print(number, "is less than 34")elif (number <= 2): print(number, "is less than or equal to 2")elif (number >= 34): print(number, "is greater than or equal to 34")

print("good bye")

Page 22: If Statements and Boolean Expressions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits:

22

Another example of using elifnumber_text = input("enter a number: ")number = float(number_text)

if (number > 2) and (number < 34): print(number, "is greater than 2") print(number, "is less than 34")elif (number <= 2): print(number, "is less than or equal to 2")else: print(number, "is greater than or equal to 34")

print("good bye")

Page 23: If Statements and Boolean Expressions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits:

23

Warning

• Be careful of the difference between = and ==

Page 24: If Statements and Boolean Expressions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits:

24

The Importance of Indentationnumber_text = input("enter a number: ")number = float(number_text)

if (number > 2) and (number < 34): print(number, "is greater than 2") print(number, "is less than 34")

print("good bye")

number_text = input("enter a number: ")number = float(number_text)

if (number > 2) and (number < 34): print(number, "is greater than 2")print(number, "is less than 34")

print("good bye")

These two programs behave differently.

How, and why?

Page 25: If Statements and Boolean Expressions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits:

25

Successive ifs, vs. if-elifnumber_text = input("enter a number: ")number = float(number_text)

if (number > 2) print(number, "is greater than 2")if (number > 10) print(number, "is greater than 10")if (number > 50) print(number, "is greater than 50")

number_text = input("enter a number: ")number = float(number_text)

if (number > 2) print(number, "is greater than 2")elif (number > 10) print(number, "is greater than 10")elif (number > 50) print(number, "is greater than 50")

These two programs behave differently.

How, and why?

Page 26: If Statements and Boolean Expressions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits:

26

Conditionals with Strings: Example 1

m = input("Enter the name of a month: ")

if (m == "January") or (m == "March") or (m == "May") or (m == "July"): print(m, "has 31 days.")elif (m == "August") or (m == "October") or (m == "December"): print(m, "has 31 days.")elif (m == "April") or (m == "June") or (m == "September") or (m == "November"): print(m, "has 30 days.")elif (m == "February"): print(m, "has 28 or 29 days.")else: print(m, "is not a valid month")

Page 27: If Statements and Boolean Expressions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits:

27

Conditionals with Strings: Example 1

text = input("Enter a word: ")

if ('a' in text) or ('A' in text): print(text, "contains at least one 'a'")else: print(text, "contains no 'a'") if ('b' in text) or ('B' in text): print(text, "contains at least one 'b'")else: print(text, "contains no 'b'")