python中的for循環對象和循環退出
判斷條件,1位true,0是flesh,成立時true,不成立flesh,not取反
if ?1; ?
? ?print ‘hello python‘
? ?print ‘true‘
? not取反,匹配取反,表示取非1大於2的正確關系,也就是說取1大於2的不正確證明的結果
if ? not 1 > 2 and ?1 == 1; ? ? ?
? ?print ‘hello python‘
? ? print ‘true‘
if ?1 > 2; ? ?print ‘hello python‘ ? ? print ‘true‘ else: ? ? ? ? ? ?第一條件1大於2不成立,則執行else裏的條件 ? ?print ‘jia‘ ? ? ? ? ? ? print ‘mei‘ ? ? ? ?這裏的print與if是同行的,是單獨的一個主進程
if中執行一個輸入數字的話,需要聲明這個數字是int類型(數值類型) ,不聲明的話會被認為是一個字符串,不會被識別為數值
#!/usr/bin/python
ss = int(raw_input("please a num: "))
if ss >= 90:
? ?print ‘A or very good‘
elif ss >= 80:
? ?print ‘B or good‘
elif ss >= 70:
? ?print ‘C or pass‘
else:
? ?print ‘D‘
if 對輸入的字符進行匹配判斷,匹配yes/no,或錯誤繼續輸入
下列說明:
將raw_input 賦值給yn,yn通過函數將字符統一為一個格式,if ?yn ?== ?‘條件‘ ?來匹配是否符合,符合並輸出或執行某些語法語句,這裏也出現了if--elif--else的用法
or ?表示鏈接多個判斷條件,也可以使用and來表示
其他(關於字符的方法)
*.lower表示把所獲取的大寫字符都變為小寫字符來處理
*.upper表示將小寫字符轉為大寫,取數值的大寫字符
#!/usr/bin/python yesno = raw_input("Please input [Yes/No]: ") yesno = yesno.lower() if yesno == ‘y‘ or yesno == ‘yes‘: ? ?print "programe is runing..." elif yesno == ‘n‘ or yesno == ‘no‘: ? ?print "programe is exit" else: ? ?print "please input [Yes/No]"
流程控制-for循環
for循環可以使用在序列裏,可以在python中遍歷序列
這裏介紹一個函數
range函數用來遍歷一個範圍內的所有數字,輸出的結果為一個列表類型的數據,可以針對結果做奇偶數選擇,如從0開始選擇數值間隔為2的數值,這樣取值的全部都會是偶數數值
range(10)
這個函數是用來遍歷一個範圍內的所有數字,遍歷數值從0開始,會輸出0-9共十個數字,如
In [1]: range(10)
Out[1]:
[0,
1,
2,
3,
4,
5,
6,
7,
8,
9,]
range取值間隔數字(字符步長間隔的數值),如range取值從0開始的每隔兩個長度間隔的數字:
In [6]: range(0,10,2)
Out[6]: [0, 2, 4, 6, 8]
for循環
python的for循環可以針對列表、數組類型的數據進行遍歷,把遍歷出來的數值進行處理(這裏是把遍歷做個相加或者3次方相乘)
使用print打印出一個變量可以讓輸出結果不換行顯示,在打印變量名後加上一個逗號
將xrange函數遍歷的數值給予一個列表中,然後使用for循環對列表進行遍歷,將遍歷出來的數值全部相加得出結果
#!/usr/bin/python
a=xrange(101)
sum=0
for i in a :
? ?sum = sum + i
print sum
for循環將函數遍歷結果進行奇偶判斷,且將的出來的偶數進行3次方相乘,print出for循環變量的結果,print變量後加逗號表示將結果輸出到一行內並以空格顯示(去除掉換行符)
[root@localhost shell]# cat 1.py
#!/usr/bin/python
for i in [i**3 for i in range(0,11) if i % 2 !=0 ]:
? ?print i,
[root@localhost shell]# python 1.py
1 27 125 343 729
range函數和xrange函數的區別
range函數是將一個範圍內的數值打印出來並存儲到內存當中,所打印出來的數值存儲為一個列表,這個列表會占用內存資源。調用時會從內存中去讀取並釋放
xrange輸出的數值則是一個引用的對象,它不是把遍歷結果輸出並保存到內存,而是在需要使用該數值時才會去遍歷這個範圍段的數值,和range不同的是,xrange不會輸出信息。xrange是一個object,只有在讀取xrange的數據時才會執行它裏面的值
xrange的幫助信息
In [7]: help(xrange)
Help on class xrange in module __builtin__:
class xrange(object)
| xrange(stop) -> xrange object
| xrange(start, stop[, step]) -> xrange object
| ?
| Like range(), but instead of returning a list, returns an object that
| generates the numbers in the range on demand. For looping, this is
| slightly faster than range() and more memory efficient.
| ?
| Methods defined here:
| ?
| __getattribute__(...)
| x.__getattribute__(‘name‘) <==> x.name
| ?
| __getitem__(...)
| x.__getitem__(y) <==> x[y]
| ?
| __iter__(...)
| x.__iter__() <==> iter(x)
| ?
| __len__(...)
| x.__len__() <==> len(x)
| ?
| __reduce__(...)
| ?
| __repr__(...)
| x.__repr__() <==> repr(x)
| ?
| __reversed__(...)
| Returns a reverse iterator.
| ?
| ----------------------------------------------------------------------
| Data and other attributes defined here:
| ?
| __new__ = <built-in method __new__ of type object>
| T.__new__(S, ...) -> a new object with type S, a subtype of T
(END)
xrange是將數值存儲到object裏,只有在調用或for遍歷時才會取出數值,不調用時則不會產生列表存儲不占用內存資源
for循環字典
遍歷序列:將序列中各個元素取出來
直接從序列取值
通過索引來取值
叠代,指重復執行一個指令
首先創建一個測試使用的字典
In [12]: nico = {‘a‘:1,‘b‘:2,‘c‘:3}
In [13]: type(nico)
Out[13]: dict
In [14]: nico.fromkeys(‘abcdefg‘,1001)
Out[14]: {‘a‘: 1001, ‘b‘: 1001, ‘c‘: 1001, ‘d‘: 1001, ‘e‘: 1001, ‘f‘: 1001, ‘g‘: 1001}
遍歷字典中的數值
In [15]: for i in nico:
? ...: print i, nico [i]
? ...: ? ?
a 1
c 3
b 2
遍歷字典並添加一個格式化的輸出
In [18]: for i in nico:
? ...: print "%s -- %s" % (i, nico[i])
? ...: ? ?
a -- 1
c -- 3
b -- 2
python循環輸出乘法口訣表
使用for嵌套的方式在for循環中再套用一個for循環,外層for循環遍歷出1-9的數字,內層循環遍歷出外層循環+1的數字,print條件中使用兩層for循環得出的輸出值相乘的出結果,再以格式化輸出顯示
[root@localhost shell]# vim chen.py
#!/usr/bin/python
for f in xrange(1,10):
? ?for j in xrange(1,f+1):
? ? ? ?print "%sx%s=%s" % (j, f, j*f),
? ?print
for循環退出
python的for循環退出也是和shell裏的三個退出參數用法一致,分別是break、continue和exit(終止本循環內容、終止這次循環和直接退出這個腳本)
for循環的else輸出
else中可以使用continue結束本次循環進入下次循環,break則是結束本次循環輸出最後一次循環輸出,exit結束這個循環及整個腳本並輸出最後內容
如這種腳本
[root@localhost shell]# vim else.py
#!/usr/bin/python
import time
for i in xrange(10):
? ?if i == 3:
? ? ? ?continue
? ?elif i == 5:
? ? ? break
? ?elif i == 6:
? ? ? ?pass
? ?elif i == 8:
? ? ? sys.exit()
? ?print i
else:
? ?print ‘bilibili‘
print ‘bilibili‘
第一次if判斷i等於不等於3,循環到等於3時結束本次循環,直接進入下次循環,等於3這次循環不會輸入任何結果,當i等於5時,結束這次循環不再循環下去及輸出結果了。如後面的 ?i等於6、i等於8都不會去循環了,並執行和for循環等行的print出來的內容。
執行出來的結果
[root@localhost shell]# python else.py
0
1
2
4
bilibili
將腳本的break中斷循環註釋或刪除(即在i等於5時不終止循環),再次測試執行結果,查看是否能夠輸出else中的內容
只有當for循環中的數值執行完成後才能夠執行等行else中的輸出或執行
如果在某以匹配條件中存在break或sys.exit()的退出操作,整個腳本就會被終止,exit是退出整個腳本,後面的語句直接不執行了,break是退出循環並會向下繼續執行非for內的語句
[root@localhost shell]# cat else.py
#!/usr/bin/python
import time
import sys
for i in xrange(10):
? ?if i == 3:
? ? ? ?continue
? ?elif i == 6:
? ? ? ?pass
? ?elif i == 8:
? ? ? ? pass
? ?print i
else:
? ?print ‘bilibili‘
print ‘bilibili‘
下面是摘抄的練習腳本
for循環腳本示例,練習腳本
#!/usr/bin/python
#coding:utf-8
import sys
import random
n=random.randint(1,20)
for i in xrange(1,7):
? ?m=int(raw_input("請輸入你的數字: "))
? ?if m==n:
? ? ? ?print"恭喜你猜對了"
? ? ? ?sys.exit()
? ?elif m<n and (6-i)==0:
? ? ? ?print"沒機會啦!!!!!!"
? ?elif m<n:
? ? ? ?print"猜小了哦!\n請繼續猜"
? ? ? ?print"你還有%s次機會" % (6-i)
? ? ? ?print
? ?elif m>n and (6-i)==0:
? ? ? ?print"沒機會啦!!!!!!"
? ?elif m>n:
? ? ? ?print"猜大了哦!\n請繼續猜"
? ? ? ?print"你還有%s次機會" % (6-i)
? ? ? ?print
else:
? ?print"正確答案是:%s" %n
? ?print "答題超過限制,你輸了!"
python中的for循環對象和循環退出