Python操作
1. 字符串
a = 'hello world'
b = '黄桃罐头'
c = '橘子罐头'
d = 'python'
e = '123'
'李' + '小龙'
'李小龙'
'Python真强大' * 3
'Python真强大Python真强大Python真强大'
len('110101199003074477')
18
len('13704372727')
11
len('李小龙')
3
'测试' in '新产品上线测试号'
True
'测试' in '我是一个正常用户'
False
'测试' not in '新产品上线测试号'
False
'测试' not in '我是一个正常用户'
True
'ABC'.find('C')
2
'Abc'.find('d')
-1
a = 'python数据分析'
a[0]
'p'
a[3]
'h'
a[1:3]
'yt'
a[:3]
'pyt'
a[6:]
'数据分析'
a[-1]
'析'
'a,b,c'.split(',')
['a', 'b', 'c']
'a|b|c'.split('|')
['a', 'b', 'c']
' a '.strip()
'a'
'\ta\t '.strip()
'a'
'AaA'.strip('A')
'a'
2. 列 表
null_list = []
int_lst = [1, 2, 3]
str_lst = ['a', 'b', 'c']
int_str_lst = [1, 2, 'a', 'b']
int_lst = [1, 2, 3]
int_lst * 3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
str_lst * 2
['a', 'b', 'c', 'a', 'b', 'c']
int_lst + str_lst
[1, 2, 3, 'a', 'b', 'c']
str_lst + int_lst
['a', 'b', 'c', 1, 2, 3]
int_lst = [1, 2, 3]
str_lst = ['a', 'b', 'c']
int_lst.extend(str_lst)
int_lst
[1, 2, 3, 'a', 'b', 'c']
str_lst.extend(int_lst)
str_lst
['a', 'b', 'c', 1, 2, 3, 'a', 'b', 'c']
int_lst = [1, 2, 3]
int_lst.append(4)
int_lst
[1, 2, 3, 4]
str_lst = ['a', 'b', 'c']
str_lst.append('d')
str_lst
['a', 'b', 'c', 'd']
int_lst = [1, 2, 3]
int_lst.insert(3, 4)
int_lst
[1, 2, 3, 4]
int_lst = [1, 2, 3]
int_lst.insert(2, 4)
int_lst
[1, 2, 4, 3]
score_list = ['一班', '一班', '三班', '二班', '一班']
score_list.count('一班')
3
sale_lst = ['倪凌霄', '乔星津', '曹觅风', '杨新竹', '王源玲']
sale_lst.index('杨新竹')
3
v = ['a', 'b', 'c', 'd', 'e']
v[0]
'a'
v[3]
'd'
i = ['a', 'b', 'c', 'd', 'e']
i[1:3]
['b', 'c']
i[:3]
['a', 'b', 'c']
str_lst = ['a', 'b', 'c', 'd']
str_lst.pop()
str_lst
['a', 'b', 'c']
str_lst = ['a', 'b', 'c', 'd']
str_lst.pop(2)
str_lst
['a', 'b', 'd']
str_lst = ['a', 'b', 'c', 'd']
str_lst.remove('b')
str_lst
['a', 'c', 'd']
s = [1, 3, 2, 4]
s.sort()
s
[1, 2, 3, 4]
3. 字 典
test_dict ={}
test_dict['张三'] = 13331351888
test_dict['李四'] = 15517896750
test_dict
{'张三': 13331351888, '李四': 15517896750}
test_dict = {'张三': 13331351888, '李四': 15517896750}
test_dict
{'张三': 13331351888, '李四': 15517896750}
contact = (['张三', 13331351888], ['李四', 15517896750])
test_dict = dict(contact)
test_dict
{'张三': 13331351888, '李四': 15517896750}
test_dict.keys()
dict_keys(['张三', '李四'])
test_dict.values()
dict_values([13331351888, 15517896750])
test_dict.items()
dict_items([('张三', 13331351888), ('李四', 15517896750)])
for item in test_dict.items():
print(item)
('张三', 13331351888)
('李四', 15517896750)
4. 元 组
tup1 = (1, 2, 3)
tup1
(1, 2, 3)
tup = ('a', 'b', 'c')
tup
('a', 'b', 'c')
len(tup1)
3
tup = (1, 2, 3, 4, 5)
tup[2]
3
tup[4]
5
tup = (1, 2, 3, 4, 5)
tup[1:3]
(2, 3)
tup[:4]
(1, 2, 3, 4)
tup[2:]
(3, 4, 5)
tup = (1, 2, 3)
list(tup)
[1, 2, 3]
t_lst = [1, 2, 3]
tuple(t_lst)
(1, 2, 3)
5. zip()函 数
"""
zip()函数用于将可迭代的对象(列表、元组)作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。
常与for循环一起搭配使用
"""
list_a = [1, 2, 3, 4]
list_b = ['a', 'b', 'c', 'd']
for i in zip(list_a, list_b):
print(i)
(1, 'a')
(2, 'b')
(3, 'c')
(4, 'd')
6. 运 算 符
10 + 20
30
10 - 20
-10
10 * 20
200
10 / 20
0.5
10 % 20
10
1 % 2
1
10 ** 20
100000000000000000000
10 // 20
0
3 // 2
1
13 % 5
3
10 == 20
False
10 != 20
True
10 > 20
False
10 < 20
True
(10 > 20) and (10 < 20)
False
(10 > 20) or (10 < 20)
True
not (10 > 20)
True
not (10 < 20)
False
|