Presentation is loading. Please wait.

Presentation is loading. Please wait.

ProgLan Python Session 4. Functions are a convenient way to divide your code into useful blocks, allowing us to: order our code, make it more readable,

Similar presentations


Presentation on theme: "ProgLan Python Session 4. Functions are a convenient way to divide your code into useful blocks, allowing us to: order our code, make it more readable,"— Presentation transcript:

1 ProgLan Python Session 4

2

3 Functions are a convenient way to divide your code into useful blocks, allowing us to: order our code, make it more readable, reuse it and save some time. Also functions are a key way to define interfaces so programmers can share their code.

4 As we have seen in previous sessions Python makes use of blocks. A block is a area of code of written in the format of: block_head: the_1st_block_line the_2nd_block_line... Where a block line is more Python code (even another block), and the block head is of the following format: block_keyword block_name(argument1,argument2,...) Block keywords you already know are "if", "for", and "while".

5 Functions in python are defined using the block keyword "def", followed with the function's name as the block's name. For example: def my_function(): print "Hello From My Function!"

6 Functions may also receive arguments (variables passed from the caller to the function) For example: def my_function_with_args(username, greeting): print "Hello, %s, From My Function!, I wish you %s"%(username, greeting)

7 Functions may return a value to the caller, using the keyword- 'return'. For example: def sum_two_numbers(a, b): return a + b

8 Simply write the function's name followed by (), placing any required arguments within the brackets. For example, lets call the functions written in the previous slides: my_function() #print a simple greeting my_function_with_args("Or Weis", "a great year!") #prints - "Hello, Or Weis, From My Function!, I wish you a great year!“ x = sum_two_numbers(1,2) #after this line x will hold the value 3 !

9

10 Objects are an encapsulation of variables and functions into a single entity. Objects get their variables and functions from classes. Classes are essentially a template to create your objects.

11 A very basic class would look something like this: class MyClass: variable = "blah" def function(self): print "This is a message inside the class.“ We'll explain why you have to include that "self" as a parameter a little bit later. First, to assign the above class(template) to an object you would do the following: myobjectx = MyClass() Now the variable "myobjectx" holds an object of the class "MyClass" that contains the variable and the function defined within the class called "MyClass".

12 To access the variable inside of the newly created object "MyObject" you would do the following: myobjectx.variable So for instance the code below would output the string "blah": print myobjectx.variable

13 You can create multiple different objects that are of the same class(have the same variables and functions defined). However, each object contains independent copies of the variables defined in the class. For instance, if we were to define another object with the "MyClass" class and then change the string in the variable above: myobjecty = MyClass() myobjecty.variable = "yackity“ Then print out both values: print myobjectx.variable # This would print "blah". print myobjecty.variable # This would print "yackity".

14 To access a function inside of an object you use notation similar to accessing a variable: myobjectx.function() The above would print out the message, "This is a message inside the class."

15 Thanks!


Download ppt "ProgLan Python Session 4. Functions are a convenient way to divide your code into useful blocks, allowing us to: order our code, make it more readable,"

Similar presentations


Ads by Google