Skip to content

Commit 4804e6a

Browse files
committed
doc: 添加数值、字符串文档
1 parent a169204 commit 4804e6a

5 files changed

Lines changed: 343 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
- [运算符](./operators.md)
3333
- [if判断](./if-statement.md)
3434
- [循环](./loops.md)
35+
- [数值](./numbers.md)
36+
- [字符串](./strings.md)
3537

3638
## Reference
3739

example/day03/numbers.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env python3
2+
# -*- coding=utf-8 -*-
3+
4+
import math
5+
import cmath
6+
7+
print('math:')
8+
print(dir(math))
9+
10+
print()
11+
12+
print('cmath:')
13+
print(dir(cmath))

example/day03/string.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env python3
2+
# -*- coding=utf-8 -*-
3+
4+
var1 = 'Hello World!'
5+
var2 = "Python"
6+
7+
print("var1[0]: ", var1[0])
8+
print("var2[1:5]: ", var2[1:5])
9+
10+
print('------------------')
11+
12+
a = "Hello"
13+
b = "Python"
14+
15+
print("a + b 输出结果:", a + b)
16+
print("a * 2 输出结果:", a * 2)
17+
print("a[1] 输出结果:", a[1])
18+
print("a[1:4] 输出结果:", a[1:4])
19+
20+
if("H" in a):
21+
print("H 在变量 a 中")
22+
else:
23+
print("H 不在变量 a 中")
24+
25+
if("M" not in a):
26+
print("M 不在变量 a 中")
27+
else:
28+
print("M 在变量 a 中")
29+
30+
print(r'\n')
31+
print(R'\n')
32+
33+
print('str.format函数格式化:')
34+
print("{} {}".format("hello", "world")) # 不设置指定位置,按默认顺序
35+
print("{0} {1}".format("hello", "world")) # 设置指定位置
36+
print("{1} {0} {1}".format("hello", "world")) # 设置指定位置
37+
38+
39+
print("网站名:{name}, 地址 {url}".format(
40+
name="xinghe", url="github.com/Gnotes/Python"))
41+
# 通过字典设置参数
42+
site = {"name": "xinghe", "url": "github.com/Gnotes/Python"}
43+
print("网站名:{name}, 地址 {url}".format(**site))
44+
# 通过列表索引设置参数
45+
my_list = ['xinghe', 'github.com/Gnotes/Python']
46+
print("网站名:{0[0]}, 地址 {0[1]}".format(my_list)) # "0" 是必须的
47+
48+
49+
class AssignValue(object):
50+
def __init__(self, value):
51+
self.value = value
52+
53+
54+
my_value = AssignValue(6)
55+
print('value 为: {0.value}'.format(my_value)) # "0" 是可选的
56+
print('value 为: {.value}'.format(my_value)) # 没有"0"

numbers.md

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# 数值
2+
3+
> 示例:example/day03
4+
5+
Python 支持四种不同的数值类型:
6+
7+
- 整型(Int) - 通常被称为是整型或整数,是正或负整数,不带小数点。
8+
- 长整型(long integers) - 无限大小的整数,整数最后是一个大写或小写的L。
9+
- 浮点型(floating point real values) - 浮点型由整数部分与小数部分组成,浮点型也可以使用科学计数法表示 `(2.5e2 = 2.5 x 10² = 250)`
10+
- 复数(complex numbers) - 复数由 `实数` 部分和 `虚数` 部分构成,可以用 `a + bj` ,或者 `complex(a,b)` 表示, 复数的实部a和虚部b都是浮点型。
11+
12+
> 长整型也可以使用小写 "L",但是还是建议您使用大写 "L",避免与数字 "1" 混淆。Python使用 "L"来显示长整型。
13+
14+
## 类型转换
15+
16+
```py
17+
int(x [,base ]) 将x转换为一个整数
18+
long(x [,base ]) 将x转换为一个长整数
19+
float(x ) 将x转换到一个浮点数
20+
complex(real [,imag ]) 创建一个复数
21+
str(x ) 将对象 x 转换为字符串
22+
repr(x ) 将对象 x 转换为表达式字符串
23+
eval(str ) 用来计算在字符串中的有效Python表达式,并返回一个对象
24+
tuple(s ) 将序列 s 转换为一个元组
25+
list(s ) 将序列 s 转换为一个列表
26+
chr(x ) 将一个整数转换为一个字符
27+
unichr(x ) 将一个整数转换为Unicode字符
28+
ord(x ) 将一个字符转换为它的整数值
29+
hex(x ) 将一个整数转换为一个十六进制字符串
30+
oct(x ) 将一个整数转换为一个八进制字符串
31+
```
32+
33+
## Python math 模块、cmath 模块
34+
35+
> Python 中数学运算常用的函数基本都在 `math` 模块、`cmath` 模块中。
36+
37+
- Python math 模块提供了许多对 `浮点数` 的数学运算函数。
38+
- Python cmath 模块包含了一些用于 `复数` 运算的函数。
39+
40+
> cmath 模块的函数跟 math 模块函数基本一致,区别是 cmath 模块运算的是复数,math 模块运算的是数学运算。
41+
42+
要使用 math 或 cmath 函数必须先导入:
43+
44+
```py
45+
import math
46+
```
47+
48+
查看 math 包中的内容
49+
50+
```py
51+
#!/usr/bin/env python3
52+
# -*- coding=utf-8 -*-
53+
54+
import math
55+
56+
print(dir(math))
57+
58+
# 输出结果
59+
60+
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']
61+
```
62+
63+
## Python数学函数
64+
65+
| 函数 | 返回值,描述 |
66+
| - | - |
67+
| abs(x) | 返回数字的绝对值,如abs(-10) 返回 10 |
68+
| ceil(x) | 返回数字的上入整数,如math.ceil(4.1) 返回 5 |
69+
| cmp(x, y) | 如果 x < y 返回 -1, 如果 x == y 返回 0, 如果 x > y 返回 1 |
70+
| exp(x) | 返回e的x次幂(ex),如math.exp(1) 返回2.718281828459045 |
71+
| fabs(x) | 返回数字的绝对值,如math.fabs(-10) 返回10.0 |
72+
| floor(x) | 返回数字的下舍整数,如math.floor(4.9)返回 4 |
73+
| log(x) | 如math.log(math.e)返回1.0,math.log(100,10)返回2.0 |
74+
| log10(x) | 返回以10为基数的x的对数,如math.log10(100)返回 2.0 |
75+
| max(x1, x2,...) | 返回给定参数的最大值,参数可以为序列。 |
76+
| min(x1, x2,...) | 返回给定参数的最小值,参数可以为序列。 |
77+
| modf(x) | 返回x的整数部分与小数部分,两部分的数值符号与x相同,整数部分以浮点型表示。 |
78+
| pow(x, y) | x**y 运算后的值。 |
79+
| round(x [,n]) | 返回浮点数x的四舍五入值,如给出n值,则代表舍入到小数点后的位数。 |
80+
| sqrt(x) | 返回数字x的平方根 |
81+
82+
## Python随机数函数
83+
84+
| 函数 | 返回值,描述 |
85+
| - | - |
86+
| choice(seq) | 从序列的元素中随机挑选一个元素,比如random.choice(range(10)),从0到9中随机挑选一个整数。 |
87+
| randrange ([start,] stop [,step]) | 从指定范围内,按指定基数递增的集合中获取一个随机数,基数缺省值为1 |
88+
| random() | 随机生成下一个实数,它在[0,1)范围内。 |
89+
| seed([x]) | 改变随机数生成器的种子seed。如果你不了解其原理,你不必特别去设定seed,Python会帮你选择seed。 |
90+
| shuffle(lst) | 将序列的所有元素随机排序 |
91+
| uniform(x, y) | 随机生成下一个实数,它在[x,y]范围内。 |
92+
93+
## Python三角函数
94+
95+
| 函数 | 返回值,描述 |
96+
| - | - |
97+
| acos(x) | 返回x的反余弦弧度值。 |
98+
| asin(x) | 返回x的反正弦弧度值。 |
99+
| atan(x) | 返回x的反正切弧度值。 |
100+
| atan2(y, x) | 返回给定的 X 及 Y 坐标值的反正切值。 |
101+
| cos(x) | 返回x的弧度的余弦值。 |
102+
| hypot(x, y) | 返回欧几里德范数 sqrt(x*x + y*y)。 |
103+
| sin(x) | 返回的x弧度的正弦值。 |
104+
| tan(x) | 返回x弧度的正切值。 |
105+
| degrees(x) | 将弧度转换为角度,如degrees(math.pi/2) , 返回90.0 |
106+
| radians(x) | 将角度转换为弧度 |
107+
108+
## Python数学常量
109+
110+
| 函数 | 返回值,描述 |
111+
| - | - |
112+
| pi | 数学常量 pi(圆周率,一般以π来表示) |
113+
| e | 数学常量 e,e即自然常数(自然常数)。 |
114+
115+
## 参考
116+
117+
- [菜鸟 - 数字](http://www.runoob.com/python/python-numbers.html)

strings.md

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
# 字符串
2+
3+
> 示例:example/day03
4+
5+
> 字符串可以使用 单引号 `'` 或 双引号 `"` 三引号 `'''` 来创建
6+
7+
```py
8+
str1 = '我是字符串'
9+
str2 = "I am a string"
10+
str3 = '''I am a string too'''
11+
```
12+
13+
## 子串获取
14+
15+
前边介绍过可以通过 下标方式: `[头下标:尾下标]`
16+
17+
```py
18+
#!/usr/bin/python
19+
20+
var1 = 'Hello World!'
21+
var2 = "Python"
22+
23+
print("var1[0]: ", var1[0])
24+
print("var2[1:5]: ", var2[1:5])
25+
```
26+
27+
## 转义字符
28+
29+
>
30+
31+
## 字符串运算
32+
33+
| 函数 | 描述 | 实例 |
34+
| - | - | - |
35+
| + | 字符串连接 | >>>a + b <br/> 'HelloPython' |
36+
| * | 重复输出字符串 | >>>a * 2 <br/> 'HelloHello' |
37+
| [] | 通过索引获取字符串中字符 | >>>a[1] <br/> 'e' |
38+
| [ : ] | 截取字符串中的一部分 | >>>a[1:4] <br/> 'ell' |
39+
| in | 成员运算符 - 如果字符串中包含给定的字符返回 True | >>>"H" in a <br/> True |
40+
| not in | 成员运算符 - 如果字符串中不包含给定的字符返回 True | >>>"M" not in a <br/>mTrue |
41+
| r/R | 原始字符串:所有的字符串都是直接按照字面的意思来使用,没有转义特殊或不能打印的字符。<br/> 原始字符串除在字符串的第一个引号前加上字母"r"(可以大小写)以外,与普通字符串有着几乎完全相同的语法。| >>>print r'\n' \n <br/> >>> print R'\n' | \n |
42+
| % | 格式字符串 | - |
43+
44+
## 字符串格式化
45+
46+
> Python 中,字符串格式化使用与 C 中 `sprintf` 函数一样的语法。
47+
48+
| 符号 | 描述 |
49+
| - | - | - |
50+
| %c | 格式化字符及其ASCII码 |
51+
| %s | 格式化字符串 |
52+
| %d | 格式化整数 |
53+
| %u | 格式化无符号整型 |
54+
| %o | 格式化无符号八进制数 |
55+
| %x | 格式化无符号十六进制数 |
56+
| %X | 格式化无符号十六进制数(大写) |
57+
| %f | 格式化浮点数字,可指定小数点后的精度 |
58+
| %e | 用科学计数法格式化浮点数 |
59+
| %E | 作用同%e,用科学计数法格式化浮点数 |
60+
| %g | %f和%e的简写 |
61+
| %G | %f 和 %E 的简写 |
62+
| %p | 用十六进制数格式化变量的地址 |
63+
64+
- 格式化操作符辅助指令:
65+
66+
| 符号 | 描述 |
67+
| - | - | - |
68+
| * | 定义宽度或者小数点精度 |
69+
| - | 用做左对齐 |
70+
| + | 在正数前面显示加号( + ) |
71+
| <sp> | 在正数前面显示空格 |
72+
| # | 在八进制数前面显示零('0'),在十六进制前面显示'0x'或者'0X'(取决于用的是'x'还是'X') |
73+
| 0 | 显示的数字前面填充'0'而不是默认的空格 |
74+
| % | '%%'输出一个单一的'%' |
75+
| (var) | 映射变量(字典参数) |
76+
| m.n. | m 是显示的最小总宽度,n 是小数点后的位数(如果可用的话) |
77+
78+
> Python2.6 开始,新增了格式化字符串的函数 [`str.format()`](http://www.runoob.com/python/att-string-format.html)
79+
> 基本语法是通过 `{}``:` 来代替以前的 `%`
80+
81+
82+
```py
83+
print("{} {}".format("hello", "world")) # 不设置指定位置,按默认顺序
84+
85+
print("{0} {1}".format("hello", "world")) # 设置指定位置
86+
87+
print("{1} {0} {1}".format("hello", "world")) # 设置指定位置
88+
```
89+
90+
## 指定参数
91+
92+
```py
93+
print("网站名:{name}, 地址 {url}".format(
94+
name="xinghe", url="github.com/Gnotes/Python"))
95+
# 通过字典设置参数
96+
site = {"name": "xinghe", "url": "github.com/Gnotes/Python"}
97+
print("网站名:{name}, 地址 {url}".format(**site))
98+
# 通过列表索引设置参数
99+
my_list = ['xinghe', 'github.com/Gnotes/Python']
100+
print("网站名:{0[0]}, 地址 {0[1]}".format(my_list)) # "0" 是必须的
101+
```
102+
103+
## 传入对象
104+
105+
```py
106+
class AssignValue(object):
107+
def __init__(self, value):
108+
self.value = value
109+
110+
111+
my_value = AssignValue(6)
112+
print('value 为: {0.value}'.format(my_value)) # "0" 是可选的
113+
print('value 为: {.value}'.format(my_value)) # 没有"0"
114+
```
115+
116+
## 数字格式化
117+
118+
```py
119+
>>> print("{:.2f}".format(3.1415926));
120+
3.14
121+
```
122+
123+
| 数字 | 格式 | 输出 | 描述 |
124+
| - | - | - | - |
125+
| 3.1415926 | {:.2f} | 3.14 | 保留小数点后两位 |
126+
| 3.1415926 | {:+.2f} | +3.14 | 带符号保留小数点后两位 |
127+
| -1 | {:+.2f} | -1.00 | 带符号保留小数点后两位 |
128+
| 2.71828 | {:.0f} | 3 | 不带小数 |
129+
| 5 | {:0>2d}| 05 | 数字补零 (填充左边, 宽度为2) |
130+
| 5 | {:x<4d} | 5xxx | 数字补x (填充右边, 宽度为4) |
131+
| 10 | {:x<4d} | 10xx | 数字补x (填充右边, 宽度为4) |
132+
| 1000000 | {:,} | 1,000,000 | 以逗号分隔的数字格式 |
133+
| 0.25 | {:.2%} | 25.00% | 百分比格式 |
134+
| 1000000000 | {:.2e} | 1.00e+09 | 指数记法 |
135+
| 13 | {:10d} | 13 | 右对齐 (默认, 宽度为10) |
136+
| 13 | {:<10d} | 13 | 左对齐 (宽度为10) |
137+
| 13 | {:^10d} | 13 | 中间对齐 (宽度为10) |
138+
| 11 | '{:b}'.format(11) <br/> '{:d}'.format(11) <br/> '{:o}'.format(11) <br/> '{:x}'.format(11) <br/> '{:#x}'.format(11) <br/> '{:#X}'.format(11) | 1011 <br/> 11 <br/> 13 <br/> b <br/> 0xb <br/> 0XB | 进制 |
139+
140+
> `^`, `<`, `>` 分别是`居中``左对齐``右对齐`,后面带宽度, `:` 号后面带填充的字符,`只能是一个字符`,不指定则`默认是用空格`填充。
141+
> `+` 表示在正数前显示 `+`,负数前显示 `-``' '`(空格)表示在正数前加空格
142+
> `b``d``o``x` 分别是二进制、十进制、八进制、十六进制。
143+
144+
- 此外我们可以使用大括号 `{}` 来转义大括号,如下实例
145+
146+
```py
147+
#!/usr/bin/python
148+
# -*- coding: UTF-8 -*-
149+
150+
print("{} 对应的位置是 {{0}}".format("runoob"))
151+
152+
# 输出结果
153+
154+
runoob 对应的位置是 {0}
155+
```

0 commit comments

Comments
 (0)