Python入门(简明Python教程)——辰

数据类型

数字 字符串 元组 列表 字典 集合(加粗者为不可变数据类型)

1.列表(list):一系列有序项目的集合

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)

结果:

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

2.元组(Tuple)

元组可以近似的看为列表,但不如列表提供的功能广泛,且不得更改。

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 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 are ('python', 'elephant', 'penguin')
Last animal brought from old zoo is penguin
Number of animals in the new zoo is 5

3.字典

ab={
    'Swaroop':'[email protected]',
    'Larry':'[email protected]',
    'Matsumoto':'[email protected]',
    'Spammer':'[email protected]'
}
print("Swaroop's address is",ab['Swaroop'])
del ab['Spammer']
print('\nThere are {} contact in the address_book\n'.format(len(ab)))
for name,address in ab.items():
    print('Contact {} at {}'.format(name,address))

ab['Guido']='[email protected]'

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

结果:

Swaroop's address is [email protected]

There are 3 contact in the address_book

Contact Swaroop at [email protected]
Contact Larry at [email protected]
Contact Matsumoto at [email protected]

Guido's address is [email protected]

4.序列

列表,元组,字典都是一种序列。序列有切片的属性。

shoplist = ['apple',	'mango',	'carrot',	'banana']
name = 'swaroop'
#	Indexing	or	'Subscription'	operation	#
#索引或“下标(Subscription)”操作符	#
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])
#	Slicing	on	a	list	#
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('Item	1	to	3	is',	shoplist[::2])

结果:

Item	1	to	3	is ['apple', 'carrot']

5.集合

两大属性:互异,无序

使用集合一般在我们关注集合间关系的时候和排除相同项的时候。

shoplist=['apple','mango','carrot','banana','carrot']
newlist=['milk','mango']
print(shoplist)
print('mango' in shoplist)
bri=set(shoplist)
print(bri)
print(bri|set(newlist))
print(bri&set(newlist))

结果:

['apple', 'mango', 'carrot', 'banana', 'carrot']
True
{'apple', 'carrot', 'mango', 'banana'}
{'apple', 'milk', 'banana', 'mango', 'carrot'}
{'mango'}

字符串:

字符串也是一种对象,也有很多自己的方法。

name='Swaroop'
if name.startswith('Swa'):
    print('Yes,the string is start with "Swa"')

if 'a' in name:
    print('Yes,the string contains "a"')

if name.find('war')!=-1:
    print('Yes,it contains the string "war"')

delimiter='_*_'
mylist=['Brazil','Russia','India','China']
print(delimiter.join(mylist))

结果:

Yes,the string is start with "Swa"
Yes,the string contains "a"
Yes,it contains the string "war"
Brazil_*_Russia_*_India_*_China

猜你喜欢

转载自blog.csdn.net/qq_40703975/article/details/81213350