Python functions, python function manual

Source: Internet
Author: User

Python functions, python function manual

This part of functions is very important in python learning. It makes your code look neat and clear, so it is convenient to call

#! /Usr/bin/env python # coding: UTF-8 # function concept: functions are well organized and reusable for single implementation, or a code segment associated with a function # rule for defining a function: the function code block starts with the def keyword, followed by the Function Identifier name and parentheses () # Any input parameter and independent variable must be placed in the middle of the parentheses, the center of the parentheses can be used to define the parameter # the content of the function starts with a colon and is indented. # Return (expression) ends the function and returns a value to the caller selectively. # Chestnut def hello (name): print "hello % s" % (name) hello (name = "fujinzhou") hello (name = "reboot ") # variable length parameter # variable names with asterisks will store all unnamed variable parameters def prininfo (arg1, * vartuple): print "output:" print arg1 print vartuple returnprininfo, 30,40) # The anonymous function lambda is an expression # lambda [arg1 [, arg2 ,..... argn]: Statement # function description sum1 = lambda arg1, arg2: arg1 * arg2 # Call the sum1 function print "after the multiplication value is:", sum1)

# Return Statement [expression] exit the function and selectively return an expression to the caller. The return statement without the Parameter Value Returns None. # Function description def count (arg1, arg2): total = arg1 + arg2 print "function:", total return total # Call the count function total = count) print "outside function:", total
# Variable scope all variables of a program are not accessible anywhere. The access permission depends on where the variable is assigned. # global variables and local variables # variables defined in the function have a local scope, global scope defined outside the function # local variables can only be accessed within the declared function, and global variables can be accessed throughout the Program # This is a global variable total = 0def sum (arg1, arg2): total = arg1 + arg2 print "the function contains local variables:", total return totalsum () print "the function is a global variable:", total

  

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.