Function Basics
返回
A First Example: Definitions and Calls
>>> def times(x,y):
... return x * y
...
>>> times(3,4)
12
>>> x = times(3.14,4)
>>> x
12.56
>>> times('Nihao', 4)
'NihaoNihaoNihaoNihao'
>>>
Python 2.3.5 (#62, Feb 8 2005, 16:23:02) [MSC v.1200 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> def intersect(seq1, seq2):
... res = []
... for x in seq1:
... if x in seq2:
... res.append(x)
... return res
...
>>> s1 = "SPAM"
>>> s2 = "SCAM"
>>> intersect(s1,s2)
['S', 'A', 'M']
>>> x = intersect([1,2,3],(1,4,8))
>>> x
[1]
>>>
返回