Python数据类型之列表-----003
一.基本数据类型
整数:int
字符串:str(注:\t等于一个tab键)
布尔值: bool
列表:list (元素的集合)
列表用[]
元祖:tuple
元祖用()
字典:dict
注:所有的数据类型都存在想对应的类列里
二.列表所有数据类型:
基本操作:
- 索引
- 切片
- 追加
- 删除
- 长度
- 切片
- 循环
- 包含
list
三.所有列表数据类型举例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
#!/usr/bin/env python # -*- coding:utf-8 -*- #append追加 name_list = [ "zhangyanlin" , "suoning" , "nick" ] name_list.append( 'zhang' ) print (name_list) #count制定字符出现几次 name_list = [ "zhangyanlin" , "suoning" , "nick" ] name_list.append( 'zhang' ) name_list.append( 'zhang' ) name_list.append( 'zhang' ) print (name_list.count( 'zhang' )) #extend可扩展,批量往里加数据 name_list = [ "zhangyanlin" , "suoning" , "nick" ] name = [ "aylin" , "zhang" , "yan" , "lin" ] name_list.extend(name) print (name_list) #index找到字符所在的位置 name_list = [ "zhangyanlin" , "suoning" , "nick" ] print (name_list.index( 'nick' )) #insert插入,往索引里面插入值 name_list = [ "zhangyanlin" , "suoning" , "nick" ] name_list.insert( 1 , "zhang" ) print (name_list) #pop在原列表中移除掉最后一个元素,并赋值给另一个变量 name_list = [ "zhangyanlin" , "suoning" , "nick" ] name = name_list.pop() print (name) #remove移除,只移除从左边找到的第一个 name_list = [ "zhangyanlin" , "suoning" , "nick" ] name_list.remove( 'nick' ) print (name_list) #reverse反转 name_list = [ "zhangyanlin" , "suoning" , "nick" ] name_list.reverse() print (name_list) #del删除其中元素,删除1到3之间的 name_list = [ "zhangyanlin" , "suoning" , "nick" ] del name_list[ 1 : 3 ] print (name_list) |
四.索引
1
2
|
name_list = [ "zhangyanlin" , "suoning" "aylin" "nick" ] print (name_list[ 0 ]) |
五.切片
1
2
|
name_list = [ "zhangyanlin" , "suoning" "aylin" "nick" ] print (name_list[ 0 : 2 ]) |
六.总长度len
1
2
|
name_list = [ "zhangyanlin" , "suoning" "aylin" "nick" ] print (name_list[ 1 : len (name_list)]) |
七.for循环
1
2
3
|
name_list = [ "zhangyanlin" , "suoning" "aylin" "nick" ] for i in name_list: print (i) |
学习记录,小白一枚