list list
- Python has four basic data types: list, set, tuple, and dict. Think of them as memory storage solutions. You can first understand the basic additions, deletions, differences, and changes .
- List is the most basic data structure in Python. The first index is 0, the second index is 1, and so on, the last index is -1.
- The data items in the list do not need to have the same type, and the data supports duplication.
- To create a list, simply enclose the different data items separated by commas in square brackets.
Add, delete, check, modify operation
l = [100, 'A', True, 3.14, "A"]
print(l, len(l), type(l))
# 追加列表
l.append('python')
# 插入列表
l.insert(0, 'java')
print(l) # java、100、A、True、3.14、A、python
# 移除列表数据,默认是最后一个元素,并且返回该元素的值
print(l.pop(-1))
# 该方法没有返回值但是会移除列表中的某个值的第一个匹配项
l.remove("A")
# 删除指定下标的对象
del l[-1]
# 更新指定下标的值
l[2] = False
# 遍历列表数据
for temp in l:
# java 100 False 3.14
print(temp, end=' ')
- Interception and splicing operations
l = [1,2,3,4,5]
# 切片可以对所有序列(字符串,list,tuple,set)进行操作
print(l[:2],l[2:],l[:])
# 列表拼接操作
l +=[6,7,8,9,10]
# append追加元素
l.append([20,30])
Common function operations
l = [1,1,2,3,4,5,6]
# 统计某个元素在列表中出现的次数
print(l.count(1))
# 从列表中找出某个值第一个匹配项的索引位置
print(l.index(3))
# 反向列表中元素
l.reverse()
print(l)
tuple data type
- Python tuples are similar to lists, except that the elements of tuples cannot be modified.
- Use parentheses for tuples and square brackets for lists.
- Tuple creation is very simple, just add elements in parentheses and separate them with commas.
Read-only tuple operations
tup1 = ('Google', 'Runoob', 1997, 2000);
tup1 = (); # 创建一个空元组
del tup; # 不能删除元组中某个元素,但是能删除整个元素
Tuple operations
l = (1, 2, 3)
print(l,type(l),id(l))
print(l[:2])
l2 = l + (4, 5, 6)
print(l2,type(l2),id(l2))
l3 = l * 3
print(l3,type(l3),id(l3))
set collection
- A set is an unordered sequence of non-repeating elements.
- Braces {} or set() function to create a collection, note: to create an empty collection you must use set() instead of {}, because {} is used to create an empty dictionary
l = {} # 空大括号代表字典,而非集合
print(type(l)) # dict
l = set()
print(type(l)) # set
l.add('java') # 没有下标不存在append
l.add('NET')
l.add('PYTHON')
l.add('NET') # 如果元素已存在,则不进行任何操作
l.add('python')
for temp in l:
print(temp, end=' ')
# 随机移除一个元素
print('\n pop移除元素为:', l.pop())
# 移除指定的元素,如果元素不存在,则会发生错误
l.remove('java')
# 移除集合中的元素,且如果元素不存在,不会发生错误
l.discard('oracle')
for temp in l:
print(temp, end=' ')
- Introduction to dict dictionary
Dictionary is another variable container model, and can store any type of object. Each key value (key=>value) pair of the dictionary is separated by a colon (:), and each pair is separated by a comma (,). The entire dictionary is enclosed in curly braces ({})
- The key must be unique, but the value need not.
- The value can take any data type, but the key must be immutable, such as a string, number, or tuple.
dict = {'Name': '小强', 'Age': 7, 'Name': '小菜鸟'}
# 采用中括号,如果key并未找到则会抛出异常,采用get则会显示缺省值
print ("dict['Name']: ", dict.get('name','旺财'))
# 以列表返回一个字典所有的键
for key in d.keys():
print(key , end=' ')
print()
# 列表返回一个字典所有的值
for val in d.values():
print(val , end = ' ')
dict = {'Name': '小强', 'Age': 7}
dict['Age'] = 8; # 更新 Age
dict['School'] = "Python人工智能" # 添加信息
print ("dict['Age']: ", dict['Age'])
print ("dict['School']: ", dict['School'])
del dict['Name'] # 删除键 'Name'
dict.clear() # 清空字典
del dict # 删除字典,功能通常
print(dict) # name 'dict' is not defined
Type feature summary