Python数据类型及操作

本文详细介绍了Python中的几种基本数据类型,包括列表、元组和字典等,并通过示例展示了如何使用这些数据类型进行常见操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



Python列表和操作符

列表List是Python最通用的数据类型,一个列表包含的项用逗号分开,用两个方括号[ ]括起来。在一某些方面,List有点向C语言的数组,一个不同的地方是:List包含的项可以是不同的数据类型。

存储在List中的值可以使用分片操作符([ ] [:])利用索引Index来访问,索引的值从列表的起始位置0开始,结束标志是-1。+号是列表连接符,星号*是重复操作符。

举例:

#!/usr/bin/python


list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]

tinylist = [123, 'john']


print list          # Prints complete list

print list[0]       # Prints first element of the list

print list[1:3]     # Prints elements starting from 2nd till 3rd 

print list[2:]      # Prints elements starting from 3rd element

print tinylist * 2  # Prints list two times

print list + tinylist # Prints concatenated lists

执行结果如下:

['abcd', 786, 2.23, 'john', 70.200000000000003]

abcd

[786, 2.23]

[2.23, 'john', 70.200000000000003]

[123, 'john', 123, 'john']

['abcd', 786, 2.23, 'john', 70.200000000000003, 123, 'John']


Python元组和操作符
元组Tuples是与列表类似的另一种序列数据类型。元组由若干个由逗号分隔的值组成。然而,与列表不同的是,元组被括在括号中。
列表和元组之间的主要区别是:列表被括在括号([])中,它们的元素和大小可以更改,而元组则被括在圆括号(())中,不能更新。元组可以被视为只读列表。

举例:

#!/usr/bin/python

tuple = ( 'abcd', 786 , 2.23, 'john', 70.2  )

tinytuple = (123, 'john')

print tuple           # Prints complete list

print tuple[0]        # Prints first element of the list

print tuple[1:3]      # Prints elements starting from 2nd till 3rd 

print tuple[2:]       # Prints elements starting from 3rd element

print tinytuple * 2   # Prints list two times

print tuple + tinytuple # Prints concatenated lists

执行结果如下:

('abcd', 786, 2.23, 'john', 70.200000000000003)

abcd

(786, 2.23)

(2.23, 'john', 70.200000000000003)

(123, 'john', 123, 'john')

('abcd', 786, 2.23, 'john', 70.200000000000003, 123, 'John')

下来代码对元组来说是无效的,因为我们试图更新元组,这是不允许的。而对于列表来说,是允许的。


#!/usr/bin/python

tuple = ( 'abcd', 786 , 2.23, 'john', 70.2  )

list = [ 'abcd', 786 , 2.23, 'john', 70.2  ]

tuple[2] = 1000    # Invalid syntax with tuple

list[2] = 1000     # Valid syntax with list

Python 字典

Python的字典Dictionary是一种哈希表类型。他们的工作像关联数组,或Perl语言中的含键-值对的散列。字典键几乎可以是任何Python类型,但通常是数字或字符串。另一方面,值可以是任意的Python对象。
字典由大括号({ })括起来,可以使用方括号([])分配和访问值。

例如:

#!/usr/bin/python

dict = {}

dict['one'] = "This is one"

dict[2]     = "This is two"

tinydict = {'name': 'john','code':6734, 'dept': 'sales'}

print dict['one']       # Prints value for 'one' key

print dict[2]           # Prints value for 2 key

print tinydict          # Prints complete dictionary

print tinydict.keys()   # Prints all the keys

print tinydict.values() # Prints all the values

执行结果如下:

This is one

This is two

{'dept': 'sales', 'code': 6734, 'name': 'john'}

['dept', 'code', 'name']

['sales', 6734, 'john']

字典中没有元素顺序的概念。说元素是“无序的”是不正确的,它们只是无序的

数据类型转换
有时,您可能需要在内置类型之间执行转换。要在类型之间转换,只需使用类型名作为函数。
有几个内置函数可以执行从一种数据类型到另一种数据类型的转换。这些函数返回一个表示转换值的新对象。

Sr.No.

Function & Description

1

int(x [,base])

Converts x to an integer. base specifies the base if x is a string.

2

long(x [,base] )

Converts x to a long integer. base specifies the base if x is a string.

3

float(x)

Converts x to a floating-point number.

4

complex(real [,imag])

Creates a complex number.

5

str(x)

Converts object x to a string representation.

6

repr(x)

Converts object x to an expression string.

7

eval(str)

Evaluates a string and returns an object.

8

tuple(s)

Converts s to a tuple.

9

list(s)

Converts s to a list.

10

set(s)

Converts s to a set.

11

dict(d)

Creates a dictionary. d must be a sequence of (key,value) tuples.

12

frozenset(s)

Converts s to a frozen set.

13

chr(x)

Converts an integer to a character.

14

unichr(x)

Converts an integer to a Unicode character.

15

ord(x)

Converts a single character to its integer value.

16

hex(x)

Converts an integer to a hexadecimal string.

17

oct(x)

Converts an integer to an octal string.





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值