# Python development in _python module

In Python, we can put some function module, a bit similar to Java, some function or the same code together, so we need to use it, you can directly call

The benefits of doing so:

1,Just write a function module, can be in after the call, the reuse of code can be reflected.

2,After the function of writing, errors do not occur. If the same function, we write it again in a module, in addition to the module was written again... So we will ensure that we in the process of writing is not wrong.

But if we can write a function module written later, he used in many places, on the one hand, use convenient, second, can guarantee the correctness

3,Code sharing

Said so much, or to the actual point of good！！！

We create a new file: fibo.py (the name can vary with the individual desire)

1 #Modules 2 3 def fib(n): # write Fibonacci series up to n 4 a,b = 0,1 5 while b < n: 6 print(b,end=' ') 7 a,b = b, a + b 8 print() 9 10 def fib2(n): # return Fibonacci series up to n 11 result = [] 12 a,b = 0,1 13 while b < n: 14 result.append(b) 15 a,b = b, a + b 16 return result 17 18 def add(numberA,numberB): #Define addition 19 return numberA + numberB 20 21 def sub(numberA,numberB): #The definition of subtraction 22 return numberA - numberB 23 24 def mul(numberA,numberB): #The definition of multiplication 25 return numberA*numberB 26 27 def div(numberA,numberB): #Definitions Division 28 if numberB != 0: 29 return numberA//numberB 30 else: 31 return 'Error'

It is our custom a simple function, which defines the methods.

We need to: test_modules.py (and fibo.py file in another file with a directory) method call in the fibo.py file:

1 import fibo 2 3 fibo.fib(1000) 4 result = fibo.fib2(1000) 5 print(result)

Effect of operation:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> ================================ RESTART ================================ >>> 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987] >>>

Now we define a file:

Some call the test_modules1.py method in fibo.py

1 from fibo import fib,fib2 2 3 #Here is able to directly use the above two method 4 fib(100) 5 6 result = fib2(1000) 7 print(result)

As described above, we use the fibo.py file in FIB, fib2 two method

Effect of operation:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> ================================ RESTART ================================ >>> 1 1 2 3 5 8 13 21 34 55 89 [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987] >>>

If we are to all the methods in the application of fibo.py, we can do this.:

1 #Methods all the application in fibo.py 2 #It is similar to the java used in the package 3 from fibo import * 4 5 #Here we can use the method in Fibo 6 7 fib(1000) 8 9 print(fib2(100)) 10 numberA = 20 11 numberB = 5 12 13 print('The calculation of adding: ',numberA,'+',numberB,'=',add(numberA,numberB)) 14 print('Subtraction calculation: ',numberA,'-',numberB,'=',sub(numberA,numberB)) 15 print('Multiplication: ',numberA,'*',numberB,'=',mul(numberA,numberB)) 16 print('Division calculation: ',numberA,'/',numberB,'=',div(numberA,numberB))

Effect of operation:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> ================================ RESTART ================================ >>> 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] The calculation of adding: 20 + 5 = 25 Subtraction calculation: 20 - 5 = 15 Multiplication: 20 * 5 = 100 Division calculation: 20 / 5 = 4 >>>

Posted by Elsa at November 27, 2013 - 9:02 AM