dict type
This type handles dictionaries.
Using {}
, {key 1: value 1, key 2: value 2, key 3: value 3}
list type
A type that handles a variable array called a list
Using []
, [value 1, value 2, value 3]
tuple type
A type that handles invariant arrays called tuples
Use ()
and (value 1, value 2, value 3)
a={"apple":1,"orange":2,"book":3}
a["ball"]=4 #Add an element
print(a)
Execution result
a={"apple":1,"orange":2,"book":3,"ball":4}
a=["apple","orange","book"]
a.append("ball") #Add an element
print(a)
Execution result
a=["apple","orange","book","ball"]
The deletion method is the same for dict type and list type. The tuple type cannot delete an element. The following two are typical ones.
dict type
a={"apple":1,"orange":2,"book":3}
a.pop("apple")
print(a)
Execution result
a={"orange":2,"book":3}
list type
a=["apple","orange","book"]
a.pop("0")
print(a)
Execution result
["orange","book"]
dict type
a={"apple":1,"orange":2,"book":3}
del a["apple"]
print(a)
Execution result
{"orange":2,"book":3}
list type
a=["apple","orange","book"]
del a[0]
print(a)
Execution result
["orange","book"]
Recommended Posts