The module is a file that consists of functions, variables, and classes. Modules in Python are considered code libraries in other languages.
The built-in functions are the parts of the Python standard module. Click the link to see the built-in functions.
2. Built-in Functions — Python 3.6.15 documentation
2. Built-in Functions — Python 3.6.15 documentation
2. Built-in Functions The Python interpreter has a number of functions and types built into it that are always available. They are listed here in alphabetical order. abs(x) Return the absolute value of a number. The argument may be an integer or a floating
docs.python.org
You must import the built-in modules.
Here are some examples of importing the modules.
math
import math
# from math import factorial
# pi
print(math.pi)
# 2 x 2 x 2
print('2 x 2 x 2 =', math.pow(2, 3))
# Factorial
print('3!=',math.factorial(3))
print(math.factorial(984))
# ceil()
print(math.ceil(3.1))
# floor()
print(math.floor(3.9))
# sqrt()
print(math.sqrt(5))
calendar
import calendar
# calendar()
cal = calendar.calendar(2019)
print(cal)
# prcal() : print calendar
calendar.prcal(2022)
# prmonth() :print month
calendar.prmonth(2022,11)
# weekday() : week information
# Mon(0),Tue(1),Wed(2),Thur(3),Fri(4),Sat(5),Sun(6)
weekday = calendar.weekday(2022,11,28)
print('weekday:', weekday)
random
import random
# random() : 0.0 ~ 1.0 random number
r1 = random.random()
print('r1=', r1)
# randint(a, b) : a ~ b random interger
r2 = random.randint(1, 10)
print('r2=', r2)
# 1 ~ 45 random number
r3 = random.randint(1, 45)
print('r3=', r3)
# choice() : choose randomly in the list
list = ['red','orange','yellow','green','blue','navy','purple']
r4 = random.choice(list)
print('r4=', r4)
Lottery program Example
import random
lot = [] # list
# lot.append(random.randint(1,45))
# lot.append(random.randint(1,45))
# print(lot)
while True:
r = random.randint(1,45)
if r not in lot:
lot.append(r)
if len(lot) == 6:
break
print(sorted(lot))

time
time() function returns the number of seconds passed since the epoch. For the Unix system, January 1, 1970, 00:00:00 at UTC is epoch (the point where time begins).
import time
print(time.time())
#localtime()
print(time.localtime(time.time()))
print(time.asctime(time.localtime(time.time())))
print(time.ctime())
print(time.strftime('%x', time.localtime(time.time())))
print(time.strftime('%c', time.localtime(time.time())))
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
#sleep()
for i in range(10):
print(i)
time.sleep(2)


web browser
import webbrowser
webbrowser.open('http://www.google.com')
webbrowser.open('http://www.naver.com')
webbrowser.open_new('member.html')
custom module Example 1)
mypi = 3.14
def area(r):
return mypi * r * r
Importing mymath (custom module)
import mymath
print(mymath.mypi) # 3.14
print(mymath.area(5)) # 78.5
custom module Example 2)
def plus(a,b):
return a+b
def minus(a,b):
return a-b
def multiply(a,b):
return a*b
def divide(a,b):
return a/b
Importing calculator (custom module)
import calculator
print(calculator.plus(10, 5))
print(calculator.minus(10, 5))
print(calculator.multiply(10, 5))
print(calculator.divide(10, 5))
When you import some parts of the module, you will only be able to use the imported functions or variables.
# from module import variable/function
from calculator import plus
from calculator import minus
print(calculator.plus(10, 5)) # error
print(plus(10, 5))
print(minus(10, 5))
print(multiply(10, 5)) # error
print(divide(10, 5)) # error
To import all the variables and functions
from calculator import *
print(plus(10, 5))
print(minus(10, 5))
print(multiply(10, 5))
print(divide(10, 5))
To use alias
This is the most used way in Python.
import calculator as c
print(c.plus(10,5))
print(c.minus(10,5))
print(c.multiply(10,5))
print(c.divide(10,5))
You can also install external modules and import them
c:\> pip install numpy
c:\> pip install pandas
c:\> pip install tensorflow
import numpy as np
import pandas as pd
import tensorflow as tf
To see if the modules are installed, go to settings -Python Interpreter and check.

Packages in Python are directories that contain modules. It is like a folder.
To create a graph with matplotlib.
import matplotlib.pyplot as plt
plt.plot([1,2,3,4,5,6,7,8,9,8,7,6,5,4,3,2,1])
plt.ylabel('some numbers')
plt.show()

'Python' 카테고리의 다른 글
Python) Data Analysis - pandas (0) | 2022.12.03 |
---|---|
Python) functions for data analysis (0) | 2022.11.30 |
Python) Data Analysis - numpy (0) | 2022.11.24 |
Python) Files input and output (0) | 2022.11.22 |
Python) Regular Expression (0) | 2022.11.21 |