There are four: lists, truples, dictionaries, and sets
"A list is a data structure that holds an ordered collection of items i.e. you can store a sequence of items in a list." - A Byte Of Python
Lists are mutable.
allies = ['USA','UK','France','New Zealand',
'Australia','Canada','Poland']; allies
['USA', 'UK', 'France', 'New Zealand', 'Australia', 'Canada', 'Poland']
len(allies)
7
allies.append('China'); allies
['USA', 'UK', 'France', 'New Zealand', 'Australia', 'Canada', 'Poland', 'China']
allies.sort(); allies
['Australia', 'Canada', 'China', 'France', 'New Zealand', 'Poland', 'UK', 'USA']
allies.sort(); allies
['Australia', 'Canada', 'China', 'France', 'New Zealand', 'Poland', 'UK', 'USA']
allies[0]
'Australia'
allies[-1]
'USA'
del allies[0]; allies
['Canada', 'China', 'France', 'New Zealand', 'Poland', 'UK', 'USA']
allies.append(3442); allies
['Canada', 'China', 'France', 'New Zealand', 'Poland', 'UK', 'USA', 3442]
"Though tuples may seem similar to lists, they are often used in different situations and for different purposes. Tuples are immutable, and usually contain an heterogeneous sequence of elements that are accessed via unpacking (or indexing (or even by attribute in the case of namedtuples). Lists are mutable, and their elements are usually homogeneous and are accessed by iterating over the list." - Python Documentation
"Tuples are heterogeneous data structures (i.e., their entries have different meanings), while lists are homogeneous sequences." - StackOverflow
Parentheses are optional, but useful.
File "<ipython-input-27-ff6bfa04b12d>", line 1 Parentheses are optional, but useful. ^ SyntaxError: invalid syntax
usa = ('Texas', 'California', 'Maryland'); usa
('Texas', 'California', 'Maryland')
(notice the USA has a state names in the nested tuple)
countries = ('canada', 'mexico', usa); countries
('canada', 'mexico', ('Texas', 'California', 'Maryland'))
countries[2]
('Texas', 'California', 'Maryland')
countries[2][2]
'Maryland'
"A dictionary is like an address-book where you can find the address or contact details of a person by knowing only his/her name i.e. we associate keys (name) with values (details). Note that the key must be unique just like you cannot find out the correct information if you have two persons with the exact same name." - A Byte Of Python
staff = {'Chris' : 'chris@stater.org',
'Jake' : 'jake@stater.org',
'Ashley' : 'ashley@stater.org',
'Shelly' : 'shelly@stater.org'
}
staff['Chris']
'chris@stater.org'
del staff['Chris']; staff
{'Jake': 'jake@stater.org', 'Ashley': 'ashley@stater.org', 'Shelly': 'shelly@stater.org'}
staff['Guido'] = 'guido@python.org'; staff
{'Jake': 'jake@stater.org', 'Ashley': 'ashley@stater.org', 'Guido': 'guido@python.org', 'Shelly': 'shelly@stater.org'}
Sets are unordered collections of simple objects.
BRI = set(['brazil', 'russia', 'india'])
'india' in BRI
True
'usa' in BRI
False
BRIC = BRI.copy()
BRIC.add('china')
BRIC.issuperset(BRI)
True
BRI.remove('russia')
BRI & BRIC
{'brazil', 'india'}