python学习笔记——4_数据结构

#coding:UTF-8
#列表开始
#help(list)

shopList=['apple','mango','carrot','banana']
print 'I have',len(shopList),'items to purchase'

print 'These items are:'
for item in shopList:
    print item

print '\n I also have to buy rice.'
shopList.append('rice')
print'My shopping list is now',shopList

print '\nI will sort my list now'
shopList.sort()
print 'Sorted shopping list is',shopList

print '\nI would not want to buy apple'
del shopList[0]
print 'At last,my shopping list is',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']

I would not want to buy apple
At last,my shopping list is ['banana', 'carrot', 'mango', 'rice']
'''

#列表结束

#==================================================

#元组开始

'''
元组和列表十分类似,只不过元组和字符串一样是 不可变的 即你不能修改元组。
元组通过圆括号中用逗号分割的项目定义。
元组通常用在使语句或用户定义的函数能够安全地采用一组值的时候,即被使用的元组的值不会改变。
'''
zoo=('python','wolf','tiger')
print 'Number of animals in the zoo is',len(zoo)

new_zoo=('monkey','dolphin',zoo)
print 'Number of animals int the new_zoo is',len(new_zoo)

print 'All the animals in the new_zoo is',new_zoo
print 'Animals brought from old zoo are',new_zoo[2]
print 'Last animal brought from old zoo is',new_zoo[2][2]

'''
结果
Number of animals in the zoo is 3
Number of animals int the new_zoo is 3
All the animals in the new_zoo is ('monkey', 'dolphin', ('python', 'wolf', 'tiger'))
Animals brought from old zoo are ('python', 'wolf', 'tiger')
Last animal brought from old zoo is tiger
'''

'''
含有0个或1个项目的元组。一个空的元组由一对空的圆括号组成,如myempty = ()。
然而,含有单个元素的元组就不那么简单了。
你必须在第一个(唯一一个)项目后跟一个逗号,这样Python才能区分元组
和表达式中一个带圆括号的对象。
即如果你想要的是一个包含项目2的元组的时候,你应该指明singleton = (2 , )。
'''


'''
print语句可以使用跟着%符号的项目元组的字符串。
这些字符串具备定制的功能。定制让输出满足某种特定的格式。
定制可以是%s表示字符串或%d表示整数。
print的这个用法使得编写输出变得极其简单,它避免了许多字符串操作。
它也避免了我们一直以来使用的逗号。
在第二个print语句中,我们使用了一个定制,后面跟着%符号后的单个项目——没有圆括号。
这只在字符串中只有一个定制的时候有效。
'''
age=22
name="sky"

print '%s is %d years old'%(name,age)
print 'Why is %s playing with that python?'%name
print '\n'
'''
结果
sky is 22 years old
Why is sky playing with that python?
'''
#元组结束

#=====================================================

#字典开始
#help(dict)

ab={u'小李':'[email protected]',
    u'小红':'[email protected]',
    u'小黑':'[email protected]',
    u'小王':'[email protected]'
    }
print ab
print u'小王的邮箱为%s'% ab[u'小王']
ab[u'小赵']='[email protected]'
print ab
del ab[u'小李']
print ab
for name,address in ab.items():
    print u'%s的地址为%s' % (name,address)
if u'小黑' in ab:#or ab.has_key(u'小黑')
    print u'小黑的地址为%s'% ab[u'小黑']
print '\n'
'''
结果
{u'\u5c0f\u674e': '[email protected]', u'\u5c0f\u738b': '[email protected]', u'\u5c0f\u7ea2': '[email protected]', u'\u5c0f\u9ed1': '[email protected]'}
小王的邮箱为[email protected]
{u'\u5c0f\u674e': '[email protected]', u'\u5c0f\u8d75': '[email protected]', u'\u5c0f\u738b': '[email protected]', u'\u5c0f\u7ea2': '[email protected]', u'\u5c0f\u9ed1': '[email protected]'}
{u'\u5c0f\u8d75': '[email protected]', u'\u5c0f\u738b': '[email protected]', u'\u5c0f\u7ea2': '[email protected]', u'\u5c0f\u9ed1': '[email protected]'}
小赵的地址为[email protected]
小王的地址为[email protected]
小红的地址为[email protected]
小黑的地址为[email protected]
小黑的地址为[email protected]
'''
#字典结束

#=====================================================

#序列开始

'''
列表、元组和字符串都是序列,但是序列是什么,它们为什么如此特别呢?
序列的两个主要特点是索引操作符和切片操作符。
索引操作符让我们可以从序列中抓取一个特定项目。
切片操作符让我们能够获取序列的一个切片,即一部分序列。
'''

fruitList=['apple','banana','pear','watermelon']
print 'Item 0 is ',fruitList[0]
print 'Item -1 is ',fruitList[-1]
print 'Item -2 is ',fruitList[-2]
#print 'Item 4 is',fruitList[4] list index out of range

print 'Item 1 to 3 is',fruitList[1:3]
print 'Item 2 to end is',fruitList[2:]
print 'Item 1 to -1 is',fruitList[1:-1]
print 'Item start to end is',fruitList[:]

name='abcdefg'

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 0 is',name[1:0] #空
print 'characters start to -1 is',name[:-1]
'''
结果
Item 0 is  apple
Item -1 is  watermelon
Item -2 is  pear
Item 1 to 3 is ['banana', 'pear']
Item 2 to end is ['pear', 'watermelon']
Item 1 to -1 is ['banana', 'pear']
Item start to end is ['apple', 'banana', 'pear', 'watermelon']
characters 1 to 3 is bc
characters 2 to end is cdefg
characters 1 to -1 is bcdef
characters start to end is abcdefg
characters 1 to 0 is 
characters start to -1 is abcdef
'''

'''
注意
Python从0开始计数
索引同样可以是负数,在那样的情况下,位置是从序列尾开始计算的。
因此,shoplist[-1]表示序列的最后一个元素而shoplist[-2]抓取序列的倒数第二个项目。

切片操作符是序列名后跟一个方括号,方括号中有一对可选的数字,并用冒号分割。
记住数是可选的,而冒号是必须的。
返回的序列从开始位置 开始 ,刚好在 结束 位置之前结束。
即开始位置是包含在序列切片中的,而结束位置被排斥在切片外。
'''
#序列结束

#=============================================================

#参考开始

'''
如果你想要复制一个列表或者类似的序列或者其他复杂的对象(不是如整数那样的简单 对象 ),
那么你必须使用切片操作符来取得拷贝。
如果你只是想要使用另一个变量名,两个名称都 参考 同一个对象,
那么如果你不小心的话,可能会引来各种麻烦。
'''

print '\nSimple Assignment'
a=2;
b=a;
a=3;
print a,b

shopList=['apple','mango','pear','watermelon']
myList=shopList
print 'shopList is',shopList
print 'myList is',myList

del shopList[0]
print 'shopList is',shopList
print 'myList is',myList

myList=shopList[:]
del myList[0]
myList[0]='cabage'
shopList.append('orange')
print 'shopList is',shopList
print 'myList is',myList

'''
结果
Simple Assignment
3 2
shopList is ['apple', 'mango', 'pear', 'watermelon']
myList is ['apple', 'mango', 'pear', 'watermelon']
shopList is ['mango', 'pear', 'watermelon']
myList is ['mango', 'pear', 'watermelon']
shopList is ['mango', 'pear', 'watermelon', 'orange']
myList is ['cabage', 'watermelon']
'''



#参考结束

#=========================================================

#字符串 开始
#help(str)

print '\n'
name="Swaroop"
if name.startswith('Swa'):
    print'Yes,%s start with "Swa"'% name
if 'a' in name:
    print 'Yes,a is in',name
if name.find('war')!=-1:
    print 'Yes,it contains the string "war"'
delimiter ="_*_"
myList=('a','b','c')
print delimiter.join(myList) #str类也有以一个作为分隔符的字符串join序列的项目的整洁的方法,它返回一个生成的大字符串

'''
结果
Yes,Swaroop start with "Swa"
Yes,a is in Swaroop
Yes,it contains the string "war"
a_*_b_*_c
'''

#字符串 结束

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章