国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

《python基礎教程》筆記之條件語句和循環語句

2019-11-14 17:21:52
字體:
來源:轉載
供稿:網友
  • 布爾變量

下面的值會被解釋器看做假(false):

False None 0 "" () {} []

其它的一切都被解釋為真。

>>> True
True
>>> False
False
>>> True == 1
True
>>> False == 0
True
>>> True + False +42
43

bool函數 -- 用來轉換其它值,如

>>> bool([])
False
>>> bool('hello,world')
True

  • 條件語句

if else elif

is 和 is not -- 判斷兩個變量是不是同一個對象

>>> x=y=[1,2,3]
>>> z=[1,2,3]
>>> x == y
True
>>> x == z
True
>>> x is y
True
>>> x is z
False

上例中可見,因為is運算符是判定同一性的。變量x和y都被綁定在同一個列表上,而變量z被綁定在另外一個具有相同數值和順序的列表上。它們的值可能相同,但是卻不是同一個對象。

in 和 not in -- 成員資格運算符

assert -- 當條件不為真時,程序崩潰

>>> x = 5
>>> assert 0<x<10
>>> assert 5<x <4

Traceback (most recent call last):
  File "<pyshell#25>", line 1, in <module>
    assert 5<x <4
AssertionError

  • 循環

range -- 內建范圍函數,它包含下限,但不包含上限,如

>>> range(0,10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

for num in range(0, 10):    PRint num,

結果如下

>>>
0 1 2 3 4 5 6 7 8 9

循環遍歷字典,可以使用序列解包,如

d = {'x':1, 'y':2}for key, value in d.items():    print key, 'corresponds to', value

結果

>>>
y corresponds to 2
x corresponds to 1

 zip -- 可以將任意多個序列“壓縮”在一起,然后返回一個元組的列表,同時他可以應付不等長的序列,當最短的序列“用完”時就會停止,如

>>> zip(range(5), xrange(10000))
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]
>>> names=['a', 'b', 'c']
>>> ages = [45, 23 ,98]
>>> zip(names, ages)
[('a', 45), ('b', 23), ('c', 98)]

并行迭代,如

names=['a', 'b', 'c']ages = [45, 23 ,98]for name, age in zip(names, ages):    print name, 'is', age, 'old'

結果

>>>
a is 45 old
b is 23 old
c is 98 old

編號迭代 -- 迭代序列中的對象,同時還要獲取當前對象的索引,如

names=['Mr.a', 'Ms.b', 'Mr.c']for index, name in enumerate(names):    if 'Mr' in name:        names[index] = 'nan'for name in names:    print name,

結果
>>>
nan Ms.b nan

翻轉和排序迭代(sorted和reversed) -- 作用域任何序列或可迭代對象上,不是原地修改對象,而是返回翻轉或排序后的版本,但是返回對象不能直接對它使用索引、分片以及調用list方法,可以使用list類型轉換返回的對象,如

>>> sorted([4,3,8,6,3,])
[3, 3, 4, 6, 8]
>>> sorted('hello, world!')
[' ', '!', ',', 'd', 'e', 'h', 'l', 'l', 'l', 'o', 'o', 'r', 'w']
>>> list(reversed('hello, world!'))
['!', 'd', 'l', 'r', 'o', 'w', ' ', ',', 'o', 'l', 'l', 'e', 'h']
>>> ''.join(reversed('hello, world!'))
'!dlrow ,olleh'

break/continue -- 跳出循環/繼續下一輪循環

循環中的else子句 -- 如果循環中沒有調用break時,else子句執行,如

from math import sqrtfor n in range(99, 81, -1):    root = sqrt(n)    if root == int(root):        print n        breakelse :    print "Didn't dind it!"

結果

>>>
Didn't dind it!

  • 列表推導式--輕量級循環

列表推導式是利用其它列表創建新列表的一種方法,如

>>> [(x,y) for x in range(3) for y in range(3)]
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
>>> girls = ['alice', 'bernice', 'clarice']
>>> boys = ['chris', 'arnold', 'bob']
>>> [b+'+'+g for b in boys for g in girls if b[0] == g[0]]
['chris+clarice', 'arnold+alice', 'bob+bernice']

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 英德市| 沁源县| 永安市| 东至县| 巫溪县| 蚌埠市| 北安市| 饶河县| 精河县| 建平县| 云和县| 新宾| 霍林郭勒市| 浦东新区| 溧水县| 兰州市| 防城港市| 静宁县| 兴安县| 蕲春县| 邢台县| 太康县| 同仁县| 湖口县| 博白县| 渑池县| 宁乡县| 巫溪县| 黄石市| 平远县| 巴南区| 柳州市| 汝州市| 巨鹿县| 阳朔县| 铜川市| 柏乡县| 柏乡县| 永吉县| 永吉县| 焉耆|