python基础笔记

==和is

1
2
3
4
5
6
7
# == 和 !=比较的是赋值是否相等
# is 和 not比较的是id是否相等,是否是同一个对象
a = [1, 2, 3]
b = [1, 2, 3]
print(a == b)
print(a is b)
print(id(a), id(b))

range 序列

1
2
3
4
5
6
7
8
9
10
# range: 起始位置、结束位置、步长
print(range(3, 10))
print(list(range(3, 10, 2)))
r = range(10)
print(r)
print(list(r))

# 通过range进行for 循环
for i in range(100):
print(i)

while 语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# while语句
i = 0
total = 0
while i < 100:
i += 1
total += i

print(total)

# break
i = 0
while i < 100:
i += 1
if i > 50:
break
print(i)

# continue
i = 0
while i < 100:
i += 1
if i % 10:
continue
print(i)

三元运算

1
2
3
4
a = 1
b = 2
max = a if (a > b) else b
print(m)