直接贴代码了,以后复习可以翻看。
1#!/usr/bin/python
2 #-*-coding: UTF-8 -*-
3
4 #无参数
5 def print_hello():
6 print "hello"
7
8 print_hello()
9
10 #带参数
11 def print_str(s):
12 print s
13 return s * 2
14 print_str("loce")
15
16 #带默认参数
17 def print_default(s = "hello"):
18 print s
19 print_default()
20 print_default("default")
21
22 #不定长参数
23 def print_args(s, *arg):
24 print s
25 for a in arg:
26 print a
27 return
28
29 print_args("hello")
30 print_args("hello", "world", "1")
31
32 #参数次序可变
33 def print_two(a, b):
34 print a, b
35
36 print_two(a = "a", b = "b")
37 print_two(b = "b", a = "a")
hello
loce
hello
default
hello
hello
world
1
a b
a b