Python中常见的内置数据类型有哪些?

王林
풀어 주다: 2023-08-20 22:57:13
앞으로
1535명이 탐색했습니다.

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 brackets. A list can have integer, string or float elements. With that, 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 elements using the square brackets

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 학습자의 빠른 성장을 도와주세요!