Presentation is loading. Please wait.

Presentation is loading. Please wait.

Python – Part 3 Functions 1. Getting help Start the Python interpreter and type help() to start the online help utility. Or you can type help(print) to.

Similar presentations


Presentation on theme: "Python – Part 3 Functions 1. Getting help Start the Python interpreter and type help() to start the online help utility. Or you can type help(print) to."— Presentation transcript:

1 Python – Part 3 Functions 1

2 Getting help Start the Python interpreter and type help() to start the online help utility. Or you can type help(print) to get information about the print function. When finished with the help utility, type quit at the prompt to return to the interpreter. Prepared by Department of Preparatory year 2

3 Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print (remainder) 1

4 Boolean expressions An expression that is either true or false Operator == >>>5==5 True >>>5==6 False

5 Boolean expressions Type bool – True and False >>>type (True) >>>type (False)

6 Boolean expressions Other operators: x != y # x is not equal to y x > y # x is greater than y x < y # x is less than y x >= y # x is greater than or equal to y x <= y # x is less than or equal to y

7 Logical operators And, or, not Semantics similar to their meaning in English x>0 and x<10 not(x>y) Any nonzero number in Python is interpreted as “true” >>> 17 and True True

8 Function Calls Function – A named sequence of statements that performs a computation – Name – Sequence of statements “call” function by name >>> type(32) – Function name – type – Argument - 32 8 Prepared by Department of Preparatory year

9 Function Calls Commonly “takes” an argument “returns” a result Result called the return value. 9 Prepared by Department of Preparatory year

10 Type conversion functions Built-in functions that convert values from one type to another >>>int (’32’) 32 >>>int (‘Hello’) ValueError: invalid literal for int(): Hello >>>int (3.9999) 3 >>> int (-2.3) -2 10 Prepared by Department of Preparatory year

11 Type conversion functions >>>float (32) 32.0 >>>float (‘3.14159’) 3.14159 >>> str (32) ’32’ >>> str (3.14159) ‘3.14159’ 11 Prepared by Department of Preparatory year

12 Math functions Module – a file that contains a collection of related functions Math module – provides most of the familiar mathematical functions 12 Prepared by Department of Preparatory year

13 Math Functions Import the module before using it >>> import math Creates a module object named math >>> print math Prints some information about it 13 Prepared by Department of Preparatory year

14 Math functions >>> ratio = signal_power / noise_power >>> decibels = 10 * math.log10(ratio) #computes logarithm base 10 of ratio >>> radians = 0.7 >>> height = math.sin(radians) #computes sine of radians 14 Prepared by Department of Preparatory year

15 Math functions >>> degrees=45 >>> radians=degrees/360.0*2*math.pi >>>math.sin(radians) 0.707106781187 Can use functions to compose more complex expressions x=math.sin(degrees/360.0*2*math.pi) 15 Prepared by Department of Preparatory year

16 Composition >>>math.sqrt(2)/2.0 0.707106781187 >>>minutes=hours*60 #right >>>hours*60=minutes #wrong! SyntaxError: can’t assign to operator 16 Prepared by Department of Preparatory year

17 Adding new functions Function definition – Name of a new function – Sequence of statements that execute when function is called def print_lyrics(): print ("I'm a lumberjack, and I'm okay.“) print ("I sleep all night and I work all day." ) 17 Prepared by Department of Preparatory year

18 Adding new functions def – keyword for function definition print_lyrics – name of the function () – no arguments Function name –same rules as for variables Avoid using variable and function with same name 18 Prepared by Department of Preparatory year

19 Adding new functions Header – first line of the function definition – Ends in colon Body – the rest – Has to be indented (always four spaces) Empty line to end the function (not necessary in a script) 19 Prepared by Department of Preparatory year

20 Adding new functions Defining a function creates a variable with the same name >>> print_lyrics() def repeat_lyrics(): print_lyrics() 20 Prepared by Department of Preparatory year

21 Parameters and Arguments math.pow(2,3) 8.0 def print_twice(bruce): print bruce Assigns the argument to a parameter named bruce 21 Prepared by Department of Preparatory year

22 Parameters and Arguments >>>print_twice (‘spam’) spam >>>print_twice(17) 17 22 Prepared by Department of Preparatory year

23 Parameters and Arguments >>>print_twice(‘spam’*2) spam Argument evaluated before function is called 23 Prepared by Department of Preparatory year

24 Parameters and Arguments Can also use a variable as an argument >>>number=17 >>>print_twice(number) 17 24 Prepared by Department of Preparatory year

25 Local variables and parameters Variable inside a function is local Exists only inside the function def cat_twice(part1, part2): cat = part1 + part2 print_twice(cat) This function takes two arguments, concatenates them, and prints the result twice. 25 Prepared by Department of Preparatory year

26 Local variables (cont’d…) >>> line1 = 'Bing‘ >>> line2 = 'bang.' >>> cat_twice(line1, line2) Bing bang. 26 Prepared by Department of Preparatory year

27 Local variables (cont’d …) When cat_twice terminates, the variable cat is destroyed. If we try to print it, we get an exception: >>> print cat NameError: name 'cat' is not defined 27 Prepared by Department of Preparatory year

28 Void functions print_twice Perform and action but does not return a value If function returns value, almost always use it as part of an expression: x = math.cos(radians) golden = (math.sqrt(5) + 1) / 2 28 Prepared by Department of Preparatory year

29 Void functions >>> result = print_twice('Bing') Bing >>> print result None 29 Prepared by Department of Preparatory year

30 Part 3 End 30


Download ppt "Python – Part 3 Functions 1. Getting help Start the Python interpreter and type help() to start the online help utility. Or you can type help(print) to."

Similar presentations


Ads by Google