欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【python】练习:分支循环操作

程序员文章站 2021-12-17 21:58:42
...
>>>if 0:
      print("Hello")

>>>if 1:
      print("Hello")
Hello
>>>if "":
      print("Hello")

>>>if []:
      print("Hello")

>>>if {}:
      print("Hello")

>>>if "Not Empty":
      print("Hello")
Hello
>>>if [10, "101"]:
      print("Hello")
Hello
>>>if True:
      print("Hello")
Hello
>>>if False:
      print("Hello")

>>>if 0 == 1:
      print("Hello")

>>>a = 10
>>>if a > 10 or a < 10:
   print("a is not 10")
else:
   print("a is 10")

a is 10
>>>if a >10:
    print("a > 10")
elif a < 10:
    print("a < 10")
else: 
    print("a = 10")

a = 10
>>>range(10)
range(0, 10)
>>>list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>for i in range(10):
       print(i, end="")
0123456789
>>>for i in [0, 1, 2, 3, 4]:
       print(i, end="")
01234
>>>for i in range(1, 10, 2):
       print(i, end="")
13579
>>>list(range(1, 10, 2))
[1, 3, 5, 7, 9]
>>>for c in "Python语言":
       print(c)
P
y
t
h
o
n
语
言
>>>for c in "Python语言":
       print(c, end=",")
P,y,t,h,o,n,,,
>>>while a > 5:
      print(a)
      a = a - 1
10
9
8
7
6

>>>while False:
       print("Hello")

>>>while True:
	print("Hello")
	break
Hello
>>>a
5
>>>while a > 0:
     a = a - 1
     if a % 2 == 0:
        continue
     print(a)

3
1
>>>try:
       a = 10/0
except:
       print("error")

error
>>>try:
       a = open("no-exist")
except:
       print("error")

error
>>>try:
    if x:
        print("Hello")
except:
       print("error")

error
>>>try:
    anyerror
except:
       print("error")

error
相关标签: python