Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CS2304: Python for Java Programmers CS2304: Functions and Control Structures Monti 2014 CS2304: Python for Java Programmers Functions • We’ve already seen the basic hello world function: def main(): print(“Hello World”) Monti 2014 CS2304: Python for Java Programmers General Function Format • Remember indentation is very important, indentation indicates a block of code. • You can’t “overload” functions but Python is flexible about the number of parameters you actually need to specify. def function(parameters): statement1 statement2 etc… Monti 2014 CS2304: Python for Java Programmers Aside: The Pass Statement • Lets say you wanted to create an empty block. • There aren’t brackets like in Java/C++. • This doesn’t work: def test_function(parameters): # nothing here • However, this works: def test_function(parameters): pass • pass is a place holder in a function, control structure, or class. Monti 2014 CS2304: Python for Java Programmers The Membership Operator: “in” • For lists, tuples, and strings we can test if something is “in” the collection. # l # t # s this is a list = [1, 2, 3, 4] here’s a tuple = (1, 2, 3) and of course a string = “Hello World!” Monti 2014 CS2304: Python for Java Programmers The Membership Operator: “in” • For lists, tuples, and strings we can test if something is “in” the collection. >>> 1 in l True >>> 5 in l False >>> 5 not in l True >>> ‘H’ in s True >>> ‘z’ in s False Monti 2014 CS2304: Python for Java Programmers Range Function # creates a sequence from 0 to num - 1 range(num) # creates a sequence from start to end – 1 range(start, end) # like previous, but skips “steps” numbers range(start, end, steps) • Conceptually you can think of range() as returning a list of numbers. # taking a range and creating a list is easy list(range(num)) • Aside: in Python 2.X it range actually returned a list. Now it’s list “like”, but more efficient. Monti 2014 CS2304: Python for Java Programmers And Now A For Loop… • General form: for var in collection: statement(s) • A first for loop: for x in range(10): print(x) Monti 2014 CS2304: Python for Java Programmers And Now A For Loop… • Range is fairly flexible, here we print every third number. for x in range(0, 10, 3): print(x) • Range also allows for more interesting start/end points: for x in range(50, 100): print(x) Monti 2014 CS2304: Python for Java Programmers And Now A For Loop… • Using the “in” operator we can also iterate through lists: for x in l: print(x) • And tuples: for x in t: print(x) Monti 2014 CS2304: Python for Java Programmers And Now A For Loop… • What about strings? For x in s: print(x) • This actually works too, iterating through the characters: H e l … Monti 2014 CS2304: Python for Java Programmers Comparison Operators • Python has the standard set of comparison operators you’ve come to expect: >>> 1 True >>> 1 False >>> 1 False >>> 1 == 1 != 1 < 1 > 1 • You also have => and <= though they aren’t shown. Monti 2014 CS2304: Python for Java Programmers Comparison Operations • Conveniently, comparison operators can be chained: >>> a = 9 >>> 0 <= a <= 10 True Monti 2014 CS2304: Python for Java Programmers Logical Operators • Python also has the standard set of logical operators you’ve come to expect: >>> True and True True >>> True and False False >>> False or True True >>> 1 and 1 1 • Instead of symbols the logical operators are English words. Monti 2014 CS2304: Python for Java Programmers After All Of That… If Statements • These work as you would expect. • General form: if condition: statement(s) • A first if statement: x = 1 if x in l: print(x) Monti 2014 CS2304: Python for Java Programmers After All Of That… If Statements • If statements using comparison operators… if x > 10: x += 1 • Multiple comparison operators can be combined using logical operators: if x > 10 and y < 5: x -= 1 y += 1 Monti 2014 CS2304: Python for Java Programmers After All Of That… If Statements • There are also the larger multi-way if statements: if condition: statement(s) else: statement(s) if condition: statement(s) elif condition2: statement(s) else: statements(s) Monti 2014 CS2304: Python for Java Programmers The While Loop • General form: while condition remains True: statement(s) • A first while loop: while x < 10: print(x) Monti 2014 CS2304: Python for Java Programmers The While Loop • No do…while loop, so code like this is common: while(True): # do something # when we want to stop the loop if x > 10: break • There is also a continue statement. • Both break and continue can be used in for / while statements. Monti 2014 CS2304: Python for Java Programmers Else Statements With Loops • There are else clauses for while and for loops: while x < 10: # assume we have some code here else: # executed when the condition # is false or the loop ends • The else clause is not executed in the event of an exception or break statement. • These exist for both for / while statements Monti 2014 CS2304: Python for Java Programmers FYI: Regarding Conditions • An empty string, list, tuple, None (null), and the number 0 all evaluate to False # empty list x = [] if not x: x += [1] # None is like null or NULL if x is None: x = SomeFunction() Monti 2014 CS2304: Python for Java Programmers Aside: Object References • Every variable, list, tuple, etc. in python is really an object reference. • Or a “pointer” to an object • Some types are immutable: • Strings, integers, tuples • Whereas, some are mutable: • Lists, dictionaries • If you change an immutable object, a new object is created, and the variable name (x) is “pointed” to the new object. • Since every variable is a reference, any variable can be set to None (null). Monti 2014 CS2304: Python for Java Programmers The Identity Operator • If you want to test whether two object references are equal, NOT their values, you may use the “is” operator: >>> a = [1, 1, 1] >>> b = [1, 1, 1] >>> a is b False >>> a = b >>> a is b True • Testing whether a variable is None is common. • Don’t use values with is, weird stuff can happen. Monti 2014 CS2304: Python for Java Programmers Exceptions • General Form: try: # code that can break statement(s) except ExceptionType1 as var1: # handle ExceptionType1 statement(s) except ExceptionType2 as var2 # handle ExceptionType2 Monti 2014 CS2304: Python for Java Programmers A More Complex Example while(True): try: l = input(“Give me an int”) t = int(l) break except ValueError as err: print(“Enter a valid integer.”) Monti 2014