Python基础知识

数据类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 除求整数
print(2//2)
# 除小数
print(2/2)
# python 表示二进制 在数字后面加 0b
# 0b10 表示二进制的10
print(0b10)
# python 表示八进制 在数字后面加 0o
# 0o10 表示八进制的10
print(0o10)
# python 表示十六进制 在数字后面加 0x
# 0x10 表示十六进制的10
print(0x10)
print(0x1F)
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
#=======进制转换=======
#将其他进制数转换为2进制
print(bin(10))
print(bin(0o7))
print(bin(0xE))
# 将其他进制数转换为8进制
print(oct(888))
print(oct(0x7777))
print(oct(0b111))
# 将其他进制数转换为10进制
print(int(0b111))
print(int(0o77))
print(int(0x11E))
# 将其他进制数转换为16进制
print(hex(888))
print(hex(0o7777))
print(hex(0b111))
#bool:类型
print(type(True))
print(type(False))
# bool True False
print(bool('abc'))
print(bool(''))
print(bool([]))
print(bool([1,2,3]))
# 表示复数 complex
print(36j)
# 转义字符
print('let\'s go')
# 换行
print("""hello\nhello""")
# 字符串拼接
print("hello"+"world")
# 字符串计算
print("hello" * 3)
# 字符串索引(负数为反向查找)
print("hello"[0])
print("hello"[3])
print("hello"[-1])
# 字符串截取
print("hello world"[0:5])
print("hello world"[0:-1])
print("hello world"[6:11])
# 原始字符串
print(r'C:\Windows')
print(R'C:\Windows')

数组元组

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
50
51
52
53
54
# 列表类型 list
print(type([1,2,3,4,5,6]))
print(type(["AA","BB",2,3,True,False]))
# 二维数组
print(type([[1,2],[3,4]]))
# 数组操作
# 使用[]来进行操作,取出的会是字符串,使用[:]进行操作,取出的会是数组
print(["A","B","C"][0])
print(["A","B","C"][2])
print(["A","B","C"][0:2])
print(["A","B","C"][-1:])
print(["A","B"] * 3)
# 元组
print(type((1,2,3)))
print(type((1,2,False)))
print((1,2,False)[2])
# int str list
# in
print(3 in [1,2,3,4,5])
print(6 in [1,2,3,4,5])
print(6 not in [1,2,3,4,5])
# 长度操作 len
print(len("HELLO !"))
# 最大最小值 max min
print(max([1,2,3,4,5]))
print(min([1,2,3,4,5]))
print(min("hello"))
# ASICII码 ord
print(ord('w'))
print(ord(" "))
# 集合 set 特点:无序,不重复
print({1,2,3,4,4,5})
print({1,2,3,4,5})
print({6,2,2,3,4,5})
# 集合不可通过以下方式操作,因为集合无序
# print({6,2,2,3,4,5}[0])
print(len({1,2,3}))
print(1 in {1,2,3})
print(1 not in {1,2,3})
# 集合的操作
# 求两个集合的差集
print({2,3,4,5} - {5,6})
# 求交集
print({2,3,4,5,6} & {5,6,7,8})
# 求并集
print({2,3,4,5,6} | {5,6,7,8})
# type
print(type({}))
print(type(set()))
# dict 字典类型 Key Value 不可以有重复key,重复会被覆盖
# {key1:value1, key2:value2}
print(type({"AA":"aa", "BB":"bb"}))
# 寻找dict中key
print({"A":"张","B":"王","C":"李","D":"赵"}["A"])

变量

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
print([1,2,3,4,5] * 3 + [1,2,3] + [1,2,3,4,5])
# 变量
A = [1,2,3,4,5]
B = [1,2,3]
print(A * 3 + B + A)
# 变量命名规则: 数字,字母,下划线,区分大小写
# 保留关键字: and in if import
a_b_c = 1
d433a = '你好'
a_b_c = {1,2,3}
# 变量名会覆盖方法名
# type = 1
# print(type)

# int str tuple (不可变)值类型 list set dict(可变) 引用类型
a = [1,2,3]
b = a
a[0] = '1'
print(b)
# 字符串
a1 = 'Hello'
a1 = a1 + 'World'
print(a1)
# id 显示内存地址
print(hex(id(a1)))
a1 = a1 + 'AAS'
print(a1)
print(hex(id(a1)))
b1 = "aaa"
print(id(b1))
b1 = b1 + "bbb"
print(id(b1))
# 添加元素[] , ()元组不可变
A3 = [1,2,3]
A3.append(4)
print(A3)
# 查找元素
B1 = (1,2,3,[1,4,5,['b','n','m']])
print(B1[3][3][1])
# 虽然元组不可变,但是元组中的列表可变
B1[3][3][1] = 'K'
print(B1[3][3][1])
# 运算符 //整除 %取余 **乘方
# 比较 and or not
print('abc' == 'abc')
print(True and False)
print(True or False)
print(not True)
# 成员运算符 in not in
G1 = [1,3,4,5]
print(2 in G1)
print(2 not in G1)
print('2' in G1)
# 只取key
G2 = 1
print(G2 in {'d':1})
G2 = 'd'
print(G2 in {'d':1})
# 身份运算:
F1 = 1
F2 = 1.0
print(F1 == F2) # 比较数值相等
print(F1 is F2) # 比较两个变量的身份是否相等
D1 = 2
D2 = 2
print(id(D1))
print(id(D2))
# 判断变量类型
D3 = 10
print(type(D3) == int)
print(isinstance(D3,int))
print(isinstance(D3,float))
print(isinstance(D3,(int,float)))

分支判断

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
# 这是单行注释
'''
这是
多行注释
'''
e = 1
if e == 1:
print(1)
else:
print(2)

# 简单判断测试
account = 'AAA'
pwd = '111'
user_account = input()
user_pwd = input()
# 逻辑运算符 优先级最低
if account == user_account and pwd == user_pwd:
print('YES')
else:
print('NO')
# constant 常量
# Python 中不存在常量,但是规定常量的写法为:变量名大写
# ACCOUNT = 'AAA'
if True:
pass # 空语句 占位语句
# elif -> else if
if True:
pass
elif pwd == 1:
pass
else:
pass
# input -> type 'str'
ty = input()
print(type(ty))

A1 = 1
A2 = 8
print(A1 or A2)

循环

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
# 循环
# while
counter = 1
while counter <= 10:
counter += 1
print(counter)
else:
print('END')
# for 遍历,循环,序列或集合字典
lista = [1,2,3,4,5]
for list_a in lista: # 遍历
print(list_a)
listb = [[1,2,3,4,5],('a','b','b')]
for x in listb:
for y in x:
print(y)
# break 中断循环
else:
print('aaa')
# 重复指定次数代码循环
for x in range(0,10): # for(int i = 0; i < 10; i++)
print(x)
for x in range(0,10,2): # 2 :间隔
print(x,end=' | ')
for x in range(10,0,-2): # 递减
print(x,end=' | ')
#
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×