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

熱線電話:13121318867

登錄
首頁精彩閱讀Python入門篇之編程習慣與特點
Python入門篇之編程習慣與特點
2017-08-18
收藏

Python入門篇之編程習慣與特點

1.代碼風格

Python中,每行程序以換行符代表結束,如果一行程序太長的話,可以用“\”符號擴展到下一行。在python中以三引號(""")括起來的字符串,列表,元組和字典都能跨行使用。并且以小括號(...)、中括號[...]和大括號{...}包圍的代碼不用加“\”符也可擴展到多行。

在Python中是以縮進來區(qū)分程序功能塊的,縮進的長度不受限制,但就一個功能塊來講,最好保持一致的縮進量。

如果一行中有多條語句,語句間要以分號(;)分隔。

以“#”號開頭的內容為注釋,python解釋器會忽略該行內容。

python中,所有標識符可以包括英文、數(shù)字以及下劃線(_),但不能以數(shù)字開頭。python中的標識符是區(qū)分大小寫的。

以下劃線開頭的標識符是有特殊意義的。以單下劃線開頭(_foo)的代表不能直接訪問的類屬性,需通過類提供的接口進行訪問,不能用“from xxx import *”而導入;以雙下劃線開頭的(__foo)代表類的私有成員;以雙下劃線開頭和結尾的(__foo__)代表python里特殊方法專用的標識,如__init__()代表類的構造函數(shù)。

在交互模式下運行python時,一個下劃線字符(_)是特殊標識符,它保留了表達式的最后一個計算結果。

代碼如下:
>>> "hello"
'hello'
>>> _
'hello'
>>> 10+10
20
>>> _
20

python中,函數(shù)、類、模塊定義的第一段代碼如果是字符串的話,就把它叫作文件字串,可通過__doc__屬性訪問。如:

代碼如下:
def test():
"this is a document string"

return 100+1000

>>>print test.__doc__
this is a document string

2.保留字

代碼如下:
 and  elif  global  or          yield
 assert  else  if  pass
 break  except  import  print
 class  exec  in  raise
 continue finally  is  return
 def  for  lambda  try
 del  from  not  while

3.Python運算符和表達式

Python運算符列表

運算符優(yōu)先順序列表(從最高到最低)

真值表

復合表達式

對于and,當計算a and b時,python會計算a,如果a為假,則取a值,如果a為真,則python會計算b且整個表達式會取b值。如:

代碼如下:
>>> a,b=10,20
>>> a and b   #a is true
20
>>> a,b=0,5   #a is false
>>> a and b
0

對于or,當計算a or b時,python會計算a,如果a為真,則整個表達式取a值,如果a為假,表達式將取b值。如:

代碼如下:
>>> a,b=10,20
>>> a or b
10
>>> a,b=0,5
>>> a or b
5

對于not,not將反轉表表達式的“實際值”,如果表達式為真,not為返回假,如為表達式為假,not為返回真。如:

代碼如下:
>>> not 2
False
>>> not 0
True
>>> not "test"
False
>>> not ""
True

4.給變量賦值

變量賦值:賦值并不是直接將一個值賦給一個變量,在Python中,對象是通過引用傳遞的。Python的賦值語句不會返回值,類似下面的語句是非法的:

代碼如下:
>>> x=1
>>> y=(x=x+1)
SyntaxError: invalid syntax

鏈式賦值沒有問題:

代碼如下:
>>> x=1
>>> y=x=x+1
>>> x,y
(2, 2)

Python不支持類似x++或--x這樣的前置/后置自增/自減運算

可以多重復值:

代碼如下:
>>> x=y=z=1
>>> x
1
>>> y
1
>>> z
1

可以多元賦值:

代碼如下:
>>> x,y,z=1,2,'a string'
>>> x
1
>>> y
2
>>> z
'a string'

通常使用小括號括起來,增強代碼的可讀性:

代碼如下:

>>> (x,y,z)=(1,2,'a string')
>>> x
1
>>> y
2
>>> z
'a string'

使用Python的多元賦值方式可以實現(xiàn)無中間變量交換兩變量的值:

代碼如下:

>>> #swapping variables in Python
>>> x,y=1,2
>>> x
1
>>> y
2
>>> x,y=y,x
>>> x
2
>>> y
1

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

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

數(shù)據(jù)分析師考試動態(tài)
數(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(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調,回調的第一個參數(shù)驗證碼對象,之后可以使用它調用相應的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".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); }