Python中表達式和語句及for、while循環(huán)練習
1)表達式
常用的表達式操作符: x + y, x - y x * y, x / y, x // y, x % y 邏輯運算: x or y, x and y, not x 成員關(guān)系運算: 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] 調(diào)用: x(...) 取屬性: x.attribute 元組:(...) 序列:[...] 字典:{...} 三元選擇表達式:x if y else z 匿名函數(shù):lambda args: expression 生成器函數(shù)發(fā)送協(xié)議:yield x 運算優(yōu)先級: (...), [...], {...} s[i], s[i:j] s.attribute s(...) +x, -x, ~x x ** y *, /, //, % +, - <<, >> & ^ | <, <=, >, >=, ==, != is, not is in, not in not and or lambda
2)語句:
賦值語句 調(diào)用 print: 打印對象 if/elif/else: 條件判斷 for/else: 序列迭代 while/else: 普通循環(huán) pass: 占位符 break: continue def return yield global: 命名空間 raise: 觸發(fā)異常 import: from: 模塊屬性訪問 class: 類 try/except/finally: 捕捉異常 del: 刪除引用 assert: 調(diào)試檢查 with/as: 環(huán)境管理器 賦值語句: 隱式賦值:import, from, def, class, for, 函數(shù)參數(shù) 元組和列表分解賦值:當賦值符號(=)的左側(cè)為元組或列表時,Python會按照位置把右邊的對象和左邊的目標自左而右逐一進行配對兒;個數(shù)不同時會觸發(fā)異常,此時可以切片的方式進行; 多重目標賦值 增強賦值: +=, -=, *=, /=, //=, %=,
3)for循環(huán)練習
練習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:逐一顯示列表中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 練習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循環(huán)練習
練習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以內(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 練習3:逐一顯示指定字典的所有鍵;并于顯示結(jié)束后說明總鍵數(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 練習4:創(chuàng)建一個包含了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] 練習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
以上這篇python 表達式和語句及for、while循環(huán)練習實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
