python基础笔记

发布: 2019-10-20 23:31:48 标签: python

==和is

01# == 和 !=比较的是赋值是否相等
02# isnot比较的是id是否相等,是否是同一个对象
03a = [1, 2, 3]
04b = [1, 2, 3]
05print(a == b)
06print(a is b)
07print(id(a), id(b))
08
复制代码

range 序列

01# range: 起始位置、结束位置、步长
02print(range(3, 10))
03print(list(range(3, 10, 2)))
04r = range(10)
05print(r)
06print(list(r))
07
08# 通过range进行for 循环
09for i in range(100):
10 print(i)
11
复制代码

while 语句

01# while语句
02i = 0
03total = 0
04while i < 100:
05 i += 1
06 total += i
07
08print(total)
09
10# break
11i = 0
12while i < 100:
13 i += 1
14 if i > 50:
15 break
16print(i)
17
18# continue
19i = 0
20while i < 100:
21 i += 1
22 if i % 10:
23 continue
24 print(i)
25
复制代码

三元运算

01a = 1
02b = 2
03max = a if (a > b) else b
04print(m)
复制代码