This is the 8th day of my participation in the August More text Challenge. For details, see: August More Text Challenge

The power of Python lies in its rich and powerful standard libraries and third-party libraries (modules). Almost anything you want to implement is supported by a Python library, just like the class libraries in C# or the jars in JAVA or the JS libraries in the front end.

What are the benefits of using modules

Modularity is a concept that exists in any programming language. You can imagine that if you write code in one file all the time, the code will become longer and harder to maintain and upgrade.

In order to write maintainable code, we split the code into different files, so that the amount of code in each file is less. In Python, a.py file can be understood as a module, and modules can refer to each other.

In addition to being able to write your own modules, Python has built-in modules called the standard library, as well as third-party libraries that need to be installed separately

Module Import Method

We already know that every.py file is actually a module, so let’s write a module

Module1.py message="hello world" def say(): print("hello")Copy the code

Import module1 If you import multiple modules separated by commas, such as import module_name1,module_name2

Module1 module1.say(); module1 module1.say();Copy the code

Run it and find that the above two import methods are not faulty, both can work normally, let’s compare the two import methods:

  1. Different invocation methods
  2. The former is to put the name of the imported module into the current module; The latter is to put the name of the imported function or variable into the module of the current operation.
  3. from ... importProvides a simple way to import all projects in a module. However, this statement should not be used too often.
  4. from ... importThe import will not conflict with the names of functions and variables in your file, which will be overwritten (see code for solution).
To prevent name conflicts, alias from Module1 import say as newsay def Say (): print(" not import module say") say() # own newsay() #Copy the code

We already know how to import modules. Is importing packages the same as importing modules

Import package_test # in __init__ from. Import test1 print(package_test1.name)Copy the code

Built-in module

Where they are generally stored

The library: How the library should be used:

1. A datetime module:

The DateTime module is an advanced encapsulation of the time module (the time package is based on C library functions).

# Import modules in different ways Import datetime print(datetime.datetime.now()) # current time 2017-12-27 10:05:16.684310 print(datetime.date.today()) # Print (datetime.datetime.now()+datetime.timedelta(days=10) Print (datetime.datetime.now()+datetime.timedelta(days=-10) Print (datetime.datetime.now()+datetime.timedelta(hours=-10) Print (datetime.datetime.now()+datetime.timedelta(seconds=120) Print (datetime.now()Copy the code

2. The random module:

The Random module in Python is used to generate random numbers

Import random print(random.random()) # 0 <= n < 1.0 print(random.randint(1,7))# print(random.randrange(1,3))# Print (random. Choice (['aa','bb','cc']) Print (random. Randrange (1,9000)+1000Copy the code

3. The sys module:

The SYS module provides a set of variables and functions for the Python runtime environment

Import sys print(sys.platform) import sys print(sys.platform) Print (sys.path) #path is a list of directories from which Python can find third-party extension modules. When Python starts, sys.path is initialized according to the built-in rules, the PYTHONPATH variable. Print (sys.builtin_module_names) # Argv [0] The first argument of the current program name sys.argv[1]Copy the code

4. The OS module:

OS modules contain common operating system functions, such as file operations, directories, and so on, regardless of the specific platform

Import OS print(os.name) # Linux returns' posix' print(os.getcwd())# get the current working directory os.rename("game.py","game1.py")# Rename the file print(os.listdir())# specify the name of all files and directories in all directories Print (os.system("dir")Copy the code

Of course, the above is just a simple let you first understand the use of the built-in module, there are many methods, in the case encountered later we go to list.

Third-party module

1. XLRD module:

Python uses the XLRD module to read and write Excel files

# PIP install XLRD # install XLRD Data = xlrd.open_workbook('test.xlsx') table=data.sheets()[0 Print (table.col_values(0)) # print(table.nrows) # print(table.nrows) Print (table.row_values(I)) print(table.cell(0,0).valueCopy the code

2. Xpinyin module:

A module that turns Chinese characters into pinyin

# PIP install xpinyin from xpinyin import Pinyin p=Pinyin() print(p.ge_pinyin (" Beijing "))Copy the code

3. The Image module:

Module for Python to manipulate images

PIL import Image import OS # PIP install Image im= image.open ("test.jpg") # PIL import Image import OS # PIP install Image im= image.open ("test The thumbnail function takes a tuple as a parameter, which corresponds to the width and height of the thumbnail image. During the thumbnail, the function keeps the image's width to height ratio. If the input parameter width and height are different from the original image aspect ratio, the original scale will be carried out according to the minimum corresponding edge. Im.save ("newtest.jpg","JPEG") # Region = IM.crop ((100,100,300,200)) region.save("croptest.jpeg") # Rotate image # left rotate 45 degrees IM = im.rotate(45) im.save("rotate-l45.jpeg")Copy the code

Python has a lot of very interesting modules and resources, let’s post an address for you to see ha

https://github.com/jobbole/awesome-python-cn