# 4. Modules and packages

## 4.1 module creation and import instruction application

Python module is a python file, ending with. py, which contains Python object definitions and python statements

### 1. Create a module

```# Create a module, including a factorial function f1(n), a list delete function f2(lst,x), a sum function f3(a,d,n) of the sequence of equal difference numbers, and the sum f4(a,b) of the sequence of continuous numbers a - > B

def f1(n):
y = 1
for i in range(1,n+1):
y = y * i
return y
# Create factorial function f1(n)

def f2(lst,x):
while x in lst:
lst.remove(x)
return lst
# Create list delete function f2(lst,x)

def f3(a,d,n):
an = a
s = 0
for i in range(n-1):
an = an + d
s = s + an
return s
# Create sum function f3(a,d,n)

def f4(a,b):
an = a
s = 0
for i in range(a,b):
s = s + an
an = an + 1
return s
#Create function f4
# Create module tm2, including 4 functions
```

### 2. Module path problem

```import pandas
print(pandas.__file__)
# View the path of the existing package, and save the created package into the modified path

import sys
# Load the sys package and add the path of the new testmodel
``` ### 3. Call module statement: import

Copy the prepared model to the site packages directory

```import tm2

print(tm2.f1(15))
print(tm2.f2([2,3,4,5,5,5,6,6,4,4,4,4],4))
print(tm2.f3(10,2,10))
print(tm2.f4(1,101))

# Directly use import to call the module,. f1() to call the module function (method)
``` ### 4. python standard module random random number

```import random

x = random.random()
y = random.random()
print(x,y*10)
# random.random() randomly generates a random number of [0:1]

m = random.randint(0,10)
print(m)
# random.randint() randomly generates an integer of [0:10]

st1 = random.choice(list(range(10)))
st2 = random.choice('abcdnehgjla')
print(st1,st2)
# random.choice() randomly gets an element in (), which must be an ordered type

lst = list(range(20))
sli = random.sample(lst,5)
print(sli)
# random.sample(a,b) randomly obtains the fragment with the specified length of B in a without changing the original sequence

lst = [1,3,5,7,9,11,13]
random.shuffle(lst)
print(lst)
# random.shuffle(list) scrambles elements in a list
``` ### 5. time module

```import time

for i in range(2):
print('hello')
time.sleep(1)
# time.sleep() program rest () seconds

print(time.ctime())
print(type(time.ctime()))
# Convert the current time to a string

print(time.localtime())
print(type(time.localtime()))
# Convert the current time to the struct time of the current time zone
# wday 0-6 means Sunday to Saturday
# ydat 1-366 day of the year
# Whether isdst is daylight saving time, the default is - 1

print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()))
# time.strftime(a,b)
# a is formatted string format
# b is the time stamp, usually localtime()
``` %y two digit year representation (00-99)
%Y four digit year representation (000-9999)
%m month (01-12)
%Day in d month (0-31)
%H 24 hours (0-23)
%I 12 hour hours (01-12)
%M minutes (00 = 59)
%S seconds (00-59)
%a local simplified week name
%A local full week name
%b local simplified month name
%B local full month name
%c local corresponding date and time representation
%j day of the year (001-366)
%P local A.M. or P.M. equivalent
%U weeks of the year (00-53) Sunday is the beginning of the week
%w week (0-6), Sunday is the beginning of the week
%W weeks in the year (00-53) Monday is the beginning of the week
%x local corresponding date representation
%X local corresponding time representation
%Z name of the current time zone
%%% itself  Published 8 original articles, won praise 3, visited 160

Tags: Python Fragment REST

Posted on Thu, 05 Mar 2020 22:28:55 -0800 by saloon12yrd