Python Custom functions

Source: Internet
Author: User

Introductionin Python, you define a function to use a def statement, write out the function name, parentheses, the arguments in parentheses, and a colon : , and then, in the indent block, write the function body, the return value of the function is returned with a return statement, and return satisfies the condition and no longer executes the following statement. Print output is also available. 1. Defining FunctionsCustomize a judgment function for an age period when the input age is greater than or equal to 18 to return ' adult ' if the value entered is non-cosmetic and returns an error
#!/usr/bin/Env python3#-*-coding:utf-8-*-#file:d ef_agedef my_age (x):ifNot Isinstance (x, (int)): Raise TypeError ('Bad operand type') Elif x>= -:        return('Adult') Elif x<7:        return(' Child')    Else:        return(' Young')

2. Calling a custom function

There are three ways to invoke custom functions

1. If the custom function has been saved as a file,

 from def_age Import my_age>>> my_age ()'adult'

Open the compiler in the file directory, since the custom function is not an intrinsic function, you need to import the function first, note that Def_age does not need to have a suffix. py

2. Call the given value inside the code

#!/usr/bin/Env python3#-*-coding:utf-8-*-#file:d ef_agedef my_age (x):ifNot Isinstance (x, (int)): Raise TypeError ('Bad operand type') Elif x>= -:        return('Adult') Elif x<7:        return(' Child')    Else:        return(' Young') Print (My_age ( A))

You do not need to import functions to execute files directly, but this method is not flexible enough.

3. Define variables before calling functions inside the code

#!/usr/bin/Env python3#-*-coding:utf-8-*-#file:d ef_agedef my_age (x):ifNot Isinstance (x, (int)): Raise TypeError ('Bad operand type') Elif x>= -:        return('Adult') Elif x<7:        return(' Child')    Else:        return(' Young') Age= APrint (My_age (age))

The same approach is not flexible enough.

3. Create an empty functionAn empty function, which is a function that does not contain execution code, uses pass as a block of code, and the pass code does nothing; sometimes a piece of code is not ready to be written in order to not affect the entire code to run, you can first save the pass to let the code run first.
Def my_dinf ():    Pass

Pass can also be placed in other code, such as:

#!/usr/bin/Env python3#-*-coding:utf-8-*-#file:d ef_agedef my_age (x):ifNot Isinstance (x, (int)): Raise TypeError ('Bad operand type') Elif x>= -: Pass Elif x<7:        return(' Child')    Else:        return(' Young') Age= APrint (My_age (age))

Or just the age of the judgment function, now in the Elif x>=18: code block below the use of pass, when the condition is satisfied after doing nothing, the entire code is executed normally.

Summary

Note:

pursuer.chen

Blog:http://www.cnblogs.com/chenmh

This site all the essays are original, welcome to reprint, but reprint must indicate the source of the article, and at the beginning of the article clearly give the link.

Welcome to the exchange of discussions

Python Custom functions

Related Article

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.