python中指定变量为1byte_Python 设置变量的数据类型

本文介绍了在Python中如何指定变量的数据类型,包括直接赋值和使用构造函数两种方式,并给出了各种常见数据类型的示例,如字符串、整型、浮点型、复数、列表、元组等。同时,文章通过购物清单的示例展示了列表操作,如添加、排序、删除元素。此外,还探讨了集合和字典的操作,如合并、查找、删除元素。
摘要由CSDN通过智能技术生成

1、指定变量数据类型

在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

2、通过构造函数指定数据类型

如果要指定数据类型,则可以使用以下构造函数:示例数据类型

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

3、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)

相关文档:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值