零基础学python保姆级教程——倒序

倒序

        正序排序,永久性排序:按字母先后顺序进行排序,a,b,c,d...数字的话从小到大啦!!!

正序

class_name = ["python", "bird", "c", "app"]
class_name.sort()
print(class_name)

class_num = ["2", "6", "1", "3"]
class_num.sort()
print(class_num)
复制代码

正序是啥样倒叙就得反过来,所以看懂了正序,倒序只不过是一个不同的方法而已!

倒序

class_name = ["python", "bird", "c", "app"]
print(sorted(class_name))
# 再次打印验证是否是临时修改
print(class_name)
复制代码

reverse()方法

class_name = ["python", "bird", "c", "app"]
# 这里我们倒叙打印放在print里面直接打印的话就会是空值
print(class_name.reverse())
# 需要用变量直接使用函数,再通过打印才行哦
class_name.reverse()
print(class_name)
复制代码

        如果用到这个方法,你就得这么写了,不然打印出来就会是空值。

取值

****取值是根据下标来取值的,从列表的第一个字符串开始,第一个字符串下标是0:

list_number = ['cat','dog','snake','elephant']
print(list_number[0])
print(list_number[1])
print(list_number[2])
#其对应的值是:
#cat
#dog
#snake
复制代码

负数取值

list_number = ['cat','dog','snake','elephant']
print(list_number[-1])
print(list_number[-2])
print(list_number[-3])
#其对应的值是:
#elephant
#snake
#dog
复制代码

范围取值

list_number = ['cat','dog','snake','elephant']
print(list_number[:3])
print(list_number[1:3])
print(list_number[1:])
print(list_number[:-1])
# 其对应的结果是:
#['cat', 'dog', 'snake']
#['dog', 'snake']
#['dog', 'snake', 'elephant']
#['cat', 'dog', 'snake']
复制代码

列表长度

list_number = ['cat','dog','snake','elephant']
print(len(list_number))
#其结果是:
#4
复制代码

下标改值

list_number = ['cat','dog','snake','elephant']
list_number[1] = 'nono'
print(list_number)
#    其结果是:
#['cat', 'nono', 'snake', 'elephant']
复制代码

连接与复制

list_number = ['cat','dog','snake','elephant']
list_1 = ['tom','tim']
print(list_number+list_1)
#    其结果是:
#['cat', 'dog', 'snake', 'elephant', 'tom', 'tim']
#复制
list_1 = ['tom','tim']
print(list_1*3)
#    其结果是:
#['tom', 'tim', 'tom', 'tim', 'tom', 'tim']
复制代码

循环列表

list_number = ['cat','dog','snake','elephant']
for i in list_number:
    print(i)
#    其结果是:
# cat
# dog
# snake
# elephant
复制代码

​​

in和not in操作

     用于判断列表中是否存在一个值,也可以用户判断其他的,此项还是非常的有用的,拿着小本本记好了。

list_number = ['cat','dog','snake','elephant']
print('cat' in list_number)    #T
print('cat' not in list_number)    #F
复制代码

便捷的赋值

便捷的赋值语句 普通的赋值语句
a+=1 a = a+1
a-=1 a = a-1
a*=1 a = a*1
a%=1 a = a%1
a/=1 a = a/1

python超全资料库安装包学习路线项目源码免费分享

上一篇:Rust中struct的function与method


下一篇:Spring_学习06