Python中表達式和語句及for、while迴圈練習 1)表達式 常用的表達式操作符: x + y, x y x y, x / y, x // y, x % y 邏輯運算: x or y, x and y, not x 成員關係運算: x in y, x not in y 對象實例測試: x is ...
Python中表達式和語句及for、while迴圈練習
1)表達式
常用的表達式操作符:
x + y, x - y
x * y, x / y, x // y, x % y
邏輯運算:
x or y, x and y, not x
成員關係運算:
x in y, x not in y
對象實例測試:
x is y, x not is y
比較運算:
x < y, x > y, x <= y, x >= y, x == y, x != y
位運算:
x | y, x & y, x ^ y, x << y, x >> y
一元運算:
-x, +x, ~x:
冪運算:
x ** y
索引和分片:
x[i], x[i:j], x[i:j:stride]
調用:
x(...)
取屬性:
x.attribute
元組:(...)
序列:[...]
字典:{...}
三元選擇表達式:x if y else z
匿名函數:lambda args: expression
生成器函數發送協議:yield x
運算優先順序:
(...), [...], {...}
s[i], s[i:j]
s.attribute
s(...)
+x, -x, ~x
x ** y
*, /, //, %
+, -
<<, >>
&
^
|
<, <=, >, >=, ==, !=
is, not is
in, not in
not
and
or
lambda
2)語句:
賦值語句
調用
print: 列印對象
if/elif/else: 條件判斷
for/else: 序列迭代
while/else: 普通迴圈
pass: 占位符
break:
continue
def
return
yield
global: 命名空間
raise: 觸發異常
import:
from: 模塊屬性訪問
class: 類
try/except/finally: 捕捉異常
del: 刪除引用
assert: 調試檢查
with/as: 環境管理器
賦值語句:
隱式賦值:import, from, def, class, for, 函數參數
元組和列表分解賦值:當賦值符號(=)的左側為元組或列表時,Python會按照位置把右邊的對象和左邊的目標自左而右逐一進行配對兒;個數不同時會觸發異常,此時可以切片的方式進行;
多重目標賦值
增強賦值: +=, -=, *=, /=, //=, %=,
3)for迴圈練習
練習1:逐一分開顯示指定字典d1中的所有元素,類似如下
k1 v1
k2 v2
...
>>> d1 = { 'x':1,'y':2,'z':3,'m':4 }
>>> for (k,v) in d1.items():
print k,v
y 2
x 1
z 3
m 4
練習2:逐一顯示列表中l1=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"]中的索引為奇數的元素;
>>> l1 = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"]
>>> for i in range(1,len(l1),2):
print l1[i]
Mon
Wed
Fri
練習3:將屬於列表l1=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],但不屬於列表l2=["Sun","Mon","Tue","Thu","Sat"]的所有元素定義為一個新列表l3;
>>> l1 = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"]
>>> l2 = ["Sun","Mon","Tue","Thu","Sat"]
>>> l3 = [ ]
>>> for i in l1:
if i not in l2:
l3.append(i)
>>> l3
['Wed', 'Fri']
練習4:已知列表namelist=['stu1','stu2','stu3','stu4','stu5','stu6','stu7'],刪除列表removelist=['stu3', 'stu7', 'stu9'];請將屬於removelist列表中的每個元素從namelist中移除(屬於removelist,但不屬於namelist的忽略即可);
>>> namelist= ['stu1','stu2','stu3','stu4','stu5','stu6','stu7']
>>> removelist = ['stu3', 'stu7', 'stu9']
>>> for i in namelist:
if i in removelist :
namelist.remove(i)
>>> namelist
['stu1', 'stu2', 'stu4', 'stu5', 'stu6']
4)while迴圈練習
練習1:逐一顯示指定列表中的所有元素;
>>> l1 = [1,2,3,4,5]
>>> i = 0
>>> while i < len(l1)
print l1[i]
i += 1
1
2
3
4
5
>>> l1 = [1,2,3,4,5]
>>> while l1:
print l1.pop(0)
1
2
3
4
5
練習2:求100以內所有偶數之和;
>>> i = 0
>>> sum = 0
>>> while i < 101:
sum += i
i += 2
print sum
2550
>>> for i in range(0,101,2):
sum+=i
print sum
2550
練習3:逐一顯示指定字典的所有鍵;並於顯示結束後說明總鍵數;
>>> d1 = {'x':1, 'y':23, 'z': 78}
>>> i1 = d1.keys()
>>> while i1:
print i1.pop(0)
else:
print len(d1)
x
y
z
3
練習4:創建一個包含了100以內所有奇數的列表;
>>> d1 = [ ]
>>> i = 1
>>> while i < 101:
d1.append(i)
i+=2
>>> print d1
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99]
>>> d1 = [ ]
>>> for i in range(1,101,2)
d1.append(i)
>>> print d1
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99]
練習5:列表l1=[0,1,2,3,4,5,6], 列表l2=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],以第一個列表中的元素為鍵,以第二個列表中的元素為值生成字典d1;
>>> l1 = [0,1,2,3,4,5,6]
>>> l2 = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"]
>>> d1 = {}
>>> count = 0
>>> if len(l1) == len(l2):
while count < len(l1):
d1[l1[count]] = l2[count]
count += 1