Standard Library Functions and the import Statement Standard library: library of pre-written functions that comes with Python Library functions perform tasks that programmers commonly need Example: print, input, range Viewed by programmers as a “black box” Some library functions built into Python interpreter To use, just call the function Standard Library Functions and the import Statement (cont’d.) Modules: files that stores functions of the standard library Help organize library functions not built into the interpreter Copied to computer when you install Python To call a function stored in a module, need to write an import statement Written at the top of the program Format: import module_name Standard Library Functions and the import Statement (cont’d.) Standard Library Functions and the import Statement (cont’d.) Numeric and Mathematical Modules numbers: Numeric abstract base classes math: Mathematical functions cmath: Mathematical functions for complex numbers decimal: Decimal fixed-point and floating-point arithmetic fractions: Rational numbers random: Generate pseudo-random numbers statistics: Mathematical statistics functions Generating Random Numbers
Random number are useful in a lot of
programming tasks random module: includes library functions for working with random numbers Dot notation: notation for calling a function belonging to a module Format: module_name.function_name() Generating Random Numbers (cont’d.) randint function: generates a random number in the range provided by the arguments Returns the random number to part of program that called the function Returned integer can be used anywhere that an integer would be used You can experiment with the function in interactive mode Generating Random Numbers (cont’d.) Generating Random Numbers (cont’d.) Generating Random Numbers (cont’d.) randrange function: similar to range function, but returns randomly selected integer from the resulting sequence Same arguments as for the range function random function: returns a random float in the range of 0.0 and 1.0 Does not receive arguments uniform function: returns a random float but allows user to specify range Random Number Seeds Random number created by functions in random module are actually pseudo- random numbers Seed value: initializes the formula that generates random numbers Need to use different seeds in order to get different series of random numbers By default uses system time for seed Can use random.seed() function to specify desired seed value The math Module math module: part of standard library that contains functions that are useful for performing mathematical calculations Typically accept one or more values as arguments, perform mathematical operation, and return the result Use of module requires an import math statement The math Module (cont’d.) Number-theoretic and representation functions math.fabs(x): Absolute value of x. math.ceil(x): Ceiling of x, the smallest integer greater than or equal to x. math.floor(x): Floor of x, the largest integer less than or equal to x. math.trunc(x): x with the fractional part removed, leaving the integer part (rounds toward 0). The math Module (cont’d.) Power and logarithmic functions math.exp(x): e raised to the power x. math.log(x[, base]): Natural logarithm of x (to base e) or logarithm of x to the given base. math.log10(x): Base-10 logarithm of x. math.sqrt(x): Square root of x. The math Module (cont’d.) Trigonometric functions math.acos(x): Arc cosine of x, in radians. The result is between 0 and pi. math.asin(x): Arc sine of x, in radians. The result is between -pi/2 and pi/2. math.atan(x): Arc tangent of x, in radians. The result is between -pi/2 and pi/2. math.cos(x): Cosine of x radians. math.sin(x): Sine of x radians. math.tan(x): Tangent of x radians. The math Module (cont’d.) Angular conversion math.degrees(x): Converts angle x from radians to degrees. math.radians(x): Converts angle x from degrees to radians. The math Module (cont’d.) Hyperbolic functions Analogs of trigonometric functions that are based on hyperbolas instead of circles. math.acosh(x): Inverse hyperbolic cosine of x. math.asinh(x): Inverse hyperbolic sine of x. math.atanh(x): Inverse hyperbolic tangent of x. math.cosh(x): Hyperbolic cosine of x. math.sinh(x): Hyperbolic sine of x. math.tanh(x): Hyperbolic tangent of x. The math Module (cont’d.) Constants math.pi: 3.141592… math.e: 2.718281… math.tau: 6.283185… (equal to 2π) math.inf: A floating-point positive infinity. (For negative infinity, use -math.inf).
Python Advanced Programming: The Guide to Learn Python Programming. Reference with Exercises and Samples About Dynamical Programming, Multithreading, Multiprocessing, Debugging, Testing and More