python從零開始學習(二)--if,while,for,運算符

2.1 if
注意冒號和縮進。

if 判斷語句:
code
elif 判斷語句:
code
else:
code

例子:

if os.name == 'nt':
#win
print "windows"
elif os.name == 'posix':
print "linux/unix"
else:
print "error"


2.2 while

while 判斷語句:
code

例子:
x = 10
while x > 0:
print x
x=x-1


2.3 for


for循環語句是python中的一個循環控制語句,任何有序的序列對象內的元素都可以遍歷,比如字符串、列表、元組等可迭代對像。

for 目標 in 對像:
    print 目標
x
例子
strTest = 'asdasdasd'

for ch in strTest:
print ch


2.4 運算符

算術運算符


比較運算符


賦值運算符


位運算符


邏輯運算符


標識運算符:
標識符比較兩個對象的內存位置。兩個運算符標識解釋如下:


運算符優先級


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章