diff options
author | Anoop Jacob Thomas | 2010-10-11 23:03:49 +0530 |
---|---|---|
committer | Anoop Jacob Thomas | 2010-10-11 23:03:49 +0530 |
commit | 1b60f79ffd3ced6e32e05d3cca9b62783f953ced (patch) | |
tree | 1f1cf1291b046e9b1b5e0709d9eac963bd87aead | |
parent | dce0bafcd9cd6355974984f7aa3763c0de0dfd91 (diff) | |
download | st-scripts-1b60f79ffd3ced6e32e05d3cca9b62783f953ced.tar.gz st-scripts-1b60f79ffd3ced6e32e05d3cca9b62783f953ced.tar.bz2 st-scripts-1b60f79ffd3ced6e32e05d3cca9b62783f953ced.zip |
added slides for dictionaries.
-rw-r--r-- | dictionaries/script.rst | 10 | ||||
-rw-r--r-- | dictionaries/slides.org | 117 | ||||
-rw-r--r-- | dictionaries/slides.tex | 229 |
3 files changed, 354 insertions, 2 deletions
diff --git a/dictionaries/script.rst b/dictionaries/script.rst index 63fca2a..31bc98e 100644 --- a/dictionaries/script.rst +++ b/dictionaries/script.rst @@ -35,6 +35,8 @@ Before we can proceed, start your IPython interpreter with the {{{ 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 the interpreter to see the content of the dictionary. Notice that 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 @@ It returned the ``list`` of values in the dictionary. 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 --git a/dictionaries/slides.org b/dictionaries/slides.org new file mode 100644 index 0000000..7ad28db --- /dev/null +++ b/dictionaries/slides.org @@ -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 --git a/dictionaries/slides.tex b/dictionaries/slides.tex new file mode 100644 index 0000000..2a65377 --- /dev/null +++ b/dictionaries/slides.tex @@ -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} |