{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Lists, tuples, dictionaries\n\nLists\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "list1 = [\"physics\", \"chemistry\", 1997, 2000]\nlist2 = [1, 2, 3, 4, 5]\nlist3 = [\"a\", \"b\", \"c\", \"d\"]\n\nprint(\"list1[0]: \", list1[0])\nprint(\"list2[1:5]: \", list2[1:5])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Updating lists\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "list1[2] = 2001\nprint(\"New value available at index 2 : \", list1[2])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Delete list item\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "del list1[2]\nprint(\"After deleting value at index 2 : \", list1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "List operations\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "len(list1) # length of list\nlist1 + list2 # concatenate list\n[\"ho\"] * 4 # repetition\n2000 in list1 # membership\nfor obj in list1: # iterations\n print(obj)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "List functions, methods\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "list1.append(\"newVal\") # append value\nlist1.append(2000)\nlist1.count(2000) # count how many times object occurs in list\nlist1.index(2000) # lowest index in list object appears\nlist1.insert(3, 2022) # insert object into list at index#\nlist1.pop() # remove last object from list\nlist1.remove(2022) # remove object from list\nlist1.reverse() # reverse objects in list\nlist2.sort() # sorts objects in list. Works only if list contains object of same dtype" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Tuples\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "tup1 = (\"physics\", \"chemistry\", 1997, 2000)\ntup2 = (1, 2, 3, 4, 5, 6, 7)\nprint(\"tup1[0]: \", tup1[0])\nprint(\"tup2[1:5]: \", tup2[1:5])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Updating tuples\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "tup3 = tup1 + tup2\nprint(tup3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Tuples operations\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "len(tup1) # length of list\ntup1 + tup2 # concatenate list\n(\"ho\") * 4 # repetition\n2000 in tup1 # membership\nfor obj in tup1: # iterations\n print(obj)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Dictionary\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "dict1 = {\"Name\": \"Zara\", \"Age\": 7, \"Class\": \"First\"}\nprint(\"dict1['Name']: \", dict1[\"Name\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Updating dictionary\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "dict1[\"Age\"] = 8 # update existing entry\ndict1[\"School\"] = \"DPS School\" # Add new entry\n\nprint(\"dict1['Age']: \", dict1[\"Age\"])\nprint(\"dict1['School']: \", dict1[\"School\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Delete elements\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "del dict1[\"Name\"] # remove entry with key 'Name'\nprint(\"dict1['Age']: \", dict1[\"Age\"])\nprint(\"dict1['School']: \", dict1[\"School\"])\ndict1.clear() # remove all entries in dict\ndel dict1 # delete entire dictionary" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Dictionary functions, methods\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "dict1 = {\"Name\": \"Zara\", \"Age\": 7, \"Class\": \"First\"}\ndict1.copy() # returns shallow copy of dict\ndict.fromkeys(dict1) # new dict with keys from sequence and values set to value\ndict1.items() # returns list of dict (key, value) tuple pairs\ndict1.keys() # return list of keys\nfor key, value in dict1.items(): # iterations\n print(key, value)" ] } ], "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.8.10" } }, "nbformat": 4, "nbformat_minor": 0 }