当前位置:Gxlcms > Python > Python流程控制代码详解

Python流程控制代码详解

时间:2021-07-01 10:21:17 帮助过:6人阅读

1.while语句

条件循环控制语句。一般需要和break一起使用,不然会进入死循环。

格式:【 while <条件>:

<内容>

break 】

  1. x=int(input('请输入一个数字:'))
  2. while x>0:
  3. print('正数')
  4. break

2.if语句

流程分支的条件控制,一般和elif和else使用。

  1. x=int(input('请输入一个数字:'))
  2. if x<0:
  3. print('负数')
  4. elif x==0:
  5. print('零')
  6. else :
  7. print('正数')

3.for语句

循环控制语句,可用来遍历某一对象,和in一起使用。

格式: 【 for <> in <对象集合>:】

  1. x=['a','b','c','d']for i in x :
  2. # i 位置的字符,只要不是关键字,可以随意用字符代表
  3. print(i)

4.range()函数

数字序列迭代器,当你迭代它时,它是一个能够像期望的序列返回连续项的对象,但为了节省空间,它并不真正构造列表。

格式: range(stop) 给出结束数值,开始数值默认为0,间隔为1。

range(start,stop) 给出开始数值和结束数值,间隔为1。

range(start,stop,step) 给出开始数值和结束数值,间隔为step数值。

  1. class range(object):
  2. """
  3. range(stop) -> range object
  4. range(start, stop[, step]) -> range object
  5. Return an object that produces a sequence of integers from start (inclusive)
  6. to stop (exclusive) by step. range(i, j) produces i, i+1, i+2, ..., j-1.
  7. start defaults to 0, and stop is omitted! range(4) produces 0, 1, 2, 3.
  8. These are exactly the valid indices for a list of 4 elements.
  9. When step is given, it specifies the increment (or decrement).
  10. """
  11. def count(self, value): # real signature unknown; restored from doc
  12. """ rangeobject.count(value) -> integer -- return number of occurrences of value """
  13. return 0
  14. def index(self, value, start=None, stop=None): # real signature unknown; restored from doc
  15. """
  16. rangeobject.index(value, [start, [stop]]) -> integer -- return index of value.
  17. Raise ValueError if the value is not present.
  18. """
  19. return 0
  20. def contains(self, *args, **kwargs): # real signature unknown
  21. """ Return key in self. """
  22. pass
  23. def eq(self, *args, **kwargs): # real signature unknown
  24. """ Return self==value. """
  25. pass
  26. def getattribute(self, *args, **kwargs): # real signature unknown
  27. """ Return getattr(self, name). """
  28. pass
  29. def getitem(self, *args, **kwargs): # real signature unknown
  30. """ Return self[key]. """
  31. pass
  32. def ge(self, *args, **kwargs): # real signature unknown
  33. """ Return self>=value. """
  34. pass
  35. def gt(self, *args, **kwargs): # real signature unknown
  36. """ Return self>value. """
  37. pass
  38. def hash(self, *args, **kwargs): # real signature unknown
  39. """ Return hash(self). """
  40. pass
  41. def init(self, stop): # real signature unknown; restored from doc
  42. pass
  43. def iter(self, *args, **kwargs): # real signature unknown
  44. """ Implement iter(self). """
  45. pass
  46. def len(self, *args, **kwargs): # real signature unknown
  47. """ Return len(self). """
  48. pass
  49. def le(self, *args, **kwargs): # real signature unknown
  50. """ Return self<=value. """
  51. pass
  52. def lt(self, *args, **kwargs): # real signature unknown
  53. """ Return self<value. """
  54. pass
  55. @staticmethod # known case of new
  56. def new(*args, **kwargs): # real signature unknown
  57. """ Create and return a new object. See help(type) for accurate signature. """
  58. pass
  59. def ne(self, *args, **kwargs): # real signature unknown
  60. """ Return self!=value. """
  61. pass
  62. def reduce(self, *args, **kwargs): # real signature unknown
  63. pass
  64. def repr(self, *args, **kwargs): # real signature unknown
  65. """ Return repr(self). """
  66. pass
  67. def reversed(self, *args, **kwargs): # real signature unknown
  68. """ Return a reverse iterator. """
  69. pass
  70. start = property(lambda self: object(), lambda self, v: None, lambda self: None) # default
  71. step = property(lambda self: object(), lambda self, v: None, lambda self: None) # default
  72. stop = property(lambda self: object(), lambda self, v: None, lambda self: None) # default
  1. for i in range(3): #运行
结果为0,1,2 print(i) for i in range(0,5): #运行结果为0,1,2,3,4 print(i) for i in range(-2,10,2): #运行结果为-2,0,2,4,6,8 print(i)


5.break和continue语句,以及循环中的else语句

1)break语句和 C 中的类似,用于跳出最近的一级 for 或 while 循环。

  1. while True:
  2. print('hello')
  3. break


2)continue语句表示循环继续执行下一次迭代:

  1. for x in range(1, 4):
  2. print(x, 'for语句')
  3. continue
  4. print(x, 'continue语句后')
  5. else:
  6. print(x, 'else语句')
  7. #运行结果
  8. for语句
  9. for语句
  10. for语句
  11. else语句


3)循环中的else

如continue的例子里,有for-else语句,else语句会在循环跳出后执行,但是break跳出循环则不会执行else,所以else可以用来处理循环中的一些异常跳出。

  1. for x in range(1, 4):
  2. print(x)
  3. else:
  4. print(x)
  5. #运行结果
  6. 2
  7. 3

6.pass语句

pass语句什么也不做。它用于那些语法上必须要有什么语句,但程序什么也不做的场合,通常用于创建最小结构的类。

另一方面,pass可以在创建新代码时用来做函数或控制体的占位符。可以让你在更抽象的级别上思考。

  1. class EmptyClass:
  2. pass

以上就是Python流程控制代码详解的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行