标签:添加 不重复 spec car title sort 空白 char value
1.使用方法修改字符串的大小写
例:
>>> name = "ada lovelace"
>>> print name.title()
Ada Lovelace
>>> print(name.upper())
ADA LOVELACE
2.合并(拼接)字符串
例:
>>> first_name = "ada"
>>> last_name = "lovelace"
>>> full_name = first_name + " " + last_name
>>> print full_name
ada lovelace
3.使用制表符或换行符来添加空白
例:
>>> print("Python")
Python
>>> print("\tPython")
Python
>>> print("\nPython\nhello")
Python
hello
>>> print("\nPython\n\thello")
Python
hello
4.删除首尾空白
例:
>>> message = ‘ python ‘
>>> message
‘ python ‘
>>> message.rstrip()
‘ python‘
>>> message.lstrip()
‘python ‘
>>> message.strip()
‘python‘
1.整数(int)
2.浮点数(float)
3.长整型(long)
4.布尔型(bool)
5.复数型(complex)
访问列表元素,索引从0而不是1开始
例:
>>> bicycles = [‘trek‘, ‘cannondale‘, ‘redline‘, ‘specialized‘]
>>> print bicycles[0]
trek
>>> print bicycles[1]
cannondale
>>> print bicycles[-1]
specialized
修改、添加和删除元素
例:
>>> motorcycles = [‘honda‘, ‘yamaha‘, ‘suzuki‘]
>>> print motorcycles
[‘honda‘, ‘yamaha‘, ‘suzuki‘]
修改
>>> motorcycles[0] = ‘ducati‘
>>> print motorcycles
[‘ducati‘, ‘yamaha‘, ‘suzuki‘]
添加
>>> motorcycles.append(‘ducati‘)
>>> print motorcycles
[‘honda‘, ‘yamaha‘, ‘suzuki‘, ‘ducati‘]
append只是在末尾添加元素
在列表中插入元素用insert()
>>> motorcycles = [‘honda‘, ‘yamaha‘, ‘suzuki‘]
>>> motorcycles.insert(0, ‘ducati‘)
>>> print motorcycles
[‘ducati‘, ‘honda‘, ‘yamaha‘, ‘suzuki‘]
删除
del方法----使用del语句将值从列表中删除后,你就无法再访问它了
>>> del motorcycles[0]
>>> print motorcycles
[‘yamaha‘, ‘suzuki‘]
pop()方法
1.删除列表的最后一个元素
>>> motorcycles.pop()
‘suzuki‘
>>> print motorcycles
[‘honda‘, ‘yamaha‘]
2.指定元素删除
>>> motorcycles.pop(0)
‘honda‘
>>> print motorcycles
[‘yamaha‘, ‘suzuki‘]
remove()方法----删除第一个指定的值
组织列表(排序)
1.使用方法sort()对列表进行永久性排序
例:
>>> cars = [‘bmw‘, ‘audi‘, ‘toyota‘, ‘subaru‘]
>>> cars.sort()
>>> print cars
[‘audi‘, ‘bmw‘, ‘subaru‘, ‘toyota‘]
反向排序----sort()传递参数reverse=True
>>> cars.sort(reverse=True)
>>> print cars
[‘toyota‘, ‘subaru‘, ‘bmw‘, ‘audi‘]
2.使用函数sorted()对列表进行临时
排序
3.倒着打印列表
例:
>>> cars = [‘bmw‘, ‘audi‘, ‘toyota‘, ‘subaru‘]
>>> cars.reverse()
>>> print cars
[‘subaru‘, ‘toyota‘, ‘audi‘, ‘bmw‘]
4.列表的长度
>>> cars = [‘bmw‘, ‘audi‘, ‘toyota‘, ‘subaru‘]
>>> len(cars)
4
操作列表
1.遍历整个列表
例:
>>> cars = [‘bmw‘, ‘audi‘, ‘toyota‘, ‘subaru‘]
>>> for car in cars:
... print car
...
bmw
audi
toyota
subaru
2.创建数值列表
① 使用函数range()
例:
>>> for i in range(1,5):
... print i
...
1
2
3
4
② 使用range()创建数字列表
例:
>>> num = list(range(1,5))
>>> print num
[1, 2, 3, 4]
3.对数字列表执行简单的统计
例:
>>> digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
>>> min(digits)
0
>>> max(digits)
9
>>> sum(digits)
45
使用列表的一部分
1.切片
例:
>>> players = [‘charles‘, ‘martina‘, ‘michael‘, ‘florence‘, ‘eli‘]
>>> print players[1:3]
[‘martina‘, ‘michael‘]
2.遍历切片
例:
>>> players = [‘charles‘, ‘martina‘, ‘michael‘, ‘florence‘, ‘eli‘]
>>> for player in players[:3]:
... print player.title()
...
Charles
Martina
Michael
修改元组元素
元组中的元素值是不允许修改的,但我们可以对元组进行连接组合
例:
>>> t = (12, 13, 14, 15, 21, 22 )
>>> t1 = t[0:3]
>>> t2 = (19,)
>>> t3 = t[4:]
>>> t = t1 + t2 + t3
>>> print t
(12, 13, 14, 19, 21, 22)
删除元组元素
例:
>>> t = (12, 13, 14, 15, 21, 22 )
>>> t1 = t[0:3] + t[4:]
>>> t = t1
>>> print t
(12, 13, 14, 21, 22)
访问字典
例:
>>> dict = {‘Name‘: ‘Runoob‘, ‘Age‘: 7, ‘Class‘: ‘First‘}
>>> print dict[‘Name‘]
Runoob
修改字典
例:
>>> dict = {‘Name‘: ‘Runoob‘, ‘Age‘: 7, ‘Class‘: ‘First‘}
>>> print dict
{‘Age‘: 7, ‘Name‘: ‘Runoob‘, ‘Class‘: ‘First‘}
>>> dict[‘Age‘] = 11
>>> print dict
{‘Age‘: 11, ‘Name‘: ‘Runoob‘, ‘Class‘: ‘First‘}
删除字典中的元素
例:
>>> dict = {‘Name‘: ‘Runoob‘, ‘Age‘: 7, ‘Class‘: ‘First‘}
>>> del dict[‘Name‘]
>>> print dict
{‘Age‘: 7, ‘Class‘: ‘First‘}
注意:创建一个空集合必须用 set() 而不是{},因为{}是用来创建一个空字典
例:
>>> student = {‘Tom‘, ‘Jim‘, ‘Mary‘, ‘Tom‘, ‘Jack‘, ‘Rose‘}
>>> print student
set([‘Mary‘, ‘Rose‘, ‘Jim‘, ‘Jack‘, ‘Tom‘])
标签:添加 不重复 spec car title sort 空白 char value
原文地址:http://www.cnblogs.com/dwz1011/p/6163951.html