Python中常見的內建資料型別有哪些?

王林
發布: 2023-08-20 22:57:13
轉載
1591 人瀏覽過

Python中常見的內建資料型別有哪些?

Python has various standard data types that are used to define the operations possible on them and the storage method for each of them.

數值類型

Python支援四種不同的數值類型 -

  • int − They are often called just integers or ints, are positive or negative whole numbers with no decimal point.

  • long − 也稱為longs,它們是無限大小的整數,以整數的形式書寫,並在後面跟著大寫或小寫的L。

  • float − Also called floats, they represent real numbers and are written with a decimal point dividing the integer and fractional parts. Floats may also be in scientific notation, with E or e indicating the power of 10 (2.5e2 = 2.5 x 102 = 250).

  • complex − 這些是形如 a bJ 的複數,其中 a 和 b 是浮點數,J(或 j)代表 -1 的平方根(即虛數)。此數的實部是 a,虛部是 b。在Python程式設計中,複數很少使用。

Example

的中文翻譯為:

範例

Let us see an example 

# Python int
val1 = 25
print(val1)

# Python float
val2 = 11.89
print(val2)

# Python complex
val3 = 6+2.9j
print(val3)

# Python hexadecimal
val4 = 0x12d
print(val4)

# Python octal
val5 = 0o021
print(val5)
登入後複製

輸出

25
11.89
(6+2.9j)
301
17
登入後複製

布林值

Example

的中文翻譯為:

範例

布林類型有兩個值,即True和False。 True代表1,False代表0。讓我們來看一個例子 -

a = (1 == True)
b = (1 == False)

print(a)
print(b)
登入後複製

輸出

True
False
登入後複製

Text Sequence Types – string

的中文翻譯為:

文字序列類型 - 字串

我們可以透過將字元用引號括起來來輕鬆建立一個字串。 Python將單引號視為雙引號的同義詞。建立字串就像將一個值賦給一個變數一樣簡單。

Let’s see how to easily create a String in Python −

myStr = Thisisit!'
登入後複製

Example

的中文翻譯為:

範例

我們現在將看到一個建立單行和多行字串的範例 

str1 = "John"
print(str1)

# Multi-line string
str2 = """ This,
is it!
"""
print(str2)
登入後複製

輸出

John
This,
is it!
登入後複製

列表

A list contains items separated by commas and enclosed within square brackets ([]). Creating a list is as simple as putting different comma-separated values between square brackting。 , we can also create a List with mixed data types.

The list can be written as a list of comma-separated values (items) between square brackets. Important thing about a list is that the items in a list need not be of the same type

##Create a Python List with Integer elements

We will create a list with 10 integer elements and display it. The elements are enclosed by square brackets. With that, we have also displayed the length of the list and how we can access specific

Example

的中文翻譯為:

範例

# Create a list with integer elements
mylist = [25, 40, 55, 60, 75, 90, 105, 130, 155, 180];

# Display the list
print("List = ",mylist)

# Display the length of the list
print("Length of the List = ",len(mylist))

# Fetch 1st element
print("1st element = ",mylist[0])

# Fetch last element
print("Last element = ",mylist[-1])
登入後複製

輸出

List =  [25, 40, 55, 60, 75, 90, 105, 130, 155, 180]
Length of the List =  10
1st element =  25
Last element =  180
登入後複製

Create a Python List with String elements

我們也可以將字串元素加入到Python列表中。我們將建立一個包含5個字串元素的清單並顯示它。元素被方括號括起來。透過這樣做,我們也顯示了清單的長度以及如何使用方括號存取第一個和最後一個元素−

Example

的中文翻譯為:

範例

# Create a list with string elements
mylist = ["BMW","Audi","Tesla","Honda","Toyota"];

# Display the list
print("List = ",mylist)

# Display the length of the list
print("Length of the List = ",len(mylist))

# Fetch 1st element
print("1st element = ",mylist[0])

# Fetch last element
print("Last element = ",mylist[-1])
登入後複製

輸出

List =  ['BMW', 'Audi', 'Tesla', 'Honda', 'Toyota']
Length of the List =  5
1st element =  BMW
Last element =  Toyota
登入後複製

Tuple

翻譯成中文為:

元組

元組是一系列不可變的Python物件。元組和列表一樣都是序列。元組和列表的主要區別在於元組是不可變的,而列表是可變的。元組使用圓括號,而列表使用方括號。

Let us first create a basic Tuple with integer elements and then move towards Tuples within a Tuple

 

#Example

的中文翻譯為:

範例

# Creating a Tuple
mytuple = (20, 40, 60, 80, 100)

# Displaying the Tuple
print("Tuple = ",mytuple)

# Length of the Tuple
print("Tuple Length= ",len(mytuple))
登入後複製

輸出

Tuple =  (20, 40, 60, 80, 100)
Tuple Length=  5
登入後複製

字典

Python的字典是一種雜湊表類型。它們的工作方式類似於Perl中的關聯數組或哈希,由鍵值對組成。創建Python字典的正確語法是以鍵:值對的形式儲存值。冒號的左邊儲存鍵,右邊儲存值,即

key:value
登入後複製

Dictionary is enclosed by curly bracket and do not allow duplicates. According to the 3.7 Python update, dictionaries are now ordered. Consider Dictionary as a set of key: value pairs, with the requirement that the keys are unique (within one dictionary). Each key in a Dictionary is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces.

We will create 4 key-value pairs, with keys Product, Model, Units and Available and values Mobile, XUT, 120 and Yes. Keys are on the left of colon, whereas values are on the right −

Example

的中文翻译为:

示例

# Creating a Dictionary with 4 key-value pairs
myprod = {
   "Product":"Mobile",
   "Model": "XUT",
   "Units": 120,
   "Available": "Yes"
}

# Displaying the Dictionary
print("Dictionary = \n",myprod)
登入後複製

输出

Dictionary = 
 {'Product': 'Mobile', 'Model': 'XUT', 'Units': 120, 'Available': 'Yes'}
登入後複製

以上是Python中常見的內建資料型別有哪些?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!