Python Libraries Importing and Calling functions.

Slides:



Advertisements
Similar presentations
Escape Sequences \n newline \t tab \b backspace \r carriage return
Advertisements

Methods Java 5.1 A quick overview of methods
Python Programming Chapter 5: Fruitful Functions Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Arithmetic in Pascal (2) Arithmetic Functions Perform arithmetic calculations Gives an argument to the function and it returns the result.
Sum and Difference Identities for Sine and Tangent
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
1 ICS103 Programming in C Lecture 5: Introduction to Functions.
CS 201 Functions Debzani Deb.
Week 3 parameters, return, math, graphics Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs with Scanner.
Week 3 parameters, return, math, graphics Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise.
1 Python Programming: An Introduction to Computer Science Chapter 3 Objects and Graphics.
Starter  Identify the separate tasks to be performed in the programming task below (break it down into numbered sections).  A program is needed to prompt.
UNIT CIRCLE. Review: Unit Circle – a circle drawn around the origin, with radius 1.
Microsoft® Small Basic The Math Object Estimated time to complete this lesson: 1 hour.
INTRODUCTION TO PYTHON PART 1 CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
CS1313: Standard Library Functions Lesson CS1313 Spring Standard Library Functions Outline 1.Standard Library Functions Outline 2.Functions in Mathematics.
Visual C++ Programming: Concepts and Projects Chapter 6A: Methods (Concepts)
Chapter 13 Section 3 Radian Measure.
Lecture 2: Static Methods, if statements, homework uploader.
Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.
Functions and Modules CSIS 1595: Fundamentals of Programming and Problem Solving 1.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs.
CSE1222: Lecture 4The Ohio State University1. Mathematical Functions (1)  The math library file cmath Yes, this is a file with definitions for common.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 6 Value-Returning.
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
Unit Circle You will use the Unit Circle for nearly every computation for the rest of Trig. Make the most of today… Memorize the angles and Radians Memorize.
CSE202: Lecture 4The Ohio State University1 Mathematical Functions.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 2.
Matlab Basics Tutorial. Vectors Let's start off by creating something simple, like a vector. Enter each element of the vector (separated by a space) between.
Python – Procedures If the same piece of code needs to be used several times we can use a loop - but only if those times are all together. If you need.
Week 2 - Friday.  What did we talk about last time?  Base systems  C literals  Representations in memory.
Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo.
Math With Java The Math Class. First, A Quick Review of Math Operators in Java Primitive Data type in Java that represent numbers: Primitive Data type.
1 ICS103 Programming in C Lecture 7: Introduction to Functions.
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS Jehan-François Pâris
Warm up. - Fundamental Trigonometric Identities Chapter 6 Analytic Trigonometry Language Objectives: We will learn about the More of Trigonometric Functions.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Week 3 parameters and graphics Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted,
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
BY: MARIAH & JON 13.4 INVERSE OF SINE AND TANGENT.
If the same piece of code needs to be used several times we can use a loop – but only if those times are all together. If you need to run the same bit.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Documenting a function. Documenting a function definition We have a template for information that we need you to put at the top of each function - if.
Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.
Let’s Learn 3. Modules Saenthong School, January – February 2016
12. MODULES Rocky K. C. Chang November 6, 2015 (Based on from Charles Dierbach. Introduction to Computer Science Using Python and William F. Punch and.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 6A Methods (Concepts)
Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same.
CS 115 Lecture 5 Math library; building a project Taken from notes by Dr. Neil Moore.
14.1 The Unit Circle Part 2. When measuring in radians, we are finding a distance ____ the circle. This is called. What is the distance around a circle?
Trigonometry Section 7.4 Find the sine and cosine of special angles. Consider the angles 20 o and 160 o Note: sin 20 o = sin160 o and cos 20 o = -cos 160.
ENGINEERING 1D04 Tutorial 4. What are we doing today? Focus Functions Scope of Variables Returning Values Objects Graphics library Aliasing Events Mouse.
Lesson 06: Functions Class Participation: Class Chat:
Exam #1 You will have exactly 30 Mins to complete the exam.
Library Functions Goals of software engineering reliable code
IGCSE 4 Cambridge Data types and arrays Computer Science Section 2
Functions, Procedures, and Abstraction
Lesson 06: Functions Class Chat: Attendance: Participation
Rocky K. C. Chang 15 November 2018 (Based on Dierbach)
COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
Introduction to Value-Returning Functions: Generating Random Numbers
How About Some PI? Trigonometry Feb 18,2009.
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.
Week 2 - Friday CS222.
What is the radian equivalent?
Using Modules.
Presentation transcript:

Python Libraries Importing and Calling functions

Library – a group of prewritten functions and predefined constants Many actions are frequently done in programming, like taking the square root, finding a sine or cosine, drawing a graphics shape, getting a random number The maker of the language (or sometimes a third party) creates a library which contains these actions already written for you. You don’t have to know how to do these things, you just know how to call the function given in the library. Many disciplines use special constants. The most famous one is pi. It is convenient to have these already defined instead of having to look them up and type them in yourself.

Importing a library Before you are allowed to use library functions, you have to import the library into your code There are 3 different ways to do this The simplest is just “import math” (to get the math library) If you use this method, you have to qualify the names of the functions with “math.” example “math.sqrt” The next way is “from math import sqrt” (to get one specific function) If you use this method, you can just use the function name “sqrt” The third way is “from math import *” (to get all functions in the library) Again, you do not have to qualify names if you do this, just “sqrt”

Using the functions After you have done the import statement, you need to call or invoke the library functions This is done in two ways, depending on whether the function returns a value or does not A function which returns a value is called as part of another statement A function which does not return a value is called as a stand-alone statement

Example of using a library function and constant from math import sin, cos def main(): angle = float(input(“Enter an angle in radians “)) radius = float(input(“Enter a radius”)) print(sin(angle), cos(angle)) s = sin(angle) c = cos(angle) area = pi * radius ** 2

Example of using a library function which does not return a value from graphics import * def main(): win = GraphWin() # returns a value win.close() # does not return a value

A few notes about libraries Note where the import statement was placed in the examples It is put before and outside of the main function definition It is better in general to be put near the top of your source file and outside all function definitions Why? Because that way all functions in your file can use the library functions (it has to do with scope, which will be discussed in chapter 5) Don’t include a library if you do not need to use it – it only confuses things and reduces the confidence of anyone reading your code! You do not need to import the math library to use the math operators like +, *, -, //, %, etc.