while
語句的語法如下:
while condition:預設情況下,statement1
statement2
print()
除了列印你提供的字串之外,還會列印乙個換行符,所以每呼叫一次print()
就會換一次行,如同上面一樣。
你可以通過print()
的另乙個引數end
來替換這個換行符,就像下面這樣,下面的程式寫入 ,例如:
#!/usr/bin/env python3
a, b = 0, 1
while b < 100:
print(b) a, b = b, a +b
#!/usr/bin/env python3
a, b = 0, 1
while b < 100:
print(b, end='')
a, b = b, a +b
print()
#這裡我們在!/usr/bin/env python3
i = 1
print("
-" * 50)
while i < 11:
n = 1
while n <= 10:
print("
".format(i * n), end='')
n += 1
() i += 1
print("
-" * 50)
while
迴圈裡使用了另乙個while
迴圈,這被稱為巢狀迴圈。你應該已經看到一條有趣的語句:
通過for
語句我們可以使用 for 迴圈。這裡的 for 迴圈遍歷任何序列(比如列表和字串)中的每乙個元素。下面給出示例:
>>> a = ['我們也能這樣做:shiyanlou
', '
is', '
powerful']
>>> for x in
a:...
(x)...
shiyanlou
ispowerful
>>> a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]range() 函式>>> for x in a[::2]:
...
(x)135
79
如果你需要乙個數值串行,內建函式 range() 會很方便,它生成乙個等差數列(並不是列表):
>>> for i in range(5):我們可以在迴圈後面使用可選的...
(i)...01
234>>> range(1, 5)
range(1, 5)
>>> list(range(1, 5))
[1, 2, 3, 4]
>>> list(range(1, 15, 3))
[1, 4, 7, 10, 13]
>>> list(range(4, 15, 2))
[4, 6, 8, 10, 12, 14]
else
語句。它將會在迴圈完畢後執行,除非有break
語句終止了迴圈。
>>> for i in range(0, 5):python 中...
(i)...
else
:...
print("
bye bye")
...012
34bye bye
for
迴圈的else
子句給我們提供了檢測迴圈是否順利執行完畢的一種優雅方法。
列表資料結構。
我們像下面這樣通過索引來訪問列表中的每乙個值:
>>>a[0]1>>> a[4]
'fedora
'
>>> a[0:-1][1, 342, 223, '
india']
>>> a[2:-2]
[223]
>>>a[:]
[1, 342, 223, '
india
', '
fedora
']
>>> a[:-2]有個辦法可以很容易地記住切片的工作方式:切片時的索引是在兩個元素之間 。左邊第乙個元素的索引為 0,而長度為 n 的列表其最後乙個元素的右界索引為 n。[1, 342, 223]
>>> a[-2:]['
india
', '
fedora
']
python 中有關下標的集合都滿足左閉右開原則,切片中也是如此,也就是說集合左邊界值能取到,右邊界值不能取到。
python 能夠優雅地處理那些沒有意義的切片索引:乙個過大的索引值(即大於列表實際長度)將被列表實際長度所代替,當上邊界比下邊界大時(即切片左值大於右值)就返回空列表:
>>> a[2:32][223, '
india
', '
fedora']
>>> a[32:]
>>> a[1::2][342, '
india
']
>>> a + [36, 49, 64, 81, 100]列表允許修改元素:[1, 342, 223, '
india
', '
fedora
', 36, 49, 64, 81, 100]
>>> cubes = [1, 8, 27, 65, 125]>>> cubes[3] = 64
>>>cubes
[1, 8, 27, 64, 125]
>>> letters = ['
a', '
b', '
c', '
d', '
e', '
f', 'g'
]>>>letters['
a', '
b', '
c', '
d', '
e', '
f', 'g'
]>>> #
替換某些值
>>> letters[2:5] = ['
c', '
d', 'e'
]>>>letters['
a', '
b', '
c', '
d', '
e', '
f', 'g'
]>>> #
現在移除他們
>>> letters[2:5] =
>>>letters['
a', '
b', '
f', 'g'
]>>> #
通過替換所有元素為空列表來清空這個列表
>>> letters[:] =
>>>letters
>>> a = ['shiyanlou
', '
is', '
cool']
>>> '
cool'in
atrue
>>> '
linux'in
afalse
>>>len(a)如果你想要檢查列表是否為空,請這樣做:3
if list_name: #列表不為空
pass
else: #
列表為空
pass
>>> a = ['a', '
b', 'c'
]>>> n = [1, 2, 3]
>>> x =[a, n]
>>>x[['
a', '
b', '
c'], [1, 2, 3]]
>>>x[0]['
a', '
b', 'c'
]>>> x[0][1]'b
'
python學習筆記(三)
python的序列 列表,元組,字串都是列表,列表的主要特點是索引和切片操作 序列的基本操作 1.len 求序列的長度 2.連線兩個序列 3.重複序列元素 4.in判斷序列是否在元組中 5.max 返回最大值 6.min 返回最小值 7.cmp tup1,tup2 比較兩個序列的值 元組 元組和字串...
python學習筆記三
一 輸出 print的幾種用法 print hello world hello world中間有空格 print hello wolrd helloworld 中間沒有空格 print hello wolrd helloworld 中間沒有空格 print hello world hello wor...
python學習筆記(三)
list也可以直接做加法 a 1,2 a 3 a 1,2,3 lambda用來定義匿名函式 lambda x x 2.0 lambda x x 2.0 平方是用 student torture不理解 for可以各種巢狀 ppl alice bob carol doug excited ppl e f...