Python learning Note 3: Functions and python learning note Functions
I. Definition
Def functionName ([arg1, arg2,...]): code
Ii. Example
#! /Usr/bin/python # coding: utf8 # coding = utf8 # encoding: utf8 # encoding = utf8def fun (x = 3, y = 'sweet '): print 'generate A ', x, 'meta', y, 'Taste cake' fun () fun (10) fun (y = 'butle ')
Redundancy parameters:
#! /Usr/bin/pythondef f (name = "jim", age = 20): print "name: % s age: % s" % (name, age) t = ('Tom ', 25) d = {'age': 25, 'name': 'Lucy'} f (* t) # name: tom age: 25f (** d) # name: lucy age: 25def f1 (x, * args): print x, arst1 = ('Tom ', 25, 'male ') f1 (* t1) # tom (25, 'male') def f2 (x, ** args): print x, argsd1 = {'X': 1, 'y ': 2} f2 (** d1) #1 {'y': 2} def f3 (x, * arg1, ** arg2): print x print arg1 print arg2f3 (1, 2, 3, a = 1, B = 2, c = 3) #1 # (2, 3) # {'A': 1, 'B': 2, 'C': 3}