99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀python表達(dá)式和語句及for、while循環(huán)練習(xí)實(shí)例
python表達(dá)式和語句及for、while循環(huán)練習(xí)實(shí)例
2018-05-18
收藏

python表達(dá)式和語句及for、while循環(huán)練習(xí)實(shí)例

下面小編就為大家?guī)硪黄?a href='/map/python/' style='color:#000;font-size:inherit;'>python 表達(dá)式和語句及for、while循環(huán)練習(xí)實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。

Python中表達(dá)式和語句及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
 
對象實(shí)例測試:
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)先級:
(...), [...], {...}
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ù)
 
  元組和列表分解賦值:當(dāng)賦值符號(=)的左側(cè)為元組或列表時(shí),Python會(huì)按照位置把右邊的對象和左邊的目標(biāo)自左而右逐一進(jìn)行配對兒;個(gè)數(shù)不同時(shí)會(huì)觸發(fā)異常,此時(shí)可以切片的方式進(jìn)行;
 
  多重目標(biāo)賦值
 
  增強(qiáng)賦值: +=, -=, *=, /=, //=, %=,

3)for循環(huán)練習(xí)
    
練習(xí)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
   
  練習(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'];請將屬于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é)束后說明總鍵數(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á)式和語句及for、while循環(huán)練習(xí)實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考

數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }