Files
2025PY/day13/02-列表的常用方法.py
2025-05-22 16:50:44 +08:00

43 lines
1.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 一.什么是方法
# 在python里面一切皆对象列表(list)也是一个对象
# 对象一般由属性 + 方法 组成
# 属性是用来描述对象的外观,特点,可以通过点符号来访问(python提供的很少)
# 方法是用来描述对象的功能,方法和函数一样,后面有一个放参数的括号,方法也是函数实现的
# 访问方法也是通过点符号实现 例如list.index() list是对象index就是方法
# 二.列表中的常用方法
# 1、list.index(元素) 查找指定元素在列表下的索引 如果找不到那么就报错
list=['zhangsan','lisi','wangwu']
print(list.index('lisi'))
# print(list.index('jack')) #报错
# 2、list.append(x) 将x作为一个元素添加到列表list里面(x可以是列表或者元素)
list=['a','b']
list.append('c')
list.append('d')
list.append(['e','f','g'])
print(list)
# 案例 创建一个列表 里面是0开始的偶数 一直到100
list=[] #创建一个空列表
for num in range(0,101): # 循环1-100
if num % 2==0: # 选择1-100的偶数
list.append(num) # 将偶数追加到列表中
print(list)