Python中表達(dá)式和語(yǔ)句及for、while循環(huán)練習(xí)
1)表達(dá)式
常用的表達(dá)式操作符:
x + y, x - y
x * y, x / y, x // y, x % y
邏輯運(yùn)算:
x or y, x and y, not x
成員關(guān)系運(yùn)算:
x in y, x not in y
對(duì)象實(shí)例測(cè)試:
x is y, x not is y
比較運(yùn)算:
x < y, x > y, x <= y, x >= y, x == y, x != y
位運(yùn)算:
x | y, x & y, x ^ y, x << y, x >> y
一元運(yùn)算:
-x, +x, ~x:
冪運(yùn)算:
x ** y
索引和分片:
x[i], x[i:j], x[i:j:stride]
調(diào)用:
x(...)
取屬性:
x.attribute
元組:(...)
序列:[...]
字典:{...}
三元選擇表達(dá)式:x if y else z
匿名函數(shù):lambda args: expression
生成器函數(shù)發(fā)送協(xié)議:yield x
運(yùn)算優(yōu)先級(jí):
(...), [...], {...}
s[i], s[i:j]
s.attribute
s(...)
+x, -x, ~x
x ** y
*, /, //, %
+, -
<<, >>
&
^
|
<, <=, >, >=, ==, !=
is, not is
in, not in
not
and
or
lambda
2)語(yǔ)句:
賦值語(yǔ)句
調(diào)用
print: 打印對(duì)象
if/elif/else: 條件判斷
for/else: 序列迭代
while/else: 普通循環(huán)
pass: 占位符
break:
continue
def
return
yield
global: 命名空間
raise: 觸發(fā)異常
import:
from: 模塊屬性訪問(wèn)
class: 類
try/except/finally: 捕捉異常
del: 刪除引用
assert: 調(diào)試檢查
with/as: 環(huán)境管理器
賦值語(yǔ)句:
隱式賦值:import, from, def, class, for, 函數(shù)參數(shù)
元組和列表分解賦值:當(dāng)賦值符號(hào)(=)的左側(cè)為元組或列表時(shí),Python會(huì)按照位置把右邊的對(duì)象和左邊的目標(biāo)自左而右逐一進(jìn)行配對(duì)兒;個(gè)數(shù)不同時(shí)會(huì)觸發(fā)異常,此時(shí)可以切片的方式進(jìn)行;
多重目標(biāo)賦值
增強(qiáng)賦值: +=, -=, *=, /=, //=, %=,
3)for循環(huán)練習(xí)
練習(xí)1:逐一分開(kāi)顯示指定字典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
練習(xí)2:逐一顯示列表中l(wèi)1=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"]中的索引為奇數(shù)的元素;
>>> l1 = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"]
>>> for i in range(1,len(l1),2):
print l1[i]
Mon
Wed
Fri
練習(xí)3:將屬于列表l1=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],但不屬于列表l2=["Sun","Mon","Tue","Thu","Sat"]的所有元素定義為一個(gè)新列表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']
練習(xí)4:已知列表namelist=['stu1','stu2','stu3','stu4','stu5','stu6','stu7'],刪除列表removelist=['stu3', 'stu7', 'stu9'];請(qǐng)將屬于removelist列表中的每個(gè)元素從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循環(huán)練習(xí)
練習(xí)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
練習(xí)2:求100以內(nèi)所有偶數(shù)之和;
>>> 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
練習(xí)3:逐一顯示指定字典的所有鍵;并于顯示結(jié)束后說(shuō)明總鍵數(shù);
>>> d1 = {'x':1, 'y':23, 'z': 78}
>>> i1 = d1.keys()
>>> while i1:
print i1.pop(0)
else:
print len(d1)
x
y
z
3
練習(xí)4:創(chuàng)建一個(gè)包含了100以內(nèi)所有奇數(shù)的列表;
>>> 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]
練習(xí)5:列表l1=[0,1,2,3,4,5,6], 列表l2=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],以第一個(gè)列表中的元素為鍵,以第二個(gè)列表中的元素為值生成字典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
以上這篇python 表達(dá)式和語(yǔ)句及for、while循環(huán)練習(xí)實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

