1 / 7

Python Libraries

Python Libraries. Importing and Callin g 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

Download Presentation

Python Libraries

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Python Libraries Importing and Calling functions

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

  3. 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”

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

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

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

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

More Related