# This is my shopping listshoplist=['apple','mango','carrot','banana']# 列表的初始化print('I have',len(shoplist),'items to purchase.')print('These items are:',end=' ')foriteminshoplist: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]delshoplist[0]# 删除列表元素print('I bought the',olditem)print('My shopping list is now',shoplist)
I have 4 items to purchase.
These items are: apple mango carrot banana
I also have to buy rice.
My shopping list is now ['apple', 'mango', 'carrot', 'banana', 'rice']
I will sort my list now
Sorted shopping list is ['apple', 'banana', 'carrot', 'mango', 'rice']
The first item I will buy is apple
I bought the apple
My shopping list is now ['banana', 'carrot', 'mango', 'rice']
zoo=('python','elephant','penguin')print('Number of animals in the zoo is',len(zoo))new_zoo='monkey','camel',zooprint('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]))
Number of animals in the zoo is 3
Number of cages in the new zoo is 3
All animals in new zoo are ('monkey', 'camel', ('python', 'elephant', 'penguin'))
Animals brought from old zoo are ('python', 'elephant', 'penguin')
Last animal brought from old zoo is penguin
Number of animals in the new zoo is 5
# ab = AdressBookab={'Swaroop':'swaroop@swaroopch.com','Larry':'larry@wall.org','Matsumoto':'matz@ruby-lang.org','Spammer':'spammer@hotmail.com'}print("Swaroop's address is",ab['Swaroop'])# 删除一对 键值-值delab['Spammer']print('\nThere are {} contacts in the address-book\n'.format(len(ab)))forname,addressinab.items():print('Contact {} at {}'.format(name,address))# 新增一对 键值-值ab['Guido']='guido@python.org'if'Guido'inab:print("\nGuido's address is",ab['Guido'])
Swaroop's address is swaroop@swaroopch.com
There are 3 contacts in the address-book
Contact Swaroop at swaroop@swaroopch.com
Contact Larry at larry@wall.org
Contact Matsumoto at matz@ruby-lang.org
Guido's address is guido@python.org
shoplist=['apple','mango','carrot','banana']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('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[:])
Item 0 is apple
Item 1 is mango
Item 2 is carrot
Item 3 is banana
Item -1 is banana
Item -2 is carrot
Character 0 is s
Item 1 to 3 is ['mango', 'carrot']
Item 2 to end is ['carrot', 'banana']
Item 1 to -1 is ['mango', 'carrot']
Item start to end is ['apple', 'mango', 'carrot', 'banana']
characters 1 to 3 is wa
characters 2 to end is aroop
characters 1 to -1 is waroo
characters start to end is swaroop
print('Simple Assignment')shoplist=['apple','mango','carrot','banana']# mylist仅仅是shoplist的另一个名字,它们指向的是同一个内容mylist=shoplistdelshoplist[0]print('shoplist is',shoplist)print('mylist is',mylist)# 删除了shoplist指向的第一项内容后,发现mylist的指向内容也是更改了的print('Copy by making a full slice')# 通过制作一份完整的切片来进行“深拷贝”mylist=shoplist[:]delmylist[0]print('shoplist is',shoplist)print('mylist is',mylist)# 此时的shoplist与mylist内容是不一样的
Simple Assignment
shoplist is ['mango', 'carrot', 'banana']
mylist is ['mango', 'carrot', 'banana']
Copy by making a full slice
shoplist is ['mango', 'carrot', 'banana']
mylist is ['carrot', 'banana']
name='Swaroop'ifname.startswith('Swa'):# 判断开头的字符串print('Yes, the string starts with "Swa"')if'a'inname:# 判断是否包含某些字符print('Yes, it contains the string "a"')ifname.find('war')!=-1:# 判断是否包含指定的字符串,如果找不到返回-1print('Yes, it contains the string "war"')delimiter='_*_'# 使用 _*_ 来连接序列中的项目mylist=['Brazil','Russia','India','China']print(delimiter.join(mylist))