Python 数据类型

简介:
本文主要介绍Python中的数据类型,包括内置数据类型、获取数据类型、设置数据类型、设置特定的数据类型,以及相关的示例代码
1、内置数据类型
使用变量存储数据时,为了更充分利用内存空间,我们可以为变量指定不同的数据类型。并且不同类型可以执行不同的操作。Python默认具有以下内置数据类型:

字符串类型str

数值类型: intfloatcomplex

序列类型: listtuplerange

映射类型: dict

集合类型: setfrozenset

布尔值类型: bool

二进制类型: bytesbytearraymemoryview

2、使用type()获取变量的数据类型
可以使用以下type()函数获取任何对象的数据类型:

例子,

打印输出变量x的数据类型:

x = 5
print(type(x))

以下是Python中所有内置数据类型:

例如:

print(type("Hello"))
print(type(3))
print(type(3.14))
print(type(1j))
print(type(["c", "java", "python"]))
print(type(("c", "java", "python")))
print(type(range(6)))
print(type({"name" : "python", "age" : 12}))
print(type({"c", "java", "python"}))
print(type(frozenset({"c", "java", "python"})))
print(type(True))
print(type(b"Hello"))
print(type(bytearray(5)))
print(type(memoryview(bytes(5))))

3、指定变量数据类型
在Python中,当将值分配给变量时,将设置数据类型:

示例数据类型
x = “Hello World”str
x = 20int
x = 20.5float
x = 1jcomplex
x = [“c”, “python”, “java”]list
x = (“c”, “python”, “java”)tuple
x = range(6)range
x = {“name” : “python”, “age” : 20}dict
x = {“c”, “python”, “java”}set
x = frozenset({“c”, “python”, “java”})frozenset
x = Truebool
x = b"Hello"bytes
x = bytearray(5)bytearray
x = memoryview(bytes(5))memoryview

4、通过构造函数指定数据类型
如果要指定数据类型,则可以使用以下构造函数:

示例数据类型
x = str(“Hello World”)str
x = int(20)int
x = float(20.5)float
x = complex(1j)complex
x = list((“c”, “python”, “java”))list
x = tuple((“c”, “python”, “java”))tuple
x = range(6)range
x = dict(name=“cjavapy”, age=3)dict
x = set((“c”, “java”, “python”))set
x = frozenset((“c”, “python”, “java”))frozenset
x = bool(5)bool
x = bytes(5)bytes
x = bytearray(5)bytearray
x = memoryview(bytes(5))memoryview

5、Python常用数据类型示例

shoplist = ['apple', 'mango', 'carrot', 'banana']

print('I have ', len(shoplist), ' items to purchase.')
print('These items are: ', end = '')
for  item in shoplist:
    print(item, end = ' ')

print('\nI also have to buy rice.')
shoplist.append('rice')
print('My shopping list is now ', shoplist)

print('I will sort my list now')
shoplist.sort()
print('Sorted shopping list is ', shoplist)

print('The first item I will buy is ', shoplist[0])
olditem = shoplist[0]
del shoplist[0]
print('I bought the ', olditem)
print('My shopping list is now ', shoplist)

zoo = ('python', 'elephant', 'penguin')
print('Number of animals in the zoo is ', len(zoo))

new_zoo = 'monkey', 'camel', zoo
print('Number of cages in the new zoo is ', len(new_zoo))
print('All animals in new zoo are ',new_zoo)
print('Animals brought from old zoo are', new_zoo[2])
print('Last animal brought from old zoo is ', new_zoo[2][2])
print('Number of animals in the new zoo is ', len(new_zoo) - 1 + len(new_zoo[2]))

ab = {  'Swaroop'    : 'Swaroop@Swaroopch.com',
        'Larry'        : 'larry@wall.org',
        'Matsumoto'    : 'matz@ruby-lang.org',
        'Spammer'    : 'spammer@hotmail.com'
}

print("Swaroop's address is ", ab['Swaroop'])

del ab['Spammer']

print('\nThere are {0} contacts in the address-book\n'.format(len(ab)))

for name, address in ab.items():
    print('Contatc {0} at {1}'.format(name, address))

ab['Guido'] = 'guido@python.org'

if 'Guido' in ab:
    print("\nGuido's address is ", ab['Guido'])

name = 'Swaroop'

print('Item 0 is ', shoplist[0])
print('Item 1 is ', shoplist[1])
print('Item 2 is ', shoplist[2])
print('Item 3 is ', shoplist[3])
print('Item -1 is ', shoplist[-1])
print('Item -2 is ', shoplist[-2])
print('Character 0 is ', name[0])

print('Item 1 to 3 is ',shoplist[1:3])
print('Item 2 to end is ',shoplist[2:])
print('Item 1 to -1 is ',shoplist[1:-1])
print('Item start to end is ',shoplist[:])

print('Character 1 to 3 is ', name[1:3])
print('Character 2 to end is ', name[2:])
print('Character 1 to -1 is ', name[1:-1])
print('Character start to end is ', name[:])

bri = set(['brazil', 'russia', 'India', 'China'])
print('India' in bri)

了解更多分析及数据抓取可查看:
http://cloud.yisurvey.com:9081/html/bd9fb365-ae11-4182-84d0-af28197bac71.html?ly=csdn
本文转载自互联网、仅供学习交流,内容版权归原作者所有,如涉作品、版权和其他问题请联系我们删除处理。
特别说明:本文旨在技术交流,请勿将涉及的技术用于非法用途,否则一切后果自负。如果您觉得我们侵犯了您的合法权益,请联系我们予以处理。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值