In the daily development work, we have to write a lot of Python code, if all written in a file, it will cause the code is particularly difficult to maintain, in order to expand the maintainability of the code, we write the letter in different files, so that each file contains fewer files, logic more clear. In Python, the file we create is basically the end of a. py, and the. py file is called a module.
In order to facilitate the management of convenient management module, Python also cited the concept of package. Each package has a __init__.py file, which must exist, otherwise Python will treat the directory as a normal directory, not a package. __init__.py can be either an empty file or a Python code, because __init__.py itself is a module, for example: The test directory has __init__.py, aaa.py,bbb.py three files, as shown in:
[Email protected] ~]# tree test
Test
├──aaa.py
├──bbb.py
└──__init__.py
0 directories, 3 files
Import Imports
Import the module using the import statement, the import statement syntax is as follows:
Import Module
Keyword Module name
Use methods such as:
Import Math #入导math模块
Math.floor () #调用math模块中的floor () function
If you want to import multiple modules at the same time, you only need to delimit them with commas before the module name:
Import Module1,module2,module3 .....
The method of importing multiple modules at the same time is less readable and understandable to beginners than the first. Therefore, when you want to import more than one module, it is recommended to use the first way, each module is a separate import, you may feel that the operation is troublesome, but easy to understand.
Import Statement Importing Module order
In the process of writing code, we may need a variety of modules, it is important to note that the import module should be placed at the beginning of the code.
Why do you want to put import module in the beginning of the program to use it?
The interpreter follows the scope principle when executing a statement. Because this is related to the scope, if the module is imported at the top level, its scope is global, and if the module is imported inside the function, its scope is only local and cannot be used by other functions. If other functions also need to use this module, it will be more cumbersome to import again.
It is best to follow this order when importing a module with an import statement:
1. Python Standard library module
2. Python third-party module
3. Custom Modules
This is only a few advanced programmers accustomed to the operation method, in the www.iplaypy.com play snake nets python learning process, we try to learn those recommended by the use of good methods.
From-import statement function
The Python from import statement is also a way to import the module, or, more specifically, the specified function method within the specified module.
From-import statement syntax
From module import name
Keyword Module Name keyword method name
For example, the floor function method in the math module of the onboarding function:
From math import Floor
Python from use methods such as:
From math import Floor #导入math模块中的floor函数方法
Floor () #调用floor () function method
Use of datetime
As follows
#-*-Coding:utf-8-*-#datetime类 #datetime是date与time的结合体, including all information on date and time. #它的构造函数如下: #datetime. DateTime (year, month, day[, hour[, minute[, second[, microsecond[, Tzinfo]]]]) #各参数的含义与date, as in the constructor of time, note The range of the value of the parameter. # 1. The class properties and methods defined by the DateTime class: #datetime. Min, datetime.max:datetime can represent the minimum and maximum value; #print: datetime.max:9999-12-31 23:59:59.999999 #print: datetime.min:0001-01-01 00:00:00 from datetime import * Import time print ' Datetime.max : ', Datetime.max print ' datetime.min: ', datetime.min #datetime. resolution:datetime minimum Unit; #print: Datetime.resolu tion:0:00:00.000001 print ' datetime.resolution: ', Datetime.resolution #datetime. Today (): Returns a DateTime object that represents the current local time; Print:today (): 2012-09-12 19:37:50.721000 print ' Today (): ', Datetime.today () #datetime. Now ([TZ]): Returns a datet that represents the current local time IME object, if the parameter tz is provided, gets the local time of the time zone referred to by the TZ parameter; #print: Now (): 2012-09-12 19:37:50.738000 print ' Now (): ', DateTime.Now () #datetim E.utcnow (): Returns the Dateti of a current UTC timeMe object; #print: 2012-09-12 11:37:50.739000 print ' UtcNow (): ', Datetime.utcnow () #datetime. Fromtimestamp (timestamp[, TZ]): Creates a DateTime object based on time timestamp, parameter TZ specifies time zone information, #print: Fromtimestamp (tmstmp): 2012-09-12 19:37:50.741000 print ' Fromtimestamp ( tmstmp): ', Datetime.fromtimestamp (Time.time ()) #datetime. Utcfromtimestamp (timestamp): Creates a DateTime object based on time timestamp; #print: Utcfromtimestamp (tmstmp): 2012-09-12 11:37:50.742000 print ' Utcfromtimestamp (tmstmp): ', Datetime.utcfromtimestamp ( Time.time ()) #datetime. Combine (date, time): Creates a DateTime object based on date and time; #print: Datetime.combine (date,time): 2012-09-12 19:46:05 d = Date (2012,9,12) from datetime import * t = time (19,46,5) print ' Datetime.combine (date,time ): ', Datetime.combine (d,t) #datetime. Strptime (date_string, format): Converts a format string to a DateTime object; #print: 2007-03-04 21:08:12 Print Datetime.strptime ("2007-03-04 21:08:12", "%y-%m-%d%h:%m:%s") #2. The DateTime class provides an instance method with a property of dt = Datetime.strptime ("2012-09-12 21:08:12", "%y-%m-%d%h:%m:%s") #prINT:2012 9 8 0 None print dt.year,dt.month,dt.day,dt.hour,dt.minute,dt.second,dt.microsecond,dt.tzinfo print D T.date () print dt.time () print dt.replace (year =) print dt.timetuple () print dt.utctimetuple () print Dt.toordina L () print dt.weekday () print Dt.isocalendar () #print dt.isoformat ([Sep]) #datetime. CTime (): Returns a C-format string of datetime equivalent to Time.ctime (Time.mktime (Dt.timetuple ())); #3. The format string # DateTime. Strftime (format) # Shorthand for%a week. Like Wednesday for the full write of Web #%A week. such as Wednesday for the Wednesday #%b month abbreviation. Like April for Apr #%b Month Full write. As in April, the string representation of April #%c: DateTime. (Example: 04/07/10 10:43:39) #%d: Days in this month (the day of the Week) #%f: microseconds (range [0,999999]) #%H: Hours (24-hour, [0]) #%I: Hours (12-hour, [0, ] #%j: Number of days in the year [001,366] (the day of the current year) #%m: Month ([01,12]) #%m: minutes ([00,59]) #%p:am or PM #%s: seconds (range = [00,61], why not [00, 59], refer to Python manual ~_~) #%u: Weeks in the year of the week of the first week of the year), Sunday as the day of the Week #%w: The number of days in this week, the range is [0, 6],6 represents Sunday #%w: Weeks in the Year of the week (is the first week of the year), Monday First day of Week #%x: date string (e.g. 04/07/10) #%x: Time string (e.g.: 10:43:39) #%y:2 number of yearsPart #%y:4 number represents the year #%z: Interval from UTC time (if local time, returns an empty string) #%z: Time zone name (returns an empty string if local time) # percent:%% =% DT = DateTime. Now () #print: (%y-%m-%d%h:%m:%s%f): 2012-09-12 23:04:27 145000 print ' (%y-%m-%d%h:%m:%s%f): ', Dt.strftime ('%y-%m-% D%h:%m:%s%f ') #print: (%y-%m-%d%h:%m:%s%p): 12-09-12 11:04:27 PM print ' (%y-%m-%d%h:%m:%s%p): ', Dt.strftime ('%y- %m-%d%i:%m:%s%p ') #print:%a:wed print '%%a:%s '% dt.strftime ('%a ') #print:%a:wednesday print '%%a:%s '% dt. Strftime ('%A ') #print:%b:sep print '%%b:%s '% dt.strftime ('%b ') #print:%b:september print '%%b:%s '% DT.STRFT IME ('%B ') #print: datetime%c:09/12/12 23:04:27 print ' datetime%%c:%s '% dt.strftime ('%c ') #print: Date%X:09/12/12 print ' date percent x:%s '% dt.strftime ('%x ') #print: Time%x:23:04:27 print ' time%%x:%s '% dt.strftime ('%x ') #print: Today is the 3rd day of this week print ' Today is this week's %s days '% dt.strftime ('%w ') #print: Today is the NO. 256 day of this year print ' Today is this year's%s day '% dt.strftime ('%j ') #print: This week is the 37th week of this year print ' This week is today Year's%s week '% dt.strftime ('%u ')
Day15-python use of common built-in modules