py笔记

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

######

print(0b100) # 二进制整数
print(0o100) # 八进制整数
print(100) # 十进制整数
print(0x100) # 十六进制整数

######

print(type(100)) # <class 'int'>
print(type(123.45)) # <class 'float'>
print(type('hello, world')) # <class 'str'>
print(type(True)) # <class 'bool'>

######

print(int('100', base=16)) # str->十六进制int
print(bool(1)) # str->bool,True
print(chr(65)) # int->str,输出'd'
print(ord('A')) # str->int,100

######

print(11//3)
print(2 ** 4)

######

a = None
print(a := 10)
print(1 <= a <= 2)

b = 11

print('%d 你好 %d' % (a, b))
print(f'{a:d} 你好 {b:.1f}')

######

if 1 <= a <= 100:
print('你好')
elif a and (not b):
print('你好1')
else:
if b:
print('wow')
else:
print('now')

match a:
case 1 | 3: print('1')
case 2: print('2')

######

'''

import time

# 0 ~ 99

for _ in range (100): # empty, only repeat
time.sleep(1) # 1s

for i in range (0, 100, 2): # even number
time.sleep(0.01)

print(sum(range(1, 3, 0.1)))

'''

######

while a <= 15:
a += 1
if a > 20:
break
elif a > 25:
continue

######

import random

a = random.randrange(1, 101)

######

items1 = [1, 1.1, '1', True]
items2 = list(range(1, 11))
items3 = list('hello') # h e l l o
print(items1 + items2 + items3 * 2)
print(2 not in items1) # False
print(items1[0], items1[-1]) # forward subscript 0 ~ 4 is equal to reversed subscript -5 ~ -1
print(items1[0:5:2]) # range [0,5), span is 2, namely 0, 2, 4, they can also be negative
print(items1[:], items1[::]) # acquiescently 0, ending, 1
# such options can also be used to modify lists

# lists can be compared, like ==, !=, <= ..., the rule is comparing each elements one by one(parallel[adv])


# type 1 to traverse each one:
for i in range(len(items1)):
print(items1[i], end = ' ')
# type 2:
for item in items1:
print(item, end = " ")


######

发布于

2025-01-01

更新于

2026-02-05

许可协议

评论