Download presentation
Presentation is loading. Please wait.
Published byCory Lang Modified over 9 years ago
1
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision Structures and Boolean Logic
2
1-2 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-2 4.1 The if Statement Concept: The if statement is used to create a decision structure, which allows a program to have more than one path of execution. The if statement causes one or more statements to execute only when a Boolean expression is true.
3
1-3 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-3 4.1 The if Statement A control structure is a logical design that controls the order in which a set of statements execute. A sequence structure is a set of statements that execute in the order that they appear. A decision (or selection) structure is a control structure that can execute a set of statements and perform a specific action only if a certain condition exists.
4
1-4 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-4 4.1 The if Statement Decision (selection) structure: Diamond symbol represents a true/false condition Single alternative decision structure provides only one alternative path of execution. Figure 4-1 A simple decision structure
5
1-5 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-5 4.1 The if Statement Decision (selection) structure: In Python … if condition: statement etc.
6
1-6 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-6 4.1 The if Statement Boolean Expressions and Relational Operators A relational operator determines whether a specific relationship exists between two values. Table 4-1 Relational operators
7
1-7 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-7 4.1 The if Statement Boolean Expressions and Relational Operators Table 4-2 Boolean expressions using relational operators
8
1-8 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-8 4.1 The if Statement Nested Blocks Figure 4-5 Nested blocks
9
1-9 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-9 4.2 The if-else Statement Concept: An if-else statement will execute one block of statements if its condition is true, or another block if its condition is false.
10
1-10 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-10 4.2 The if-else Statement A dual alternative decision structure provides two possible paths of execution – one path is taken if a condition is true, and the other path is taken if the condition is false. Figure 4-6 A dual alternative decision structure
11
1-11 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-11 4.2 The if-else Statement Dual alternative In Python … if condition: statement etc. else: statement etc.
12
1-12 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-12 4.2 The if-else Statement Figure 4-7 Conditional execution in an if-else statement
13
1-13 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-13 4.3 Comparing Strings Concept: Python allows you to compare strings. This allows you to create decision structures that test the value of a string.
14
1-14 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-14 4.3 Comparing Strings Program 4-3 (password.py)
15
1-15 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-15 4.3 Comparing Strings Other String Comparisons Figure 4-10 Character codes for the strings 'Mary' and 'Mark' Figure 4-11 Comparing each character in a string
16
1-16 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-16 4.4 Nested Decision Structures and the if-elif-else Statement Concept: To test more than one condition, a decision structure can be nested inside another decision structure.
17
1-17 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-17 4.4 Nested Decision Structures and the if-elif-else Statement Figure 4-14 A nested decision structure
18
1-18 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-18 4.4 Nested Decision Structures and the if-elif-else Statement Figure 4-16 Nested blocks
19
1-19 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-19 4.4 Nested Decision Structures and the if-elif-else Statement The if-elif-else Statement if condition_1: statement etc. elif condition_2: statement etc. else: statement etc.
20
1-20 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-20 4.4 Nested Decision Structures and the if-elif-else Statement The if-elif-else Statement if score < 60: print ‘Your grade is F.’ elif score < 70: print ‘Your grade is D.’ elif score < 80: print ‘Your grade is C.’ elif score < 90: print ‘Your grade is B.’ else: print ‘Your grade is A.’
21
1-21 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-21 4.5 Logical Operators Concept: The logical and operator and the logical or operator allow you to connect multiple Boolean expressions to create a compound expression. The logical not operator reverses the truth of a Boolean expression.
22
1-22 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-22 4.5 Logical Operators Table 4-3 Logical operators
23
1-23 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-23 4.5 Logical Operators Table 4-4 Compound Boolean expressions using logical operators
24
1-24 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-24 4.5 Logical Operators The and Operator if temperature 12: print ‘The temperature is in the danger zone.’ Table 4-5 Truth table for the and operator
25
1-25 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-25 4.5 Logical Operators The or Operator if temperature 100: print ‘The temperature is too extreme.’ Table 4-6 Truth table for the or operator
26
1-26 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-26 4.5 Logical Operators The not Operator if not(temperature > 100): print ‘This is below the maximum temperature.’ Table 4-7 Truth table for the not operator
27
1-27 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-27 4.5 Logical Operators Checking Numeric Ranges with Logical Operators if x >= 20 and x <= 40: print ‘The value is in the acceptable range.’ if x 40: print ‘The value is outside the acceptable range.’ # This is an error! if x 40: print ‘The value is outside the acceptable range.’
28
1-28 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-28 4.6 Boolean Variables Concept: A Boolean variable can reference one of two values: True or False. Boolean variables are commonly used as flags, which indicate whether a specific condition exists.
29
1-29 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-29 4.6 Boolean Variables if sales_quota_met == True: print ‘You have met your sales quota!.’ if sales_quota_met: print ‘You have met your sales quota!.’ OR if sales >= 50000: sales_quota_met = True else: sales_quota_met = False
30
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley QUESTIONS ? Chapter 4 Decision Structures and Boolean Logic
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.