Python 流程式控制制,python流程式控制制

來源:互聯網
上載者:User

Python 流程式控制制,python流程式控制制

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數值。

 1 class range(object): 2     """ 3     range(stop) -> range object 4     range(start, stop[, step]) -> range object 5      6     Return an object that produces a sequence of integers from start (inclusive) 7     to stop (exclusive) by step.  range(i, j) produces i, i+1, i+2, ..., j-1. 8     start defaults to 0, and stop is omitted!  range(4) produces 0, 1, 2, 3. 9     These are exactly the valid indices for a list of 4 elements.10     When step is given, it specifies the increment (or decrement).11     """12     def count(self, value): # real signature unknown; restored from __doc__13         """ rangeobject.count(value) -> integer -- return number of occurrences of value """14         return 015 16     def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__17         """18         rangeobject.index(value, [start, [stop]]) -> integer -- return index of value.19         Raise ValueError if the value is not present.20         """21         return 022 23     def __contains__(self, *args, **kwargs): # real signature unknown24         """ Return key in self. """25         pass26 27     def __eq__(self, *args, **kwargs): # real signature unknown28         """ Return self==value. """29         pass30 31     def __getattribute__(self, *args, **kwargs): # real signature unknown32         """ Return getattr(self, name). """33         pass34 35     def __getitem__(self, *args, **kwargs): # real signature unknown36         """ Return self[key]. """37         pass38 39     def __ge__(self, *args, **kwargs): # real signature unknown40         """ Return self>=value. """41         pass42 43     def __gt__(self, *args, **kwargs): # real signature unknown44         """ Return self>value. """45         pass46 47     def __hash__(self, *args, **kwargs): # real signature unknown48         """ Return hash(self). """49         pass50 51     def __init__(self, stop): # real signature unknown; restored from __doc__52         pass53 54     def __iter__(self, *args, **kwargs): # real signature unknown55         """ Implement iter(self). """56         pass57 58     def __len__(self, *args, **kwargs): # real signature unknown59         """ Return len(self). """60         pass61 62     def __le__(self, *args, **kwargs): # real signature unknown63         """ Return self<=value. """64         pass65 66     def __lt__(self, *args, **kwargs): # real signature unknown67         """ Return self<value. """68         pass69 70     @staticmethod # known case of __new__71     def __new__(*args, **kwargs): # real signature unknown72         """ Create and return a new object.  See help(type) for accurate signature. """73         pass74 75     def __ne__(self, *args, **kwargs): # real signature unknown76         """ Return self!=value. """77         pass78 79     def __reduce__(self, *args, **kwargs): # real signature unknown80         pass81 82     def __repr__(self, *args, **kwargs): # real signature unknown83         """ Return repr(self). """84         pass85 86     def __reversed__(self, *args, **kwargs): # real signature unknown87         """ Return a reverse iterator. """88         pass89 90     start = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default91 92     step = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default93 94     stop = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
range
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語句') #運行結果1 for語句2 for語句3 for語句3 else語句

 3)迴圈中的else

如continue的例子裡,有for-else語句,else語句會在迴圈跳出後執行,但是break跳出迴圈則不會執行else,所以else可以用來處理迴圈中的一些異常跳出。

for x in range(1, 4):        print(x)else:        print(x)#運行結果1233

6.pass語句

pass語句什麼也不做。它用於那些文法上必須要有什麼語句,但程式什麼也不做的場合,通常用於建立最小結構的類。

另一方面,pass可以在建立新代碼時用來做函數或控制體的預留位置。可以讓你在更抽象的層級上思考。

class EmptyClass:    pass

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.