08. 公共方法

1. 运算符

2. 内置函数

3. 标准库

3.1 数学计算:math

3.2 随机数生成:random

3.3 文件通配符:glob

3.4 日期和时间:time

 

1. 运算符

运算符 Python表达式 结果 描述 支持的数据类型
+  [1, 2] + [3, 4] [1, 2, 3, 4] 合并 字符串、列表、元组
* "Hi!" * 4 Hi!Hi!Hi!Hi! 复制 字符串、列表、元组
in  3 in (1, 2, 3) True 元素是否存在 字符串、列表、元组、字典
not in 4 not in (1, 2, 3) True 元素是否不存在 字符串、列表、元组、字典

 

 1 >>> # +
 2 >>> "abc" + "def"
 3 'abcdef'
 4 >>> [1, 2] + [2, 3]
 5 [1, 2, 2, 3]
 6 >>> (1, 2) + (2, 3)
 7 (1, 2, 2, 3)
 8 >>> 
 9 >>> # *
10 >>> "abc" * 4
11 'abcabcabcabc'
12 >>> [1, 2] * 4
13 [1, 2, 1, 2, 1, 2, 1, 2]
14 >>> (1, 2) * 4
15 (1, 2, 1, 2, 1, 2, 1, 2)
16 >>> [(1, 2), (2, 2)] * 2
17 [(1, 2), (2, 2), (1, 2), (2, 2)]
18 >>>
19 >>># in / not in 
20 >>> 1 in [1, 2]
21 True
22 >>> 1 in {2: 1, 3: 4} # 判断的是字典的键
23 False
24 >>> 2 in {2: 1, 3: 4}  
25 True

注意:+= 可以合并列表和元组的元素,但 + 不能。

1 >>> a = [1, 2]
2 >>> a += (3, 4)
3 >>> a
4 [1, 2, 3, 4]
5 >>>
6 >>> a = a + (3, 4)
7 Traceback (most recent call last):
8   File "<stdin>", line 1, in <module>
9 TypeError: can only concatenate list (not "tuple") to list

 

2. 内置函数

Python中查看内置函数的方法:

>>> dir()
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'b', 'di', 'keyword', 's']
>>> dir(__builtins__)  # 查看内置函数
  • len() 在操作字典数据时,返回的是键值对的个数。

  • del 有两种用法,一种是del加空格,另一种是del()。

 1 >>> max(["a", 1, 2])  # max() 函数无法比较不同类型的数据
 2 Traceback (most recent call last):
 3   File "<stdin>", line 1, in <module>
 4 TypeError: '>' not supported between instances of 'int' and 'str'
 5 >>> max([1, 2])
 6 2
 7 >>> max(["a", "b"])
 8 'b'
 9 >>> max([[1, 2], [2, 3]])  # 多维列表/元组的比较,仅比较首元素
10 [2, 3]
11 >>> max([[1, 2], [2, 1]])
12 [2, 1]
13 >>> max([[1, 2], [2]])
14 [2]

3. 标准库

3.1 数学计算:math

math 模块提供了许多对浮点数的数学运算函数。

 1 >>> import math
 2 >>> dir(math)  # 查看math模块的方法
 3 ['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']
 4 >>> math.floor(1.3)  # 向下取整
 5 1
 6 >>> math.ceil(1.3)  # 向上取整
 7 2
 8 >>> math.sqrt(100)  # 平方根
 9 10.0
10 >>> math.pow(10, 3)  # 10的3次方
11 1000.0
12 >>> math.fabs(-1)  # 绝对值
13 1.0

3.2 随机数生成:random

random提供了生成随机数的工具,常用方法如下:

  • random():返回随机生成的一个实数,它在 [0,1) 范围内。

  • randint(a, b):用于生成一个指定范围 [a, b] 间的整数。

  • randrange(a, b, c):以c为步长,随机生成 [a, b) 间的整数。

  • uniform(a, b):随机生成 [a, b] 间的浮点数。

  • choice(x):随机选取序列x中的一个元素。

  • sample(x, num):随机选取序列x中的指定个数(num)元素。

  • shuffle(x):对序列x中的元素顺序进行打乱。

 1 >>> import random
 2 >>> random.random()
 3 0.8626173237509037
 4 >>> random.randint(1, 10)
 5 8
 6 >>> random.randrange(1, 10)
 7 3
 8 >>> random.randrange(1, 11, 2)  # 返回奇数
 9 1
10 >>> random.uniform(1, 10)
11 4.971518666776693
12 >>> random.choice("abcd")
13 'b'
14 >>> random.choice([1, 2, 3])
15 2
16 >>> random.sample([1, 2, 3, 4], 2)
17 [2, 1]
18 >>> li = [1, 2, 3, 4]  
19 >>> random.shuffle(li)  # 在原列表中进行了顺序打乱
20 >>> li
21 [2, 4, 3, 1]

3.3 文件通配符:glob

glob模块提供了一个函数用于从目录通配符搜索中生成文件列表。

1 >>> import glob
2 >>> glob.glob("*.py")
3 ['test.py', 'test1.py', 'test2.py']
4 >>> glob.glob("*.txt")
5 []

3.4 日期和时间:time

1 >>> import time
2 >>> time.localtime()
3 time.struct_time(tm_year=2020, tm_mon=2, tm_mday=16, tm_hour=17, tm_min=43, tm_sec=41, tm_wday=6, tm_yday=47, tm_isdst=0)
4 >>> # 格式化
5 >>> time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
6 '2020-02-16 20:51:16'

猜你喜欢

转载自www.cnblogs.com/juno3550/p/12318695.html
今日推荐