Skip to content
Snippets Groups Projects
lecture.ipynb 7.07 KiB
Newer Older
nyfelix's avatar
nyfelix committed
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Extended Data Types\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Agenda\n",
    "\n",
    "* Lists (repetition)\n",
    "* Tuples\n",
    "* Sets\n",
    "* Dictionaries"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " ## Introduction\n",
    "\n",
    "* We already know lists\n",
    "* Lists are only one of 4 built-in data types in Phython that store collections of data:\n",
    "  * Lists\n",
    "  * Tuples\n",
    "  * Sets\n",
    "  * Dictionaries\n",
    "* Collections are used to store multiple items in a single variable.\n",
    "* Working with collections is something that makes Python extremly powerful\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Recap of what we know about Lists\n",
    "\n",
    "1. Lists are created with square brackets ```[ ]```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "list = [1,2,5,3]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. List items are ordered, changeable, and allow duplicate values\n",
    "    * Orders: Items have a defined order, and that order will not change unless we add or remove items\n",
    "    * Changable: We can change, add, and remove items in a list after it has been created\n",
    "3. We access list items by thier index (the position in the list, starting with 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "list = [1,2,3,3,2]\n",
    "print(list[3])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Accessing List Items\n",
    "\n",
    "Besides accessing list items with ```[index]``` Python offers some additional options:\n",
    "\n",
    "1. Negative Indexing: ```[-1]``` refers to the last item, ```[-2]``` refers to the second last item etc.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "d\n",
      "c\n"
     ]
    }
   ],
   "source": [
    "my_list = ['a','b','c','d']\n",
    "print(my_list[-1])\n",
    "print(my_list[-2])\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. Instead of selecting a single item we can also select a subset of the list with the schema ```start-index:end-index```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['b', 'c']\n"
     ]
    }
   ],
   "source": [
    "my_list = ['a','b','c','d']\n",
    "print(my_list[1:3])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## List Functions\n",
    "\n",
    "We have already seen the most important list methods (functions). Here is a complete table:\n",
    "\n",
    "| Method    | Description                                                                  |\n",
    "| :-------- | :--------------------------------------------------------------------------- |\n",
    "| append()  | Adds an element at the end of the list                                       |\n",
    "| clear()   | Removes all the elements from the list                                       |\n",
    "| copy()    | Returns a copy of the list                                                   |\n",
    "| count()   | Returns the number of elements with the specified value                      |\n",
    "| extend()  | Add the elements of a list (or any iterable), to the end of the current list |\n",
    "| index()   | Returns the index of the first element with the specified value              |\n",
    "| insert()  | Adds an element at the specified position                                    |\n",
    "| pop()     | Removes the element at the specified position                                |\n",
    "| remove()  | Removes the item with the specified value                                    |\n",
    "| reverse() | Reverses the order of the list                                               |\n",
    "| sort()    | Sorts the list                                                               |\n",
    "\n",
    "See https://www.w3schools.com/python/python_lists_methods.asp for more details."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Tuples\n",
    "\n",
    "* A tuple is a collection which is ordered and **unchangeable**.\n",
    "* Tuples are written with round brackets.\n",
    "* Tuples are orded and allow duplicate values.\n",
    "* Items of tuples are accessed exactly the same way as lists with ```[]```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "my_tuple = (\"mia\", \"mio\", 1)\n",
    "print(my_tuple[2])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* Tuples only know the methods ```count()```and ```index()```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Sets\n",
    "* A set is a collection which is unordered, and unindexed and its itmes are unchangeable.\n",
    "  * Once a set is created, you cannot change its items, but you can add new items.\n",
    "  * Dublicate items will be treated as one item\n",
    "* Like lists and tuples, sets can contain multiple data types\n",
    "* You cannot access items in a set by referring to an index or a key (since they have no order)\n",
    "* Sets are written with curly brackets."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "3\n",
      "5\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "my_set = {1, 5, 3, 1} # last value will be ignored\n",
    "my_set.add(1) # will be ignored\n",
    "for x in my_set:\n",
    "  print(x)\n",
    "print(2 in my_set)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* Sets are highly useful to efficiently remove duplicate values from a collection like a list and to perform common math operations like unions and intersections."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Dictionaries"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}