列表
代碼實現(xiàn)
List = ['Objective-c','Swift','Python','Java','C++','C']
print('List now content:',List)
print('List length is',len(List))
for item in List:
print('List content: ',item,end=' \n')
List.append('Ruby')//追加元素
print('List now content:',List)
List.sort()//排序
print('List sort content:',List)
print('List first item:',List[0])
firstItem = List[0]
del List[0]//刪除元素
print('firstItem is:',firstItem)
print('List finally is:',List)
運行結(jié)果
List now content: ['Objective-c', 'Swift', 'Python', 'Java', 'C++', 'C']
List length is 6
List content: Objective-c
List content: Swift
List content: Python
List content: Java
List content: C++
List content: C
List now content: ['Objective-c', 'Swift', 'Python', 'Java', 'C++', 'C', 'Ruby']
List sort content: ['C', 'C++', 'Java', 'Objective-c', 'Python', 'Ruby', 'Swift']
List first item: C
firstItem is: C
List finally is: ['C++', 'Java', 'Objective-c', 'Python', 'Ruby', 'Swift']
列表是可變的,字符串是不可變的
元組 Tuple
元組是不可變的枕扫,不可以編輯或更改元組
代碼實現(xiàn)
mouth = ('day 1','day 2','day 3','day 4','day 5')
print('mouth length is :',len(mouth))
new_mouth = mouth,'day 11','day'
print('new mouth is :',new_mouth)
print('old mouth is :',new_mouth[0])
print('last item of old mouth is :',new_mouth[0][len(mouth)-1])
print('new mouth length is :',len(new_mouth))
運行結(jié)果
mouth length is : 5
new mouth is : (('day 1', 'day 2', 'day 3', 'day 4', 'day 5'), 'day 11', 'day')
old mouth is : ('day 1', 'day 2', 'day 3', 'day 4', 'day 5')
last item of old mouth is : day 5
new mouth length is : 3
new mouth actually length is 7
包含一個元素的元組
mouth = ('day1', )//必須在第一個元素后面加逗號辞做,以便程序區(qū)分
字典
鍵值對,key-value侥袜。key 只能是不可變得對象,value 是可變和不可變都可以瓜晤。
樣式:dict = {name:joy,age:18}
示例代碼
student = {
'name' : 'joy',
'age' : 18,
'stuNo': '036',
'birth':'1994-11-24',
}
print('student message is:',student)
print('student name is:',student['name'])
del student['birth']
print('now student message is {}'.format(student))
for name, age in student.items():
print('name:{} and age :{}'.format(name,age))
student['place'] = 'BeiJing'
if 'place' in student:
print('now student message is {}'.format(student))
輸出結(jié)果
student message is: {'name': 'joy', 'age': 18, 'stuNo': '036', 'birth': '1994-11-24'}
student name is: joy
now student message is {'name': 'joy', 'age': 18, 'stuNo': '036'}
name:name and age :joy
name:age and age :18
name:stuNo and age :036
now student message is {'name': 'joy', 'age': 18, 'stuNo': '036', 'place': 'BeiJing'}
序列
代碼實現(xiàn)
List = ['Objective-c','Swift','Python','Java','C++','C']
new_item = 'single Language'
print('item 0 is:',List[0])
print('item 3 is:',List[3])
print('item -2 is:',List[-2])
print('item -1 is:',List[-1])
print('new_item is:',new_item[7])
print('===========================')
# 列表截取
print('item 1-3:',List[1:3])
print('item 2-end is:',List[2:])
print('item 1to-1 is:',List[1:-1])
print('item 0-end is:',List[:])
# 字符串截取
print('new_item 7-end is:',new_item[7:])
print('item 0-4:',new_item[0:4])
print('item 4to-4:',new_item[4:-4])
輸出結(jié)果
item 0 is: Objective-c
item 3 is: Java
item -2 is: C++
item -1 is: C
new_item is: L
===========================
item 1-3: ['Swift', 'Python']
item 2-end is: ['Python', 'Java', 'C++', 'C']
item 1to-1 is: ['Swift', 'Python', 'Java', 'C++']
item 0-end is: ['Objective-c', 'Swift', 'Python', 'Java', 'C++', 'C']
new_item 7-end is: Language
item 0-4: sing
item 4to-4: le Lang
使用索引獲取序列中各個項目包吝,“[ ]”中指定序列的制定數(shù)組饼煞,Python 將返回該序列制定位置的項目,Python計數(shù)從0開始诗越。索引計數(shù)也可以為負數(shù)砖瞧,負數(shù)表示從序列的末尾向首位開始計數(shù)。
還可以設(shè)置截取跨度長度
List[ : : 2]//表示從起始-結(jié)尾 截取長度為2的項目
集合
代碼實現(xiàn)
stu = set(['jone','tom','jerry','jack'])
print('stu:',stu)
print('jack is in stu? ','jack' in stu)
print('lee is in stu? ','lee' in stu)
stus = stu.copy()
stus.add('jodan')
print('stus:',stus)
print('stus is stu superset? ',stus.issuperset(stu))
print('stus and stu all item? ',stu | stus)
stus.remove('tom')
stus.add('robert')
print('stu:',stu)
print('stus:',stus)
print('stus and stu all item? ',stu | stus)
print('stus and stu intersection item? ',stu & stus)
輸出結(jié)果
stu: {'jack', 'tom', 'jerry', 'jone'}
jack is in stu? True
lee is in stu? False
stus: {'tom', 'jack', 'jerry', 'jodan', 'jone'}
stus is stu superset? True
stus and stu all item? {'tom', 'jack', 'jerry', 'jodan', 'jone'}
stu: {'jack', 'tom', 'jerry', 'jone'}
stus: {'jack', 'robert', 'jerry', 'jodan', 'jone'}
stus and stu all item? {'tom', 'jack', 'robert', 'jerry', 'jodan', 'jone'}
stus and stu intersection item? {'jack', 'jerry', 'jone'}
set 支持拷貝嚷狞、增加块促、刪除等基本操作。
引用
創(chuàng)建一個對象床未,分配給該對象某個變量竭翠,變量只會 refer 某個對象,不會代表對象本身薇搁。變量名只是指向計算機內(nèi)存儲該對象的內(nèi)存地址斋扰。在引用時候會出現(xiàn)不同的效果。
案例
List = ['Objective-c','Swift','Python','Java','C++','C']
new_list = List
# new_list 只是指向 List 地址的名稱,List 改變传货,new_list也會隨之改變
List.remove("C")
print('List:',List)
print('newList:',new_list)
# new_list 與 List 指向同一對象
copy_list = List[:]
# 通過切片 復制出一份 List 的副本
print('copy_list:',copy_list)
del copy_list[0]
print('List:',List)
print('newList:',new_list)
print('copy_list:',copy_list)
輸出結(jié)果
List: ['Objective-c', 'Swift', 'Python', 'Java', 'C++']
newList: ['Objective-c', 'Swift', 'Python', 'Java', 'C++']
copy_list: ['Objective-c', 'Swift', 'Python', 'Java', 'C++']
List: ['Objective-c', 'Swift', 'Python', 'Java', 'C++']
newList: ['Objective-c', 'Swift', 'Python', 'Java', 'C++']
copy_list: ['Swift', 'Python', 'Java', 'C++']
如果想操作而又不影響源數(shù)據(jù)的情況下屎鳍,使用 “ List[ : ] ” 這樣得到的序列是拷貝出一份副本,互相操作不會影響问裕;如果采用 “ a=b ”逮壁,則操作會造成聯(lián)動,慎用粮宛!
字符串補充
代碼實現(xiàn)
List = ['Objective-c','Swift','Python','Java','C++','C']
str = 'python'
if str.startswith('py'):
# 字符串以什么開頭
print('str start at \'py\'')
if 'on' in str:
# 字符串包含
print('str contain \'on\'')
if str.find('th') != -1:
# 查找字符串的位置窥淆,如果找不到則返回"-1"
print('\'th\' in str location is:',str.find('th'))
otherStr = ' ~ '
print('List change str is:',otherStr.join(List))
#otherStr 是作為List 中每個項目的分隔符,返回一個更大的字符串
輸出結(jié)果
str start at 'py'
str contain 'on'
'th' in str location is: 2
List change str is: Objective-c ~ Swift ~ Python ~ Java ~ C++ ~ C