python2學習------基礎文法

來源:互聯網
上載者:User

標籤:span   對象   str   list   --   pre   ase   spl   number   

1、變數類型

Numbers(數字):int,float,longString(字串)List(列表)tuple(元組)dict(字典)bool(布爾):True,False

# 刪除變數 
del 變數名;

 

2、常用函數

<1> 輸出資訊       print 輸出資訊;<2> 互動       raw_input(‘請輸入內容‘); <3> 類型轉換       int(x [,base])            將x轉換為一個整數       long(x [,base] )          將x轉換為一個長整數       float(x)                  將x轉換到一個浮點數       complex(real [,imag])    建立一個複數       str(x)                     將對象 x 轉換為字串       repr(x)                     將對象 x 轉換為運算式字串       eval(str)                 用來計算在字串中的有效Python運算式,並返回一個對象       tuple(s)                 將序列 s 轉換為一個元組       list(s)                    將序列 s 轉換為一個列表       set(s)                     轉換為可變集合       dict(d)                     建立一個字典。d 必須是一個序列 (key,value)元組       frozenset(s)             轉換為不可變集合       chr(x)                     將一個整數轉換為一個字元       unichr(x)                將一個整數轉換為Unicode字元       ord(x)                     將一個字元轉換為它的整數值       hex(x)                     將一個整數轉換為一個十六進位字串       oct(x)                    將一個整數轉換為一個八進位字串<4> 查看變數類型       type(變數名)<5> 查看與變數相關的函數       dir(變數名)<6> 查看變數某個函數的具體使用方式       help(變數名.函數名)

 

3、注釋

<1> #        # 注釋內容<2> ‘‘‘       ‘‘‘        注釋內容1        注釋內容2        ...       ‘‘‘<3> """       """        注釋內容1        注釋內容2        ...       """

 

4、字串常用操作

<1> 長度      a=‘test‘;      print a.__len__(); #4<2>擷取子串      a=‘test‘;      print a[0:1]; # t      print a[0:2]; # te      print a[0:3]; # tes      print a[0:4]; # test      print a[0:5]; # test      print a[:4]; # test      print a[1:]; # est      print a[1:3]; # es
print a[-1:]; # t
print a[-2:]; # st
print a[-3:]; # est
print a[-4:]; # test
print a[-3:-1]; # es
print a[-2:-1]; # s<3>判斷是否存在某個字元以及出現的次數(大於0則存在) a=‘test‘; print a.count(‘t‘);# 2<4>重複擴充字元串 a=‘test‘; a=a*2; print a; # testtest<5>分割字串 a=‘test,測試‘; a=a.split(‘,‘); print a; # [‘test‘, ‘\xb2\xe2\xca\xd4‘] type(a); # <type ‘list‘><6>替換字串 a=‘testtesttest‘; a.replace(‘test‘,‘hello‘); # hellohellohello a.replace(‘test‘,‘hello‘,1);# hellotesttest a.replace(‘test‘,‘hello‘,2);# hellohellotest a.replace(‘test‘,‘hello‘,3);# hellohellohello<7>轉換為位元組數組 a="abcabc"; a=list(a);#不去重 [‘a‘,‘b‘,‘c‘,‘a‘,‘b‘,‘c‘] a=set(a);#去重[‘a‘,‘b‘,‘c‘]
<8>轉義特殊標識(如換行標識)
a=‘hello \n python‘;
b=r"hello \n python";
print a;# 分兩行顯示
print b;# hello \n python

 

5、字典常用操作

<1> 擷取所有key值    a={‘name‘:‘lxh‘,‘nation‘:‘China‘};    print a.keys(); # [‘name‘,‘nation‘]<2> 擷取所有value值    a={‘name‘:‘lxh‘,‘nation‘:‘China‘};    print a.values(); # [‘name‘,‘nation‘] 
<3> 擷取指定key值
a={‘name‘:‘lxh‘,‘nation‘:‘China‘};
print a[‘name‘]; # lxh

 

6、待完善

python2學習------基礎文法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.