Python Mini-Course University of Oklahoma Department of Psychology Day 1 – Lesson 4 Beginning Functions 4/5/09 Python Mini-Course: Day 1 - Lesson 4 1.

Slides:



Advertisements
Similar presentations
TWO STEP EQUATIONS 1. SOLVE FOR X 2. DO THE ADDITION STEP FIRST
Advertisements

Basic Java Constructs and Data Types – Nuts and Bolts
Combining Like Terms. Only combine terms that are exactly the same!! Whats the same mean? –If numbers have a variable, then you can combine only ones.
Combining Like Terms. Only combine terms that are exactly the same!! Whats the same mean? –If numbers have a variable, then you can combine only ones.
0 - 0.
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
MULTIPLICATION EQUATIONS 1. SOLVE FOR X 3. WHAT EVER YOU DO TO ONE SIDE YOU HAVE TO DO TO THE OTHER 2. DIVIDE BY THE NUMBER IN FRONT OF THE VARIABLE.
Addition Facts
Python Mini-Course University of Oklahoma Department of Psychology
Lilian Blot PART III: ITERATIONS Core Elements Autumn 2012 TPOP 1.
CMPT 120 Functions and Decomposition
Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
Lilian Blot PART V: FUNCTIONS Core elements Autumn 2013 TPOP 1.
Past Tense Probe. Past Tense Probe Past Tense Probe – Practice 1.
Addition 1’s to 20.
Test B, 100 Subtraction Facts
Week 1.
Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1.
Lilian Blot CORE ELEMENTS PART V: FUNCTIONS PARAMETERS & VARIABLES SCOPE Lecture 5 Autumn 2014 TPOP 1.
CHAPTER 11 FILE INPUT & OUTPUT Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Chapter Modules CSC1310 Fall Modules Modules Modules are the highest level program organization unit, usually correspond to source files and.
Setting the PYTHONPATH PYTHONPATH is where Python looks for modules it is told to import List of paths Add new path to the end with: setenv PYTHONPATH.
Lecture 04 – Classes.  Python has a number of classes built-in  lists, dictionaries, sets, int, float, boolean, strings  We can define our own classes.
Perkovic, Chapter 7 Functions revisited Python Namespaces
User Defined Functions Lesson 1 CS1313 Fall User Defined Functions 1 Outline 1.User Defined Functions 1 Outline 2.Standard Library Not Enough #1.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 8 Fruitful Functions 05/02/09 Python Mini-Course: Day 2 - Lesson 8 1.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 28 Classes and Methods 6/17/09 Python Mini-Course: Lesson 28 1.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 26 Classes and Objects 6/16/09 Python Mini-Course: Lesson 26 1.
Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 15 Tuples 5/02/09 Python Mini-Course: Day 4 – Lesson 15 1.
Python Programming Chapter 3: Functions Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Recitation 1 Programming for Engineers in Python.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
FUNCTIONS. Function call: >>> type(32) The name of the function is type. The expression in parentheses is called the argument of the function. Built-in.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Functions.
Python – Part 3 Functions 1. Function Calls Function – A named sequence of statements that performs a computation – Name – Sequence of statements “call”
Module 3 Fraser High School. Module 3 – Loops and Booleans import statements - random use the random.randint() function use while loop write conditional.
Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo.
Functions Chapter 4 Python for Informatics: Exploring Information
Overview Intro to functions What are functions? Why use functions? Defining functions Calling functions Documenting functions Top-down design Variable.
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.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
Python Functions : chapter 3
FUNCTIONS. Topics Introduction to Functions Defining and Calling a Void Function Designing a Program to Use Functions Local Variables Passing Arguments.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
3. COMPUTING WITH NUMBERS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
Python Basics  Functions  Loops  Recursion. Built-in functions >>> type (32) >>> int(‘32’) 32  From math >>>import math >>> degrees = 45 >>> radians.
Functions Chapter 4 Python for Informatics: Exploring Information Slightly modified by Recep Kaya Göktaş on March 2015.
PROGRAMMING USING PYTHON LANGUAGE ASSIGNMENT 1. INSTALLATION OF RASPBERRY NOOB First prepare the SD card provided in the kit by loading an Operating System.
CS 115 Lecture 5 Math library; building a project Taken from notes by Dr. Neil Moore.
Lesson 06: Functions Class Participation: Class Chat:
Exam #1 You will have exactly 30 Mins to complete the exam.
Topics Introduction to Functions Defining and Calling a Void Function
Fundamentals of Programming I Managing the Namespace
Functions CIS 40 – Introduction to Programming in Python
Introduction to Programming
Functions, Procedures, and Abstraction
Lesson 06: Functions Class Chat: Attendance: Participation
Variables and Expressions
Bryan Burlingame 13 February 2019
Python for Informatics: Exploring Information
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Introduction to Value-Returning Functions: Generating Random Numbers
Python Modules.
Functions, Procedures, and Abstraction
 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.
Using Modules.
Presentation transcript:

Python Mini-Course University of Oklahoma Department of Psychology Day 1 – Lesson 4 Beginning Functions 4/5/09 Python Mini-Course: Day 1 - Lesson 4 1

Lesson objectives 1. State the purpose of functions and modules in Python 2. Use built-in functions 3. Import modules and use imported functions 4. Create custom void functions 5. Discuss the concept of variable scope 4/5/09 Python Mini-Course: Day 1 - Lesson 4 2

Functions Function A named sequence of statements that performs a computation or action Functions are called by name Most functions accept inputs (arguments) Some functions return results (return value) 4/5/09 Python Mini-Course: Day 1 - Lesson 4 3

Functions We’ve already seen some functions: type() Type casting functions int(), float(), str() 4/5/09 Python Mini-Course: Day 1 - Lesson 4 4

Modules Module A file that contains a collection of related functions Python has hundreds of standard modules These are known as the Python Standard Library ( You can also create and use add-in modules 4/5/09 Python Mini-Course: Day 1 - Lesson 4 5

Using import To use a module, you first have to import it into your namespace To import the entire module import module_name To import specific functions from module_name import function_name 4/5/09 Python Mini-Course: Day 1 - Lesson 4 6

The math module The standard math module includes: Number-theoretic and representation functions Power and logarithmic functions Trigonometric functions Hyperbolic functions Angular conversion Constants 4/5/09 Python Mini-Course: Day 1 - Lesson 4 7

Using the math module import math degrees = 45 radians = degrees / \ * 2 * math.pi print math.sin(radians) x = math.sin(degrees / \ * 2 * math.pi) 4/5/09 Python Mini-Course: Day 1 - Lesson 4 8

Dot notation Why did we use math.sin() instead of just sin() ? Try this: print sin(radians) Dot notation allows the Python interpreter to organize and divide the namespace 4/5/09 Python Mini-Course: Day 1 - Lesson 4 9

More on Importing from math import * print sin(2) Be careful when using the import * command. It can easily lead to namespace conflicts. 4/5/09 Python Mini-Course: Day 1 - Lesson 4 10

Creating your own functions You have to define the function Example: def print_lyrics(): print "I'm a lumberjack, and I'm okay." print "I sleep all night and I work all day." 4/5/09 Python Mini-Course: Day 1 - Lesson 4 11

Composing functions def repeat_lyrics(): print_lyrics() repeat_lyrics() 4/5/09 Python Mini-Course: Day 1 - Lesson 4 12

Functions with arguments def print_twice(in_text): print in_text print_twice(‘Spam’) print_twice(‘Spam’*4) 4/5/09 Python Mini-Course: Day 1 - Lesson 4 13

Variable scope Scope The enclosing context where values and expressions are associated (partition in namespace) Variables inside functions are local 4/5/09 Python Mini-Course: Day 1 - Lesson 4 14

Variable scope def cat_string(part1, part2): cat = part1 + part2 print cat cat_string(‘This ‘, ‘works’) print cat 4/5/09 Python Mini-Course: Day 1 - Lesson 4 15

Documentation You can document functions in the code immediately after the function header Example: func_doc.py 4/5/09 Python Mini-Course: Day 1 - Lesson 4 16

Before next time Practice creating and using your own functions (try the exercises on pp 26-28) Practice using the math module (see for documentation) 4/5/09 Python Mini-Course: Day 1 - Lesson 4 17