python基礎之資料類型

來源:互聯網
上載者:User

標籤:添加   不重複   spec   car   title   sort   空白   char   value   

python基礎之資料類型字串(string)
  • 用引號括起的都是字串,其中的引號可以是單引號, 也可以是雙引號

1.使用方法修改字串的大小寫

例:>>> name = "ada lovelace">>> print name.title()Ada Lovelace>>> print(name.upper())ADA LOVELACE

2.合并(拼接)字串

例:>>> first_name = "ada">>> last_name = "lovelace">>> full_name = first_name + " " + last_name>>> print full_nameada lovelace

3.使用定位字元或分行符號來添加空白

例:>>> print("Python")Python>>> print("\tPython")    Python>>> print("\nPython\nhello")Pythonhello>>> print("\nPython\n\thello")Python    hello

4.刪除首尾空白

例:>>> message = ‘ python ‘>>> message‘ python ‘>>> message.rstrip()‘ python‘>>> message.lstrip()‘python ‘>>> message.strip()‘python‘    
數字

1.整數(int)

2.浮點數(float)

3.長整型(long)

4.布爾型(bool)

5.複數型(complex)

列表(list)
  • 列表由一系列按特定順序排列的元素組成,用方括弧([])來表示列表,並用逗號來分隔其中的元素

訪問列表元素,索引從0而不是1開始

例:>>> bicycles = [‘trek‘, ‘cannondale‘, ‘redline‘, ‘specialized‘]>>> print bicycles[0]trek>>> print bicycles[1]cannondale>>> print bicycles[-1]specialized

修改、添加和刪除元素

例:>>> motorcycles = [‘honda‘, ‘yamaha‘, ‘suzuki‘]>>> print motorcycles[‘honda‘, ‘yamaha‘, ‘suzuki‘]修改      >>> motorcycles[0] = ‘ducati‘    >>> print motorcycles    [‘ducati‘, ‘yamaha‘, ‘suzuki‘]添加    >>> motorcycles.append(‘ducati‘)    >>> print motorcycles    [‘honda‘, ‘yamaha‘, ‘suzuki‘, ‘ducati‘]    append只是在末尾添加元素    在列表中插入元素用insert()    >>> motorcycles = [‘honda‘, ‘yamaha‘, ‘suzuki‘]    >>> motorcycles.insert(0, ‘ducati‘)    >>> print motorcycles    [‘ducati‘, ‘honda‘, ‘yamaha‘, ‘suzuki‘]刪除    del方法----使用del語句將值從列表中刪除後,你就無法再訪問它了         >>> del motorcycles[0]    >>> print motorcycles    [‘yamaha‘, ‘suzuki‘]    pop()方法    1.刪除列表的最後一個元素    >>> motorcycles.pop()    ‘suzuki‘    >>> print motorcycles    [‘honda‘, ‘yamaha‘]    2.指定元素刪除    >>> motorcycles.pop(0)    ‘honda‘    >>> print motorcycles    [‘yamaha‘, ‘suzuki‘]    remove()方法----刪除第一個指定的值

組織列表(排序)

1.使用方法sort()對列表進行永久性排序

例:>>> cars = [‘bmw‘, ‘audi‘, ‘toyota‘, ‘subaru‘]>>> cars.sort()>>> print cars[‘audi‘, ‘bmw‘, ‘subaru‘, ‘toyota‘]反向排序----sort()傳遞參數reverse=True>>> cars.sort(reverse=True)>>> print cars[‘toyota‘, ‘subaru‘, ‘bmw‘, ‘audi‘]

2.使用函數sorted()對列表進行臨時排序

  • 調用函數sorted()後,列表元素的排列順序並沒有變

3.倒著列印列表

例:>>> cars = [‘bmw‘, ‘audi‘, ‘toyota‘, ‘subaru‘]>>> cars.reverse()>>> print cars[‘subaru‘, ‘toyota‘, ‘audi‘, ‘bmw‘]

4.列表的長度

>>> cars = [‘bmw‘, ‘audi‘, ‘toyota‘, ‘subaru‘]>>> len(cars)4

巨集指令清單

1.遍曆整個列表

例:>>> cars = [‘bmw‘, ‘audi‘, ‘toyota‘, ‘subaru‘]>>> for car in cars:...     print car...bmwauditoyotasubaru

2.建立數值列表

① 使用函數range()

例:>>> for i in range(1,5):...     print i...1234

② 使用range()建立數字列表

例: >>> num = list(range(1,5))>>> print num[1, 2, 3, 4]

3.對數字列表執行簡單的統計

例:>>> digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]>>> min(digits)0>>> max(digits)9>>> sum(digits)45

使用列表的一部分

1.切片

例:>>> players = [‘charles‘, ‘martina‘, ‘michael‘, ‘florence‘, ‘eli‘]>>> print players[1:3][‘martina‘, ‘michael‘]

2.遍曆切片

例:>>> players = [‘charles‘, ‘martina‘, ‘michael‘, ‘florence‘, ‘eli‘]>>> for player in players[:3]:...     print player.title()...CharlesMartinaMichael
元組(tupe)
  • 不可變的列表被稱為元組
  • 元組看起來猶如列表,但使用圓括弧而不是方括弧來標識
  • 元組的運算與列表一樣

修改元組元素

元組中的元素值是不允許修改的,但我們可以對元組進行串連組合

例:>>> t = (12, 13, 14, 15, 21, 22 )>>> t1 = t[0:3]>>> t2 = (19,)>>> t3 = t[4:]>>> t = t1 + t2 + t3>>> print t(12, 13, 14, 19, 21, 22)

刪除元組元素

例:>>> t = (12, 13, 14, 15, 21, 22 )>>> t1 = t[0:3] + t[4:]>>> t = t1>>> print t(12, 13, 14, 21, 22)
字典(dict)
  • 字典是另一種可變容器模型,且可儲存任意類型對象
  • 字典的每個索引值(key=>value)對用冒號(:)分割,每個對之間用逗號(,)分割,整個字典包括在花括弧({})中

訪問字典

例:>>> dict = {‘Name‘: ‘Runoob‘, ‘Age‘: 7, ‘Class‘: ‘First‘}>>> print dict[‘Name‘]Runoob

修改字典

例:>>> dict = {‘Name‘: ‘Runoob‘, ‘Age‘: 7, ‘Class‘: ‘First‘}>>> print dict{‘Age‘: 7, ‘Name‘: ‘Runoob‘, ‘Class‘: ‘First‘}>>> dict[‘Age‘] = 11>>> print dict{‘Age‘: 11, ‘Name‘: ‘Runoob‘, ‘Class‘: ‘First‘}

刪除字典中的元素

例:>>> dict = {‘Name‘: ‘Runoob‘, ‘Age‘: 7, ‘Class‘: ‘First‘}>>> del dict[‘Name‘]>>> print dict{‘Age‘: 7, ‘Class‘: ‘First‘}
集合(set)
  • 無序不重複元素的序列
  • 準系統是進行成員關係測試和重複資料刪除元素
  • 可以使用大括弧({})或者 set()函數建立集合

注意:建立一個空集合必須用 set() 而不是{},因為{}是用來建立一個空字典

例:>>> student = {‘Tom‘, ‘Jim‘, ‘Mary‘, ‘Tom‘, ‘Jack‘, ‘Rose‘}>>> print studentset([‘Mary‘, ‘Rose‘, ‘Jim‘, ‘Jack‘, ‘Tom‘])

python基礎之資料類型

聯繫我們

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