提交 8b657683 编写于 作者: W wizardforcel

2020-06-26 11:53:29

上级 48a2448c
......@@ -149,7 +149,7 @@ False
```
**note:**
**注意**
您不能使用`<``>``>=``<=`等其他关系运算符来比较字典。
......
......@@ -16,7 +16,7 @@
| --- | --- |
| `<=` | 小于或等于 |
| `<` | 小于 |
| `>` | 比...更棒 |
| `>` | 大于 |
| `>=` | 大于或等于 |
| `==` | 等于 |
| `!=` | 不等于 |
......@@ -65,7 +65,7 @@ else:
在这里您可以看到,如果数字为偶数,则将打印`"Number is even"`。 否则打印`"Number is odd"`
**note:**
**注意**
`else`子句是可选的,您可以根据需要仅使用`if`子句,如下所示:
......
......@@ -98,7 +98,7 @@ print(s)
```
**Expected Output:**
**预期输出**
```py
start should be less than end
......@@ -115,7 +115,7 @@ print(test())
```
**Expected Output:**
**预期输出**
```py
None
......@@ -149,7 +149,7 @@ func()   # calling function func()
```
**Expected Output:**
**预期输出**
```py
12
......@@ -171,7 +171,7 @@ print(xy) # this will print global xy variable i.e 100
```
**Expected Output:**
**预期输出**
```py
200
......@@ -194,7 +194,7 @@ print(t) # Displays 2
```
**Expected Output:**
**预期输出**
```py
2
......@@ -219,7 +219,7 @@ print(t) # Displays 101
```
**Expected Output:**
**预期输出**
```py
101
......@@ -260,7 +260,7 @@ func(2) # here no value is passed to j, so default value will be used
```
**Expected Output:**
**预期输出**
```py
2 100
......@@ -274,7 +274,7 @@ func(2, 300) # here 300 is passed as a value of j, so default value will not be
```
**Expected Output:**
**预期输出**
```py
2 300
......@@ -339,7 +339,7 @@ my_func(12, c=13, b=14)
```
**Expected Output:**
**预期输出**
```py
12 13 14
......@@ -367,7 +367,7 @@ print(type(s))
```
**Expected Output:**
**预期输出**
```py
(100, 12)
......
......@@ -65,7 +65,7 @@ for i in range(1, 10):
```
**Expected Output:**
**预期输出**
```py
1
......@@ -109,7 +109,7 @@ for i in range(1, 20, 2):
```
**Expected Output:**
**预期输出**
```py
1
......@@ -150,7 +150,7 @@ while count < 10:
```
**Expected Output:**
**预期输出**
```py
0
......@@ -189,7 +189,7 @@ print("out of while loop")
`count`等于`5`时,如果条件评估为`True`,并且`break`关键字跳出循环。
**Expected Output:**
**预期输出**
```py
inside loop 1
......@@ -217,7 +217,7 @@ while count < 10:
```
**Expected Output:**
**预期输出**
```py
1
......
......@@ -25,7 +25,7 @@ import math
```
| Method | Description |
| 方法 | 描述 |
| --- | --- |
| `ceil(x)` | 此函数将数字四舍五入并返回其最接近的整数 |
| `floor(x)` | 此函数将向下取整并返回其最接近的整数 |
......
......@@ -70,7 +70,7 @@ f.close()  # where f is a file pointer
要从文件读回数据,您需要以下三种方法之一。
| 方法 | Description |
| 方法 | 描述 |
| --- | --- |
| `read([number])` | 从文件中返回指定数量的字符。 如果省略,它将读取文件的全部内容。 |
| `readline()` | 返回文件的下一行。 |
......
......@@ -76,7 +76,7 @@ print(p1.name)
```
**Expected Output:**
**预期输出**
```py
jerry
......@@ -123,7 +123,7 @@ print(b1.checkbalance())
```
**Expected Output:**
**预期输出**
```py
Insufficient funds
......@@ -140,7 +140,7 @@ print(b1.__balance)
```
**Expected Output:**
**预期输出**
```py
AttributeError: 'BankAccount' object has no attribute '__balance'
......
......@@ -86,18 +86,18 @@ print(c3.getRadius())
Python 还有许多其他特殊方法,例如`__add__()`,请参见下面的列表。
| 操作员 | 功能 | 方法说明 |
| 运算符 | 函数 | 方法说明 |
| --- | --- | --- |
| `+` | `__add__(self, other)` | 加 |
| `+` | `__add__(self, other)` | 加 |
| `*` | `__mul__(self, other)` | 乘法 |
| `-` | `__sub__(self, other)` | 减法 |
| `%` | `__mod__(self, other)` | 余 |
| `/` | `__truediv__(self, other)` | |
| `<` | `__lt__(self, other)` | 于 |
| `<=` | `__le__(self, other)`,小于或等于 | |
| `==` | `__eq__(self, other)`等于 | |
| `!=` | `__ne__(self, other)`,不等于 | |
| `>` | `__gt__(self, other)`,大于 | |
| `%` | `__mod__(self, other)` | 余 |
| `/` | `__truediv__(self, other)` | 除法 |
| `<` | `__lt__(self, other)` | 于 |
| `<=` | `__le__(self, other)` | 小于或等于 |
| `==` | `__eq__(self, other)` | 等于 |
| `!=` | `__ne__(self, other)` | 不等于 |
| `>` | `__gt__(self, other)` | 大于 |
`>=``__ge__(self, other)`,大于或等于`[index]``__getitem__(self, index)`,索引运算符`in``__contains__(self, value)`,检查成员资格`len``__len__(self)`,元素数`str``__str__(self)`的字符串表示形式
......@@ -149,7 +149,7 @@ print(c3) # Became possible because we have added __str__ method
```
**Expected Output:**
**预期输出**
```py
4
......
......@@ -121,7 +121,7 @@ c.method_super2()
```
**Expected Output:**
**预期输出**
```py
method_super1 method called
......@@ -159,7 +159,7 @@ c.m1()
```
**Expected Output:**
**预期输出**
```py
m1 from B
......@@ -168,7 +168,7 @@ m1 from B
在这里,我们从基类中重写`m1()`方法。 尝试在`B`类中注释`m1()`方法,现在将运行`Base`类中的`m1()`方法,即`A`类。
**Expected Output:**
**预期输出**
```py
m1 from A
......
......@@ -66,11 +66,11 @@ finally:
`except`子句类似于`elif`。 发生异常时,将检查该异常以匹配`except`子句中的异常类型。 如果找到匹配项,则执行匹配大小写的处理程序。 另请注意,在最后的`except`子句中,`ExceptionType`被省略。 如果异常不匹配最后一个`except`子句之前的任何异常类型,则执行最后一个`except`子句的处理程序。
**note:**
**注意**
`else`子句下的语句仅在没有引发异常时运行。
**note:**
**注意**
无论是否发生异常,`finally`子句中的语句都将运行。
......@@ -99,7 +99,7 @@ finally:
```
**note:**
**注意**
`eval()`函数允许 python 程序在其内部运行 python 代码,`eval()`需要一个字符串参数。
......@@ -151,7 +151,7 @@ age is even
再次运行该程序并输入一个负数。
**Expected Output:**
**预期输出**
```py
Enter your age: -12
......@@ -186,7 +186,7 @@ except NameError as ex:
运行程序并输入一个数字。
**Expected Output:**
**预期输出**
```py
Enter a number: 34
......@@ -196,7 +196,7 @@ The number entered is 34
再次运行程序并输入一个字符串。
**Expected Output:**
**预期输出**
```py
Enter a number: one
......
......@@ -88,7 +88,7 @@ my_three(*a) # here list is broken into three elements
```
**note:**
**注意**
仅当参数数量与可迭代变量中的元素数量相同时,此方法才有效。
......
......@@ -159,7 +159,7 @@ for i in match:
```
**Expected Output:**
**预期输出**:
```py
[('12345', '41521'), ('78963', '85214')]
......@@ -180,7 +180,7 @@ Second group 85214
`re.search()`和`re.findall()`都接受可选参数称为标志。 标志用于修改模式匹配的行为。
| 标志 | Description |
| 标志 | 描述 |
| --- | --- |
| `re.IGNORECASE` | 忽略大写和小写 |
| `re.DOTALL` | 允许(`.`)匹配换行符,默认(`.`)匹配除换行符之外的任何字符 |
......
......@@ -74,7 +74,7 @@ pip search "your query"
```
**note:**
**注意**
您无需在搜索字词前后添加引号。
......
......@@ -101,7 +101,7 @@ with open("myfile.txt") as f:
```
Try it out:
试一试:
```py
# these two lines create a file "myfile.txt" with data "Learning Python"
......
......@@ -71,7 +71,7 @@ print(my_module.__name__)
```
**Expected Output:**
**预期输出**
```py
100
......
......@@ -53,7 +53,7 @@ print(r(12, 3)) # call the lambda function
```
**Expected Output:**
**预期输出**
```py
12
......
......@@ -36,12 +36,12 @@ Sam has 12 red balls and 31 yellow balls
| 格式码 | 描述 |
| --- | --- |
| `d` | 于整数 |
| `d` | 于整数 |
| `f` | 用于浮点数 |
| `b` | 于二进制数 |
| `b` | 于二进制数 |
| `o` | 八进制数 |
| `x` | 八进制十六进制数 |
| `s` | 于字符串 |
| `s` | 于字符串 |
| `e` | 用于指数格式的浮点 |
以下示例将使事情更加清楚。
......@@ -55,7 +55,7 @@ Sam has 12 red balls and 31 yellow balls
在这里,我们指定精度的`2`位,`f`用于表示浮点数。
**Expected Output:**
**预期输出**
```py
Floating point 345.79
......@@ -72,7 +72,7 @@ Floating point 345.79
在这里,我们指定`3`精度数字,`10`表示宽度,`f`表示浮点数。
**Expected Output:**
**预期输出**
```py
Floating point 3.142
......@@ -88,7 +88,7 @@ Floating point 3.142
这里`{1:d}`中的`d`表示整数值。
**Expected Output:**
**预期输出**
```py
Floating point pi = 3.142, with 3 digit precision
......@@ -104,7 +104,7 @@ Floating point pi = 3.142, with 3 digit precision
```
**Expected Output:**
**预期输出**
```py
Sam has 31 red balls and 12 yellow balls
......@@ -118,7 +118,7 @@ Sam has 31 red balls and 12 yellow balls
```
**Expected Output:**
**预期输出**
```py
In binary 4 is 100
......@@ -133,7 +133,7 @@ array = [34, 66, 12]
```
**Expected Output:**
**预期输出**
```py
A = 34, B = 66, C = 12
......@@ -152,7 +152,7 @@ d = {
```
**Expected Output:**
**预期输出**
```py
Sam had 122 hats and 42 mats
......@@ -189,7 +189,7 @@ and {0} bats'.format(3, red = 12, green = 31)
因此,`%d`被替换为元组的第一值,即`12`,而`%.2f`被替换为第二值,即`150.87612`
**Expected Output:**
**预期输出**
```py
12 pens cost = 150.88
......@@ -198,7 +198,7 @@ and {0} bats'.format(3, red = 12, green = 31)
一些更多的例子。
**Example 1:**
**示例 1**
新:
......@@ -214,14 +214,14 @@ and {0} bats'.format(3, red = 12, green = 31)
```
**Expected Output:**
**预期输出**
```py
12 31
```
**Example 2:**
**示例 2**
New:
......@@ -237,14 +237,14 @@ Old:
```
**Expected Output:**
**预期输出**
```py
12.32 89.654
```
**Example 3:**
**示例 3**
New:
......@@ -260,7 +260,7 @@ Old:
```
**Expected Output:**
**预期输出**
```py
Hello 107 45836.13 45
......
......@@ -63,7 +63,7 @@ print(list(zip_obj))
```
Try it out:
试一试:
```py
for i, j, k, l in zip([1, 2, 3], "foo",
......@@ -87,7 +87,7 @@ for i, j, k, l in zip([1, 2, 3], "foo",
```
Try it out:
试一试:
```py
keys = ['alpha', 'beta', 'gamma']
......
......@@ -74,7 +74,7 @@ for i in f:
```
Try it out:
试一试:
```py
def is_even(x):
......@@ -124,7 +124,7 @@ print( list(filter(None, [1, 45, "", 6, 50, 0, {}, False])) )
```
Try it out:
试一试:
```py
# lambda is used in place of a function
......
......@@ -76,7 +76,7 @@ print(reduce(do_sum, [1, 2, 3, 4]))
```
Try it out:
试一试:
```py
def do_sum(x1, x2):
......
......@@ -75,7 +75,7 @@ print(sorted(ages, reverse=True))
```
Try it out:
试一试:
```py
fruits = ['lime', 'blueberry', 'plum', 'avocado']
......@@ -108,7 +108,7 @@ print(ages)
```
Try it out:
试一试:
```py
name = "Alfred Hajos"
......@@ -136,7 +136,7 @@ print(sorted(name, reverse=True))
```
Try it out:
试一试:
```py
t = ( 'ff', 'xx', 'gg', 'aa')
......@@ -223,7 +223,7 @@ for k in sorted(d, reverse=True):
```
Try it out:
试一试:
```py
fruits = ['lime', 'blueberry', 'plum', 'avocado']
......@@ -288,7 +288,7 @@ print(sorted(t, key=str.lower))
```
Try it out:
试一试:
```py
fruits = ['lime', 'blueberry', 'plum', 'avocado']
......
......@@ -86,7 +86,7 @@ for index, value in enumerate("hello"):
```
Try it out:
试一试:
```py
for index, value in enumerate([110, 45, 12, 891, "one"]):
......@@ -127,7 +127,7 @@ for index, value in enumerate({1, 290, -88, 10}):
```
Try it out:
试一试:
```py
print( list(enumerate("hello", start=2)) )
......
......@@ -130,7 +130,7 @@ TypeError: argument to reversed() must be a sequence
```
Try it out:
试一试:
```py
from collections import namedtuple
......
......@@ -64,7 +64,7 @@ range(5, 10)
```
Try it out:
试一试:
```py
print(range(5, 10))
......@@ -87,7 +87,7 @@ print(list(range(5, 10)))
```
Try it out:
试一试:
```py
print(list(range(-2, 2)))
......@@ -109,7 +109,7 @@ range(1, 20, 3)
```
Try it out:
试一试:
```py
print( range(1, 20, 3))
......@@ -132,7 +132,7 @@ print(list(range(1, 20, 3)))
```
Try it out:
试一试:
```py
print(list(range(20, 10, -1)))
......@@ -156,7 +156,7 @@ print(list(range(20, 10, -5)))
```
Try it out:
试一试:
```py
for i in range(5):
......
......@@ -67,7 +67,7 @@ print(sum({1: "one", 2: "two", 3: "three"})) # sum values in a
```
Try it out:
试一试:
```py
print(sum([10, 20, 30], 100))
......
......@@ -152,7 +152,7 @@ TypeError: unorderable types: list() > int()
```
Try it out:
试一试:
```py
print(max("c", "b", "a", "Y", "Z"))
......@@ -174,7 +174,7 @@ print(max("c", "b", "a", "Y", "Z", key=str.lower))
```
Try it out:
试一试:
```py
print(max(("python", "lua", "ruby")))
......
......@@ -30,7 +30,7 @@ min(a, b, c, ...[, key=func]) -> value
```
| PARAMETER | 描述 |
| 参数 | 描述 |
| --- | --- |
| `a, b, c ...` | 比较项目 |
| `key`(可选) | 它引用单个参数函数以自定义排序顺序。 该功能适用​​于每个项目。 |
......@@ -112,7 +112,7 @@ print(min([], default=0))
```
Try it out:
试一试:
```py
print(min(20, 10, 30, -5))
......@@ -159,7 +159,7 @@ TypeError: unorderable types: list() > int()
```
Try it out:
试一试:
```py
print(min("c", "b", "a", "Y", "Z"))
......@@ -180,7 +180,7 @@ print(min("c", "b", "a", "Y", "Z", key=str.lower))
```
Try it out:
试一试:
```py
print(min(("java", "python", "z++")))
......
......@@ -100,7 +100,7 @@ print(eval("'hello' + 'py'"))
```
Try it out:
试一试:
```py
print(eval("abs(-11)"))
......
......@@ -73,7 +73,7 @@ TypeError: object of type 'generator' has no len()
```
Try it out:
试一试:
```py
def gen_func():
......@@ -122,7 +122,7 @@ print(len(gen_func()))
```
Try it out:
试一试:
```py
class Stack:
......
......@@ -21,7 +21,7 @@ globals() -> dictionary
**示例 1**
**module1.py**
**`module1.py`**
```py
#!/usr/bin/python3
......@@ -76,7 +76,7 @@ pprint(globals())
**示例 2**
**module1.py**
**`module1.py`**
```py
#!/usr/bin/python3
......@@ -92,7 +92,7 @@ def foo():
```
**module2.py**
**`module2.py`**
```py
#!/usr/bin/python3
......@@ -106,7 +106,7 @@ module1.foo()
```
**Expected Output:**
**预期输出**
```py
{'__builtins__': { ... }
......
......@@ -106,7 +106,7 @@ print(cursor)
```
**Expected Output**:
**预期输出**
```py
<_mysql.connection open to '127.0.0.1' at 239e2c0>
......@@ -145,7 +145,7 @@ print(number_of_rows)
```
**Expected Output**:
**预期输出**
```py
4079
......
......@@ -77,7 +77,7 @@ print("hello world")
```
Try it out:
试一试:
```py
# This program prints "hello world"
......@@ -98,7 +98,7 @@ print("hello world") # display "hello world"
当注释以这种形式出现时,它们称为最终注释。
Try it out:
试一试:
```py
# This program prints "hello world"
......@@ -127,7 +127,7 @@ print(b)
```
Try it out:
试一试:
```py
a, b = 10, 20
......@@ -156,7 +156,7 @@ print(b)
```
Try it out:
试一试:
```py
x = 1 # initial value of x is 1
......@@ -202,7 +202,7 @@ Python 即有 5 种标准数据类型。
```
Try it out:
试一试:
```py
name = input("Enter your name: ")
......@@ -221,7 +221,7 @@ Enter your age: 22
```
Try it out:
试一试:
```py
age = int(input("Enter your age: "))
......@@ -268,7 +268,7 @@ import module_name_1, module_name_2
```
Try it out:
试一试:
```py
import math, os
......
......@@ -79,9 +79,9 @@ db.close()
**语法**`cursor_object.executemany(statement, arguments)`
**语句**:包含要执行的查询的字符串。
**`statement`**:包含要执行的查询的字符串。
**自变量**:一个包含要在`insert`语句中使用的值的序列。
**`arguments`**:一个包含要在`insert`语句中使用的值的序列。
让我们举个例子。
......
......@@ -114,7 +114,7 @@ open('todo.md', 'rt')
这里有些例子:
**poem.txt**
**`poem.txt`**
```py
The caged bird sings
......@@ -269,7 +269,7 @@ but longed for still
为了写入数据,文件对象提供了以下两种方法:
| Method | Description |
| 方法 | 描述 |
| --- | --- |
| `write(s)` | 将字符串`s`写入文件并返回写入的数字字符。 |
| `writelines(s)` | 将序列`s`中的所有字符串写入文件。 |
......@@ -539,7 +539,7 @@ b'The caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for
到目前为止,我们已经线性地读写文件。 但是也可以在特定位置进行读写。 为此,文件对象提供了以下两种方法:
| Method | Description |
| 方法 | 描述 |
| --- | --- |
| `tell()` | 返回文件指针的当前位置。 |
| `seek(offset, [whence=0])` | 将文件指针移动到给定的`offset``offset`引用字节计数,`whence`确定`offset`将相对于文件指针移动的位置。 `whence`的默认值为 0,这意味着`offset`将使文件指针从文件的开头移开。 如果`wherece`设置为`1``2`,则偏移量将分别将文件的指针从当前位置或文件的末尾移动。 |
......@@ -680,7 +680,7 @@ b'longed for still'
`fseek()``whence`自变量的值在`os`模块中也定义为常量。
| 值 | 不变 |
| 值 | 常量 |
| --- | --- |
| `0` | `SEEK_SET` |
| `1` | `SEEK_CUR` |
......
......@@ -22,7 +22,7 @@ CSV 文件的第一行表示标题,该标题包含文件中的列名列表。
CSV 文件通常用于表示表格数据。 例如,考虑下表:
| ID | Name | 国家代码 | 区 | 人口 |
| ID | 名称 | 国家代码 | 区域 | 人口 |
| --- | --- | --- | --- | --- |
| 1 | Kabul | AFG | Kabol | 1780000 |
| 2 | Qandahar | AFG | Qandahar | 237500 |
......@@ -44,7 +44,7 @@ CSV 文件通常用于表示表格数据。 例如,考虑下表:
如果 CSV 文件中的值包含逗号,则必须将其用双引号引起来。 例如:
| Name | 年龄 | 地址 |
| 名称 | 年龄 | 地址 |
| --- | --- | --- |
| Jerry | 10 | 2776 McDowell Street, Nashville, Tennessee |
| Tom | 20 | 3171 Jessie Street, Westerville, Ohio |
......@@ -155,7 +155,7 @@ with open('employees.csv', 'rt') as f:
```
**Expected Output:**
**预期输出**
```py
id name email
......@@ -180,7 +180,7 @@ with open('employees.csv', 'rt') as f:
```
**Expected Output:**
**预期输出**
```py
1 John john@mail.com
......@@ -213,7 +213,7 @@ with open('employees.csv', 'rt') as f:
* * *
**employee_pipe.csv**
**`employee_pipe.csv`**
```py
id|name|email|age|designation
......@@ -236,7 +236,7 @@ with open('employees.csv', 'rt') as f:
```
**Expected Output:**
**预期输出**
```py
['id', 'name', 'email', 'age', 'designation']
......@@ -250,7 +250,7 @@ with open('employees.csv', 'rt') as f:
* * *
**Basketball_players.csv**
**`Basketball_players.csv`**
```py
"Name", "Team", "Position", "Height(inches)", "Weight(lbs)", "Age"
......@@ -278,7 +278,7 @@ with open('baseball_players.csv', 'rt') as f:
```
**Expected Output:**
**预期输出**
```py
['Name', 'Team', 'Position', 'Height(inches)', 'Weight(lbs)', 'Age']
......@@ -297,7 +297,7 @@ with open('baseball_players.csv', 'rt') as f:
* * *
**address.csv**
**`address.csv`**
```py
Name, Age, Address
......@@ -322,7 +322,7 @@ with open('addresses.csv', 'rt') as f:
```
**Expected Output:**
**预期输出**
```py
['Name', 'Age', 'Address']
......@@ -345,7 +345,7 @@ with open('housing.csv', 'rt') as f:
```
**Expected Output:**
**预期输出**
```py
['Name', 'Age', 'Address']
......@@ -359,7 +359,7 @@ with open('housing.csv', 'rt') as f:
* * *
**comments.csv**
**`comments.csv`**
```py
Id, User, Comment
......@@ -385,7 +385,7 @@ with open('employees.csv', 'rt') as f:
```
**Expected Output:**
**预期输出**:
```py
['Id', 'User', 'Comment']
......@@ -409,7 +409,7 @@ with open('employees.csv', 'rt') as f:
```
**Expected Output:**
**预期输出**:
```py
['Id', 'User', 'Comment']
......@@ -424,7 +424,7 @@ with open('employees.csv', 'rt') as f:
* * *
**dialogs.csv**
**`dialogs.csv`**
```py
Id, Actor, Dialogue
......@@ -449,7 +449,7 @@ for line in csv_reader:
```
**Expected Output:**
**预期输出**:
```py
['Id', 'Actor', 'Dialogue']
......@@ -473,7 +473,7 @@ with open('employees.csv', 'rt') as f:
```
**Expected Output:**
**预期输出**:
```py
['Id', 'Actor', 'Dialogue']
......@@ -494,13 +494,13 @@ with open('employees.csv', 'rt') as f:
| 参数 | 描述 |
| --- | --- |
| `fileobj` | (required) It refers to the file object |
| `dialect` | (optional) Dialect refers to the different ways of formatting the CSV document. By default, the `csv` module uses the same format as Microsoft Excel. We will discuss dialect in detail later in this post. |
| `fileobj` | (必需)它引用文件对象 |
| `dialect` | (可选)方言是指格式化 CSV 文档的不同方式。 默认情况下,`csv`模块使用与 Microsoft Excel 相同的格式。 我们将在本文后面详细讨论方言。 |
| `fmtparam` | (可选)格式化参数,与`reader()`的功能相同。 |
`writer`实例提供以下两种写入数据的方法:
| 方法 | Description |
| 方法 | 描述 |
| --- | --- |
| `writerow(row)` | 写入一行数据并返回写入的字符数。 `row`必须是字符串和数字的序列。 |
| `writerows(rows)` | 写入多行数据并返回`None`。 `rows`必须是一个序列。 |
......@@ -556,7 +556,7 @@ with open('customers.csv', 'wt') as f:
这两个清单生成的输出将是相同的,看起来像这样:
**customer.csv**
**`customer.csv`**
```py
id,name,address,zip
......@@ -593,9 +593,9 @@ with open('customers.csv', 'wt') as f:
```
**Expected Output:**
**预期输出**:
**customers.csv**
**`customers.csv`**
```py
"id","name","address","zip"
......@@ -632,7 +632,7 @@ with open('customers.csv', 'wt') as f:
```
**Expected Output:**
**预期输出**:
```py
"id","name","address","zip"
......@@ -671,7 +671,7 @@ with open('customers.csv', 'wt') as f:
```
**Expected Output:**
**预期输出**:
```py
id,name,address,zip
......@@ -708,7 +708,7 @@ with open('customers.csv', 'wt') as f:
```
**Expected Output:**
**预期输出**:
```py
Traceback (most recent call last):
......@@ -745,7 +745,7 @@ with open('customers.csv', 'wt') as f:
```
**Expected Output:**
**预期输出**
```py
id,name,address,zip
......@@ -782,7 +782,7 @@ id,name,address,zip
**示例 1** :
**customers.csv**
**`customers.csv`**
```py
id,name,address,zip
......@@ -805,7 +805,7 @@ with open('customers.csv', 'rt') as f:
```
**Expected Output:**
**预期输出**:
```py
{'id': '1', 'name': 'Hannah', 'zip': '99503', 'address': '4891 Blackwell Street, Anchorage, Alaska'}
......@@ -813,10 +813,10 @@ with open('customers.csv', 'rt') as f:
{'id': '3', 'name': 'Sam', 'zip': '93704', 'address': '3952 Little Street, Akron, Ohio'}
{'id': '4', 'name': 'Chris', 'zip': '62677', 'address': '3192 Flinderation Road, Arlington Heights, Illinois'}
{'id': '5', 'name': 'Doug', 'zip': '61257', 'address': '3236 Walkers Ridge Way, Burr Ridge'}
**Note:** Order of keys in the result may vary. Since dictionary doesn't preserve the order of elements.
```
**注意**:结果中键的顺序可能会有所不同。 由于字典不保留元素的顺序。
在这种情况下,字段名称是从 CSV 文件的第一行(或标题)推断出来的。
**示例 2** :使用`fieldnames`参数
......@@ -845,7 +845,7 @@ with open('customers.csv', 'rt') as f:
```
**Expected Output:**
**预期输出**:
```py
{'name': 'Hannah', 'zip': '99503', 'id': '1', 'address': '4891 Blackwell Street, Anchorage, Alaska'}
......@@ -881,7 +881,7 @@ with open('customers.csv', 'rt') as f:
```
**Expected Output:**
**预期输出**:
```py
{'id': '1', 'name': 'Hannah', 'extra': ['4891 Blackwell Street, Anchorage, Alaska', '99503']}
......@@ -919,7 +919,7 @@ with open('customers.csv', 'rt') as f:
```
**Expected Output:**
**预期输出**:
```py
{'id': '1', 'name': 'Hannah', 'email': 'NA', 'phone': 'NA', 'address': '4891 Blackwell Street, Anchorage, Alaska', 'zip': '99503'}
......@@ -949,15 +949,15 @@ with open('customers.csv', 'rt') as f:
`DictWriter`提供以下三种写入数据的方法。
| Method | Description |
| 方法 | 描述 |
| --- | --- |
| `writeheader()` | 将标头(即`fieldnames`)写入 CSV 文件并返回`None`。 |
| `writerow(row)` | Writes a single row of data and returns the number of characters written. The `row` must be a sequence of strings and number. |
| `writerows(rows)` | Writes multiple rows of data and returns `None`. The `rows` must be a sequence. |
| `writerow(row)` | 写入一行数据并返回写入的字符数。 `row`必须是字符串和数字的序列。 |
| `writerows(rows)` | 写入多行数据并返回`None`。 `rows`必须是一个序列。 |
Let's take some examples:
**Example 1**:
**示例 1**:
```py
import csv
......@@ -982,9 +982,9 @@ with open('dictcustomers.csv', 'wt') as f:
```
**Expected Output:**
**预期输出**:
**dictcustomers.csv**
**`dictcustomers.csv`**
```py
id,name,address,zip
......@@ -1021,9 +1021,9 @@ with open('dictcustomers.csv', 'wt') as f:
```
**Expected Output:**
**预期输出**:
**dictcustomers.csv**
**`dictcustomers.csv`**
```py
id,name,address,zip,email
......@@ -1069,9 +1069,9 @@ with open('dictcustomers.csv', 'wt') as f:
在此,词典包含一个名为`zip`的附加键,该键不在`header`列表中。 为了防止引发异常,我们将`extrasaction`设置为`ignore`。
**Expected Output:**
**预期输出**:
**dictcustomers.csv**
**`dictcustomers.csv`**
```py
id,name,address
......@@ -1097,7 +1097,7 @@ id,name,address
下表列出了所有格式参数及其默认值:
| Argument | 默认 | Description |
| 参数 | 默认值 | 描述 |
| --- | --- | --- |
| `delimiter` | `,` | 它是指用于分隔 CSV 文件中的值(或字段)的字符。 |
| `skipinitialspace` | `False` | 它控制定界符后面的空格的解释方式。 如果为`True`,则将删除初始空格。 |
......@@ -1139,7 +1139,7 @@ with open('oscars.csv', 'wt') as f:
```
**Expected Output:**
**预期输出**:
oscars.csv
......
......@@ -182,7 +182,7 @@ Python 提供了一个称为`json`的内置模块,用于对对象进行序列
当我们反序列化对象时,JSON 类型将转换回其等效的 Python 类型。 下表中描述了此操作:
| JSON Type | Python Type |
| JSON 类型 | Python 类型 |
| --- | --- |
| `object` | `dict` |
| `array` | `list` |
......
......@@ -82,15 +82,15 @@ Python 具有不同的运算符,可让您在程序中执行所需的计算。
`+``-``*`可以正常工作,其余的运算符需要一些解释。
| Name | 含义 | 例 | 结果 |
| 名称 | 含义 | 示例 | 结果 |
| --- | --- | --- | --- |
| `+` | 加 | `15+20` | `35` |
| `+` | 加 | `15+20` | `35` |
| `-` | 减法 | `24.5-3.5` | `21.0` |
| `*` | 乘法 | `15*4` | `60` |
| `/` | 浮动分部 | `4/5` | `0.8` |
| `//` | 整数 | `4//5` | `0` |
| `/` | 浮点除法 | `4/5` | `0.8` |
| `//` | 整数除法 | `4//5` | `0` |
| `**` | 求幂 | `4**2` | `16` |
| `%` | 余 | `27%4` | `3` |
| `%` | 余 | `27%4` | `3` |
**浮点除法(`/`)**`/`运算符进行除法并以浮点数形式返回结果,这意味着它将始终返回小数部分。 例如
......@@ -204,15 +204,15 @@ a = b = c
类似地,您可以将`-`,`%`,`//`,`/`,`*`和`**`与赋值运算符一起使用以构成扩展赋值运算符。
| 操作员 | Name | Example | 当量 |
| 运算符 | 名称 | 示例 | 等价于 |
| --- | --- | --- | --- |
| `+=` | 加法分配 | `x += 4` | `x = x + 4` |
| `-=` | 减法分配 | `x -= 2` | `x = x - 2` |
| `*=` | 乘法分配 | `x *= 5` | `x = x * 5` |
| `/*=` | 部门分配 | `x /= 5` | `x = x / 5` |
| `//*=` | 整数除法分配 | `x //= 5` | `x = x // 5` |
| `%*=` | 剩余分配 | `x %= 5` | `x = x % 5` |
| `**=` | 指数分配 | `x **= 5` | `x = x ** 5` |
| `+=` | 加法赋值 | `x += 4` | `x = x + 4` |
| `-=` | 减法赋值 | `x -= 2` | `x = x - 2` |
| `*=` | 乘法赋值 | `x *= 5` | `x = x * 5` |
| `/*=` | 除法赋值 | `x /= 5` | `x = x / 5` |
| `//*=` | 整数除法赋值 | `x //= 5` | `x = x // 5` |
| `%*=` | 余数赋值 | `x %= 5` | `x = x % 5` |
| `**=` | 指数赋值 | `x **= 5` | `x = x ** 5` |
在下一篇文章中,我们将学习 python 字符串。
......
......@@ -110,7 +110,7 @@ t
```
Try it out:
试一试:
```py
name = "tom"
......@@ -135,7 +135,7 @@ tom and jerry
```
Try it out:
试一试:
```py
s = "tom and " + "jerry"
......@@ -180,7 +180,7 @@ el
```
Try it out:
试一试:
```py
s = "Welcome"
......@@ -214,7 +214,7 @@ print(s[1:-1])
```
Try it out:
试一试:
```py
ch = 'b'
......@@ -230,7 +230,7 @@ print(ord('A'))
* * *
| 功能名称 | 功能说明 |
| 函数名称 | 函数说明 |
| --- | --- |
| `len ()` | 返回字符串的长度 |
| `max()` | 返回具有最高 ASCII 值的字符 |
......@@ -246,7 +246,7 @@ print(ord('A'))
```
Try it out:
试一试:
```py
print(len("hello"))
......@@ -256,7 +256,7 @@ print(max("abc"))
print(min("abc"))
```
## 在而不是在操作员
## 在而不是在运算符
* * *
......@@ -272,7 +272,7 @@ False
```
Try it out:
试一试:
```py
s1 = "Welcome"
......@@ -311,7 +311,7 @@ True
```
Try it out:
试一试:
```py
print("tim" == "tie")
......@@ -343,7 +343,7 @@ hello
```
**note:**
**注意**
默认情况下,`print()`函数用换行符打印字符串,我们通过传递名为`end`的命名关键字参数来更改此行为,如下所示。
......@@ -354,7 +354,7 @@ print("my string", end="foo") # now print() will print foo after every string
```
Try it out:
试一试:
```py
s = "hello"
......@@ -397,7 +397,7 @@ True
```
Try it out:
试一试:
```py
s = "welcome to python"
......@@ -441,7 +441,7 @@ False
```
Try it out:
试一试:
```py
s = "welcome to python"
......@@ -506,7 +506,7 @@ s = "string in python"
```
Try it out:
试一试:
```py
s = "string in python"
......
......@@ -124,7 +124,7 @@ True
`end`索引也是可选的,如果省略,它将被设置为列表的最后一个索引。
**note:**
**注意**
如果为`start >= end`,则`list[start : end]`将返回一个空列表。 如果`end`指定的位置超出列表的`end`,则 Python 将使用`end`的列表长度。
......@@ -153,7 +153,7 @@ True
```
## `in`或`not in`操作员
## `in`或`not in`运算符
* * *
......@@ -192,7 +192,7 @@ False
* * *
| Method name | Description |
| 方法 | 描述 |
| --- | --- |
| `append(x: object): None` | 在列表的末尾添加元素`x`并返回`None`。 |
| `count(x: object): int` | 返回元素`x`在列表中出现的次数。 |
......@@ -247,7 +247,7 @@ False
* * *
**note:**
**注意**
本主题需要具有 [Python 循环](/loops/)的使用知识。
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册