#!/usr/bin/env python # coding: utf-8 # # 3 Python Standard Libraries # # Open In Colab # # ## Topics # - standard libraries # - include and use libraries # # ## 3.1 Standard libraries # # - Python has several standard libraries (modules) you can readily import # - one can use the names (functions and data/constants) defined in those imported modules # - list of all the Python standard libraries: # https://docs.python.org/3/library/index.html # # - syntax # # ```python # # first import library # import libraryName # # # use data and functions provided by the library # libraryName.data # libraryName.function() # ``` # ## 3.2 math library # https://docs.python.org/3/library/math.html # # - an important library that provides mathematical functions # - run help(moduleName) to get more information about the module # In[1]: import math # In[2]: help(math) # In[3]: num = 10.5 # math.ceil(x) - return the ceiling (or round up) of x, # the smallest integer greater than or equal to x print(math.ceil(num)) # In[4]: # math.floor(x) # return the floor (or round down) of x, the largest integer less than or equal to x print(math.floor(num)) # In[5]: # math.gcd(a, b) # return the greatest common divisor of the integers a and b # if both and b are 0, returns 0 print(math.gcd(0, 0)) print(math.gcd(10, 20)) # In[6]: # math.pow(x, y) # returns x raised to the power y print(math.pow(2, 10)) # In[7]: # math.sqrt(x, y) # returns the square root of x print(math.sqrt(100)) # In[8]: # math.radians(x) # convert and return angle x in degrees to radians rad = math.radians(90) # In[9]: # math.sin(x) # return the sine of x radians print(math.sin(rad)) # In[10]: # Some constants/data defined in math module math.pi # In[11]: math.inf # In[12]: math.e # ## 3.3 Other common libraries # - all Python libraries: https://docs.python.org/3/library/index.html # - some libraries we'll use and you may want to explore more # # - **os** - operating system related # - **time** - time access and conversion # - **random** - generate pseudo-random numbers # - **sys** - system specific data and functions # - **string** - common string operations and data # In[ ]: