python数据类型怎么使用,Python数据类型用法详解

变量可以保存不同数据类型的值。 Python是一种动态类型的语言, 因此我们在声明变量时不必定义变量的类型。解释器隐式地将值与其类型绑定。

Python使我们能够检查程序中使用的变量的类型。 Python为我们提供了type()函数, 该函数返回传递的变量的类型。

考虑以下示例, 以定义不同数据类型的值并检查其类型。

A=10

b="Hi Python"

c = 10.5

print(type(a));

print(type(b));

print(type(c));

输出

标准数据类型

变量可以包含不同类型的值。例如, 一个人的姓名必须存储为字符串, 而其ID必须存储为整数。

Python提供了各种标准数据类型, 这些数据类型定义了每种数据类型的存储方法。 Python中定义的数据类型如下。

号码

String

list

tuple

字典

在本教程的这一部分中, 我们将对上述数据类型进行简要介绍。在本教程的后面, 我们将详细讨论其中的每一个。

号码

数字存储数值。将数字分配给变量后, Python会创建Number对象。例如;

a = 3 , b = 5 #a and b are number objects

Python支持4种类型的数字数据。

int(有符号整数, 如10、2、29等)

long(长整数, 用于更大范围的值, 例如908090800L, -0x1929292L等)

float(float用于存储1.9、9.902、15.2等浮点数)

复数(复数, 例如2.14j, 2.0 + 2.3j等)

Python允许我们使用小写L来与长整数一起使用。但是, 我们必须始终使用大写字母L以避免混淆。

复数包含一个有序对, 即x + iy, 其中x和y分别表示实部和虚部。

String

可以将字符串定义为引号中表示的字符序列。在python中, 我们可以使用单引号, 双引号或三引号来定义字符串。

python中的字符串处理是一项简单的任务, 因为提供了各种内置函数和运算符。

在字符串处理的情况下, 运算符+用于连接两个字符串, 因为操作” hello” +” python”返回” hello python”。

运算符*被称为重复运算符, 因为运算” Python” * 2返回” Python Python”。

以下示例说明了python中的字符串处理。

str1 = 'hello srcmini' #string str1

str2 = ' how are you' #string str2

print (str1[0:2]) #printing first two character using slice operator

print (str1[4]) #printing 4th character of the string

print (str1*2) #printing the string twice

print (str1 + str2) #printing the concatenation of str1 and str2

输出

he

o

hello srcminihello srcmini

hello srcmini how are you

list

列表类似于C中的数组。该列表可以包含不同类型的数据。列表中存储的项目用逗号(, )分隔, 并括在方括号[]中。

我们可以使用slice [:]运算符来访问列表中的数据。串联运算符(+)和重复运算符(*)使用列表的方式与处理字符串的方式相同。

考虑以下示例。

l = [1, "hi", "python", 2]

print (l[3:]);

print (l[0:2]);

print (l);

print (l + l);

print (l * 3);

输出

[2]

[1, 'hi']

[1, 'hi', 'python', 2]

[1, 'hi', 'python', 2, 1, 'hi', 'python', 2]

[1, 'hi', 'python', 2, 1, 'hi', 'python', 2, 1, 'hi', 'python', 2]

tuple

元组在很多方面都类似于列表。像列表一样, 元组也包含不同数据类型的项的集合。元组的项目用逗号(, )分隔并括在括号()中。

元组是只读的数据结构, 因为我们无法修改元组项目的大小和值。

让我们看一个简单的元组示例。

t = ("hi", "python", 2)

print (t[1:]);

print (t[0:1]);

print (t);

print (t + t);

print (t * 3);

print (type(t))

t[2] = "hi";

输出

('python', 2)

('hi', )

('hi', 'python', 2)

('hi', 'python', 2, 'hi', 'python', 2)

('hi', 'python', 2, 'hi', 'python', 2, 'hi', 'python', 2)

Traceback (most recent call last):

File "main.py", line 8, in

t[2] = "hi";

TypeError: 'tuple' object does not support item assignment

字典

字典是项的键值对的有序集合。就像一个关联数组或哈希表, 其中每个键都存储一个特定值。键可以保存任何原始数据类型, 而值是一个任意的Python对象。

字典中的项目用逗号分隔, 并括在大括号{}中。

考虑以下示例。

d = {1:'Jimmy', 2:'Alex', 3:'john', 4:'mike'};

print("1st name is "+d[1]);

print("2nd name is "+ d[4]);

print (d);

print (d.keys());

print (d.values());

输出

1st name is Jimmy

2nd name is mike

{1: 'Jimmy', 2: 'Alex', 3: 'john', 4: 'mike'}

[1, 2, 3, 4]

['Jimmy', 'Alex', 'john', 'mike']

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值