Numeric Types — int, float, complex
Operation |
Result |
x + y |
sum of x and y |
x - y |
difference of x and y |
x * y |
product of x and y |
x / y |
quotient of x and y |
x // y |
floored quotient of x and y |
x % y |
remainder of x / y |
-x |
x negated |
+x |
x unchanged |
abs(x) |
absolute value or magnitude of x |
int(x) |
x converted to integer |
float(x) |
x converted to floating point |
complex(re, im) |
a complex number with real part re, imaginary part im. im defaults to zero. |
c.conjugate() |
conjugate of the complex number c |
divmod(x, y) |
the pair (x // y, x % y) |
pow(x, y) |
x to the power y |
x ** y |
x to the power y |
Sequence Types — list, tuple, range
Operation |
Result |
x in s |
True if an item of s is equal to x, else False |
x not in s |
False if an item of s is equal to x, else True |
s + t |
the concatenation of s and t |
s * n or n * s |
equivalent to adding s to itself n times |
s[i] |
ith item of s, origin 0 |
s[i:j] |
slice of s from i to j |
s[i:j:k] |
slice of s from i to j with step k |
len(s) |
length of s |
min(s) |
smallest item of s |
max(s) |
largest item of s |
s.index(x[, i[, j]]) |
index of the first occurrence of x in s (at or after index i and before index j) |
s.count(x) |
total number of occurrences of x in s |
不可变类型
Numbers
round(x[,n])
math.ceil(x)
math.floor(x)
2.75.as_integer_ratio()
String
"hello".capitalize()
casefold()
str.count(sub[, start[, end]])
str.endswith(suffix[, start[, end]])
str.startswith(prefix[, start[, end]])
str.find(sub[, start[, end]])
"The sum of 1 + 2 is {0}".format(1+2)
"12345abbb".isalnum()
"12345abbb".isalpha()
"12345".isnumeric()
a=["1","2","3","4"]
c="n".join(a)
str.lstrip([chars])
str.rstrip([chars])
str.strip([chars])
c="hello".partition("l")
'1,2,3'.split(',')
"42".zfill(5)
Tuple
tu=(2,3,4)
tu = tuple([2,3,4])
tu = tuple('string')
tu[0]
print(tu)
tu=(1,2,3)+(4,5,6)
tu = (1,2,3)*4
tu=(1,2,3)
a,b,c=tu
tu=(1,2,3,4,5,6,7,8,9)
a,b,*rest = tu
a,b,*_ = tu
可变类型
List
a=[1,2,3]
b=a.copy()
print(b)
a.extend([3,4])
print(a)
a.append(3)
print(a)
print(a.count(2))
print(a.index(2))
a.sort()
print(a)
a.insert(2, 4)
print(a)
a.pop(2)
print(a)
a.remove(1)
print(a)
a.reverse()
print(a)
a.clear()
print(a)
Set
a={0,1,2,3}
b={2,3,4}
a.add(5)
print(a)
print(a.pop())
print(a)
a.remove(5)
print(a)
c = a.difference(b)
print(c)
a.difference_update(b)
print(a)
c = a.intersection(b)
print(c)
a.intersection_update(b)
print(a)
print(a.isdisjoint(b))
print(a.issubset(b))
print(a.issuperset(b))
c=a.symmetric_difference(b)
print(c)
a.symmetric_difference_update(b)
print(a)
c=a.union(b)
print(c)
a.update(b)
print(a)
Dictionary
a = {'a': 1, 'b': 2, 'c': 3}
b = {'x': 1, 'y': 2, 'c': 4}
a.update(b)
print(a)
a.pop('x')
print(a)
print(a.keys())
print(a.values())
for k, v in a.items():
print(k, v)
'''
a 1
b 2
c 4
y 2
'''
print(a.get('b'))
k, v = a.popitem()
print(k, v)
print(a)
a.setdefault('k', 0)
print(a['k'])
print(a)
a.setdefault('k', 10)
print(a['k'])
print(a)