Python Basic Operations _ tuple _ dictionary operation

Source: Internet
Author: User

#元组
‘‘‘
Tuples are immutable lists and cannot be changed.
The same value as the list
‘‘‘
Tp= (a)
tp1= (' 127.0.0.1 ', ' 3307 ')
#元组只有count and index two methods.
lis=[' 127.0.0.1 ', ' 8080 ']
Tuple (LIS) #强制类型转换
#字典, legends are important.
#字典里的KYE不能重复
info={
' Name ': ' Xiaoming ',
' ID ': 1
}
Print (info[' name ']) #取不到值的时候会报错
Print (Info.get (' name ')) #取不到的时候报空
Print (Info.get (' Add ')) #取不到的时候报空
Print (Info.get (' Add ', ' Beijing ')) #取不到的时候就默认beijing
info[' Add ']= ' Shanghai ' #新增一个值
Info.setdefault (' pho ', 123333) #新增一个值
info[' id ']=4# Modify a value
Print (info)
#删除
Del info[' name ']
Info.pop (' Add ') #字典是无序的, so you must make a delete key when using pop
#info. Clear () #清空字典
Print (info)
new_info={
' Baobao ': {
' ID ': 1,
' Name ': ' Baobao ',
},
' Qiao ': {
' ID ': 1,
' Name ': ' Qiao ',
},
}
Print (new_info[' Baobao '].get (' name '))
New_info.get (' Baobao ') [' ID ']=new_info.get (' Qiao ') [' ID ']=7
Print (New_info.get (' Baobao '). Get (' id '))
Print (New_info.get (' Qiao '). Get (' id '))
#几个方法
Print (New_info.keys ())
Print (New_info.values ())
Print (New_info.items ())
#for循环写key和v
For k,v in Info.items ():
Print (K,V)
For J in Info:
Print (J,info.get (j)) #效率比第一个要高.

Python Basic Operations _ tuple _ dictionary operation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.