1. 程式人生 > 其它 >Python3 內建資料型別分析

Python3 內建資料型別分析

parse inner datatype in python

背景

For computers, each data type can be thought of as being quite different, like words and numbers, so we will have to be careful about how we use them to assign values and how we manipulate(operate) them through operations.

對於計算機而言,像字元和數字,不同的資料型別會被以不同的方式進行解析。所以我們需要小心的對不同型別的資料進行賦值和進行操作


基本型別

Number

Any number you enter in Python will be interpreted as a number; you are not required to declare what kind of data type you are entering.

任何我們向終端輸入的數字都會被解釋為數字,我們不需要對數字型別資料進行宣告。(這種資料型別被隨機分配儲存空間,用完就找不到它的儲存位置)

Boolean

The Boolean data type can be one of two values, either True or False. Booleans are used to represent the truth values that are associated with the logic branch of mathematics, which informs algorithms in computer science.

布林型資料僅有兩個值:True or False. 布林值與被應用於電腦科學的邏輯代數相關,代表事件的真假

Sring

A string is a sequence of one or more characters (letters, numbers, symbols) that can be either a constant or a variable. Strings exist within either single quotes ' or double quotes " in Python . Strings are important for communicating information to the user, and for the user to communicate information back to the program.

字串可以是由一系列字元(字母、數字、符號)構成的常量或者變數. 通常字串被單引號或雙引號包裹。字串是使用者與計算機資訊溝通的載體,我們可以從字串中獲取計算機傳遞的資訊,也可以將使用者目標操作返回給計算機供給計算機去識別人的意圖。

字串可通過偏移進行索引,但無法進行賦值

小結

  • 數字源自於數學計算、布林型源自於邏輯判斷(運算)、字元型引申至字串源自於Comunicate(人與人的交流、人機互動等)
  • 基本型別,具有不變性的特徵,稱之為“基本”

資料結構

涉及到對基本型別進行組合排列等操作,區分於大部分資料中的六種基本資料型別

List

A list is a mutable, or changeable, ordered sequence of elements. Each element or value that is inside of a list is called an item. Just as strings are defined as characters between quotes, lists are defined by having values between square brackets [ ].

Lists are a very flexible data type because they are mutable in that they can have values added, removed, and changed

Tuple

A tuple is used for grouping data. It is an immutable, or unchangeable, ordered sequence of elements.
Tuples are very similar to lists, but they use parentheses ( ) instead of square brackets and because they are immutable their values cannot be modified.

Set

A set can be created by { } or set(). But a blank set must be constructed by set() not { }. Because { } is for a blank Dictionary.

Dictionary

The dictionary is Python’s built-in mapping type. This means that dictionaries map keys to values and these key-value pairs are a useful way to store data in Python. A dictionary is constructed with curly braces on either side { }.

不允許同一個鍵出現兩次. 建立時如果同一個鍵被賦值兩次,後一個值會被記住

  >>>dict = {'Name': 'Runoob', 'Age': 7, 'Name': '小菜鳥'}
   
  >>>print ("dict['Name']: ", dict['Name'])
  >>>dict['Name']:  小菜鳥

鍵必須不可變,所以可以用數字,字串或元組充當,而用列表就不行,值可以取任意資料型別

比較

List & Set & Dictionary

  • 可變資料,可進行增加、刪除、修改等操作

List & Set

List中的元素可以不相同,它支援數字、字串、甚至是List的巢狀.Set的數學中的特性:元素間的互異性——兩者的根本區別,即涉及到重複性元素的情況



  >>> {1,3,5,'six','six','a','a'}
  >>> {1, 3, 5, 'six', 'a'}   #Set資料型別

List & Tuple

Tuple是不可變資料結構,元組中的元素無法進行修改,即資料為靜態型別,而非動態型別

List & Dictionary

Dictionary採用鍵-值方式進行存取,而非通過偏移,即兩者的區別為資料存取方式. 列表是有序的物件集合,字典是無序物件的集合(字典相當於給列表的資料增加了索引,可以保證列表資料的任意性、重複性存在,但是鍵能夠提供唯一的訪問特定資料的入口,方便了索引)

小結

四種資料結構區別所隱含的邏輯——以List為中心

  • 靜態&動態-儲存方式
  • 重複性元素的過濾
  • 資料存取方式——有序與無序

Reference

Understanding Data Type in Python3
Python3基本資料型別