<jupyter_start><jupyter_text>Sequences in PythonArrays, Tuples, and ListWhile _Arrays_(i.e. multiple itams of the same data type) are a common feature of most programming languages, (as far as I know) they don't exist in Python. Instead two specialised type exist: _Tuples_ and _Lists_.  The main difference is that the former is _immutable_ (i.e. of a fixed, predefined length making it faster); while the latter is _mutable_ (and can be dynamically assigned value(s) or expand).<jupyter_code>shoppingList = ['milk', 'bread','egs']<jupyter_output><empty_output><jupyter_text>Note: Although _lists_ are similar to _tuples_, the delimiters are different and act as a cue to the programmer and Python:  the former uses square brackets in its notation, while the latter uses parehtheses.<jupyter_code>shoppingList[2] = 'eggs'
shoppingList<jupyter_output><empty_output><jupyter_text>Since _lists_ are mutable, you can modify existing values.<jupyter_code>shoppingList.append('sugar')
shoppingList<jupyter_output><empty_output><jupyter_text>You can add items at the end of the _list_ using the _append_ method.<jupyter_code>shoppingList.remove('eggs')
shoppingList<jupyter_output><empty_output><jupyter_text>That said, the 'opposite' _remove_ eliminate matching elements regardless of their position.  However, if it doesn't exist, you'll get an error! Learning Activity:  what happens when you invoke _remove_ and there are duplicates?<jupyter_code>shoppingList2 = ['eggs', 'flour']
shoppingList = shoppingList + shoppingList2
shoppingList<jupyter_output><empty_output><jupyter_text>You can concatenate _lists_<jupyter_code>shoppingList3a = ['bread','milk','sugar']
shoppingList3a += shoppingList2
shoppingList3a<jupyter_output><empty_output><jupyter_text>You can also use "+=" as short-hand instead of retyping the left-hand side of the equation.<jupyter_code>shoppingList3b = shoppingList3a.copy()<jupyter_output><empty_output><jupyter_text>The _copy_ methods is used because ehen you make a straight Python references the same address in memory (read as: points to same variable).  Instead, we duplivate the contents and not the pointer.  I experienced trouble with this - it may save you some debugging time!<jupyter_code>shoppingList3a.sort()
shoppingList3a<jupyter_output><empty_output><jupyter_text>By default it is sorted by character in ascening order.<jupyter_code>fish =[1, 2, 'blue', 'red']<jupyter_output><empty_output><jupyter_text>_Lists_ can also be assigned heterogenous data.<jupyter_code>fish.sort(reverse = True)<jupyter_output><empty_output><jupyter_text>However, you can't _sort_ a _list_ containing different data types The _sort_ method takes two parameters:  _reverse_ and _key_.  If the former is set to _True_. it is descending. So far, I've had no need to use the latter but it might be worth investigating in case you have to use it in the future.<jupyter_code>sorted(shoppingList3b)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup