# 8.4: Standard Library Functions

$$\newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$

$$\newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}}$$

$$\newcommand{\id}{\mathrm{id}}$$ $$\newcommand{\Span}{\mathrm{span}}$$

( \newcommand{\kernel}{\mathrm{null}\,}\) $$\newcommand{\range}{\mathrm{range}\,}$$

$$\newcommand{\RealPart}{\mathrm{Re}}$$ $$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$

$$\newcommand{\Argument}{\mathrm{Arg}}$$ $$\newcommand{\norm}[1]{\| #1 \|}$$

$$\newcommand{\inner}[2]{\langle #1, #2 \rangle}$$

$$\newcommand{\Span}{\mathrm{span}}$$

$$\newcommand{\id}{\mathrm{id}}$$

$$\newcommand{\Span}{\mathrm{span}}$$

$$\newcommand{\kernel}{\mathrm{null}\,}$$

$$\newcommand{\range}{\mathrm{range}\,}$$

$$\newcommand{\RealPart}{\mathrm{Re}}$$

$$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$

$$\newcommand{\Argument}{\mathrm{Arg}}$$

$$\newcommand{\norm}[1]{\| #1 \|}$$

$$\newcommand{\inner}[2]{\langle #1, #2 \rangle}$$

$$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\AA}{\unicode[.8,0]{x212B}}$$

$$\newcommand{\vectorA}[1]{\vec{#1}} % arrow$$

$$\newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow$$

$$\newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$

$$\newcommand{\vectorC}[1]{\textbf{#1}}$$

$$\newcommand{\vectorD}[1]{\overrightarrow{#1}}$$

$$\newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}}$$

$$\newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}}$$

$$\newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$

$$\newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}}$$

There are many modules that do not load into memory when you start Python, but they come with the installation, and these contain functions that you can use, but you must import them to use them.  It is best practice to put all of your import statements at the top of your code, so someone who is reading it can instantly see what  modules you need to import. These modules are single files that can define functions, classes and variables. To use a function you treat it as a class method, with the following syntax

module_name.function_name(arguments)


To find what type of modules you have installed you can run the following command on your computer or Raspberry Pi

#the following code will list the modules installed on your computer.
help('modules')


If you want to see what modules are running on the Jupyter Hub of LibreText you can run the command below.  It will take a minute, and probably give you a few warnings.

#the following code will list modules installed on LibreText's Jupyter Hub
#Note: you may get multiple warning for modules being deprecated, just be patient, and you will see the list
help('modules')
Hello world!

To run a module you first need to import it

## Import Statement

The import statement loads a module into the program's memory. Please go to the modules section to learn about specific modules.  To see this, we will look at the random module.  The following code will generate an integer between 1 and 999.

random_integer=random.randint(1,999)
print(random_integer)

        hello world


The above failed because we did not load the random module into memory

import random
random_integer=random.randint(1,999)
print(random_integer)

        hello world


Note, if you ran the code above, you loaded the random module into memory, and you do not need to reload it (the code that did not work will now work).

random_integer=random.randint(1,999)
print(random_integer)

        hello world


Checking the class of the random module

import random
print(type(random))
Hello world!

## Some Common Standard Modules

Here are some of the standard modules we will use in this class.  Any code that uses these functions must first import them.

### datetime

• Docs Page
• offers different classes for working with time.  Note, epoch time
import datetime
print(datetime.datetime.now())
print(datetime.date.today())
print(10*"*")
now=datetime.datetime.now()
print(now.strftime("%H:%M:%S"))

Hello world!

### math

• Docs Page
• Provides basic mathematical  operations. Note, cmath deals with complex numbers
import math
print(math.pi)
print(math.ceil(3.4))
print(math.floor(3.4))
print(math.pow(2,8))
print(math.sqrt(16))
#to convert 120 degrees to radians
#to get the cosine of 120
print(math.sin(rad))
Hello world!

### random

import random
print(random.randint(1,100))
print(random.randint(1,100))
print(10*"*")
print(random.randrange(0,100,2))
print(random.randrange(0,100,5))
print(random.randrange(0,100,10))
print(random.choice('word'))
even_numbers=[2,4,6,8]
random.shuffle(even_numbers)
print(even_numbers)

Hello world!

#### Random Seed

Random seed lets you reproduce a random number every time.

import random
random.seed(111)
#run this code multiple times and you always get 28,41,64
print(random.randint(1,100))
print(random.randint(1,100))
print(random.randint(1,100))
Hello world!

### Socket

The socket module lets you work with network protocols and enable communication between devices over the network.  we use it in the "Find IP Address" code that we use to get the IP address of the Raspberry Pi

### Statistics

import statistics
print(f"The median is the value in the middle of a data set of 2,2,2,4,6, which is: {statistics.median([2,2,2,4,6])}")
print(f"The mean is the average value of 2,2,2,4,6, which is: {statistics.mean([2,2,2,4,6])}")
print(f"The mode is the most common value of 2,2,2,4,6, which is: {statistics.mode([2,2,2,4,6])}")
print(f"The standard deviation is the square root of the varians, which for 2.11,2.20,2.14,2.14,2.16, which is: {statistics.stdev([2.11,2.20,2.14,2.14,2.16])}")

Hello world!

### Subprocess

• Docs Page
• This allows you to start other processes from your python  code,  and these can be run in different languages.  For example, the following code on a windows machine will read the current directory from the command line. NOTE: if you are on a WINDOWS machine, change the "ls" to "dir",   as that is the windows command for showing files in the current directory.
import subprocess
result = subprocess.run(["ls"], shell=True, capture_output=True, text=True)
print(result.stdout)


So the above code runs the BASH command ls from python

### time

• Docs Page
• provides functions to work with time related tasks
• epoch time is the number of seconds since Jan 1, 1970
• the sleep function is very useful in data collection, but you must be careful if you run it before or after a process, as in the later case, the process time is added to the sleep time.
import time
print(time.gmtime(0))
print(time.gmtime())
print(f"time since epoch: {time.time()} seconds")
print("print now")
time.sleep(3)
print("it is 3 seconds later")
Hello world!

### urllib

This page titled 8.4: Standard Library Functions is shared under a not declared license and was authored, remixed, and/or curated by Robert Belford.