Python中有while循环和for循环
下面以一个小例子来说明一下用法,用户输入一些数字,输出这些数字中的最大值和最小值
array = [5,4,3,1] for i in array: print(i) largest = None smallest = None while True: num = input("Enter a number: ") if num == "done" : break if len(num) < 1 : break # check for empty line try: num = int(num) except: print("Invalid input") continue if largest is None: largest = num if smallest is None: smallest = num if num > largest: largest = num if num < smallest: smallest = num print("Maximum is " + str(largest)) print("Minimum is " + str(smallest))
需要说明一下的是:Python中None和==的区别,前者是值和类型都相等,就是完全一样的东西才会返回True(注意大小写),后者==是只进行值的比较
None是一种特殊类型,如下图所示