Similarities between list and string in Python
The four main types of objects you will work with are strings, lists, dictionaries and tuples. Knowing how to manipulate these objects is incredibly important in Python. Before discussing each object in detail, I wanted to give an overview of the four objects.
Definitions:
Immutable vs mutable:
The in operator:The in operator (and not in operator) is a boolean operator takes in two arguments that determines if the first argument is a substring or a element of the second argument.
The len function:The function len() can be used to:
Methods for each type of object (dot notation)For strings, lists and dictionaries, there are set of methods you can use to manipulate the objects. Because tuples are immutable, there are no methods to modify the objects. In general, the notation for methods is the dot notation. The syntax is the name of the objects followed by a dot (or period) followed by the name of the method.
Here we use the method split() which is applied to a string (making this a string method). The method splits the string at a given delimiter (in this case the white space ). In the subsequent pages, I will compare and contrast the similarities and differences of strings, lists, dictionaries and tuples in greater detail. In general, lists are more common than tuples (mostly because they are mutable), but there are a few reasons why tuples may be preferable:
Again, because tuples are immutable, they do not provide methods like split(). In addition to the built-in objects in Python, you can also create your own object (or class). I will discuss this idea in greater detail a bit further on. For now, lets formally discuss strings, lists, dictionaries and tuples. |