列表,元組,集合,字典
列表——[] 可讀可寫,可以對列裏元素修改
元組——() 只讀,不可以對列裏元素修改
1.列表
in:
#列表
students = ["jack","robin","lina",3]
print (students[-2])#這兒索引爲負數-2,表示倒數第二個元素
out:
lina
in:
students[3] = "python"
print (students[3])
print (students)
out:
python
['jack', 'robin', 'lina', 'python']
in:
len(students)#求列表長度
out:
4
in:
students[1:3]#按區間(切片)查找元素
out:
['robin', 'lina']
in:
students = ["jack1","jack2","robin1","robin2","lina1","lina2",3]
students[1:5:2]#按區間(切片)查找元素,並設定步長爲2
out:
['jack2', 'robin2']
in:
students.append('helloworld')#添加元素
students
out:
['jack1', 'jack2', 'robin1', 'robin2', 'lina1', 'lina2', 3, 'helloworld']
in:
students.pop(7)#刪除元素——按索引
students
out:
['jack1', 'jack2', 'robin1', 'robin2', 'lina1', 'lina2', 3]
in:
students.insert(1,'helloworld2')#插入元素——按索引
students
out:
['jack1', 'helloworld2', 'jack2', 'robin1', 'robin2', 'lina1', 'lina2', 3]
in:
scores = [10,20,30]
students.insert(1,scores)
students
out:
['jack1',
[10, 20, 30],
[10, 20, 30],
[10, 20, 30],
[10, 20, 30],
[10, 20, 30],
[10, 20, 30],
[10, 20, 30],
'helloworld2',
'jack2',
'robin1',
'robin2',
'lina1',
'lina2',
3]
in:
l=students
l
2.元組
in:
students = ("jack1","jack2","robin1","robin2","lina1","lina2",3)
print(students[2])
out:
robin1
in:
students.append(score)#添加元素
students
in:
teachers = ("jack1","jack2","robin1","robin2","lina1","lina2",scores)
teachers
out:
('jack1', 'jack2', 'robin1', 'robin2', 'lina1', 'lina2', [10, 20, 30])
3.集合
主要有2個功能: 1.進行集合操作 2.消除重複元素 集合的格式是:set(元素)
in:
a = set("abcnmaaaaggsng")
print('a=',a)
b = set("cdfm")
print('b=',b)
#交集
x = a & b
print('x=',x)
#並集
y = a|b
print('y=',y)
#差集
z = a - b
print('z=',z)
#去除重複元素
new = set(a)
print('z=',z)
out:
a= {'g', 'b', 'm', 's', 'a', 'c', 'n'}
b= {'d', 'm', 'f', 'c'}
x= {'m', 'c'}
y= {'g', 'b', 'm', 'd', 's', 'f', 'a', 'c', 'n'}
z= {'g', 'b', 'n', 's', 'a'}
z= {'g', 'b', 'n', 's', 'a'}
in:
strs = 'abc'
strs[1]
out:
'b'
in:
L = {'a','b','c','c'}
L
out:
{'a', 'b', 'c'}
in:
set_L = set(L)
list_L = list(set_L)
list_L
out:
['b', 'a', 'c']
in:
tuple_L = tuple(set_L)
tuple_L
out:
('b', 'a', 'c')
in:
list(tuple_L)
out:
['b', 'a', 'c']
4.字典
python中字典也叫關聯數組,用{}括起來。每個元素是一個k-v對
in:
#字典
zidian = {"name":"robin","home":"yunnan"}
print(zidian["home"])
#添加字典裏的項目
zidian["like"] = "music"
print(zidian["name"])
print (zidian["like"])
out:
yunnan
robin
music
in:
zidian
out:
{'home': 'yunnan', 'like': 'music', 'name': 'robin'}
5.常用python關鍵字
in:
#常用python關鍵字
#查看一下有哪些關鍵字
import keyword
print(keyword.kwlist)
out:
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
6.python運算符與表達式
算術運算符
in:
#"*":兩個數相乘或字符串重複
a = 4 * 7
print (a)
b = "hello "*3
print (b)
out:
28
hello hello hello
in:
#"**":求冪運算
a2 = 2 ** 3 #相當於2的三次方(冪),就是2*2*2
print (a2)
out:
8
in:
#"//":除法運算,返回其商的整數部分,舍掉餘數
a3 = 11 // 3
print (a3)
a4 = 11.0 // 3
print (a4)
out:
3
3.0
in:
#"%":除法運算,返回其商的餘數部分,舍掉商
a5 = 11 % 3
print (a5)
out:
2
range
in:
range(0,10,2)
alist = range(10)
[x*x for x in alist]
out:
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
in:
[x*x for x in alist if x%3]
out:
[1, 4, 16, 25, 49, 64]