Python 迴圈結構 新手向

2021-10-11 10:35:31 字數 4229 閱讀 5931

while

條:語句

gif例項

break與continue語句

break:結束整個for迴圈(如果是迴圈巢狀情況就結束內迴圈,進入下一次外迴圈)

continue:結束當前迴圈,進入下乙個迴圈(比如在第二次迴圈時讀到continue時,會直接結束第二次迴圈,進行第三次迴圈)

n =

0while n <10:

n = n +

1if n %2==

0:# 如果n是偶數,執行continue語句

continue

# continue語句會直接繼續下一輪迴圈,後續的print()語句不會執行

print

(n)#13579

n =

0while n <10:

n = n +

1if n %2==

0:# 如果n是偶數,執行break語句

break

# 迴圈結束

print

(n)#

1

while與else共用while … else 在迴圈條件為 false 時執行 一次else 語句塊

例項:

count =

0while count <5:

print count,

" is less than 5"

count = count +

1else

:print count,

" is not less than 5"

以上例項輸出結果為:

0is less than 5

1is less than 5

2is less than 5

3is less than 5

4is less than 55is

not less than 5

for迴圈可以遍歷任何序列的專案,如列表或者字串。

一般格式如下:

for iter_var in iterable:

suite_to_repeat

#每次迴圈, iter_var 迭代變數被設定為可迭代物件(序列, 迭代器, 或者是其他支援迭代的

#對 象)的當前元素, 提供給 suite_to_repeat 語句塊使用.

#iterable多變,下面講述幾種常見的

1.range()range(start,end,step=1):step指間隔

range(5):預設step=1,start=0,生成可迭代物件,包含[0, 1, 2, 3, 4 ]

range(1,5):指定start=1,end=5,預設step=1,包含[1, 2, 3, 4]

range(1,10,2):指定start=1,end=10,step=2,包含[1, 3, 5, 7, 9]

>>

>

for i in

range(5

):..

.print

(i)...

0123

4# 計算圓周率 σ(1/(16**c))*(4/(8*c+1) - 2/(8*c+4) - 1/(8*c+5) - 1/(8*c+6)) #1/(16**c)

x =0

for c in

range

(100):

x +=(1

/(16**c))*

(4/(

8*c+1)

-2/(

8*c+4)

-1/(

8*c+5)

-1/(

8*c+6)

)# 1/(16**c)是錯的

print

(x)# 輸出的結果是圓周率3.141592653589793

2.列表

3.檔案

'''

a.txt:

hello everyone

'''>>

> f=

open

('a.txt'

,'r'

)#r表示此檔案唯讀

for i in f:

print

(i.strip())

hello

everyone

實際上,for可以可遍歷序列成員(字串,列表,元組),可遍歷任何可迭代物件(字典,檔案等。

for與else共用

原理參見上方while與else相同

s =

"python"

while s !="":

for c in s:

if c ==

"t":

break

print

(c, end="")

# print(c, end="") 可以使輸出結果在同一行

s = s[:-

1]# 使s逐漸變短

# print

# pypypypypyp

rows =

int(

input

('輸入列數: '))

i = j = k =

1#宣告變數,i用於控制外層迴圈(圖形行數),j用於控制空格的個數,k用於控制*的個數

#等腰直角三角形1

print

("等腰直角三角形1"

)for i in

range(0

, rows)

:for k in

range(0

, rows - i)

:print

(" * "

, end="")

#注意這裡的"end=""",一定不能省略,可以起到不換行的作用

k +=

1 i +=

1print

("\n"

)

選擇排序與氣泡排序

# 選擇排序

array =[8

,2,6

,3,4

,5,7

,1,10

,9]l =

len(array)

for i in

range(1

, l)

: temp = array[i]

array.remove(array[i]

)for j in

range

(i):

if array[j]

> temp:

array.insert(j , temp)

break

else

: array.insert(i , temp)

print

(array)

# 氣泡排序

array =[8

,2,6

,3,4

,5,7

,1,10

,9]l =

len(array)

for i in

range

(l):

for j in

range

(l-i)

:if array[l-j-1]

< array[l-j-2]

: array[l-j-1]

, array[l-j-2]

= array[l-j-2]

, array[l-j-

1]

print(array)

注意事項:

無限迴圈可用ctrl+c退出迴圈

條件:任何非0和非空(null)值為true,0 或者 null為false。

Python 分支結構 新手向

基本格式 if條 語句塊1 else 語句塊2 或者 語句塊1 if條else 語句塊2 if條件1 語句塊1 elif 語句塊2 else 語句塊n ps if else 後面的 不要忘記哦 示例 如果判斷需要多個條件需同時判斷時,可以使用 or 或 表示兩個條件有乙個成立時判斷條件成功 使用 a...

Python新手學習基礎之迴圈結構 For語句

for語句 在python裡,迴圈語句除了while語句,還有for語句。通常我們用for迴圈來遍歷 按約定的順序,對每個點進行訪問,且只做一次訪問 有序列的內容,比如列表和字串 列表內容我們會在之後的章節詳細介紹 for遍歷迴圈的 語法如下 for x in list1 print x 這裡變數會...

Python新手學習基礎之迴圈結構練習

有幾個母音字母?有乙個字串 i learn python from maya 我們想要查詢出它裡面的母音字母 aeiou 其實是找出這幾個小寫字母 並統計出其母音字元的個數。這個時候我們就可以結合條件語句和迴圈語句對字串進行處理了。sentence是乙個字串,你需要去統計這個字串的母音字母數 sen...