第七节 数据结构

元组

圆括号包裹

()

age =22
name = 'Swaroop'
test = 'test'
print '%s is %d years old'%(name,age)
print '%s is %d years old %s'%(name,age,test)
print 'Why is %s playing with that python?'%name

~~~~~~~~~~~~~~~~~~~~~~~~↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Swaroop is 22 years old
Swaroop is 22 years old test
Why is Swaroop playing with that python?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ab = {'Swaroop':'swaroopch@byteofpython.info',
      'Larry':'larry@wall.org',
      'Matsumoto':'matz@ruby-lang.org',
      'Spammer':'spammer@hotmail.com'
      }
print "Swaroop's address is %s" %ab['Swaroop']
ab['Guido'] = 'guido@python.org'

del ab['Spammer']
print '\n There are %d contacts in the address-book \n' %len(ab)
for name,address in ab.items():
    print 'Contact%s at %s'%(name,address)
if 'Guido' in ab:
    print "\n Guido's address is %s" %ab['Guido']

~~~~~~~~~~~~~~~~~~~~~~~~↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Swaroop's address is swaroopch@byteofpython.info

 There are 4 contacts in the address-book

ContactSwaroop at swaroopch@byteofpython.info
ContactMatsumoto at matz@ruby-lang.org
ContactLarry at larry@wall.org
ContactGuido at guido@python.org

 Guido's address is guido@python.org

 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

序列

shoplist = ['apple','mango','carrot','banana']

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 '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[:]

name = 'swaroop'
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
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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

name = 'Swaroop'
if name.startswith('Swa'):
    print 'Yes, the string starts with "Swa"'
if 'a' in name:
    print 'Yes, it contains the string "a"'
if name.find('war')!= -1:
    print 'Yes, it contains the string "war"'
delimiter = '_*_'
mylist = ['Brazil','Russia','India','China']
print mylist
print delimiter.join(mylist)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Yes, the string starts with "Swa"
Yes, it contains the string "a"
Yes, it contains the string "war"
['Brazil', 'Russia', 'India', 'China']
Brazil_*_Russia_*_India_*_China

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

posted @ 2016-06-15 15:05  ~乖乖~  Views(99)  Comments(0)    收藏  举报