人工智能学习日记day5

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放入mult
squares = [n**2 for n in range(10)]
squares
loud_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()
>>>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值