python学习-第六篇

"""
Version: 0.1
Author: freshbin
Date: 2019年8月20日
"""

print("=================================数据结构 start================================================")
# python内置的四种数据结构:列表、元组、字典和集合

print("=================================列表 start================================================")

shoplist = ['apple', 'mange', 'banana']

print('I have', len(shoplist), 'items to purchase.')
print('These items are:', end=' ')
for item in shoplist:
    print(item, end=' ')
print('\nI alse 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)

print("=================================列表 end================================================")





print("=================================元组 start================================================")

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]))

print("=================================元组 end================================================")





print("=================================字典 start================================================")

ab = {
    'one': 'one@one.com',
    'two': 'two@two.com',
    'three': 'three@three.com'
}

print('one address is', ab['one'])
del ab['one']
print('\n There are {} contacts in the address-book\n'.format(len(ab)))

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

ab['four'] = 'four@four.com'

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

print("=================================字典 end================================================")





print("=================================序列 start================================================")

shoplist = ['apple', 'mango', 'carrot', 'banana']
name = 'swaroop'
print('Item 0 is', shoplist[0])
print('Item -1 is', shoplist[-1])
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('characters 1 to 3 is', name[1:3])
print('characters 2 to end is', name[2:])
print('characters 1 to -1 is', name[1:-1])
print('characters start to end is', name[:])
print('characters -1 to -3 is', name[-1:-3])#
print('characters -1 to 3 is', name[-1:3])#
print('characters -1 to 6 is', name[-1:6])#
print('characters 6 to -1 is', name[6:-1])#

# 步长
print(shoplist[::1])
print(shoplist[::2])
print(shoplist[::3])
print(shoplist[::-1])

print("=================================序列 end================================================")





print("=================================集合 start================================================")

bri = set(['brazil', 'russia', 'india'])
print('india' in bri)
print('usa' in bri)
bric = bri.copy()
bric.add('china')
print(bric.issuperset(bri))
bri.remove('russia')
print(bri & bric)
print(bric)

print("======================")
#引用
print('Simple Assignment')
shoplist = ['apple', 'mango', 'carrot', 'banana']
mylist = shoplist
del shoplist[0]
del mylist[1]
print('shoplist is', shoplist)
print('mylist is', mylist)
print('Copy by making a full slice')
mylist = shoplist[:]
del mylist[0]
print('shoplist is', shoplist)
print('mylist is', mylist)

#
name = 'Swaroop'
if name.startswith('Swa'):
    print('Yes')
if 'a' in name:
    print('Yes')
if name.find('war') != -1:
    print('Yes')
delimiter = '_*_'
mylist = ['Brazil', 'Russia', 'India', 'China']
print(delimiter.join(mylist))

print("=================================集合 end================================================")


print("=================================数据结构 end================================================")

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值