Python learning functions

Source: Internet
Author: User

Python learning functions

#-*-Config = UTF-8 -*-############################## ###### functions ################################# # def func_02 (, b): # a B: formal parameter if a> B: print ("a greater than B") else: print ("B greater than or equal to a"); func_02 (2, 4 ); #2 4: actual parameters ###################################### default Value #################################### ##### def mashine (x = 0, y = 0): # x = 0 indicates setting the default value print (x, "+", y, "Calculation Result:", x + y); mashine (); # use the default value 0 + 0 Calculation Result: 0 mashine (); # transfer value 21 + 34 Calculation Result: 55 mashine (21); # do not specify parameter 21 + 0 calculation result: 21 mashine (y = 21); # specify the parameter to pass the value 0 + 21 calculation result: 21 ####################################### ### variable scope ################################## ####### x = "I am global var "; # global variable def func_03 (): B = 200; # local variable print (x, B); func_03 (); # using global to forcibly declare variables inside the function as global variables the function must first be called m = "C-D-E"; def func_04 (): global m; m = "A-B-C "; print (m); func_04 (); # A-B-Cprint (m ); # The A-B-C function must be called ################################## ##### function return value ################################ ###### 1. return returns the function execution result and terminates the function #2. Nonedef func_05 (x, y): print (x + y); return x + y; sum = func_05 (12, 23) by default ); # receive function processing result print (sum );

#-*-Config = UTF-8 -*-############################## ####### multi-type parameters ########################Print ("% s: % s" % ('A', 'B'); # format Output A: Barr = ('A', 'B ', 'C'); # tuples list = list (range (10); # tuples map = {"a": "A", "B": "B ", "c": "C"}; # dictionary # uses the tuples as the def func_01 (x, y, z): print (x, y, z) parameter ); func_01 (* arr); # a B c # default parameter def func_02 (name = "James", age = 12): print ("name: % s" % name ); print ("age: % s" % age); func_02 (); # use the default name: Zhang San func_02 ("Li Si", 30) when it is not worthwhile; # name: li Si # The parameter for passing the dictionary function must correspond to the dictionary key value func_02 (age = 50, name = "Kb"); # name: Kb age: 50map2 = {"age ": 40, "name": "Wang Wu"}; func_02 (** map2); # name: Wang Wu age: 40map2 ["age"] = 41; func_02 (** map2 ); # age: 41 ############################### process excess parameters passed ### ##################### * args tuples store excess real parameters def func_03 (x, * args): print (x); print (args); func_03 (1); #1 () tuples func_03 (1, "a", "C", 4 ); #1 ('A', 'C', 4) tuples # store the excess real parameter def func_04 (x, * agrs, ** kwargs) of the key = value type in a dictionary ): print (x); print (agrs); print (kwargs); func_04 (2, "F", "D", "H", a = "", c = "C"); #2 ('F', 'D', 'H') {'C': 'C', 'A ': 'A'} func_04 (2, "F", "D", "H", x = "A", c = "C "); # This is a problem because x is transferred multiple times.

 

#################################### Lambda (anonymous)) function ################################### lambda function is A minimum function that quickly defines a single row, it is used for reference from Lisp and can be used wherever a function is needed # A Python operator used to assign a function to a variable is returned by default, so the return keyword def func_05 (x, y): return x * y; g = lambda x, y: x * y ); # <function <lambda> at 0x0000000000000c6a8> Returns a function object print (g (408); # reduce in Python # The reduce built-in function in python is a binary operation function, it is used to store all Perform the following operations on the data: # Use the function func () passed to reduce (must be a binary operation function) to operate on the first 1 or 2 pieces of data in the set, # Use the func () function to calculate the obtained result with the third data, and finally obtain a result. # After python 3.0.0.0, reduce is no longer in the built-in function. to use it, you have to use it from functools import performancefrom functools import reduce; # Calculate the factorial 5*4*3*2 * 1def func_06 (x, y): return x * y; fh = list (range )); print (fh); print (reduce (func_06, fh); #120 print (reduce (lambda x, y: x * y, fh); #120 use lambda functions for processing

 

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.