< Tanzhou Education >-python Learning notes @ Basic data type

Source: Internet
Author: User

python basic data with Ganso and dictionaries

Ganso (tuple) is the basic data type of Python, with () means that elements are separated by ', ', and once created, the progenitor cannot be changed

In []: s = (' stone ', ' stone ', ' Cathy ')  # # #元祖可以有重复元素In [[]: sout[46]: (' stone ', ' stone ', ' Cathy ') in []: S.count (' St One ')  # # Element Count out[47]: 2In [0In]: S.count (' s ')  # # #没有的话返回0Out []: [in]: S.index (' Stone ')  # #查找索引Out [49]: 0In []: S.index (' Stone ', 1)  out[50]: 1

Dictionary (dict) is a data type consisting of key-value pairs, which is faster than a dictionary lookup compared to other data types. Dictionaries are not sorted by default (no sorting required), and value is found based on key.
in [74]: ditout[74]: {' Age': 18,'Marry':'No',' Work':'Yes'}#Increase#1
In []: dit[' Book'] ='Note' ##增加元素In [76]: ditout[76]: {' Age': 18,' Book':'Note','Marry':'No',' Work':'Yes'}
#2In [[Dit.update]: {' Station':'Quanta'} # # # Update a dictionary type in [78]: ditout[78]: {' Station':'Quanta',' Age': 18,' Book':'Note','Marry':'No',' Work':'Yes'}
# #3生成一个字典
in [+]: Dit.fromkeys ([1,2,2,3,4],3)  # # # # # # # # # # # # # # # # # # # of objects, generate a dictionary out[90]: {1:3, 2:3, 3:3, 4:3} 
#minusIn [Bayi]: Dit.pop (' Station')##删掉一个元素, returns the value of the elementOUT[81]:'Quanta'In [[+]: Dit.popitem ()## Delete a key-value pair and return the deleted key-value pair in the form of a meta-ancestorOUT[82]: (' Work','Yes') in []: Dit.clear ()##清空字典#element Valuein [84]: Dit.items () # #返回一个类似集合类型的对象, typically used to traverse a dictionary key or value out[(+): Dict_items ([(' Age', 18), (' Book','Note'), ('Marry','No')]) in [86]: dit.values () out["Dict_values" ([18,'Note','No'])

#a Set-like object providing a view on D ' s keysin [87]: Dit.keys () out[[+]: Dict_keys ([' Age',' Book','Marry'])
In [the]: Dit.get (' age ')  # # #判断元素是否存在, there is no error, will return none. OUT[91]:
  
# Other in [89]: Dit.copy () # #拷贝Out []: { " age ": 18, " book ": " note , " marry ": " no " }
#补充: In [107]: ditout[107]: {' age ': + ' book ': ' Note ', ' books ': ' Marry ': ' No '}in [108]: Dit.setdefault (' books ', ') 
   
    # #字典中有这个元素, this element is returned, the dictionary does not change out[108]: 45In [109]: ditout[109]: {' age ': + ' book ': ' Note ', ' books ': ' Marry ': ' No '}in [110] : Dit.setdefault (' name ', ' Stone ') # #字典没有这个元素, returns the value of the set, the dictionary adds out[110]: ' Stone ' in [111]: ditout[111]: {' Age ': ' "book ': ' Note ', ' books ': ' Marry ': ' No ', ' name ': ' Stone '}
   

  




Dictionary traversal:

For I in dit: # # High Efficiency
Print I, Dit[i]

For k,v in Dit.items (): # #效率低
Print K, V

Collection (set) is

< Tanzhou Education >-python Learning notes @ Basic data type

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.