程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python variable data type

編輯:Python

Catalog

  • 1. list
  • 2. Dictionaries
  • 3. aggregate


1. list

# add to :append Add... At the end ,insert(2, Elements ) Add... At the specified location , extend Add data at the end 
# append and extend What's the difference? :append It's an object ,extend Data is transmitted one by one 
list2 = [' autumn waters ',' False bamboo ',' Zhan Zhao ']
print(list2,id(list2))
list2.append(' Zhiruo ')
print(list2,id(list2))
list2.insert(0,' optimal ')
print(list2,id(list2))
list2.extend('a')
print(list2,id(list2))
# Delete 
# pop( Subscript ) Index delete remove( Elements ) Through elements del Delete one or the whole or several 
list2 = [' autumn waters ',' False bamboo ',' Zhan Zhao ']
# pop Deleting will return the deleted data , Not filling in the brackets will delete the last data 
# print(list2.pop())
# remove The value of the element will not be returned after deletion 
# print(list2.remove(' False bamboo '))
#del Delete one or the whole or several 
# del list2[1]
# print(list2)
# del list2[1:]
# print(list2)
# del list2
# print(list2)

2. Dictionaries

dict2 = {
'name': ' False bamboo ', 'age': 1000, 'sex': ' demon '}
dict3 = {
'name': ' False bamboo ', 'age': 1003, 'sex': ' demon ', 'address':' East China Sea Dragon Palace '}
print(dict2,id(dict2))
dict2.update(dict3)
print(dict2,id(dict2))
# The same Key, The difference in value is the update , Different key, The same value increases 
# Inquire about key keys value values Key value items
dict2 = {
'name': ' False bamboo ', 'age': 1000, 'sex': ' demon '}
keys = dict2.keys()
print(keys,type(keys))
values = dict2.values()
print(values,type(values))
ims = dict2.items()
print(ims,type(ims))
# Can't operate directly dict_keys Convert to list 
keys = dict2.keys()
print(keys)
print(list(keys)[1])
# Create a new dictionary fromKeys( Key list , value )
# {'name': '10','age':'10','sex':'10'}
dict1 = {
}
list2 = ['name','age','sex']
dict3 = dict1.fromkeys(list2,10)
print(dict3)
# Delete del[ Delete key ] Or delete the entire pop Specified key deletion popitem() Delete the last one clear() Empty dictionary 
dict1 = {
'name':' Stars ', 'age':18, 'sex':' Woman '}
del dict1['name']
print(dict1)
dict1.pop('age')
print(dict1)
dict1.popitem()
print(dict1)
dict1.clear()
print(dict1)

3. aggregate

# aggregate : disorder , The characteristic data cannot be repeated 
# Definition : {} and set() Are identified 
# establish 
set1 = {
1, 2, 3}
print(set1, type(set1))
set2 = set((1, 2, 3))
# Empty set Out of commission {} To create an empty collection 
set1={
}
set2=set()
print(set1,type(set1))
print(set2,type(set2))
# increase add update
set1 = {
' autumn waters ', ' False bamboo ', ' Zhan Zhao '}
set1.add(5)
print(set1)
#add Only single data can be added 
set1.add(5, '7')
print(set1)
set1.update((5, 6, 7))
print(set1)
# update Only data of the sequence can be added 
set1.update((5))
print(set1)
# Delete 
# remove( Elements ) There are no misstatements 
# pop Delete an element arbitrarily 
# discard( Specifies that the element delete ) No mistake. 
# clear Clear all 
set1.pop()
print(set1)
set1.remove(' Zhan Zhao ')
set1.discard(' Zhan Zhao ')
print(set1)
set1.clear()
print(set1)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved