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

2021-09-29 00:07:53 字數 1398 閱讀 6482

,共勉

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的習慣寫法:4個空格,不要使用tab,更不要混合tab和空格

3,在python互動環境下敲**,還要特別留意縮排,並且退出縮排需要多敲一行回車

二,for迴圈

#求四個成績的平均數

l = [75, 92, 59, 68]

sum = 0.0

for child in l:

sum = sum + child

print sum / 4

三,while 迴圈

#求100以內奇數的和

sum = 0

x = 1

while x<100:

sum = sum+x

x = x+2

print sum

四,break 退出迴圈

#計算 1 + 2 + 4 + 8 + 16 + ... 的前20項的和。

sum = 0

x = 1

n = 1

while true:

child = 2**(n-1)

sum = sum + child

n = n+1

if ( n > 20 ):

break

print sum

五,continue 跳過後續迴圈,直接進行下乙個迴圈

#通過增加 continue 語句,使得只計算0-100奇數的和

sum = 0

x = 0

while true:

x = x + 1

if x > 100:

break

if x%2 == 0:

continue

sum = sum + x

print sum

實戰

#對100以內的兩位數,請使用乙個兩重迴圈列印出所有十位數數字比個位數數字小的數,例如,23(2 < 3)

for x in [ 1,2,3,4,5,6,7,8,9 ]:

for y in [ 1,2,3,4,5,6,7,8,9 ]:

if (x < y):

print x*10 + y

Linux Shell(三) 條件判斷

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

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

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語句,注意 中間是e...

MySQL 基礎三(條件查詢)

條件查詢語法 select 查詢列表 from 表名where 篩選條件 執行順序 from 表名 篩選條件 查詢列表 分類 示例 案例1 查詢工資 12000的員工資訊 select from employees where salary 12000 案例2 查詢部門編號不等於90號的員工名和部門...