1. Get the current path
>>> Import OS
>>> Os.path ()
>>> OS.GETCWD ()
' D:\\python34 '
Os.path.abspath ('. ')
' D:\\python34 '
In Python programming, the import of modules requires a process called "Path search".
Locate the mymodule.py file in the file system "predefined area" (If you import MyModule).
These predefined areas are just a collection of your Python search paths.
The default search path is specified at compile time or during installation. It can be modified in one or two places.
One is the PYTHONPATH environment variable that launches the shell or command line of Python. The content of the variable is a set of directory paths separated by colons.
If you want the interpreter to use this variable, be sure to set or modify the variable before starting the interpreter or executing a Python script.
After the interpreter is started, the search path can also be accessed, which is stored in the Sys.path variable of the SYS module.
However, it is not a colon-delimited string, but it contains a list of each independent path.
A sample of a Unix machine search path.
Note: The search path is generally different under different systems.
>>>sys.path
[‘‘,
'/usr/local/lib/python2.x/',
'/usr/local/lib/python2.x/plat-sunos5 ',
'/usr/local/lib/python2.x/lib-tk ',
'/usr/local/lib/python2.x/lib-dynload ',
'/usr/local/lib/python2.x/site-packages ',]
This list of search paths can be modified as needed at any time.
If you know what module you need to import, and its path is not in the search path, you just need to call the Append () method of the list, just like this:
Sys.path.append ('/home/wesc/py/lib ')
Once you have added a search path, you can load your own modules.
2. Function "http://www.cnblogs.com/jiu0821/p/4491603.html"
2.1 function definition
def function_name (arg1,arg2[,...]):
Statement
[Return value]
Format:
def < function name > (< formal parameter list >):
< function body >
Python functions are defined by the DEF keyword. def keyword followed by a function name followed by a pair of parentheses. Parentheses can include some variable names separated by commas. The line ends with a colon. Next is a piece of statement, which is the function body. If the function has a return value, use return < expression > form directly.
2.2 Name of function
The function name must begin with an underscore or letter and can contain any combination of letters, numbers, or underscores. Cannot use any punctuation marks;
Function names are case-sensitive.
The function name cannot be a reserved word.
Example 1-1 defines and uses a function that prints the maximum value of two numbers. The function has a parameter of two and no return value.
Solution:
# Filename:printmax.py
# Definition of function
Def Printmax (A, B):
If a > B:
Print (A, ' is maximum ')
Else
Print (b, ' is maximum ')
Note: You can paste the script into the command window and call it again.
#函数的使用
Printmax (6, 17) # Call a function directly using the literal number
x = 2
y = 8
Printmax (x, y) #使用变量为实参调用函数
Operation Result:
C:\python31>python printmax.py
Maximum
8 is maximum
Other calling methods:
>>> Import Printmax
>>> Printmax (3,88)
>>> Printmax.printmax (3,88)
Example 1-2 defines and uses a function that asks for the maximum value of two numbers. The function has a parameter of two, and the return value is the maximum number.
Solution:
# Filename:funcmax.py
# Definition of function
Def Funcmax (A, B):
If a > B:
Return a
Else
Return b
#函数的使用
Print (Funcmax (6, 17)) # Use literal direct call function, direct output result
x = 2
y = 8
Z=funcmax (x, y) #使用变量为实参调用函数并将结果赋赋给另一变量
Print (z)
Operation Result:
C:\python31>python funcmax.py
17
8
2.3 Common functions
Type (< expression >) to get the data type of an expression
Int (' #转换为整数 ')
Int (' 1101 ', 2) #将二进制字符串转换为十进制整数
Float (' 43.4 ') #转换为浮点数
STR (#转换为字符串)
Bin (#将十进制整数转换为二进制数)
The repr () function is used to obtain the canonical string representation of an object.
2.4 Math functions, Import Math
MATH.LOG10 (#以10为底的对数)
Math.sin (MATH.PI/2) #正弦函数, Unit radians
Math.PI #常数pi, 3.141592653589793
Math.exp (8) #e的8次幂
Math.pow (32,4) #32的4次幂.
MATH.SQRT (2) #2开平方.
Math.Cos (MATH.PI/3) #余弦函数.
Math.fabs ( -32.90) #求绝对值.
Math.factorial (n) #求n的 factorial
In the edit window, enter "math." and move the mouse cursor to "." You can open the math list with a little pause.
2.5 lambda function
Python allows you to define a small single-line function. The form of a lambda function is defined as follows:
Lambda < parameter table;: expression
Where parameters are separated by commas. The lambda function returns the value of an expression by default. You can also assign it to a variable. A lambda function can accept any parameter, including optional arguments, but the expression has only one:
>>> g = lambda x, y:x*y
>>> g (3,4)
12
>>> g = lambda x, y=0, z=0:x+y+z
>>> g (1)
1
>>> g
2.6 Example
Writing a program mymax.py
Def mymin (A, B):
If a<b:
Return a
Else
Return b
Def Mymax (A, B):
If a>b:
Return a
Else
Return b
Run:
>>> from Mymax Import *
>>> Help (Mymax)
Help on function Mymax in module Mymax:
Mymax (A, B)
>>> Mymax (99,3)
99
>>> Mymin (99,3)
3
>>> help (' Mymax ')
Help on module Mymax:
NAME
Mymax
FILE
d:\python279\mymax.py
FUNCTIONS
Mymax (A, B)
Mymin (A, B)
3. Module "http://www.cnblogs.com/jiu0821/p/4491607.html"
A module base is a file that contains defined functions and variables. In order to reuse modules in other programs, the file name of the module must be an extension of. py. When you use a function in a module, the file begins to write:
Import < module name >
Use functions with:
< module name >.< function name > (< parameter table >)
Note that the module name is the name of the file that does not contain the extension, and that the file is in the same folder as the current file. When using a function, the function name has a module name and a dot number ".".
Example 1-3 writes a module file with a function that asks for the maximum value in writing another file, which uses the function in the preceding module file.
Solution:
Module file, file name module_max.py
# Filename:module_max.py
# Ask for maximum value
Def Funcmax (A, B):
If a > B:
Return a
Else
Return b
#求最小值
Def funcmin (A, B):
If a < b:
Return a
Else
Return b
Use of the module:
# Filename:usemodule.py
Import Module_max//Importing module files
X=9
y=37
Print (Module_max.funcmax (9,37))
There is another way to use the module is in the From...import format, for example:
# Filename:usemodule2.py
From Module_max import Funcmax
X=9
y=37
Print (Funcmax (9,37))
Where the required function names are already included in the From...import, then the specific function is used, and the module name is no longer included.
python_06-Functions and modules