160 likes | 464 Views
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
E N D
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.