python语句-if、while

if语句

用if语言比较是否相等/不相等:

a = 1
if a == 1 :
	print("equal")
else :
	print("not equal")

b = "test"
if b != "hello" :
	print("not equal")
else :
	print("equal")

输出:
equal
not equal

if语句多个条件的与/或判断:

a = 90
if a >= 80 and a <= 100 :
	print("A")


b = 60
c = 85
if b >= 80 or c >= 60 :
	print("have a A")

输出:
A
have a A

多重判断if-elif-else:

a = 90
if a >= 80 and a <= 100 :
	print("A")
elif a >=60 and a <= 80 :
	print("B")
else :
	print("C")

输出:
A

if语句在列表中的应用:

numbers = [40,50,50]
if 40 in numbers:
	print("Yes")

if 50 not in numbers:
	print("No")

输出:
Yes

while语句

for循环语句主要用于遍历集合中的元素,使用while语句能够进行自定义的循环:

i = 0
while i < 10 :
	print(i, end=" ")
	i += 1

输出:
0 1 2 3 4 5 6 7 8 9

使用break退出循环:

i = 0
while i < 10:
	if (i == 5):
		break;
	print(i, end=" ")
	i += 1

输出:
0 1 2 3 4

使用continue跳过当前循环:

i = 0
while i < 10:
	if (i == 5):
		i += 1
		continue;
	print(i, end=" ")
	i += 1

输出:
0 1 2 3 4 6 7 8 9

你可能感兴趣的:(python)