04 Branching Statements in Python.pptx

Full Transcript

BRANCHING S TAT E M E N T S I N PYTHON INTRODUCTION TO IF-ELSE S TAT E M E N T S Definition of If-Else Conditions in If-Else Statements Statements Branching Represented as statement in logical expressions programming Dete...

BRANCHING S TAT E M E N T S I N PYTHON INTRODUCTION TO IF-ELSE S TAT E M E N T S Definition of If-Else Conditions in If-Else Statements Statements Branching Represented as statement in logical expressions programming Determine which Executes blocks of code blocks to code based on execute conditions S I M P L E I F S TAT E M E N T S Y N TA X Simple If Statement Syntax Use 'if' followed by a logical expression Code block is executed if the expression is true Simple If-Else Statement Syntax Use 'if' followed by a logical expression Code block 1 is executed if the expression is true Use 'else' for the alternative condition Code block 2 is executed if the expression is false Initial Condition Check Python checks if P is true If true, executes code block 1 and ends : Subsequent Condition Checks EXTENDED If P is false, checks if Q is true IF-ELSE If true, executes code block 2 and ends S TAT E M E N If false, checks if R is true T S Y N TA X If true, executes code block 3 and ends Final Condition If P, Q, and R are all false, executes code block 4 Elif and Else Statements Any number of elif statements allowed N E S T E D I F S TAT E M E N T S Definition of Nested Example Code Statements Explanation A statement contained within another Function: my_nested_branching(x, y) statement of the same type Checks if x > 2 Example: Nested if-statement If true, checks if y < 2 Calculates out based on conditions LOGICAL FUNCTIONS FOR BRANCHING Logical Functions for Branching isinstance: Checks if a variable has a certain data type any: Computes to true if any element in an array is true all: Computes to true only if all elements in an array are true Designing Functions to Check Inputs Ensures proper use of the function Prevents errors or unexpected results Uses raise statement with TypeError exception EXAMPLES OF BRANCHING S TAT E M E N T S Function is_odd Function Returns 'odd' if input is an odd number my_circ_calc Calculates area or circumference of a Returns 'even' if input is an even circle number Input arguments: radius (r) and Uses modulo to check divisibility by 2 calculation type (calc) Uses NumPy for array operations INTRODUCTION T O T E R N A RY O P E R AT O R S Known as conditional expressions Provide a mechanism using one-line code Functionality Evaluates the first expression if the condition is true Evaluates the second expression if the condition is false Definition of Ternary Operator Used for conditional expressions Syntax: expression_if_true if T E R N A RY condition else expression_if_false O P E R AT O R Example of Ternary S Y N TA X Operator is_student = True person = "student" if is_student else "not student" print(person) outputs "student" Used for conditional expressions in a single line EXAMPLES OF T E R N A RY expression_if_true if condition else O P E R AT O R Syntax expression_if_fals e S is_student = True Equivalent Code if is_student: Block person = "student" A D VA N TA G E S O F T E R N A RY O P E R AT O R S Ternary Operators Role in List Comprehensions Provide a simple way for branching Proven to be useful Make codes concise PROBLEM 1: Function Definition TIP my_tip_calc(bill, party) C A L C U L AT O R Calculates tip based on party size Tip Calculation Criteria 15% for party < 6 18% for party < 8 20% for party < 11 25% for party >= 11 Test Cases 109.29, 3 -> 16.3935 109.29, 7 -> 19.6722 109.29, 9 -> 21.8580 Function Definition Function name: my_mult_operation Arguments: a, b, operation Operation: 'plus', 'minus', 'mult', 'div', 'pow' PROBLEM 2: Test Cases M U LT I P L E Test Case 1: my_mult_operation(x,y,'plus') O P E R AT I O N Test Case 2: my_mult_operation(x,y,'minus') S Test Case 3: my_mult_operation(x,y,'mult') Test Case 4: my_mult_operation(x,y,'div') Test Case 5: my_mult_operation(x,y,'pow') PROBLEM Function Definition Function name: my_inside_triangle(x, y) 3: INSIDE Determines the position of a point (x, y) relative to a triangle TRIANGLE Triangle Vertices Vertices at (0, 0), (1, 0), and (0, 1) Output Conditions Returns 'outside' if the point is outside the triangle Returns 'border' if the point is on the border of the triangle Returns 'inside' if the point is inside the triangle Example Outputs my_inside_triangle(.5,.5) returns 'border' my_inside_triangle(.25,.25) returns 'inside' Function Description my_make_size10(x) takes an array x as input Returns first 10 elements if x has more than 10 elements Pads with zeros to make length 10 if x has less than 10 elements PROBLEM Example Outputs 4: MAKE my_make_size10(range(1,2)) returns [1,2,0,0,0,0,0,0,0,0] SIZE 10 my_make_size10(range(1,15)) returns [1,2,3,4,5,6,7,8,9,10] my_make_size10([3,6,13,4]) returns [3,6,13,4,0,0,0,0,0,0] Challenge Write my_make_size10 without using if-statements PROBLEM 5: Function Definition Function name: my_letter_grader LETTER GRADER Parameter: percent Grade Categories A+: greater than 97 A: greater than 93 A-: greater than 90 B+: greater than 87 B: greater than 83 B-: greater than 80 C+: greater than 77 Examples PROBLEM Function Purpose Monitors temperature readings from three 6: NUKE sensors Triggers an alarm if any two readings disagree by more than 10 degrees ALARM Function Definition Function name: my_nuke_alarm(s1, s2, s3) Parameters: s1, s2, s3 (temperature readings from three sensors) Returns: 'alarm!' if any two readings disagree by more than 10 degrees, 'normal' otherwise Example Outputs my_nuke_alarm(94, 96, 90) returns 'normal' my_nuke_alarm(94, 96, 80) returns 'alarm!' my_nuke_alarm(100, 96, 90) returns 'normal' PROBLEM 7: Quadratic Equation Q(x) Q UA D R AT I C Q(x) = ax² + bx + c ROOTS Roots of Q(x) are values of r such that Q(r) =0 Quadratic Formula r = (-b ± √(b² - 4ac)) / 2a Types of Roots Two real roots if b² > 4ac Two imaginary roots if b² < 4ac One root if b² = 4ac Function my_n_roots(a,b,c) Returns n_roots and r n_roots is 2 for two real roots, 1 for one root, -2 for two imaginary roots PROBLEM 8: Function Definition my_split_function(f,g,a,b,x) SPLIT FUNCTION f and g are function objects Output Conditions Returns f(x) if x = b Returns 0 otherwise Assumption b>a Example Outputs my_split_function(np.exp,np.sin,2,4,1) returns 2.713 my_split_function(np.exp,np.sin,2,4,3) returns 0

Use Quizgecko on...
Browser
Browser