{"id":403,"date":"2020-08-27T07:02:39","date_gmt":"2020-08-27T07:02:39","guid":{"rendered":"https:\/\/aaidm.buas.nl\/?page_id=403"},"modified":"2020-08-27T19:17:24","modified_gmt":"2020-08-27T19:17:24","slug":"pr1-preparation-for-week-5-lists-and-tuples","status":"publish","type":"page","link":"https:\/\/aaidm.buas.nl\/?page_id=403","title":{"rendered":"PR1 &#8211; Preparation for Week 5 &#8211; Lists and tuples"},"content":{"rendered":"\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Author:<\/td><td>Carlos Santos<\/td><\/tr><tr><td>Learning Line:<\/td><td>Programming<\/td><\/tr><tr><td>Course:<\/td><td>PR1: Introduction to Programming<\/td><\/tr><tr><td>Week:<\/td><td>5<\/td><\/tr><tr><td>Competencies:<\/td><td>Students will be able to effectively define and use variables, programming flow control.<\/td><\/tr><tr><td>BoKS:<\/td><td>\u00ad 3bK2, The student understands the principles of data related software like Python, R, Scala and\/or Java and knows how to write (basic) scripts.<\/td><\/tr><tr><td>Learning Goals:<\/td><td>Creating and manipulating Python Lists and Tuples<\/td><\/tr><\/tbody><\/table><\/figure>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Lists<\/h2>\n\n\n\n<p>Lists&nbsp;are arguably Python\u2019s most versatile, useful&nbsp;data types, and you will find them every.<\/p>\n\n\n\n<p>In short, a list is a ordered collection of values, and are defined by a comma-separated sequence of values inside square brackets <code>[]<\/code>, as shown below:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:false,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;idea&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">&gt;&gt;&gt; thisIsAList = [1,2,3,&quot;I am also an element&quot;, True]<\/pre><\/div>\n\n\n\n<p>The important characteristics of Python lists are as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Lists are ordered.<\/li><li>Lists can contain any arbitrary objects.<\/li><li>List elements can be accessed by index.<\/li><li>Lists are mutable.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Lists Are Ordered<\/h3>\n\n\n\n<p>A list is not merely a collection of objects. It is an ordered collection of objects. The order in which you specify the elements when you define a list is an innate characteristic of that list and is maintained for that list\u2019s lifetime. (You will see a Python data type that is not ordered in the next tutorial on dictionaries.)<\/p>\n\n\n\n<p>Lists that have the same elements in a different order are not the same:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:false,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;idea&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">&gt;&gt;&gt; a = ['foo', 'bar', 'baz', 'qux']\n&gt;&gt;&gt; b = ['baz', 'qux', 'bar', 'foo']\n&gt;&gt;&gt; a == b\nFalse\n&gt;&gt;&gt; a is b\nFalse\n\n&gt;&gt;&gt; [1, 2, 3, 4] == [4, 1, 3, 2]\nFalse<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Lists Can Contain Arbitrary Objects<\/h3>\n\n\n\n<p>A list can contain any assortment of objects. The elements of a list can all be the same type:&gt;&gt;&gt;<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:false,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;idea&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">&gt;&gt;&gt; a = [2, 4, 6, 8]\n&gt;&gt;&gt; a\n[2, 4, 6, 8]<\/pre><\/div>\n\n\n\n<p>Or the elements can be of varying types:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:false,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;idea&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">&gt;&gt;&gt; a = [21.42, 'foobar', 3, 4, 'bark', False, 3.14159]\n&gt;&gt;&gt; a\n[21.42, 'foobar', 3, 4, 'bark', False, 3.14159]<\/pre><\/div>\n\n\n\n<p>List objects needn\u2019t be unique. A given object can appear in a list multiple times:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:false,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;idea&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">&gt;&gt;&gt; a = ['bark', 'meow', 'woof', 'bark', 'cheep', 'bark']\n&gt;&gt;&gt; a\n['bark', 'meow', 'woof', 'bark', 'cheep', 'bark']<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">List Elements Can Be Accessed by Index<\/h3>\n\n\n\n<p>Individual elements in a list can be accessed using an index in square brackets. This is exactly analogous to accessing individual characters in a string. List indexing is zero-based as it is with strings.<\/p>\n\n\n\n<p>Consider the following list:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:false,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;idea&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">&gt;&gt;&gt; a = ['foo', 'bar', 'baz', 'qux', 'quux', 'corge']<\/pre><\/div>\n\n\n\n<p>The first element will have index 0, and the second has the index 1, and so forth&#8230;<\/p>\n\n\n\n<p>Here is Python code to access some elements of&nbsp;<code>a<\/code>:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:false,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;idea&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">&gt;&gt;&gt; a[0]\n'foo'\n&gt;&gt;&gt; a[2]\n'baz'\n&gt;&gt;&gt; a[5]\n'corge'<\/pre><\/div>\n\n\n\n<p>Virtually everything about string indexing works similarly for lists. For example, a negative list index counts from the end of the list:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:false,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;idea&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">&gt;&gt;&gt; a[-1]\n'corge'\n&gt;&gt;&gt; a[-2]\n'quux'\n&gt;&gt;&gt; a[-5]\n'bar'<\/pre><\/div>\n\n\n\n<p>Slicing also works. If&nbsp;<code>a<\/code>&nbsp;is a list, the expression&nbsp;<code>a[m:n]<\/code>&nbsp;returns the portion of&nbsp;<code>a<\/code>&nbsp;from index&nbsp;<code>m<\/code>&nbsp;to, but not including, index&nbsp;<code>n<\/code>:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:false,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;idea&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">&gt;&gt;&gt; a = ['foo', 'bar', 'baz', 'qux', 'quux', 'corge']\n\n&gt;&gt;&gt; a[2:5]\n['baz', 'qux', 'quux']<\/pre><\/div>\n\n\n\n<p>Several Python operators and built-in functions can also be used with lists in ways that are analogous to strings:The&nbsp;<code>in<\/code>&nbsp;and&nbsp;<code>not in<\/code>&nbsp;operators:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:false,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;idea&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">&gt;&gt;&gt; a\n['foo', 'bar', 'baz', 'qux', 'quux', 'corge']\n\n&gt;&gt;&gt; 'qux' in a\nTrue\n&gt;&gt;&gt; 'thud' not in a\nTrue<\/pre><\/div>\n\n\n\n<p>The concatenation (<code>+<\/code>) and replication (<code>*<\/code>) operators:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:false,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;idea&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">&gt;&gt;&gt; a\n['foo', 'bar', 'baz', 'qux', 'quux', 'corge']\n\n&gt;&gt;&gt; a + ['grault', 'garply']\n['foo', 'bar', 'baz', 'qux', 'quux', 'corge', 'grault', 'garply']\n&gt;&gt;&gt; a * 2\n['foo', 'bar', 'baz', 'qux', 'quux', 'corge', 'foo', 'bar', 'baz',\n'qux', 'quux', 'corge']<\/pre><\/div>\n\n\n\n<p>The&nbsp;<code>len()<\/code>,&nbsp;<code>min()<\/code>, and&nbsp;<code>max()<\/code>&nbsp;functions:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:false,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;idea&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">&gt;&gt;&gt; a\n['foo', 'bar', 'baz', 'qux', 'quux', 'corge']\n\n&gt;&gt;&gt; len(a)\n6\n&gt;&gt;&gt; min(a)\n'bar'\n&gt;&gt;&gt; max(a)\n'qux'<\/pre><\/div>\n\n\n\n<p>It\u2019s not an accident that strings and lists behave so similarly. They are both special cases of a more general object type called an iterable, which you will encounter in more detail in the upcoming tutorial on definite iteration.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Lists Are Mutable<\/h3>\n\n\n\n<p>Most of the data types you have encountered so far have been atomic types. Integer or float objects, for example, are primitive units that can\u2019t be further broken down. These types are immutable, meaning that they can\u2019t be changed once they have been assigned. It doesn\u2019t make much sense to think of changing the value of an integer. If you want a different integer, you just assign a different one.<\/p>\n\n\n\n<p>By contrast, the string type is a composite type. Strings are reducible to smaller parts\u2014the component characters. It might make sense to think of changing the characters in a string. But you can\u2019t. In Python, strings are also immutable.<\/p>\n\n\n\n<p>The list is the first mutable data type you have encountered. Once a list has been created, elements can be added, deleted, shifted, and moved around at will. Python provides a wide range of ways to modify lists.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Modifying a Single List Value<\/h4>\n\n\n\n<p>A single value in a list can be replaced by indexing and simple assignment:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:false,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;idea&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">&gt;&gt;&gt; a = ['foo', 'bar', 'baz', 'qux', 'quux', 'corge']\n&gt;&gt;&gt; a\n['foo', 'bar', 'baz', 'qux', 'quux', 'corge']\n\n&gt;&gt;&gt; a[2] = 10\n&gt;&gt;&gt; a[-1] = 20\n&gt;&gt;&gt; a\n['foo', 'bar', 10, 'qux', 'quux', 20]<\/pre><\/div>\n\n\n\n<p>A list item can be deleted with the&nbsp;<code>del<\/code>&nbsp;command:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:false,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;idea&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">&gt;&gt;&gt; a = ['foo', 'bar', 'baz', 'qux', 'quux', 'corge']\n\n&gt;&gt;&gt; del a[3]\n&gt;&gt;&gt; a\n['foo', 'bar', 'baz', 'quux', 'corge<\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">Prepending or Appending Items to a List<\/h4>\n\n\n\n<p>Additional items can be added to the start or end of a list using the&nbsp;<code>+<\/code>&nbsp;concatenation operator or the&nbsp;<code>+=<\/code>&nbsp;augmented assignment operator:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:false,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;idea&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">&gt;&gt;&gt; a = ['foo', 'bar', 'baz', 'qux', 'quux', 'corge']\n\n&gt;&gt;&gt; a += ['grault', 'garply']\n&gt;&gt;&gt; a\n['foo', 'bar', 'baz', 'qux', 'quux', 'corge', 'grault', 'garply']\n\n&gt;&gt;&gt; a = ['foo', 'bar', 'baz', 'qux', 'quux', 'corge']\n\n&gt;&gt;&gt; a = [10, 20] + a\n&gt;&gt;&gt; a\n[10, 20, 'foo', 'bar', 'baz', 'qux', 'quux', 'corge']\n<\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">Methods That Modify a List<\/h4>\n\n\n\n<p>Finally, Python supplies several built-in methods that can be used to modify lists. Information on these methods is detailed below.<\/p>\n\n\n\n<p><code>a.append(&lt;obj&gt;)<\/code><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Appends an object to a list.<\/p><\/blockquote>\n\n\n\n<p><code>a.append(&lt;obj&gt;)<\/code>&nbsp;appends object&nbsp;<code>&lt;obj&gt;<\/code>&nbsp;to the end of list&nbsp;<code>a<\/code>:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:false,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;idea&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">&gt;&gt;&gt; a = ['a', 'b']\n&gt;&gt;&gt; a.append(123)\n&gt;&gt;&gt; a\n['a', 'b', 123]\n<\/pre><\/div>\n\n\n\n<p><code>a.remove(&lt;obj&gt;)<\/code><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Removes an object from a list.<\/p><\/blockquote>\n\n\n\n<p><code>a.remove(&lt;obj&gt;)<\/code>&nbsp;removes object&nbsp;<code>&lt;obj&gt;<\/code>&nbsp;from list&nbsp;<code>a<\/code>. If&nbsp;<code>&lt;obj&gt;<\/code>&nbsp;isn\u2019t in&nbsp;<code>a<\/code>, an exception is raised:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:false,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;idea&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">&gt;&gt;&gt; a = ['foo', 'bar', 'baz', 'qux', 'quux', 'corge']\n&gt;&gt;&gt; a.remove('baz')\n&gt;&gt;&gt; a\n['foo', 'bar', 'qux', 'quux', 'corge']\n\n&gt;&gt;&gt; a.remove('Bark!')\nTraceback (most recent call last):\n  File &quot;&lt;pyshell#13&gt;&quot;, line 1, in &lt;module&gt;\n    a.remove('Bark!')\nValueError: list.remove(x): x not in list<\/pre><\/div>\n\n\n\n<p><code>a.pop(index=-1)<\/code><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Removes an element from a list.<\/p><\/blockquote>\n\n\n\n<p>This method differs from&nbsp;<code>.remove()<\/code>&nbsp;in two ways:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>You specify the index of the item to remove, rather than the object itself.<\/li><li>The method returns a value: the item that was removed.<\/li><\/ol>\n\n\n\n<p><code>a.pop()<\/code>&nbsp;simply removes the last item in the list:&gt;&gt;&gt;<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:false,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;idea&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">&gt;&gt;&gt; a = ['foo', 'bar', 'baz', 'qux', 'quux', 'corge']\n\n&gt;&gt;&gt; a.pop()\n'corge'\n&gt;&gt;&gt; a\n['foo', 'bar', 'baz', 'qux', 'quux']\n\n&gt;&gt;&gt; a.pop()\n'quux'\n&gt;&gt;&gt; a\n['foo', 'bar', 'baz', 'qux']<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Python Tuples<\/h2>\n\n\n\n<p>Python provides another type that is an ordered collection of objects, called a tuple.<\/p>\n\n\n\n<p>Pronunciation varies depending on whom you ask. Some pronounce it as though it were spelled \u201ctoo-ple\u201d (rhyming with \u201cMott the Hoople\u201d), and others as though it were spelled \u201ctup-ple\u201d (rhyming with \u201csupple\u201d). My inclination is the latter, since it presumably derives from the same origin as \u201cquintuple,\u201d \u201csextuple,\u201d \u201coctuple,\u201d and so on, and everyone I know pronounces these latter as though they rhymed with \u201csupple.\u201d<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Defining and Using Tuples<\/h3>\n\n\n\n<p>Tuples are identical to lists in all respects, except for the following properties:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Tuples are defined by enclosing the elements in parentheses (<code>()<\/code>) instead of square brackets (<code>[]<\/code>).<\/li><li>Tuples are immutable.<\/li><\/ul>\n\n\n\n<p>Here is a short example showing a tuple definition, indexing, and slicing:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:false,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;idea&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">&gt;&gt;&gt; t = ('foo', 'bar', 'baz', 'qux', 'quux', 'corge')\n&gt;&gt;&gt; t\n('foo', 'bar', 'baz', 'qux', 'quux', 'corge')\n\n&gt;&gt;&gt; t[0]\n'foo'\n&gt;&gt;&gt; t[-1]\n'corge'\n&gt;&gt;&gt; t[1::2]\n('bar', 'qux', 'corge')\n<\/pre><\/div>\n\n\n\n<p>Never fear! Our favorite string and list reversal mechanism works for tuples as well:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:false,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;idea&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">&gt;&gt;&gt; t[::-1]\n('corge', 'quux', 'qux', 'baz', 'bar', 'foo')\n<\/pre><\/div>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;Even though tuples are defined using parentheses, you still index and slice tuples using square brackets, just as for strings and lists.<\/p>\n\n\n\n<p>Everything you\u2019ve learned about lists\u2014they are ordered, they can contain arbitrary objects, they can be indexed and sliced, they can be nested\u2014is true of tuples as well. But they can\u2019t be modified!<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:false,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;idea&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">&gt;&gt;&gt; t = ('foo', 'bar', 'baz', 'qux', 'quux', 'corge')\n&gt;&gt;&gt; t[2] = 'Bark!'\nTraceback (most recent call last):\n  File &quot;&lt;pyshell#65&gt;&quot;, line 1, in &lt;module&gt;\n    t[2] = 'Bark!'\nTypeError: 'tuple' object does not support item assignment\n<\/pre><\/div>\n\n\n\n<p>Why use a tuple instead of a list?<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Program execution is faster when manipulating a tuple than it is for the equivalent list. (This is probably not going to be noticeable when the list or tuple is small.)<\/li><li>Sometimes you don\u2019t want data to be modified. If the values in the collection are meant to remain constant for the life of the program, using a tuple instead of a list guards against accidental modification.<\/li><li>There is another Python data type that you will encounter shortly called a dictionary, which requires as one of its components a value that is of an immutable type. A tuple can be used for this purpose, whereas a list can\u2019t be.<\/li><\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Author: Carlos Santos Learning Line: Programming Course: PR1: Introduction to Programming Week: 5 Competencies: Students will be able to effectively define and use variables, programming flow control. BoKS: \u00ad 3bK2, The student understands the principles of data related software like Python, R, Scala and\/or Java and knows how to write (basic) scripts. Learning Goals: Creating [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":0,"parent":99,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-403","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>PR1 - Preparation for Week 5 - Lists and tuples - Applied Artificial Intelligence and Data Management<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/aaidm.buas.nl\/?page_id=403\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PR1 - Preparation for Week 5 - Lists and tuples - Applied Artificial Intelligence and Data Management\" \/>\n<meta property=\"og:description\" content=\"Author: Carlos Santos Learning Line: Programming Course: PR1: Introduction to Programming Week: 5 Competencies: Students will be able to effectively define and use variables, programming flow control. BoKS: \u00ad 3bK2, The student understands the principles of data related software like Python, R, Scala and\/or Java and knows how to write (basic) scripts. Learning Goals: Creating [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/aaidm.buas.nl\/?page_id=403\" \/>\n<meta property=\"og:site_name\" content=\"Applied Artificial Intelligence and Data Management\" \/>\n<meta property=\"article:modified_time\" content=\"2020-08-27T19:17:24+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/aaidm.buas.nl\/?page_id=403\",\"url\":\"https:\/\/aaidm.buas.nl\/?page_id=403\",\"name\":\"PR1 - Preparation for Week 5 - Lists and tuples - Applied Artificial Intelligence and Data Management\",\"isPartOf\":{\"@id\":\"https:\/\/aaidm.buas.nl\/#website\"},\"datePublished\":\"2020-08-27T07:02:39+00:00\",\"dateModified\":\"2020-08-27T19:17:24+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/aaidm.buas.nl\/?page_id=403#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/aaidm.buas.nl\/?page_id=403\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/aaidm.buas.nl\/?page_id=403#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/aaidm.buas.nl\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Year 1\",\"item\":\"https:\/\/aaidm.buas.nl\/?page_id=80\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Year 1 &#8211; Block A\",\"item\":\"https:\/\/aaidm.buas.nl\/?page_id=82\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"PR1: Introduction to Programming\",\"item\":\"https:\/\/aaidm.buas.nl\/?page_id=99\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"PR1 &#8211; Preparation for Week 5 &#8211; Lists and tuples\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/aaidm.buas.nl\/#website\",\"url\":\"https:\/\/aaidm.buas.nl\/\",\"name\":\"Applied Artificial Intelligence and Data Management\",\"description\":\"Course Information\",\"publisher\":{\"@id\":\"https:\/\/aaidm.buas.nl\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/aaidm.buas.nl\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/aaidm.buas.nl\/#organization\",\"name\":\"Applied Artificial Intelligence and Data Management\",\"url\":\"https:\/\/aaidm.buas.nl\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/aaidm.buas.nl\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/aaidm.buas.nl\/wp-content\/uploads\/2020\/06\/buas-logo.png\",\"contentUrl\":\"https:\/\/aaidm.buas.nl\/wp-content\/uploads\/2020\/06\/buas-logo.png\",\"width\":382,\"height\":132,\"caption\":\"Applied Artificial Intelligence and Data Management\"},\"image\":{\"@id\":\"https:\/\/aaidm.buas.nl\/#\/schema\/logo\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PR1 - Preparation for Week 5 - Lists and tuples - Applied Artificial Intelligence and Data Management","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/aaidm.buas.nl\/?page_id=403","og_locale":"en_US","og_type":"article","og_title":"PR1 - Preparation for Week 5 - Lists and tuples - Applied Artificial Intelligence and Data Management","og_description":"Author: Carlos Santos Learning Line: Programming Course: PR1: Introduction to Programming Week: 5 Competencies: Students will be able to effectively define and use variables, programming flow control. BoKS: \u00ad 3bK2, The student understands the principles of data related software like Python, R, Scala and\/or Java and knows how to write (basic) scripts. Learning Goals: Creating [&hellip;]","og_url":"https:\/\/aaidm.buas.nl\/?page_id=403","og_site_name":"Applied Artificial Intelligence and Data Management","article_modified_time":"2020-08-27T19:17:24+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/aaidm.buas.nl\/?page_id=403","url":"https:\/\/aaidm.buas.nl\/?page_id=403","name":"PR1 - Preparation for Week 5 - Lists and tuples - Applied Artificial Intelligence and Data Management","isPartOf":{"@id":"https:\/\/aaidm.buas.nl\/#website"},"datePublished":"2020-08-27T07:02:39+00:00","dateModified":"2020-08-27T19:17:24+00:00","breadcrumb":{"@id":"https:\/\/aaidm.buas.nl\/?page_id=403#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/aaidm.buas.nl\/?page_id=403"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/aaidm.buas.nl\/?page_id=403#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/aaidm.buas.nl\/"},{"@type":"ListItem","position":2,"name":"Year 1","item":"https:\/\/aaidm.buas.nl\/?page_id=80"},{"@type":"ListItem","position":3,"name":"Year 1 &#8211; Block A","item":"https:\/\/aaidm.buas.nl\/?page_id=82"},{"@type":"ListItem","position":4,"name":"PR1: Introduction to Programming","item":"https:\/\/aaidm.buas.nl\/?page_id=99"},{"@type":"ListItem","position":5,"name":"PR1 &#8211; Preparation for Week 5 &#8211; Lists and tuples"}]},{"@type":"WebSite","@id":"https:\/\/aaidm.buas.nl\/#website","url":"https:\/\/aaidm.buas.nl\/","name":"Applied Artificial Intelligence and Data Management","description":"Course Information","publisher":{"@id":"https:\/\/aaidm.buas.nl\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/aaidm.buas.nl\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/aaidm.buas.nl\/#organization","name":"Applied Artificial Intelligence and Data Management","url":"https:\/\/aaidm.buas.nl\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/aaidm.buas.nl\/#\/schema\/logo\/image\/","url":"https:\/\/aaidm.buas.nl\/wp-content\/uploads\/2020\/06\/buas-logo.png","contentUrl":"https:\/\/aaidm.buas.nl\/wp-content\/uploads\/2020\/06\/buas-logo.png","width":382,"height":132,"caption":"Applied Artificial Intelligence and Data Management"},"image":{"@id":"https:\/\/aaidm.buas.nl\/#\/schema\/logo\/image\/"}}]}},"_links":{"self":[{"href":"https:\/\/aaidm.buas.nl\/index.php?rest_route=\/wp\/v2\/pages\/403","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aaidm.buas.nl\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/aaidm.buas.nl\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/aaidm.buas.nl\/index.php?rest_route=\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/aaidm.buas.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=403"}],"version-history":[{"count":6,"href":"https:\/\/aaidm.buas.nl\/index.php?rest_route=\/wp\/v2\/pages\/403\/revisions"}],"predecessor-version":[{"id":414,"href":"https:\/\/aaidm.buas.nl\/index.php?rest_route=\/wp\/v2\/pages\/403\/revisions\/414"}],"up":[{"embeddable":true,"href":"https:\/\/aaidm.buas.nl\/index.php?rest_route=\/wp\/v2\/pages\/99"}],"wp:attachment":[{"href":"https:\/\/aaidm.buas.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}