1.if 條件語句
Python中,如果是全部大寫,就代表常量,不過python的常量不一定不能改變。
USER = 'root'
在python中,這樣寫是為了讓程序員知道它不想改變,可以作為常量。其實與其他的變量一樣,沒有區(qū)別。
eg:
if (user == USER ) and (password == PASSWORD) :
print 'welcome to library system'
python中是以:來表示,而不是使用{}
eg:
if xxx:
xxx
elif xxx:
xxx
else:
xxx
2.while 循環(huán)
1)python中的while 語句的一般形式:
while 判斷條件:
執(zhí)行語句
eg:
i = 0
while i <= 100:
print i # 注意:這里一定要有tab的空格??梢允?個空格,也可以是4個空格,一般是4個空格
i += 1
2)while...else
while 判斷條件:
執(zhí)行語句
else:
執(zhí)行語句
eg:
i = 0
while i <= 100:
i += 1
else:
print i # 101
注意:
在python 中是沒有++這種自增的語法??梢允褂?code>+=1。
3.For循環(huán)
1)for...
形式:
for 變量 in 序列:
執(zhí)行語句
else:
執(zhí)行語句
注意:
Python中序列的介紹:http://www.iplaypy.com/jinjie/jj106.html
List和Tuple是最長被用到的序列在python中。字符串也是序列。
eg:
nums = [1, 2, 3, 4, 5]
for i in nums:
print i
eg:
chars = "Hello, my name is Leo maer!"
for char in chars:
print (char.upper())
4.range()
eg:
for i in range(10):
print i # 0-9
range(-2, 10) # 生成-2-9
總結(jié):
一般程序中的range啊,起止啊,都是:前者是開區(qū)間,后者是半開。
-1, 10代表-1到9,等等。
5.break 和 continue語句
break可以跳出for和while的循環(huán)體。
如果你從for或者while循環(huán)中終止。
break是結(jié)束整個循環(huán)體,continue是結(jié)束單次循環(huán)。
continue結(jié)束本次循環(huán),不執(zhí)行本次循環(huán)的剩余代碼,開始下一次循環(huán)。
使用\來進行換行:

圖片.png
Python的pass語句
Python pass是空語句,是為了保持程序結(jié)構(gòu)的完整性。
pass 不做任何事情,一般用做占位語句。