This series will feature short notes on Python topics. The idea is to reinforce my learning through "note taking".
Tuple
- Tuple is just like an array. It is immutable, and holds a sequence of values. However, unlike a C/C++ array, a tuple can store values of mixed types. An example:
myTuple = ("Python", "PHP", "Ruby", 3.1428, 2009, "Django")
- Just like a string, a tuple can be indexed, sliced and concatenated with another tuple. When called on a tuple, the len() function returns the total number of elements in the tuple.
totElements = len(myTuple)
print totElements # It prints 6
List
- A list is like a C/C++ dynamic array. Elements can be added, deleted and sorted
- Just like a tuple, it can store data of mixed types.
- A list is enclosed in square brackets. For example:
myList = ["Python", "PHP", "Ruby", "Perl", ('a', 'e', 'i', 'o', 'u')]
emptyList = [] # this creates an empty list
- All the tuple operations are applicable to lists
Dictionary
- A dictionary is like a hash table in other programming languages. It stores data as key-value pairs.
- A dictionary is enclosed in curly braces.
- The key must be an immutable data type, i.e. string or tuple. For example,
myDict = {"name" : "Charles Martel", "occupation" : "Palace Mayor, coup leader", "country" : "France"}
emptyDict = {} # This creates an empty dictionary
No comments:
Post a Comment