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,

Slides:



Advertisements
Similar presentations
Python Objects and Classes
Advertisements

Lists Introduction to Computing Science and Programming I.
Main task -write me a program
Lesson15. JavaScript Objects Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together.
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
Python 3 Some material adapted from Upenn cis391 slides and other sources.
1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming.
Hello AP Computer Science!. What are some of the things that you have used computers for?
PHP Overview CS PHP PHP = PHP: Hypertext Preprocessor Server-side scripting language that may be embedded into HTML One goal is to get PHP files.
1 Python Control of Flow and Defining Classes LING 5200 Computational Corpus Linguistics Martha Palmer.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
PMS /134/182 HEX 0886B6 PMS /39/80 HEX 5E2750 PMS /168/180 HEX 00A8B4 PMS /190/40 HEX 66CC33 By Adrian Gardener Date 9 July 2012.
4.1 Instance Variables, Constructors, and Methods.
Course A201: Introduction to Programming 11/04/2010.
1 Procedures Blocks of code which can be called from anywhere in a program Enable us to avoid needless repetition of the same code Can take parameters.
By the end of this session you should be able to...
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
Python Programming Using Variables and input. Objectives We’re learning to build functions and to use inputs and outputs. Outcomes Build a function Use.
Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and.
CSE 114 Computer Science I Objects Lake Superior, Michigan.
Working With Objects Tonga Institute of Higher Education.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 6 Defining Functions.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Python Functions.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Working With Objects Tonga Institute of Higher Education.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)
Python Let’s get started!.
Chapter 6 Methods Chapter 6 - Methods.
More Python Proglan Python Session 2. Lists  Lists are like flexible arrays. They can contain as many variables as you wish, and all variable types are.
Object Oriented Programming Session # 03.  Abstraction: Process of forming of general and relevant information from a complex scenarios.  Encapsulation:
6. FUNCTIONS Rocky K. C. Chang October 4, 2015 (Adapted from John Zelle’s slides)
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
COSC 1223 Computer Science Concepts I Joe Bryan. What is a function? Like a mini-program that performs a specific task Two types: Built-in functions Functions.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Chapter 6 Functions The Tic-Tac-Toe Game. Chapter Content In this chapter you will learn to do the following: 0 Write your own functions 0 Accept values.
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
Operator Overloading Introduction
Topic: Classes and Objects
Python Let’s get started!.
loops for loops iterate over a given sequence.
Variables, Expressions, and IO
Functions CIS 40 – Introduction to Programming in Python
Functions.
Review.
Python Classes By Craig Pennell.
Learning to Program in Python
PYTHON Varun Jain & Senior Software Engineer
CMSC201 Computer Science I for Majors Lecture 16 – Classes and Modules
Coding Concepts (Sub- Programs)
Passing Parameters by value
Miscellaneous C++ Topics
An Introduction to Object Orientated Programming
Elements of a Python Program
Functions Christopher Harrison | Content Developer
Functions Jay Summet.
Java Programming Language
COMPUTER PROGRAMMING SKILLS
Introduction to Computer Science
What is a Function? Takes one or more arguments, or perhaps none at all Performs an operation Returns one or more values, or perhaps none at all Similar.
Computer Organization and Assembly Language
def-ining a function A function as an execution control structure
 A function is a named sequence of statement(s) that performs a computation. It contains  line of code(s) that are executed sequentially from top.
Functions Jay Summet.
Programming Techniques
Presentation transcript:

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, reuse it and save some time. Also functions are a key way to define interfaces so programmers can share their code.

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".

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!"

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)

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

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 !

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.

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".

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

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".

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."

Thanks!