python基礎(3) 程式結構

2021-09-03 09:59:29 字數 4103 閱讀 6281

python和其他的程式語言一樣,也有三種程式結構。順序結構,選擇結構,迴圈結構。

1.順序結構

順序結構按照順序執行程式,不做過多解釋。

2.選擇結構

2.1 if 語句

if condition:

expression

示例:[root@willispython]# cat if.py

#!/usr/bin/env python

if 3 < 5:

print "3 less than 5" # 語句塊裡面可以是多個語句if 3 > 4:

print "3 greate than 4"

[root@yeahmonitor python]# ./if.py

3 less than 5

2.2else 子句

if condition:

expression

else:

expression

if語句裡面還可以巢狀if.python是允許語句的巢狀的。else子句總是if語句的最後乙個分支。不能出現在elif子句前面。

2.3 elif 字句

if condition:

expresssion

elif condition:

expression

# 可以有多個elif 子句, 但是一旦其中乙個分支沒執行了,就不會往下再去匹配執行了。

python中沒有switch語句。只能用elif模擬switch.

2.4 邏輯值(bool)

用來表示諸如:對於錯,真與假,空與非空等概念

邏輯值包括兩個值:

true:表示非空的量(如:string,tuple,list,set,dictonary 等所有非零)

false:表示 0,none,空的量等

作用:主要用於判斷語句中,用來判斷

乙個字串是否是空的

乙個運算結果是否是零

乙個表示式是否可用

>>> if true:

... print "ok"

ok>>> if false:

... print "ok"

>>> if 1:

... print "ok"

ok>>> if 0:

... print "ok"

例1:

#!/usr/bin/python

def fun():

return 1

if fun():

print "ok"

else:print "no"

if 和 else 中間不可以出現其他不相關的**

例子2:

#!/usr/bin/python

x=int(raw_input("please input x: "))

y=int(raw_input("please input y: "))

if x>=90:       多個條件判斷

if y>=90:

print "a"

elif x>=80:

print "b"

elif x>=70:

print "c"

else:

print "no"

例子3:邏輯結構 and, or, not

#!/usr/bin/python

x=int(raw_input("please input x: "))

y=int(raw_input("please input y: "))

if x>=90 and y>=90:print "a"

elif x>=80:

print "b"

elif x>=70:

print "c"

else:

print "no"

3.迴圈結構

3.1while迴圈

while 迴圈

#!/usr/bin/python

while true:

print "hello"  #死迴圈

while 迴圈一定要有條件

#!/usr/bin/python

while true:

print "hello"

x = raw_input("please input q for quit")

if x == "q":

break

或者#!/usr/bin/python

當 x=q 時迴圈結束

exit()函式可以跳出整個程式x = ""

while x != "q":

print "hello"

x = raw_input("please input q for quit")

又或者#!/usr/bin/python

x = ""

while x != "q":

print "hello"

x = raw_input("please input q for quit")

if not x : 直接回車退出

break

#!/usr/bin/python

x = ""

while x != "q":

print "hello"

x = raw_input("please input q for quit")

if not x : 直接回車退出

break

if x == "c": 輸入 c 繼續迴圈

continue

print "hello world" 如果兩次 if 都不滿足 print

else:

print "ending...."

while 條件判斷失敗執行 else 如果是 break 不執行 else

3.2for迴圈

for 迴圈

for x in [1,2,3,4]

print x

迭代序列指數(索引)

for x in range(100)     預設從 0 開始

print x

for x in range(1,11) 不包含最後乙個值 11

print x

for x in range(1,11,2) 2 為步進值,不指預設為 1

print x

#!/usr/bin/python

num=0

for x in range(1,101)

num += x

print num

遍歷序列

s="willis"

for x in range(len(s))

print s[x]

l=[1,2,3,'a','b']

for x in l:

if x>=2:

print x

d=for x in d:

print d[x]

for k,v in d.items():

print k

print v

for x in range(1,11):

print x

if x==2:

pass #**樁,站位

if x==3:

print "hello"

continue

if x==6:

#continue 跳過當次迴圈的餘下語句進入下次迴圈

break

#break 結束本次迴圈

print "#"*50

else:

print "ending"

python 程式結構

多路分支 if語句其他 2 迴圈語句 for else語句 break,contineu,pass while迴圈 3 函式 4 返回值 5 函式文件 input gender input 請輸入性別 print 你輸入的性別是 format gender 表示這裡是新增的字串,0表示第乙個字串 s...

Python程式結構

if 條件表示式 語句1語句2 語句3.if語句1 字串的真假 只有空字串為false,其餘全為true a 字串非空 if a print 輸出為true else print 輸出為false 執行結果 輸出為true if語句2 age 19if age 16 print 去網咖 else p...

Python程式結構

條件語句 if 判斷 滿足判斷條件執行的 塊 else 不滿足判斷條件執行的 塊 if 判斷1 滿足判斷條件1執行的 塊 elif 判斷2 不滿足判斷1,滿足判斷2執行的 塊 else 不滿足所有判斷條件執行的 塊 條件語句可以巢狀 while 判斷 滿足判斷條件執行的迴圈體 continue 跳過...