Analysis on the usage of the module in python Development

Source: Internet
Author: User
This article mainly introduces the usage of the module in python development, and analyzes in detail the functions, definitions, and related usage skills of the module in Python in the form of instances, which has some reference value, for more information about how to use the module in python development, see the example in this article. We will share this with you for your reference. The details are as follows:

In python, We Can modularize some functions, just like in java, and put some function-related or identical code together, so that we can directly call it when needed.

Benefits:

1. Once a function module has been written, it can be called in the future, and code reuse can be reflected.

2. After the function is written, no error will occur. If a function is the same, we write it again in one module, and write it again in another module ...... in this way, we will inevitably ensure that no errors will occur during the writing process.

However, if we can write a function module, we will use it in many places. First, it is convenient to use, and second, it can ensure its correctness.

3. Code sharing

If you have said so much, it is better to come to reality !!!

Create a new file: fibo. py (of course, this name can be used as needed)

# Modulesdef fib (n): # write Fibonacci series up to n a, B = 0, 1 while B <n: print (B, end = '') a, B = B, a + B print () def fib2 (n): # return Fibonacci series up to n result = [] a, B = 0, 1 while B <n: result. append (B) a, B = B, a + B return resultdef add (numberA, numberB): # define addition return numberA + numberBdef sub (numberA, numberB ): # define the subtraction return numberA-numberBdef mul (numberA, numberB): # define the multiplication return numberA * NumberBdef p (numberA, numberB): # defines the division if numberB! = 0: return numberA // numberB else: return 'error'

The above is a simple custom function module, which defines some methods.

We need to call the method in fibo. py in the file test_modules.py (another file in the same directory as the fibo. py file:

import fibofibo.fib(1000)result = fibo.fib2(1000)print(result)

The running effect is as follows:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32Type "copyright", "credits" or "license()" for more information.>>> ================================ RESTART ================================>>> 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 have defined another file:

Test_modules1.py call some methods in fibo. py

From fiber import fib, fiber 2 # Here is the fib (100) result = fiber 2 (1000) print (result) that can be directly used in the preceding two methods)

As described above, we have applied the fib and fiber 2 methods in the fiber. py file.

Running effect:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32Type "copyright", "credits" or "license()" for more information.>>> ================================ RESTART ================================>>> 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 want to apply all the methods in fibo. py, we can do this:

# Apply fibo. all the methods in py # A bit similar to the form of the introduced package in java from fibo import * # Here we can use the method used in FIO fib (1000) print (fiber 2 (100 )) numberA = 20 numberB = 5 print ('addition calculation: ', numberA,' + ', numberB,' = ', add (numberA, numberB) print ('subtraction calculation: ', numberA,'-', numberB,' = ', sub (numberA, numberB) print ('multiplication calculation:', numberA, '*', numberB, '=', mul (numberA, numberB) print ('division calculation: ', numberA,'/', numberB,' = ', p (numberA, numberB ))

Running effect:

Python 3.3.2 (v3.3.2: d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32Type "copyright", "credits" or "license () "for more information. >>> ================================== RESTART ======== ======================>> 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] addition calculation: 20 + 5 = 25 subtraction calculation: 20-5 = 15 multiplication calculation: 20*5 = 100 Division calculation: 20/5 = 4 >>>

I hope this article will help you with Python programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.