Introduction to Python Basics Lesson 5 -- Modules

    1 Introduction

        Today is the annual May 1st International Labor Day, do you still remember the seven-day holiday? Wake up bro, it doesn't exist. But first of all, I wish everyone a happy Labor Day. Today, you can treat yourself, take a rest, go out for a walk, it's all okay.

                   

        However, as the flower of the motherland, I still have to continue to work, happiness is achieved through struggle!

        This time I will mainly introduce you to one of the important concepts encountered in Python learning - module, what is this for? What is the use? Why give it such a name? I believe that everyone should have developed hardware. The hardware I am more familiar with is MCU and Arduino. When I use these two hardware systems for competitions, I will encounter some problems: Hey, how can this board function so well, I want to use it to collect data What should I do if I collect voice, collect temperature and humidity, and collect dust concentration?

        In Arduino, we collectively refer to different sensors as modules. Common ones are buzzer module, infrared module, acceleration test module, temperature and humidity sensor module. After adding these, we build hardware circuit and add modules to software programming. We can implement our corresponding functions in the program, and the functions become more powerful , that is, one step closer to winning the prize!

        So we can naturally think that the module is an extension that is imported into Python to enhance its function, and we need to use the special command import to import the module!

    Note: The python code generated in the HTML/XML editor will have a tag language similar to <span style= "color:#333333;" >, this part is not in the code, please ignore it automatically.

    2. Use of modules

        Here we use the built-in math module in our commonly used standard library to see how the module is used.
        First look at this code:
>>>import math #Import math module
>>>math.floor(32.9)#Call the floor method in the math module
32.0

        Pay attention to how it works magic, import the module with import, and then use the function of this module in the format of "module.function" , isn't it very simple! This is indeed the case. All you need to do is to remember the name of the module and the name and usage of the function in the module, which is very convenient when you want to call it. In addition to the above calling method, the following two calling methods are also worthy of our attention and experience.

>>> from math import sqrt
>>> sqrt(9)
3.0
>>>import math
>>> foo=math.sqrt
>>> foo(9)
3.0
>>>import sys #Get the string collection of the specified module search path
>>>sys.path['/Users/yangjiayuan/PycharmProjects/day/day02', '/Users/yangjiayuan/PycharmProjects/day', '/usr/local/Cellar/python@2/2.7.14_3/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/usr/local/Cellar/python@2/2.7.14_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/usr/local/Cellar/python@2/2.7.14_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/usr/local/Cellar/python@2/2.7.14_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/usr/local/Cellar/python@2/2.7.14_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/usr/local/Cellar/python@2/2.7.14_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/usr/local/Cellar/python@2/2.7.14_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/usr/local/Cellar/python@2/2.7.14_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/Users/yangjiayuan/Library/Python/2.7/lib/python/site-packages', '/usr/local/lib/python2.7/site-packages', '/usr/local/lib/python2.7/site-packages/robot-20071211-py2.7.egg', '/usr/local/lib/python2.7/site-packages/framework-0.1.0-py2.7.egg', '/usr/local/lib/python2.7/site-packages/robotframework_ride-2.0a1-py2.7.egg', '/usr/local/lib/python2.7/site-packages/wx-3.0-osx_cocoa', '/Applications/PyCharm.app/Contents/helpers/pycharm_matplotlib_backend']/Users/yangjiayuan/Library/Python/2.7/lib/python/site-packages', '/usr/local/lib/python2.7/site-packages', '/usr/local/lib/python2.7/site-packages/robot-20071211-py2.7.egg', '/usr/local/lib/python2.7/site-packages/framework-0.1.0-py2.7.egg', '/usr/local/lib/python2.7/site-packages/robotframework_ride-2.0a1-py2.7.egg', '/usr/local/lib/python2.7/site-packages/wx-3.0-osx_cocoa', '/Applications/PyCharm.app/Contents/helpers/pycharm_matplotlib_backend']/Users/yangjiayuan/Library/Python/2.7/lib/python/site-packages', '/usr/local/lib/python2.7/site-packages', '/usr/local/lib/python2.7/site-packages/robot-20071211-py2.7.egg', '/usr/local/lib/python2.7/site-packages/framework-0.1.0-py2.7.egg', '/usr/local/lib/python2.7/site-packages/robotframework_ride-2.0a1-py2.7.egg', '/usr/local/lib/python2.7/site-packages/wx-3.0-osx_cocoa', '/Applications/PyCharm.app/Contents/helpers/pycharm_matplotlib_backend']/usr/local/lib/python2.7/site-packages/wx-3.0-osx_cocoa', '/Applications/PyCharm.app/Contents/helpers/pycharm_matplotlib_backend']/usr/local/lib/python2.7/site-packages/wx-3.0-osx_cocoa', '/Applications/PyCharm.app/Contents/helpers/pycharm_matplotlib_backend']
import them
print os.getcwd() #Get the current path
print os.listdir('/mnt') #List all directories and files under /mnt
print os.mkdir('wuyijie') #Create directory wuyijie
print os.mkdir('people') #Create directory people
print os.rmdir('people')                        #删除people

Simple use of two commonly used sys and os modules: ( note that one of the following codes is executed in the shell, and the other is executed in pycharm )

    3. Use your own modules

    As shown in the figure below, there are two python files in my pycharm day02 project, one is sys.py and the other is login.py.

    

    Among them, login.py is a simple login program written by myself. As long as you enter the correct user name and password, you can see the welcome prompt for successful login. You can also modify the name and password yourself. The program is very simple, and everyone can understand it at a glance.

import getpass
_username='alex'
_password='123'
user=raw_input("Please input username:") #raw_input is user input
pwd= raw_input("Please input password:")
if user==_username and pwd==_password:
    print("Welcome user %s login"%user)
else:
    print("Wrong username or password")

    The other is the content in sys.py, where I commented out the previous part, leaving only the last import login, which is equivalent to calling the login module written by myself in a new location, but you must remember to Save your own login module and save it in the same project file.

'''
import sys


print sys.path
print sys.argv[0]
print sys.argv[1]


import them

os_res=os.system("ls -l")
os.mkdir("new_dir")

os_res=os.popen("ls -l").read()


print os_res'''


import login

    At the same time, remember to go to sys.py to execute, enter different user names and passwords, and you will get the results similar to the following.


     4.Attention

  1.  Some of the above codes are executed in the Python shell, and some are executed in pycharm.
  2. For more information about the module, please refer to: Liao Xuefeng's official website , learn more.
  3. All my programs are the result of running in the environment of Python 2.7, including the interpreter in pycharm, which is also Python 2.7. If you install a higher version of Python, you need to pay attention to the use of some syntax.
  4. Next chapter preview: Strings




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325776328&siteId=291194637