Function Definition instance analysis in python development and python instance analysis

Source: Internet
Author: User

Function Definition instance analysis in python development and python instance analysis

This article describes how to define functions in python development. We will share this with you for your reference. The details are as follows:

Below are some columns I have made:

# Define a function in python. Use and pass the def_str = ''' \ function in python to declare it in the following form: def function name ([parameter 1, parameter 2, parameter 3...]): Execute the statement such as def helloWorld (): print ('hello') if _ name _ = '_ main _': helloWorld () output: hello '''print (def_str) # The following example shows def helloWorld (): print ('output: hello ') if _ name _ = '_ main _': helloWorld () print ('''\################################## ############# functions can contain parameters and return values, parameters are matched from left to right. default values can be set for parameters. When a function is used, no corresponding parameter is assigned, the default value is assigned ####### ######################################## #''') # Define a method: The yth power of x def myMethod (x, y): return x ** ydef fib (n): a, B = 0, 1 while a <n: print ('a, end = ') a, B = B, a + B print () # Getting a new array # @ param oldList original array # @ param length the length to be added def getList (oldList, length): if length> 0: for I in range (0, length): oldList. append (I) return oldList else: return 'your input length is less than 0' def ask_ OK (prompt, retries = 4, complaint = 'yes or no, Please! '): While True: OK = input (prompt) if OK in ('y', 'Ye', 'yes'): return True if OK in ('n ', 'No', 'nop', 'nope'): return False retries = retries-1 if retries <0: raise IOError ('refusenik user') print (complaint) if _ name _ = '_ main _': x = 3 y = 4 n = 2000 print (x, ', y,' (', x, '**', y, ') =', myMethod (x, y) print ('function fib (n), when n = ', n) fib (n) print (getList (['begin'],-10) ask_ OK ('y ')

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 ======== ==========================================>>> functions in python are declared in the following form: def function name ([parameter 1, parameter 2, parameter 3 ......]): execution statement such as def helloWorld (): print ('helhel') if _ name _ = '_ main _': helloWorld () Output: hello output: hello ############## ################################## Functions can contain parameters and return values, parameters are matched from left to right. default values can be set for parameters. When a function is used, no corresponding parameters are provided, the value is assigned by default ################################### ############ Power 4 (3 ** 4) = 81 function fib (n), when n = 20001 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 your input length is less than 0y output: helloYes or no, please! YYes or no, please! YYes or no, please! YYes or no, please! YTraceback (most recent call last): File "E:/Python33/python_workspace/test_function.py", line 80, in <module> ask_ OK ('y') File "E: /Python33/python_workspace/test_function.py ", line 69, in ask_ OK raise IOError ('refusenik user') OSError: refusenik user >>>

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.