python--function 1

Source: Internet
Author: User

1 Concepts

In general, a small function module will be made into a function.

In general, a piece of code needs to be reused multiple times, and this code has a specific function, we will put this piece of code

The organization becomes a separate function module, which can be called a function.

2. Definition and invocation of functions

Definition: Functions cannot be defined repeatedly

def function name ():

code block for feature implementation ...

Call: Function name ()

Functions can be repeated calls must be defined first and then called

1 #printing 99 multiplication tables with functions2 defshow99 ():3i = 14      whileI <10:5j = 16          whileJ <=I:7             Print("%d *%d =%d\t"% (j,i,i*j))8J +=19i + = 1Ten         Print("") OneShow99 ()

3. Parameters of the function, return value

Parameters

With parameters, no parameters

return value

There is a return value, no return value

4. Encapsulate a function to complete the sum of two numbers

Parameters: 2 Parameters

Returns the value return and

Import Random

Random.randint (1,6)

1 #returns a call to a random number function2 ImportRandom3 #call the random function with the return value4Random = Random.randint (1,6)5 6 #define a function that asks for two numbers7 defGetsum (A, b):8     #sum1 = a + b9     #return sum1Ten     returnA +b OneNUM1 = Int (input ("Enter first number")) Anum2 = Int (input ("Enter a second number")) - #call a custom function, get two numbers and save in Resul -result =getsum (num1,num2) the #seeking num1 and result -RESULT1 =getsum (Num1,result) -     

python--function 1

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.