python學習筆記 5 條件 迴圈和其他語句

2022-05-01 09:45:10 字數 1051 閱讀 3202

import somemodule

匯入模組

from somemodule import somefuction

匯入函式

import math as foobar

匯入模組,並使用別名

from math import sqrt as foobar

匯入函式,並使用別名

>>> x,y,z=1,2,3

>>> print x,y,z

1 2 3

>>> x,y=y,x

>>> print x,y,z

2 1 3

>>> scoundrel=

>>> key, value = scoundrel.popitem()

>>> print key, value

name robin

num=0

if num > 0:

print "positive"

elif num < 0:

print "negative"

else:

print "zero"

x=1

while x < 100:

print x

x += 1

numbers = [0,1,2,3,4,5,6,7,8,9]

for number in numbers:

print number

for number in range(1,10):

print number

d=for key, value in d.items():

print key, value

zip: 把key序列和value序列合併成字典

enumerate: 返回所引和值序列

sort:返回排序後的結果,作用於序列或可迭代的物件

break;跳出迴圈

continue:結束當前迴圈

Python 學習筆記3(條件 迴圈)

1 條件判斷 這個和c語言有點類似,看下面 age 20if age 18 print your age is age print adult 你發現了什麼,if後面有個冒號,而且如果age小於18你猜結果會是怎樣,如果是c語言那麼肯定會輸出 adult 而py不會輸出 說明py的 執行和縮排有關 ...

Python學習(5)條件語句

python條件語句是通過一條或多條語句的執行結果 true或者false 來決定執行的 塊。可以通過下圖來簡單了解條件語句的執行過程 python程式語言指定任何非0和非空 null 值為true,0 或者 null為false。python 程式設計中 if 語句用於控制程式的執行,基本形式為 ...

Python學習 2 5條件判斷 迴圈

if 條件判斷1 執行1 elif 條件判斷2 執行2 elif 條件判斷3 執行3 else 執行4 age 21 if age 30 print age elif age 20 print 年齡是 age else print ok birth input birth if birth 2000...