mirror of
https://github.com/apachecn/ailearning.git
synced 2026-05-04 11:52:19 +08:00
2020-10-19 21:08:55
This commit is contained in:
@@ -8,7 +8,7 @@
|
||||
|
||||
In [1]:
|
||||
|
||||
```
|
||||
```py
|
||||
def add(x, y):
|
||||
"""Add two numbers"""
|
||||
a = x + y
|
||||
@@ -32,13 +32,13 @@ def add(x, y):
|
||||
|
||||
In [2]:
|
||||
|
||||
```
|
||||
```py
|
||||
print add(2, 3)
|
||||
print add('foo', 'bar')
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
5
|
||||
foobar
|
||||
|
||||
@@ -48,12 +48,12 @@ foobar
|
||||
|
||||
In [3]:
|
||||
|
||||
```
|
||||
```py
|
||||
print add(2, "foo")
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
---------------------------------------------------------------------------
|
||||
TypeError Traceback (most recent call last)
|
||||
<ipython-input-3-6f8dcf7eb280> in <module>()
|
||||
@@ -72,12 +72,12 @@ TypeError: unsupported operand type(s) for +: 'int' and 'str'
|
||||
|
||||
In [4]:
|
||||
|
||||
```
|
||||
```py
|
||||
print add(1, 2, 3)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
---------------------------------------------------------------------------
|
||||
TypeError Traceback (most recent call last)
|
||||
<ipython-input-4-ed7bae31fc7d> in <module>()
|
||||
@@ -88,12 +88,12 @@ TypeError: add() takes exactly 2 arguments (3 given)
|
||||
|
||||
In [5]:
|
||||
|
||||
```
|
||||
```py
|
||||
print add(1)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
---------------------------------------------------------------------------
|
||||
TypeError Traceback (most recent call last)
|
||||
<ipython-input-5-a954233d3b0d> in <module>()
|
||||
@@ -106,13 +106,13 @@ TypeError: add() takes exactly 2 arguments (1 given)
|
||||
|
||||
In [6]:
|
||||
|
||||
```
|
||||
```py
|
||||
print add(x=2, y=3)
|
||||
print add(y="foo", x="bar")
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
5
|
||||
barfoo
|
||||
|
||||
@@ -122,12 +122,12 @@ barfoo
|
||||
|
||||
In [7]:
|
||||
|
||||
```
|
||||
```py
|
||||
print add(2, y=3)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
5
|
||||
|
||||
```
|
||||
@@ -138,7 +138,7 @@ print add(2, y=3)
|
||||
|
||||
In [8]:
|
||||
|
||||
```
|
||||
```py
|
||||
def quad(x, a=1, b=0, c=0):
|
||||
return a*x**2 + b*x + c
|
||||
|
||||
@@ -148,12 +148,12 @@ def quad(x, a=1, b=0, c=0):
|
||||
|
||||
In [9]:
|
||||
|
||||
```
|
||||
```py
|
||||
print quad(2.0)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
4.0
|
||||
|
||||
```
|
||||
@@ -162,24 +162,24 @@ print quad(2.0)
|
||||
|
||||
In [10]:
|
||||
|
||||
```
|
||||
```py
|
||||
print quad(2.0, b=3)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
10.0
|
||||
|
||||
```
|
||||
|
||||
In [11]:
|
||||
|
||||
```
|
||||
```py
|
||||
print quad(2.0, 2, c=4)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
12.0
|
||||
|
||||
```
|
||||
@@ -190,12 +190,12 @@ print quad(2.0, 2, c=4)
|
||||
|
||||
In [12]:
|
||||
|
||||
```
|
||||
```py
|
||||
print quad(2.0, 2, a=2)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
---------------------------------------------------------------------------
|
||||
TypeError Traceback (most recent call last)
|
||||
<ipython-input-12-101d0c090bbb> in <module>()
|
||||
@@ -210,7 +210,7 @@ TypeError: quad() got multiple values for keyword argument 'a'
|
||||
|
||||
In [13]:
|
||||
|
||||
```
|
||||
```py
|
||||
def add(x, *args):
|
||||
total = x
|
||||
for arg in args:
|
||||
@@ -223,13 +223,13 @@ def add(x, *args):
|
||||
|
||||
In [14]:
|
||||
|
||||
```
|
||||
```py
|
||||
print add(1, 2, 3, 4)
|
||||
print add(1, 2)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
10
|
||||
3
|
||||
|
||||
@@ -239,7 +239,7 @@ print add(1, 2)
|
||||
|
||||
In [15]:
|
||||
|
||||
```
|
||||
```py
|
||||
def add(x, **kwargs):
|
||||
total = x
|
||||
for arg, value in kwargs.items():
|
||||
@@ -253,12 +253,12 @@ def add(x, **kwargs):
|
||||
|
||||
In [16]:
|
||||
|
||||
```
|
||||
```py
|
||||
print add(10, y=11, z=12, w=13)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
adding y
|
||||
adding z
|
||||
adding w
|
||||
@@ -270,7 +270,7 @@ adding w
|
||||
|
||||
In [17]:
|
||||
|
||||
```
|
||||
```py
|
||||
def foo(*args, **kwargs):
|
||||
print args, kwargs
|
||||
|
||||
@@ -278,7 +278,7 @@ foo(2, 3, x='bar', z=10)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
(2, 3) {'x': 'bar', 'z': 10}
|
||||
|
||||
```
|
||||
@@ -291,7 +291,7 @@ foo(2, 3, x='bar', z=10)
|
||||
|
||||
In [18]:
|
||||
|
||||
```
|
||||
```py
|
||||
from math import atan2
|
||||
|
||||
def to_polar(x, y):
|
||||
@@ -304,7 +304,7 @@ print r, theta
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
5.0 0.927295218002
|
||||
|
||||
```
|
||||
@@ -313,19 +313,19 @@ print r, theta
|
||||
|
||||
In [19]:
|
||||
|
||||
```
|
||||
```py
|
||||
print to_polar(3, 4)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
(5.0, 0.9272952180016122)
|
||||
|
||||
```
|
||||
|
||||
因为这个元组中有两个值,所以可以使用
|
||||
|
||||
```
|
||||
```py
|
||||
r, theta = to_polar(3, 4)
|
||||
```
|
||||
|
||||
@@ -335,13 +335,13 @@ r, theta = to_polar(3, 4)
|
||||
|
||||
In [20]:
|
||||
|
||||
```
|
||||
```py
|
||||
a, b, c = [1, 2, 3]
|
||||
print a, b, c
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
1 2 3
|
||||
|
||||
```
|
||||
@@ -350,7 +350,7 @@ print a, b, c
|
||||
|
||||
In [21]:
|
||||
|
||||
```
|
||||
```py
|
||||
def add(x, y):
|
||||
"""Add two numbers"""
|
||||
a = x + y
|
||||
@@ -361,7 +361,7 @@ print add(*z)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
5
|
||||
|
||||
```
|
||||
@@ -372,7 +372,7 @@ print add(*z)
|
||||
|
||||
In [22]:
|
||||
|
||||
```
|
||||
```py
|
||||
def add(x, y):
|
||||
"""Add two numbers"""
|
||||
a = x + y
|
||||
@@ -383,7 +383,7 @@ print add(**w)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
5
|
||||
|
||||
```
|
||||
@@ -394,7 +394,7 @@ print add(**w)
|
||||
|
||||
In [23]:
|
||||
|
||||
```
|
||||
```py
|
||||
def sqr(x):
|
||||
return x ** 2
|
||||
|
||||
@@ -403,14 +403,14 @@ print map(sqr, a)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
[4, 9, 16]
|
||||
|
||||
```
|
||||
|
||||
其用法为:
|
||||
|
||||
```
|
||||
```py
|
||||
map(aFun, aSeq)
|
||||
```
|
||||
|
||||
@@ -420,7 +420,7 @@ map(aFun, aSeq)
|
||||
|
||||
In [24]:
|
||||
|
||||
```
|
||||
```py
|
||||
def add(x, y):
|
||||
return x + y
|
||||
|
||||
@@ -430,7 +430,7 @@ print map(add,a,b)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
[12, 8, 7]
|
||||
|
||||
```
|
||||
Reference in New Issue
Block a user