current position:Home>Python note-day6-tuple dictionary collection

Python note-day6-tuple dictionary collection

2022-05-15 06:03:00Wandering mage 12

Preface
python Grammar learning , Leave it to those who need it , Understand everything !!

# coding=utf8
# @time:2022/3/29 20:11
# Author  Haoyu 

# 1. Tuples  -  Tuples are ordered and unchangeable sets . stay  Python  in , Tuples are written in parentheses 
'''''''''
Python  A tuple of is similar to a list , The difference is that the elements of a tuple cannot be modified .
 Tuples use braces  ( ), Use square brackets for lists  [ ].
 Tuples are easy to create , You just need to add elements in parentheses , And separate them with commas .

tup1 = ('Google', 'Runoob', 1997, 2000)
print(tup1)
'''''''''
# 1) Create an empty tuple 
'''''''''
tup = ()
print(tup)
'''''''''

# 2) When a tuple contains only one element , You need to add a comma after the element  , , Otherwise parentheses will be used as operators :
'''''''''
#  No commas 
tup = (50)
print(type(tup))
#  Output 
<class 'int'>

#  put a comma 
tup = (50,)
print(type(tup))

#  Output 
<class 'tuple'>
'''''''''

#  Be careful : Tuples are similar to strings , Subscript index from  0  Start , You can intercept , Combination etc. .

#  Parameter reading of tuple 、 modify 、 Delete 、 Combinations are similar to lists , Not described here one by one , Reference list notes ;




# 2 aggregate  -  aggregate (set) Is an unordered sequence of non-repeating elements .
#  You can use braces  { }  perhaps  set()  Function to create a collection ,
#  Be careful : To create an empty collection, you must use the  set()  instead of  { }, because  { }  Is used to create an empty dictionary .
'''''''''
 Create format :
parame = {
    value01,value02,...}
 perhaps 
set(value)
'''''''''
# 1) Additive elements 
#  aggregate .add( Elements )
#  Be careful : Add elements to the collection , If the element already exists , Nothing is done .
'''''''''
s = set(("Google", "Runoob", "Taobao"))
print(s)
s.add("Facebook")
print(s)
'''''''''

#  There's another way , You can also add elements , And the parameter can be a list , Tuples , Dictionary, etc , The syntax is as follows :
#  aggregate .update(  Elements  )
'''''''''
thisset = set(("Google", "Runoob", "Taobao"))
thisset.update({
    1,3})
print(thisset)
{
    1, 3, 'Google', 'Taobao', 'Runoob'}
thisset.update([1,4],[5,6])
print(thisset)
{
    1, 3, 4, 5, 6, 'Google', 'Taobao', 'Runoob'}
'''''''''
# 2) Remove elements 
#  grammar : aggregate .remove( Elements )
'''''''''
s = set(("Google", "Runoob", "Taobao"))
s.discard("Facebook")  #  No, no error occurs 
print(s)
'''''''''

#  There is also a way to remove elements from the collection , And if the element does not exist , There will be no errors . The format is as follows :
# s.discard( x )
'''''''''
thisset = set(("Google", "Runoob", "Taobao"))
thisset.discard("Facebook")  #  No, no error occurs 
=print(thisset)
{
    'Taobao', 'Google', 'Runoob'}
'''''''''

#  Randomly delete an element of the set 
#  aggregate .pop
'''''''''
thisset = set(("Google", "Runoob", "Taobao", "Facebook"))
x = thisset.pop()
print(thisset)
print(x)
'''''''''




# 3. Dictionaries 
#  Dictionary is another variable container model , And can store any type of object .
#  Each key value of the dictionary  key=>value  Yes, with a colon  :  Division , Use commas... Between each pair (,) Division , The whole dictionary is enclosed in curly brackets  {}  in  , The format is as follows :
# d = {key1 : value1, key2 : value2, key3 : value3 }

#  The key must be unique , But values don't have to be .
#  The value can take any data type , But the bond has to be immutable , Such as a string , Numbers .
'''''''''
tinydict = {
    'name': 'runoob', 'likes': 123, 'url': 'www.runoob.com'}
'''''''''
# 1) Visit the values in the dictionary 
#  Put the corresponding key in square brackets 
'''''''''
tinydict = {
    'Name': 'Runoob', 'Age': 7, 'Class': 'First'}
print("tinydict['Name']: ", tinydict['Name'])
'''''''''

# 2) Revise the dictionary 
#  The way to add new content to the dictionary is to add new keys / It's worth it , Modify or delete existing keys / The value pairs are as follows :
'''''''''
tinydict = {
    'Name': 'Runoob', 'Age': 7, 'Class': 'First'}
tinydict['Age'] = 8  #  to update  Age
print(tinydict)
tinydict['School'] = " rookie "  #  Add information 
print(tinydict)
'''''''''

# 3) Delete Dictionary 
#  Can delete a single element can also empty the dictionary , Emptying takes only one operation .
#  Explicitly delete a dictionary with del command , The following example :
'''''''''
tinydict = {
    'Name': 'Runoob', 'Age': 7, 'Class': 'First'}
del tinydict['Name']  #  Delete key  'Name'
print(tinydict)
tinydict.clear()  #  Empty dictionary 
print(tinydict)
del tinydict  #  Delete Dictionary 
# print(tinydict)
'''''''''

More secure sharing , Please pay attention to 【 Security info】 WeChat official account !

copyright notice
author[Wandering mage 12],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/131/202205110607424690.html

Random recommended