Python基礎學習三 條件判斷和迴圈

2021-07-10 00:03:14 字數 1264 閱讀 3891

#if語句,注意縮排是4個空格

age=20

if age>=18:

print 'your age is',age

print 'adult'

print 'end'

#if-else語句

if age >=18:

print 'adult'

else:

print 'teenager'

#if-elif-else語句,注意

#中間是elif是else

if的簡寫,最後是else

if age>=18:

print 'adult'

elif age>=6:

print 'teenager'

elif age>=3:

print 'kid'

else:

print 'baby

#for 迴圈,用於遍歷list或tuple

list_class=['adam', 'lisa', 'bart','shawn']

for name in list_class:

print name

#while迴圈

n=10

x=0while x#python好像不支援++x和x++,注意一下

x+=1

#break退出迴圈

sum=0

x=1#針對於沒有或不在乎判斷條件下使用true

while

true:

sum+=x

x+=1

if x>100:

break

print sum

#continue用與剔除不要的東西

list_scores=[75,98,59,81,66,43,69,95]

sum=0.0

n=0for x in list_scores:

if x<60:

continue

sum+=sum

n=n+1

print sum/n

#多重迴圈

for x in ['a', 'b', 'c']:

for y in ['1', '2', '3']:

print x + y

#x 每迴圈一次,y 就會迴圈 3 次,

#這樣,我們可以列印出乙個全排列:

'''a1

a2a3

b1b2

b3c1

c2c3

'''

Linux Shell(三) 條件判斷

str1 str2 當兩個串有相同內容 長度時為真 str1 str2 當串str1和str2不等時為真 n str1 當串的長度大於0時為真 串非空 z str1 當串的長度為0時為真 空串 str1 當串str1為非空時為真 int1 eq int2 兩數相等為真 int1 ne int2 兩數...

Python學習筆記(三)條件判斷和迴圈

if 條件判斷1 執行1 elif 條件判斷2 執行2 elif 條件判斷3 執行3 else 執行4 age 20 if age 6 print teenager elif age 18 print adult else print kid 如果在某個選擇支為 true,則忽略掉剩下的 elif ...

python 基礎知識三 條件判斷和迴圈

共勉score 85 if score 90 print excellent elif score 80 print good elif score 60 print passed else print failed if特性 1,ython 的縮排規則。具有相同縮排的 被視為 塊 2,python...