# HG changeset patch # User Nishanth # Date 1286820375 -19800 # Node ID 56dc2183f1de6a5ea5ee9397d70c76b6e543978d # Parent 04fc3e7a04807aa2f68cec96e05bdd2077cbbd48# Parent a130a1f494c3be1344045fda11cefeaa8b6777e9 Merged diff -r 04fc3e7a0480 -r 56dc2183f1de dictionaries/script.rst --- a/dictionaries/script.rst Mon Oct 11 23:32:30 2010 +0530 +++ b/dictionaries/script.rst Mon Oct 11 23:36:15 2010 +0530 @@ -35,6 +35,8 @@ {{{ start ipython interpreter by issuing command ipython -pylab }}} +{{{ switch to next slide, Creating dictionary }}} + Let us start by creating an empty dictionary, type the following in your IPython interpreter. :: @@ -74,6 +76,8 @@ in dictionaries the order cannot be predicted and you can see that the values are not in the order that we entered in. +{{{ switch to next slide, accessing elements }}} + Like in lists, the elements in a dictionary can be accessed using the index, here the index is the key. Try, :: @@ -153,14 +157,16 @@ exercise }}} Now let us try to print the data in the dictionary. We can use ``for`` -loop to iterate. +loop to iterate. Pause here and try to do it yourself. + +It can be solved as, :: for each in extensions.keys(): print each, "-->", extensions[each] -{{{ switch to next slide, recap }}} +{{{ switch to next slide, summary }}} This brings us to the end of this tutorial, we learned dictionaries and saw how to create an empty dictionary, build a dictionary with diff -r 04fc3e7a0480 -r 56dc2183f1de dictionaries/slides.org --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dictionaries/slides.org Mon Oct 11 23:36:15 2010 +0530 @@ -0,0 +1,117 @@ +#+LaTeX_CLASS: beamer +#+LaTeX_CLASS_OPTIONS: [presentation] +#+BEAMER_FRAME_LEVEL: 1 + +#+BEAMER_HEADER_EXTRA: \usetheme{Warsaw}\usecolortheme{default}\useoutertheme{infolines}\setbeamercovered{transparent} +#+COLUMNS: %45ITEM %10BEAMER_env(Env) %10BEAMER_envargs(Env Args) %4BEAMER_col(Col) %8BEAMER_extra(Extra) +#+PROPERTY: BEAMER_col_ALL 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 :ETC + +#+LaTeX_CLASS: beamer +#+LaTeX_CLASS_OPTIONS: [presentation] + +#+LaTeX_HEADER: \usepackage[english]{babel} \usepackage{ae,aecompl} +#+LaTeX_HEADER: \usepackage{mathpazo,courier,euler} \usepackage[scaled=.95]{helvet} + +#+LaTeX_HEADER: \usepackage{listings} + +#+LaTeX_HEADER:\lstset{language=Python, basicstyle=\ttfamily\bfseries, +#+LaTeX_HEADER: commentstyle=\color{red}\itshape, stringstyle=\color{darkgreen}, +#+LaTeX_HEADER: showstringspaces=false, keywordstyle=\color{blue}\bfseries} + +#+TITLE: Dictionaries +#+AUTHOR: FOSSEE +#+EMAIL: info@fossee.in +#+DATE: + +#+DESCRIPTION: +#+KEYWORDS: +#+LANGUAGE: en +#+OPTIONS: H:3 num:nil toc:nil \n:nil @:t ::t |:t ^:t -:t f:t *:t <:t +#+OPTIONS: TeX:t LaTeX:nil skip:nil d:nil todo:nil pri:nil tags:not-in-toc + +* Outline + - Creating dictionaries + - empty dictionaries + - with data + - Keys and values + - Checking for elements + - Iterating over elements + +* Overview of Dictionaries + - A dictionary contains meaning of words + - /Word/ is the /key/ here. + - /Meaning/ is the /value/ here. + - A Key-Value pair data structure + - Provide key-value mappings + +* Creating dictionary + - Empty dictionary + - ~mt_dict = {}~ + - ~[]~ - lists + - ~{}~ - dictionaries + - With data + #+begin_src python + extensions = {'jpg' : 'JPEG Image', + 'py' : 'Python script', + 'html' : 'Html document', + 'pdf' : 'Portable Document Format'} + #+end_src + + *Note* - ordering in dictionaries cannot be relied on +* Accessing Elements + - syntax + : extensions[key] + + : In []: print extensions['jpg'] + : Out []: JPEG Image + : In []: print extensions['zip'] +* Adding and Deleting values + - Adding a new value + : In []: extension['cpp'] = 'C++ code' + adds a new key /cpp/ with /C++ code/ as value + - Deleting values + : In []: del extensions['pdf'] + deletes the key-value pair identified by /pdf/ + - Changing value associated with a key + : In []: extension['cpp'] = 'C++ source code' + changes the value of the existing key +* Checking for container-ship of keys + : In []: 'py' in extensions + : Out []: True + Returns *True* if the /key/ is found. + : In []: 'odt' in extensions + : Out []: False + Returns *False* if the /key/ is not found. + +* Retrieve keys and values + - ~.keys()~ method + : In []: extensions.keys() + Returns a list of keys in the dictionary. + - ~.values()~ method + : In []: extensions.values() + Returns the list of values in the dictionary. +* Exercise 1 + Print the keys and values in the dictionary one by one. +* Summary + - Creating dictionaries + - empty dictionaries + - with data + - ~.keys()~ method + - ~.values()~ method + - Iterating over dictionaries +* Thank you! +#+begin_latex + \begin{block}{} + \begin{center} + This spoken tutorial has been produced by the + \textcolor{blue}{FOSSEE} team, which is funded by the + \end{center} + \begin{center} + \textcolor{blue}{National Mission on Education through \\ + Information \& Communication Technology \\ + MHRD, Govt. of India}. + \end{center} + \end{block} +#+end_latex + + diff -r 04fc3e7a0480 -r 56dc2183f1de dictionaries/slides.tex --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dictionaries/slides.tex Mon Oct 11 23:36:15 2010 +0530 @@ -0,0 +1,229 @@ +% Created 2010-10-11 Mon 23:02 +\documentclass[presentation]{beamer} +\usepackage[latin1]{inputenc} +\usepackage[T1]{fontenc} +\usepackage{fixltx2e} +\usepackage{graphicx} +\usepackage{longtable} +\usepackage{float} +\usepackage{wrapfig} +\usepackage{soul} +\usepackage{t1enc} +\usepackage{textcomp} +\usepackage{marvosym} +\usepackage{wasysym} +\usepackage{latexsym} +\usepackage{amssymb} +\usepackage{hyperref} +\tolerance=1000 +\usepackage[english]{babel} \usepackage{ae,aecompl} +\usepackage{mathpazo,courier,euler} \usepackage[scaled=.95]{helvet} +\usepackage{listings} +\lstset{language=Python, basicstyle=\ttfamily\bfseries, +commentstyle=\color{red}\itshape, stringstyle=\color{darkgreen}, +showstringspaces=false, keywordstyle=\color{blue}\bfseries} +\providecommand{\alert}[1]{\textbf{#1}} + +\title{Dictionaries} +\author{FOSSEE} +\date{} + +\usetheme{Warsaw}\usecolortheme{default}\useoutertheme{infolines}\setbeamercovered{transparent} +\begin{document} + +\maketitle + + + + + + + + + +\begin{frame} +\frametitle{Outline} +\label{sec-1} + +\begin{itemize} +\item Creating dictionaries + +\begin{itemize} +\item empty dictionaries +\item with data +\end{itemize} + +\item Keys and values +\item Checking for elements +\item Iterating over elements +\end{itemize} +\end{frame} +\begin{frame} +\frametitle{Overview of Dictionaries} +\label{sec-2} + +\begin{itemize} +\item A dictionary contains meaning of words + +\begin{itemize} +\item \emph{Word} is the \emph{key} here. +\item \emph{Meaning} is the \emph{value} here. +\end{itemize} + +\item A Key-Value pair data structure + +\begin{itemize} +\item Provide key-value mappings +\end{itemize} + +\end{itemize} +\end{frame} +\begin{frame}[fragile] +\frametitle{Creating dictionary} +\label{sec-3} + +\begin{itemize} +\item Empty dictionary + +\begin{itemize} +\item \texttt{mt\_dict = \{\}} + +\begin{itemize} +\item \texttt{[]} - lists +\item \texttt{\{\}} - dictionaries +\end{itemize} + +\end{itemize} + +\item With data +\begin{verbatim} +extensions = {'jpg' : 'JPEG Image', + 'py' : 'Python script', + 'html' : 'Html document', + 'pdf' : 'Portable Document Format'} +\end{verbatim} + + \textbf{Note} - ordering in dictionaries cannot be relied on +\end{itemize} +\end{frame} +\begin{frame}[fragile] +\frametitle{Accessing Elements} +\label{sec-4} + +\begin{itemize} +\item syntax +\begin{verbatim} + extensions[key] +\end{verbatim} + +\end{itemize} + + +\begin{verbatim} + In []: print extensions['jpg'] + Out []: JPEG Image + In []: print extensions['zip'] +\end{verbatim} +\end{frame} +\begin{frame}[fragile] +\frametitle{Adding and Deleting values} +\label{sec-5} + +\begin{itemize} +\item Adding a new value +\begin{verbatim} + In []: extension['cpp'] = 'C++ code' +\end{verbatim} + + adds a new key \emph{cpp} with \emph{C++ code} as value +\item Deleting values +\begin{verbatim} + In []: del extensions['pdf'] +\end{verbatim} + + deletes the key-value pair identified by \emph{pdf} +\item Changing value associated with a key +\begin{verbatim} + In []: extension['cpp'] = 'C++ source code' +\end{verbatim} + + changes the value of the existing key +\end{itemize} +\end{frame} +\begin{frame}[fragile] +\frametitle{Checking for container-ship of keys} +\label{sec-6} + +\begin{verbatim} + In []: 'py' in extensions + Out []: True +\end{verbatim} + + Returns \textbf{True} if the \emph{key} is found. +\begin{verbatim} + In []: 'odt' in extensions + Out []: False +\end{verbatim} + + Returns \textbf{False} if the \emph{key} is not found. +\end{frame} +\begin{frame}[fragile] +\frametitle{Retrieve keys and values} +\label{sec-7} + +\begin{itemize} +\item \texttt{.keys()} method +\begin{verbatim} + In []: extensions.keys() +\end{verbatim} + + Returns a list of keys in the dictionary. +\item \texttt{.values()} method +\begin{verbatim} + In []: extensions.values() +\end{verbatim} + + Returns the list of values in the dictionary. +\end{itemize} +\end{frame} +\begin{frame} +\frametitle{Exercise 1} +\label{sec-8} + + Print the keys and values in the dictionary one by one. +\end{frame} +\begin{frame} +\frametitle{Summary} +\label{sec-9} + +\begin{itemize} +\item Creating dictionaries + +\begin{itemize} +\item empty dictionaries +\item with data +\end{itemize} + +\item \texttt{.keys()} method +\item \texttt{.values()} method +\item Iterating over dictionaries +\end{itemize} +\end{frame} +\begin{frame} +\frametitle{Thank you!} +\label{sec-10} + + \begin{block}{} + \begin{center} + This spoken tutorial has been produced by the + \textcolor{blue}{FOSSEE} team, which is funded by the + \end{center} + \begin{center} + \textcolor{blue}{National Mission on Education through \\ + Information \& Communication Technology \\ + MHRD, Govt. of India}. + \end{center} + \end{block} +\end{frame} + +\end{document}