重學Python,學python

來源:互聯網
上載者:User

重學Python,學python
元組

 

元組:特點就是內容不可變,算唯讀列表,可以被查詢,不能被修改

a = 2,print(a)print(type(a))b = ('a','b','c')print(b[1])

運行結果:

(2,)<class 'tuple'>b

 

字典

 

字典:內部資料列印時是無序的,索引值是雜湊的,有唯一性

#建立字典a = {'a':'python','b':'ruby','c':'java'}  #直接建立一個字典print(a)#內建字典方法建立:dicta = dict((('a','b'),))print(a)a = dict([['a','b'],])print(a)#修改字典a = {'a': 'b'}set1 = a.setdefault("c","java")print(a)print(set1)#查詢操作a = {'a':'python','b':'ruby','c':'java'}print(a.keys())print(a.values())print(a.items())print(a['a'])#update方法a = {'a':'python','b':'ruby','c':'java'}b = {'d':'shell'}a.update(b)print(a)a.update({'a':'javascript'})print(a)#刪除方法a = {'a':'python','b':'ruby','c':'java'}a.clear()print(a) # 清空字典a = {'a':'python','b':'ruby','c':'java'}del a['a']print(a)print("********************")a = {'a':'python','b':'ruby','c':'java'}b = a.pop('b')print(a)print(b)print("********************")a = {'a':'python','b':'ruby','c':'java'}b = a.popitem()print(a)print(b)a = {'a':'python','b':'ruby','c':'java'}del a  #整個字典被刪除print('其他字典方法')a = {'first':'python'}b = dict.fromkeys(['a','b'],['test'])print(a)print(b)print('字典的遍曆')a = {'a':'python','b':'ruby','c':'java'}for i,j in a.items():    print(i,j)for i in a:    #效率比上面的高    print(i,a[i])

 

字串

 只列舉幾個簡單的應用

print('join 字串合并方法')a = ['python','ruby','java']b = ''.join(a)print(b)c = '*'.join(a)print(c)print('字串常用方法')a = 'python py'print(a.count('p'))print(a.capitalize())print("format格式化輸出")a = "my favorite book {name} and {name2}"print(a.format_map({'name':'python','name2':'java'}))print(a.format(name = 'shell',name2 = 'bash'))print(a.isdigit())

 

 

PS:

前期的資料類型只是基礎,掌握了常用的那幾個內建函數,基礎的文法就可以,在學習函數,對象一些進階話題時,可以繼續鞏固,所以繼續加油吧

今天上班時看python時,被c++的開發鄙視了,說太簡單,唉,繼續加油吧

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.