python的类型和运算

来源:互联网 发布:vb人事管理系统 编辑:程序博客网 时间:2024/05/20 01:38

数字类型与运算

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
L = [1,2]
L.append(L)
print(L) # [1, 2, [...]]
a = 123 * 321
print(a) #39483
a = len(str(2 ** 10000)) #输出长度
print(a) #3011
import math
print(math.pi) #3.141592653589793
print(math.sqrt(400)) #20.0
import random
print(random.random()) #0.3304375451093339
print(random.choice([1,2,3,4])) #1
print(10/0) #ZeroDivisionError: division by zero

字符串

数组序列

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
s = "limingyang"
#数组
print(s[0])
print(s[1])
print(s[-1]) #等于s[len(s)-1]
print(len(s))
# print(dir(s))
#序列
print(s[-2])
print(s[1:])
print(s[:1])
print(s[0:5])
print(s[:-1])
print(s[:])
#l
#i
#g
# 10
# n
# imingyang
# l
# limin
# limingyan
# limingyang

拼接

1
2
3
4
5
str = "limingyang"
print(str) #limingyang
print(str + "hello") #limingyanghello
print(str * 2) #limingyanglimingyang
print("H"+str[1:]) #Himingyang

常用操作

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
str = "limingyang "
print(str.find("min"))
print(str.endswith("g"))
print(str.index("y"))
print(str.replace("i","YY"))
print(str.split("i"))
print(str.rstrip()) #去字母后空格
print(str.upper())
print(str.isalpha())
# 2
# False
# 6
# lYYmYYngyang
# ['l', 'm', 'ngyang ']
# limingyang
# LIMINGYANG
# False
print("%s,2,and %s " % ("1","3")) #1,2,and 3
print("{0},2,and {1}".format("1","3")) #1,2,and 3
help(str.format()) #帮助
# Use help() to get the interactive help utility.
# Use help(str) for help on the str class.

模式匹配

1
2
3
4
5
6
7
8
9
import re
match = re.match("hello[ \t]*(.*)world","hello python world")
print(match.groups()) # ('python ',)
print(match.group(1)) # python
# 解释: 字符串以hello开头 跟着0个或者多个制表符 world结尾 如果找到 将匹配部分保存为组
match1 = re.match("/(.*)/(.*)/(.*)","/li/ming/yang")
print(match1.groups()) # ('li', 'ming', 'yang')
print(match1.group(1)) # li

列表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
L = ["a","b","c"]
print(len(L)) # 3
print(L[0]) # a
L.append("d") #追加
print(L) # ['a', 'b', 'c', 'd']
L.pop(2) # 删除 填入下标
print(L) #['a', 'b', 'd']
L.sort()
print(L) # 排序 ['a', 'b', 'd']
L.reverse() # 翻转 ['d', 'b', 'a']
print(L)
m = [[1,2,3],[4,5,6],[7,8,9]]
print(m) #[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(m[1][2]) #6

列表解析

1
2
3
4
5
6
7
8
9
10
11
m = [[1,2,3],[4,5,6],[7,8,9]]
print([row[1] + 1 for row in m]) # [3, 6, 9]
print([row[0] for row in m]) # [1, 4, 7]
print([m[i][i] for i in [0,1,2]]) # [1, 5, 9]
print([c * 2 for c in "lmy"]) # ['ll', 'mm', 'yy']
print([sum(row) for row in m]) # [6, 15, 24]
print(list(map(sum,m))) # [6, 15, 24]
print({i : sum(m[i]) for i in range(3)}) #{0: 6, 1: 15, 2: 24}
print({ord(i) for i in "lmmy"}) #{121, 108, 109} set去重 ord转化为unicode
print([ord(i) for i in "lmmy"]) #[108, 109, 109, 121] list ord 转化为unicode
print({i:ord(i) for i in "lmmy"}) #{'l': 108, 'm': 109, 'y': 121} map ord 转化为unicode

字典

1
2
3
4
5
6
7
8
9
10
11
12
13
D = {"l":"li","m":"ming","y":"yang"}
print(D["l"]) #li
D1 = {"l":{"a":"aa"},"m":{"b":"bb"},"y":{"c":"cc"}}
print(D1["l"]) #{'a': 'aa'}
k = list(D1.keys())
print(k) #['l', 'm', 'y']
for key in k:
print(key,"=>",D1[key]) # l => {'a': 'aa'} m => {'b': 'bb'} y => {'c': 'cc'}
for key in sorted(k): #最新的排序函数
print(key, "=>", D1[key]) # l => {'a': 'aa'} m => {'b': 'bb'} y => {'c': 'cc'}

遍历

循环

1
2
3
4
5
6
7
8
9
10
11
for c in "lmy":
print(c) # l m y
x = 4
while x > 0:
print("**"*x)
x-=1
# ********
# ******
# ****
# **

迭代和优化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
s = [x ** 2 for x in [1,2,3,4]]
print(s) #[1, 4, 9, 16]
s = []
for x in [1,2,3,4]:
s.append(x**2)
print(s) #[1, 4, 9, 16]
a = 1 > 2
print(a) #false
map = {"a":"aa","b":"bb"}
print("c" in map) #false
map["c"] = "cc"
if "c" not in map:
print("我不存在")
else:
print("存在")

元组

1
2
3
4
5
6
7
8
9
T = (1,2,3,4,5) # 元组 可添加任意类型 用()
print(T) # (1, 2, 3, 4, 5)
T = T + (6,7,8)
print(T) #(1, 2, 3, 4, 5, 6, 7, 8)
print(T.index(2)) # 1 第一次出现的位置
print(T.count(3)) # 1 出现的次数

文件

1
2
3
4
5
6
7
8
9
10
11
12
# 写入文件
f = open("1.txt","w")
f.write("hello\n") #6
f.write("world\n") #6
f.close()
#文件原来的内容丢失
#读取文件
f = open("1.txt")
text = f.read()
print(text)

其他

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
a = set("abc")
print(a) #{'a', 'b', 'c'}
import decimal
d = decimal.Decimal(3.1415)
print(d+1) # 4.141500000000000181188397619
decimal.getcontext().prec = 2 #保留两位
print(decimal.Decimal(1)/decimal.Decimal(3)) #0.33
print(bool("lmy")) #true
x = None
print(x) #None
print([x] * 100) #None, None, None, None, None, None, None, None,.....
print(type(1)) # <class 'int'>
原创粉丝点击