You can get the value of the entire container by simply printing it.
In python, that is done with the
print
function.
print(container)
If you ran the code:
aList=[True,"abc",[1,2,3]]
aTuple=(False,534.2,50)
aDict={'0':'Yes','Answer':42}
aSet={'A','B','C'}
print(f'{aList} is a list.')
print(f'{aTuple} is a tuple.')
print(f'{aDict} is a dictionary.')
print(f'{aSet} is a set.')
Python would output:
[True,"abc",[1,2,3]] is a list.
(False,534.2,50) is a tuple.
{'0':'Yes','Answer':42} is a dictionary.
{'B', 'C', 'A'} is a set.
To get a single value, use indexing
Click
HERE to learn more about indexing.
Try it out yourself on lists, tuples or any data type from the previous page!
For dictionaries, just call the dictionary's keys.
Each element will have a corrolating key.
Each key must be a string, but the key's value can be any data type.
You can't actually call specific values from a
set
.
What you can do however is ask if the value is
in
the set.
This will return
True
or
False
.
If you ran the code:
aList=[True,"abc",[1,2,3]]
aTuple=(False,534.2,50)
aDict={'0':'Yes','Answer':42}
aSet={'A','B','C'}
print(f'List index 1 is {aList[1]}.')
print(f'List index 2,1 is {aList[2][1]}.')
print(f'Tuple index 2 is {aTuple[2]}.')
aDictValue = aDict['Answer']
print(f'Dictionary: Answer is {aDictValue}.')
AaSet = 'A' in aSet
print(f'{AaSet}: A is in the set.')
Python would output:
List index 1 is abc.
List index 2,1 is 2.
Tuple index 2 is 50.
Dictionary: Answer is 42.
True: A is in the set.
To add values to a container, check examples below because every container is different.
Refrain from adding values to tuples, because they are immutable.
Just use any other container instead.
If you ran the code:
aList=[True,"abc",[1,2,3]]
aTuple=(False,534.2,50)
aDict={'0':'Yes','Answer':42}
aSet={'A','B','C'}
aList.append(4.2)
aTuple=aTuple+(20,)
aDict['Add']='Sub'
aSet.add('D')
print(f'{aList} is a list.')
print(f'{aTuple} is a tuple.')
print(f'{aDict} is a dictionary.')
print(f'{aSet} is a set.')
Python would output:
[True, 'abc', [1, 2, 3], 4.2] is a list.
(False, 534.2, 50, 20) is a tuple.
{'0': 'Yes', 'Answer': 42, 'Add': 'Sub'} is a dictionary.
{'D', 'B', 'C', 'A'} is a set.
To remove values to a container, check examples below because every container is different.
Refrain from adding values to tuples, because they are immutable.
Just use any other container instead.
If you ran the code:
aList=[True,"abc",[1,2,3],4.2]
aDict={'0':'Yes','Answer':42,'Add':'Sub'}
aSet={'A','B','C','D'}
aList.remove(True)
del aDict['Answer']
aSet.remove('B')
print(f'{aList} is a list.')
print(f'{aDict} is a dictionary.')
print(f'{aSet} is a set.')
Python would output:
["abc", [1, 2, 3],4.2] is a list.
{'0': 'Yes', 'Add': 'Sub'} is a dictionary.
{'C', 'A', 'D'} is a set.