程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Creation of function module based on Python

編輯:Python

About bloggers : Former Internet manufacturer tencent staff , Network security giant Venustech staff , Alibaba cloud development community expert blogger , WeChat official account java Quality creators of basic notes ,csdn High quality creative bloggers , Entrepreneur , Knowledge sharers , Welcome to your attention , give the thumbs-up , Collection .

Catalog

      • One 、 background
      • Two 、 Module creation
      • 3、 ... and 、 Reference resources
      • Four 、 summary


One 、 background

In the actual development process , You will often encounter many identical or very similar operations , At this time , Code that implements similar operations can be encapsulated as functions , Then call the function where you need it . This can not only realize code reuse , It can also make the code more organized , Increase code reliability . Now let's introduce python The creation of the function module of .



Two 、 Module creation

stay Python in , Every Python Files can be used as a module , The name of the module is the file name .
for example : There is a file “test.py”, It defines the function to find the smaller value minimal().

def minimal(x, y): # Custom calculate small value function 
if x > y: # If x>y establish , return y Value 
return y
else: # Otherwise return to x Value 
return x

example : Programming , By calling “test” Module minimal() function , Calculate the smaller of two numbers .

import test # Import test modular 
a = float(input(' Enter the first data :')) # Input a Value 
b = float(input(' Enter the second data :')) # Input b Value 
c = test.minimal(a, b) # Call function , Assign a smaller value to c
print(' The smaller value is :',c) # Output c Value 

give the result as follows .

In the actual development process , In order to make the module achieve the desired effect in the project , Will add test information to the module by itself .
for example : stay “test_1.py” Add the following test code to the file :

import test_1 # Import test_1 modular 
c = test_1.minimal(5,6) # Call function , Assign a smaller value to c
print(' The smaller value is :',c) # Output smaller values c
def minimal(x, y): # Custom calculate small value function 
if x > y: # If x>y establish , return y Value 
return y
else: # Otherwise return to x Value 
return x
# For testing 
r = minimal(2,3)
print(' test 2 and 3 The smaller value of is :',r)

example : Run the following program , Analyze the running results .


3、 ... and 、 Reference resources

1、 Liao Xuefeng's official website
2、python Official website
3、Python Programming case tutorial


Four 、 summary

The above is about Python Knowledge about the creation of function modules , You can refer to it , If you think it's good , Welcome to thumb up 、 Collection 、 Looking at , Welcome to wechat search java Basic notes , Relevant knowledge will be continuously updated later , Make progress together .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved