The function of Python learning

Source: Internet
Author: User

Recently followed by the Liaoche teacher's Python tutorial is studying, summarizes the study achievement

function definition:

When no return statement is returned, the result is None

def function name ():    function Body     return value 

Function call:

We can call a function in Python, or we can call a custom function, provided that the correct arguments are passed in

Function parameters:

The function parameter is divided into the required parameter default parameter, the variable parameter, the keyword parameter and the named keyword parameter (the order of the parameter definition is also such)

The *args is a variable parameter and is accepted as a tuple

**KW is a keyword parameter that accepts a dict

Practice:

The following function allows you to calculate the product of two numbers, modify it slightly to accept one or more numbers and calculate the product:

def product (x,*kw):    s=1 for in      range (len (kw)):        s=s* Kw[i]     Print (x*s) product (+/-)

Product (1,2,3,4,5,6)

The results of the operation are as follows:

Thinking:

I didn't know how to do it at first, it was a headache, and here's my thinking steps.

1. Temporarily consider some of the column numbers that can be entered as a variable parameter, using the traversal of a tuple to loop all the numbers out

2. Set an initial variable s and initialize s to 1, after each loop, multiply with s and give s re-assign value

3. Output results can be

Summary:

  The code is still more thinking, more practice

The function of Python learning

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.