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

Python流程控制代码详解

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

1.while语句

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

格式:【 while <条件>:

<内容>

break 】

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

2.if语句

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

x=int(input('请输入一个数字:'))
if x<0:
    print('负数')
elif x==0:
    print('零')
else :
    print('正数')

3.for语句

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

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

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

4.range()函数

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

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

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

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

class range(object):
    """
    range(stop) -> range object
    range(start, stop[, step]) -> range object
    
    Return an object that produces a sequence of integers from start (inclusive)
    to stop (exclusive) by step.  range(i, j) produces i, i+1, i+2, ..., j-1.
    start defaults to 0, and stop is omitted!  range(4) produces 0, 1, 2, 3.
    These are exactly the valid indices for a list of 4 elements.
    When step is given, it specifies the increment (or decrement).
    """
    def count(self, value): # real signature unknown; restored from doc
        """ rangeobject.count(value) -> integer -- return number of occurrences of value """
        return 0

    def index(self, value, start=None, stop=None): # real signature unknown; restored from doc
        """
        rangeobject.index(value, [start, [stop]]) -> integer -- return index of value.
        Raise ValueError if the value is not present.
        """
        return 0

    def contains(self, *args, **kwargs): # real signature unknown
        """ Return key in self. """
        pass

    def eq(self, *args, **kwargs): # real signature unknown
        """ Return self==value. """
        pass

    def getattribute(self, *args, **kwargs): # real signature unknown
        """ Return getattr(self, name). """
        pass

    def getitem(self, *args, **kwargs): # real signature unknown
        """ Return self[key]. """
        pass

    def ge(self, *args, **kwargs): # real signature unknown
        """ Return self>=value. """
        pass

    def gt(self, *args, **kwargs): # real signature unknown
        """ Return self>value. """
        pass

    def hash(self, *args, **kwargs): # real signature unknown
        """ Return hash(self). """
        pass

    def init(self, stop): # real signature unknown; restored from doc
        pass

    def iter(self, *args, **kwargs): # real signature unknown
        """ Implement iter(self). """
        pass

    def len(self, *args, **kwargs): # real signature unknown
        """ Return len(self). """
        pass

    def le(self, *args, **kwargs): # real signature unknown
        """ Return self<=value. """
        pass

    def lt(self, *args, **kwargs): # real signature unknown
        """ Return self<value. """
        pass

    @staticmethod # known case of new
    def new(*args, **kwargs): # real signature unknown
        """ Create and return a new object.  See help(type) for accurate signature. """
        pass

    def ne(self, *args, **kwargs): # real signature unknown
        """ Return self!=value. """
        pass

    def reduce(self, *args, **kwargs): # real signature unknown
        pass

    def repr(self, *args, **kwargs): # real signature unknown
        """ Return repr(self). """
        pass

    def reversed(self, *args, **kwargs): # real signature unknown
        """ Return a reverse iterator. """
        pass

    start = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

    step = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

    stop = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
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 循环。

while True:
    print('hello')
    break


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

for x in range(1, 4):
        print(x, 'for语句')
        continue
        print(x, 'continue语句后')
else:
        print(x, 'else语句')
 
#运行结果
for语句
for语句
for语句
else语句


3)循环中的else

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

for x in range(1, 4):
        print(x)
else:
        print(x)

#运行结果
2
3

6.pass语句

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

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

class EmptyClass:
    pass

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

人气教程排行