1、运算符
2、print()
print(1, 2, 3, sep=' < ') print(1,2,3,end="")
3、函数
max()
min()
abs() #绝对值
4、自定义函数
def least_difference(a, b, c):
diff1 = abs(a - b)
diff2 = abs(b - c)
diff3 = abs(a - c)
return min(diff1, diff2, diff3)
5、List
[ ];
[1,2,3];
len() #长度
planets.append('Pluto') #结尾加上 planets.index('Earth') #检索位置2,0 1 2
6、Tuples
()
有序,固定
定义后,无法删除,无法更改,顺序固定
7、Loops 循环
multiplicands = (2, 2, 2, 3, 3, 5) product = 1 for mult in multiplicands: product = product * mult product #把multiplicands放入multsquares = [n**2 for n in range(10)] squaresloud_short_planets = [planet.upper() + '!' for planet in planets if len(planet) < 6] loud_short_planets
8、String
# ALL CAPS claim = "Pluto is a planet!" claim.upper() #小写转大写# all lowercase claim.lower() #大写转小写claim.index('plan') #检索 11
9、import()
from math import * #直接用数学公式 从math.pi 到 pi
import numpy in np #重命名 numpy.array 到 np.array
sorted()排序/lambda匿名函数/ cmp()函数
Python 3.11.7 | packaged by Anaconda, Inc. | (main, Dec 15 2023, 18:05:47) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> L=[('b',3),('a',1),('c',4),('d',2)];
>>> a=sorted(L,key=lambda x:x[0]);
>>> a
[('a', 1), ('b', 3), ('c', 4), ('d', 2)]
>>> b=sorted(L,key=lambda x:x[1]);
>>> b
[('a', 1), ('d', 2), ('b', 3), ('c', 4)]
>>> c=sorted(L,cmp=lambda x,y:cmp(x[0],y[0]));
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'cmp' is an invalid keyword argument for sort()
>>> c=sorted(L,cmp=lambda x,y:cmp(x[1],y[1]));
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'cmp' is an invalid keyword argument for sort()
>>> M=[('a', 1), ('b', 2), ('c', 3), ('d', 4)];
>>> c = sorted(M,cmp = lambda x,y:cmp(x[1],y[1]));
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'cmp' is an invalid keyword argument for sort()
>>>

871

被折叠的 条评论
为什么被折叠?



