PYTHON常见数据类型示例

时间:2023-03-08 17:12:15
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)

PYTHON常见数据类型示例