140 likes | 286 Views
Day II. boolean expression, if-else, while, and function Special thanks to Scott Shawcroft, Ryan Tucker, Paul Beck, Hélène Martin, Kim Todd, John Kurkowski, and Marty Stepp for their work on these slides.
E N D
Day II boolean expression, if-else, while, and function Special thanks to Scott Shawcroft, Ryan Tucker, Paul Beck, Hélène Martin, Kim Todd, John Kurkowski, and Marty Stepp for their work on these slides. Except where otherwise noted, this work is licensed under:http://creativecommons.org/licenses/by-nc-sa/3.0 Adapted for use in in-house training by Paruj Ratanaworabhan
Boolean Expressions I • The constants True and False of the type bool. • These constants can be combined to form Boolean expressions via the logical operators and, or, and not. • Precedence: not then and then or • The and of two Boolean expressions is True if both of the expressions are True. • The or of two Boolean expressions is True if at least one of the expressions is True. • Examples: • http://www.codeskulptor.org/#examples-more-1b_logic_and_comparisons-booleans.py • http://www.codeskulptor.org/#examples-more-1b_logic_and_comparisons-logic.py
Boolean Expressions II • The values of two arithmetic expressions can be compared using the operators ==, !=, <, >, <=, >=. • These comparisons return either True or False. >>> 1 < 2 True >>> 10 == 8+2 True >>> 5 > 40 False >>> (10 > 3) and (not (3 == 7)) True
Boolean Expressions III • For strings, the operators compare them according tolexicographical order: characters are compared from front to back, and the corresponding pair of character is compared by their ASCII code. Hence, "Z" is less than "a” because Z comes before a in the ASCII table. >>> "cat" < "dog“ True >>> "abcdef" >= "Abcdef“ True >>> "wow" == "wow“ True
More Examples of Boolean Expressions • Examples in CodeSkulptor: • http://www.codeskulptor.org/#examples-more-1b_logic_and_comparisons-comparison.py • http://www.codeskulptor.org/#examples-more-1b_logic_and_comparisons-expressions.py
If Statement • Form I if <<boolean expression>>: <<statement X>> <<statement Y>> <<statement Z>> Example: . x = -10 if x > 0: print 'x is positive' print ‘Outside if' • Form II • if <<boolean expression>>: • <<statement X>> • <<statement Y>> • . • else: • <<statement X’>> • <<statement Y’>> • . Example: . • x = 425 • if x % 2 == 0: • print 'x is even' • else: • print 'x is odd'
If Statement Example: . • score = 72.6 • if score >= 80: • print 'grade A' • elif score >= 70: • print 'grade B' • elif score >= 60: • print 'grade C' • elif score >= 50: • print 'grade D' • else: • print 'grade F' • Form III if <<boolean expression 1>> <<statement>> <<statement>> . elif <<boolean expression 2>> <<statement>> <<statement>> . elif <<boolean expression n>> <<statement>> <<statement>> . else: <<statement>> <<statement>>
Nested If • Satements under if, elif, or else blocks can themselves be if-else statements x = 10 y = 20 if x == y: print "x and y are equal" else: if x < y: print "x is less than y" else: print "x is greater than y"
Iteration • Python provides the while statement for performing iteration. • Consider the following loop. Can you guess how it will behave? while n > 0: print n n = n - 1 print 'Blastoff!' • Here is the flow of execution for a while statement: • Evaluate the condition, yielding True or False. • If the condition is false, exit the while statement and continue execution at the next statement. • If the condition is true, execute the body and go back to step 1.
Function • Using functions: <<function name>>(<<argument 1>>, <<argument 2>>, ..., <<argument n>>) • Argument is a value that the function takes as an input. • Built-in functions : • int(…), str(…), float(…), type(…) • max and min; examples: >>> max(1, 7, 2, 5) 7 >>> min(4, 3, 2, -10) -10 • abs and len (absolute and length of string); examples: >>> abs(-4.5) 4.5 >>> len("ab" * 10) 20
Modules • A module is a file that contains a collection of related functions (and other things as well). • Need to import <<module name>> before use • Math module usage examples: import math >>> math.pi 3.1415926535897931 >>> math.sqrt(2) 1.4142135623730951 >>> math.exp(1) 2.7182818284590451
Defining Functions • The syntax for function declaration is as follows: def <<function name>>(<<name 1>>, <<name 2>>, ... <<name n>>): <<statement 1>> <<statement 2>> . . • a function that computes the following mathematical function: def f(x): if x > 0: return x*x else: return 0 print f(10), f(0), f(8)
Defining Functions • The syntax for function declaration is as follows: def <<function name>>(<<name 1>>, <<name 2>>, ... <<name n>>): <<statement 1>> <<statement 2>> . . • a function that computes the following mathematical function: def f(x): if x > 0: return x*x else: return 0 print f(10), f(0), f(8)
More Function Examples • Note that functions may have zero argument or may not contain a return statement • See more examples from CodeSkulptor: • http://www.codeskulptor.org/#examples-functions.py • http://www.codeskulptor.org/#examples-more-1b_functions-examples.py • http://www.codeskulptor.org/#examples-more-1b_functions-structure.py