summaryrefslogtreecommitdiff
path: root/dictionaries
diff options
context:
space:
mode:
authorJovina2011-07-06 16:35:05 +0530
committerJovina2011-07-06 16:35:05 +0530
commitab0b9d6fa8b1971433d1daf092afeb83aae2256c (patch)
treec4375ac3b92f9cc44c86185d8765bf65d2e94a4a /dictionaries
parent8b90cd2534e9b68c249f94a384f4d815c05fc616 (diff)
downloadst-scripts-ab0b9d6fa8b1971433d1daf092afeb83aae2256c.tar.gz
st-scripts-ab0b9d6fa8b1971433d1daf092afeb83aae2256c.tar.bz2
st-scripts-ab0b9d6fa8b1971433d1daf092afeb83aae2256c.zip
Major changes to script & slides of 'Dictionaries'.
Diffstat (limited to 'dictionaries')
-rw-r--r--dictionaries/script.rst265
-rw-r--r--dictionaries/slides.org111
-rw-r--r--dictionaries/slides.tex202
3 files changed, 370 insertions, 208 deletions
diff --git a/dictionaries/script.rst b/dictionaries/script.rst
index a8099c8..e272e07 100644
--- a/dictionaries/script.rst
+++ b/dictionaries/script.rst
@@ -4,9 +4,8 @@
.. At the end of this tutorial, you will be able to
.. 1. Create dictionaries
-.. #. Add data to dictionaries
-.. #. Retrieve data
-.. #. use ``.keys()`` and ``.values()`` methods
+.. #. Add and delete data from dictionaries
+.. #. Retrieve data from dictionaries
.. #. Check for container-ship of keys
.. #. Iterate over elements
@@ -23,175 +22,343 @@
Language Reviewer : Bhanukiran
Checklist OK? : <11-11-2010, Anand, OK> [2010-10-05]
-.. #[Puneeth: Quickref]
============
Dictionaries
============
-{{{ show the welcome slide }}}
+.. L1
-Welcome to the spoken tutorial on dictionaries.
+{{{ Show the first slide containing title, name of the production
+team along with the logo of MHRD }}}
-{{{ switch to next slide, outline slide }}}
+.. R1
-In this tutorial, we will see how to create empty dictionaries, learn
-about keys and values of dictionaries. Checking for elements and
-iterating over elements.
+Hello friends and Welcome to the spoken tutorial on 'dictionaries'.
+
+.. L2
+
+{{{ switch to slide containing objectives }}}
+
+.. R2
+
+At the end of this tutorial, you will be able to,
+
+ 1. Create dictionaries.
+ #. Add and delete data from dictionaries.
+ #. Retrieve data from dictionaries.
+ #. Check for container-ship of keys.
+ #. Iterate over elements.
+
+.. L3
+
+{{{ Switch to the pre-requisite slide }}}
+
+.. R3
+
+Before beginning this tutorial,we would suggest you to complete the
+tutorial on "Basic datatypes and operators".
+
+.. L4
{{{ switch to next slide on overview of dictionaries }}}
-A dictionary in general, is designed to look up meanings of
+.. R4
+
+A dictionary in general, is designed to look up for meanings of
words. Similarly, Python dictionary is also designed to look up for a
specific key and retrieve the corresponding value. Dictionaries are
data structures that provide key-value mappings. Dictionaries are
similar to lists except that instead of the values having integer
indexes, dictionaries have keys or strings as indexes.
-We need ipython interpreter for this tutorial, start it by issuing the
-command ``ipython`` in command line.
+.. R5
+
+We start our ipython interpreter as,
-.. #[Puneeth: We don't need pylab]
+.. L5
-{{{ start ipython interpreter by issuing command ipython }}}
+{{{ Open the terminal }}}
+::
-{{{ switch to next slide, Creating dictionary }}}
+ ipython
-Let us start by creating an empty dictionary, type the following in
+.. R6
+
+Let us start by creating an empty dictionary. Type the following in
your IPython interpreter.
+
+.. L6
::
mt_dict = {}
-Notice that unlike lists, curly braces are used define ``dictionary``.
+.. R7
-{{{ move the mouse over curly braces to grab attention }}}
+Notice that unlike lists, curly braces are used to define a ``dictionary``.
+
+.. L7
+
+{{{ move the mouse over curly braces }}}
+
+.. R8
Now let us see how to create a non-empty dictionary,
+
+.. L8
::
- extensions = {'jpg' : 'JPEG Image', 'py' : 'Python script', 'html' : 'Html document', 'pdf' : 'Portable Document Format'}
+ extensions = {'jpg' : 'JPEG Image', 'py' : 'Python script',
+ 'html' : 'Html document', 'pdf' : 'Portable Document Format'}
+
+.. R9
-Notice that each key-value pair is separated by a comma
+Notice that each key-value pair is separated by a comma.
-{{{ move the mouse over the commas to grab attention }}}
+.. L9
+
+{{{ move the mouse over the commas }}}
+
+.. R10
and each key and value are separated using a colon.
-{{{ move the mouse over the colon one by one to grab attention }}}
+.. L10
+
+{{{ move the mouse over the colon one by one }}}
-Here, we defined four entries in the dictionary extensions. The keys
-are
+.. R11
-{{{ spell the keys letter by letter }}}
+Here, we have defined four entries in the dictionary extensions.
+The keys are
jpg, py, html, and pdf.
-Simply type,
+Simply type,extensions in the interpreter to see the content
+of the dictionary.
+
+.. L11
::
extensions
-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.
+.. R12
+
+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.
+
+.. L12
{{{ switch to next slide, accessing elements }}}
+.. R13
+
Like in lists, the elements in a dictionary can be accessed using the
-index, here the index is the key. Try,
+index, here the index is the key. We type,
+
+.. L13
+
+{{{ Switch to terminal }}}
::
print extensions['jpg']
+.. R14
+
It printed JPEG Image. And now try,
+
+.. L14
::
print extensions['zip']
+.. R15
+
Well it gave us an error, saying that the key 'zip' is not in the
dictionary.
Pause here for some time and try few more keys. Also try jpg in
capital letters.
-
-{{{ switch to next slide, adding and deleting keys and values in
-dictionaries }}}
-
+<continue from paused state>
Well that was about creating dictionaries, now how do we add or delete
items. We can add new items into dictionaries as,
+
+.. L15
::
extensions['cpp'] = 'C++ code'
+.. R16
+
and delete items using the ``del`` keyword as,
+
+.. L16
::
- del extension['pdf']
+ del extensions['pdf']
+
+.. R17
Let us check the content of the dictionary now,
+
+.. L17
::
extensions
+.. R18
+
So the changes have been made. Now let us try one more thing,
+
+.. L18
::
extensions['cpp'] = 'C++ source code'
extensions
+.. R19
+
As you can see, it neither added a new thing nor gave an error, but it
simply replaced the existing value with the new one.
Now let us learn how to check if a particular key is present in the
-dictionary. For that we can use ``in``,
+dictionary. For that we can use the method ``in``,
+
+.. L19
::
'py' in extensions
'odt' in extensions
+.. R20
+
It will return ``True`` if the key is found in the dictionary, and
will return ``False`` if key is not present. Note that we can check
only for container-ship of keys in dictionaries and not values.
+.. L20
+
{{{ switch to next slide, Retrieve keys and values }}}
+.. R21
+
Now let us see how to retrieve the keys and values. We can use the
method ``keys()`` for getting a list of the keys in a particular
dictionary and the method ``values()`` for getting a list of
-values. Let us try them,
+values.
+
+.. R22
+
+Let us try them,
+
+.. L22
+
+{{{ Switch to terminal }}}
::
extensions.keys()
+.. R23
+
It returned the ``list`` of keys in the dictionary extensions. And now
the other one,
+
+.. L23
::
extensions.values()
+.. R24
+
It returned the ``list`` of values in the dictionary.
-{{{ switch to next slide, problem statement for the next solved
-exercise }}}
+Pause the video here, try out the following exercise and resume the video.
+
+.. L24
+
+.. L25
+
+{{{ Show slide with exercise 1 }}}
+
+.. R25
+
+ Print the keys and values in the dictionary one by one.
+
+.. R26
+
+Switch to terminal for solution.
-Now let us try to print the data in the dictionary. We can use ``for``
-loop to iterate. Pause here and try to do it yourself.
+.. L26
-It can be solved as,
+{{{continue from paused state}}}
+{{{ Switch to the terminal }}}
::
for each in extensions.keys():
print each, "-->", extensions[each]
-{{{ switch to next slide, summary }}}
+.. L27
+
+{{{ Show summary slide }}}
+
+.. R27
+
+This brings us to the end of this tutorial.In this tutorial,
+we have learnt to,
+
+ 1. Create dictionaries namely --
+ - empty dictionaries
+ - dictionaries with data.
+ #. Access elements in the dictionaries using the keys.
+ #. Add elements to a dictionary by assigning a value to a key.
+ #. Delete elements from a dictionary by using the function ``del``.
+ #. Retrieve the keys and values by using the methods ``.keys()`` and
+ ``.values()`` respectively.
+ #. Iterate over elements of a dictionary using a ``for`` loop.
+
+.. L28
+
+{{{Show self assessment questions slide}}}
+
+.. R28
+
+Here are some self assessment questions for you to solve
+
+
+1. Container-ship of values can be checked in a python dictionary
+
+ - True
+ - False
+
+2. Consider the python dictionary ``x = {'a' : ['a','b','c'], 'b' :
+ (1, 2, 3), 1 : {1 : 'one', 2 : 'two'}, 10 : {10 : 'ten', 11 :
+ 'eleven'}}``. What will the following code return?
+ ``(1, 2, 3) in x.values()``.
+
+ - True
+ - False
+ - Container-ship of values cannot be checked in dictionaries
+ - The dictionary is invalid
+
+.. L29
+
+{{{solution of self assessment questions on slide}}}
+
+.. R29
+
+And the answers,
+
+1. False.Container-ship of only keys can be checked in a python
+ dictionary.
+
+2. True
+
+.. L30
-This brings us to the end of this tutorial, we learned dictionaries
-and saw how to create an empty dictionary, build a dictionary with
-some data in it, adding data, ``keys()`` and ``values()`` methods, and
-iterating over the dictionaries.
+{{{ switch to thank you slide }}}
-{{{ switch to next slide, thank you slide }}}
+.. R30
+Hope you have enjoyed this tutorial and found it useful.
Thank you!
diff --git a/dictionaries/slides.org b/dictionaries/slides.org
index 7ad28db..b618829 100644
--- a/dictionaries/slides.org
+++ b/dictionaries/slides.org
@@ -18,7 +18,7 @@
#+LaTeX_HEADER: commentstyle=\color{red}\itshape, stringstyle=\color{darkgreen},
#+LaTeX_HEADER: showstringspaces=false, keywordstyle=\color{blue}\bfseries}
-#+TITLE: Dictionaries
+#+TITLE:
#+AUTHOR: FOSSEE
#+EMAIL: info@fossee.in
#+DATE:
@@ -29,7 +29,25 @@
#+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
+*
+#+begin_latex
+\begin{center}
+\vspace{12pt}
+\textcolor{blue}{\huge Dictionaries}
+\end{center}
+\vspace{18pt}
+\begin{center}
+\vspace{10pt}
+\includegraphics[scale=0.95]{../images/fossee-logo.png}\\
+\vspace{5pt}
+\scriptsize Developed by FOSSEE Team, IIT-Bombay. \\
+\scriptsize Funded by National Mission on Education through ICT\\
+\scriptsize MHRD,Govt. of India\\
+\includegraphics[scale=0.30]{../images/iitb-logo.png}\\
+\end{center}
+#+end_latex
+
+* Objectives
- Creating dictionaries
- empty dictionaries
- with data
@@ -44,45 +62,9 @@
- 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()
@@ -91,27 +73,56 @@
: In []: extensions.values()
Returns the list of values in the dictionary.
* Exercise 1
- Print the keys and values in the dictionary one by one.
+ - Print the keys and values in the dictionary one by one.
* Summary
- - Creating dictionaries
+ In this tutorial, we have learnt to,
+
+ - Create dictionaries namely --
- empty dictionaries
- - with data
- - ~.keys()~ method
- - ~.values()~ method
- - Iterating over dictionaries
-* Thank you!
+ - dictionaries with data.
+ - Access elements in the dictionaries using the keys.
+ - Add elements to a dictionary by assigning a value to a key.
+ - Delete elements from a dictionary by using the function ``del''.
+ - Retrieve the keys and values by using the methods ``.keys()'' and
+ ``.values()'' respectively.
+ - Iterate over elements of a dictionary using a ``for'' loop.
+* Evaluation
+1. Container-ship of values can be checked in a python dictionary
+
+ - True
+ - False
+
+2. Consider the python dictionary
+
+ : x = {'a':['a','b','c'], 'b':(1, 2, 3),
+ : 1:{1:'one', 2:'two'},
+ : 10:{10:'ten', 11:'eleven'}}
+
+ What will the following code return?
+ ~(1, 2, 3) in x.values()~
+
+ - True
+ - False
+ - Container-ship of values cannot be checked in dictionaries
+ - The dictionary is invalid
+* Solutions
+1. False
+
+2. True
+*
#+begin_latex
\begin{block}{}
\begin{center}
- This spoken tutorial has been produced by the
- \textcolor{blue}{FOSSEE} team, which is funded by the
+ \textcolor{blue}{\Large THANK YOU!}
\end{center}
+ \end{block}
+\begin{block}{}
\begin{center}
- \textcolor{blue}{National Mission on Education through \\
- Information \& Communication Technology \\
- MHRD, Govt. of India}.
+ For more Information, visit our website\\
+ \url{http://fossee.in/}
\end{center}
\end{block}
#+end_latex
+
diff --git a/dictionaries/slides.tex b/dictionaries/slides.tex
index 2a65377..b2522b7 100644
--- a/dictionaries/slides.tex
+++ b/dictionaries/slides.tex
@@ -1,4 +1,4 @@
-% Created 2010-10-11 Mon 23:02
+% Created 2011-07-06 Wed 16:17
\documentclass[presentation]{beamer}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
@@ -8,7 +8,6 @@
\usepackage{float}
\usepackage{wrapfig}
\usepackage{soul}
-\usepackage{t1enc}
\usepackage{textcomp}
\usepackage{marvosym}
\usepackage{wasysym}
@@ -24,14 +23,13 @@ commentstyle=\color{red}\itshape, stringstyle=\color{darkgreen},
showstringspaces=false, keywordstyle=\color{blue}\bfseries}
\providecommand{\alert}[1]{\textbf{#1}}
-\title{Dictionaries}
+\title{}
\author{FOSSEE}
\date{}
\usetheme{Warsaw}\usecolortheme{default}\useoutertheme{infolines}\setbeamercovered{transparent}
\begin{document}
-\maketitle
@@ -41,18 +39,35 @@ showstringspaces=false, keywordstyle=\color{blue}\bfseries}
+
+\begin{frame}
+
+\begin{center}
+\vspace{12pt}
+\textcolor{blue}{\huge Dictionaries}
+\end{center}
+\vspace{18pt}
+\begin{center}
+\vspace{10pt}
+\includegraphics[scale=0.95]{../images/fossee-logo.png}\\
+\vspace{5pt}
+\scriptsize Developed by FOSSEE Team, IIT-Bombay. \\
+\scriptsize Funded by National Mission on Education through ICT\\
+\scriptsize MHRD,Govt. of India\\
+\includegraphics[scale=0.30]{../images/iitb-logo.png}\\
+\end{center}
+\end{frame}
\begin{frame}
-\frametitle{Outline}
-\label{sec-1}
+\frametitle{Objectives}
+\label{sec-2}
+
\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
@@ -60,56 +75,26 @@ showstringspaces=false, keywordstyle=\color{blue}\bfseries}
\end{frame}
\begin{frame}
\frametitle{Overview of Dictionaries}
-\label{sec-2}
+\label{sec-3}
+
\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}
@@ -117,68 +102,20 @@ extensions = {'jpg' : 'JPEG Image',
\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}
+\frametitle{Retrieve keys and 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
+\item \verb~.keys()~ method
\begin{verbatim}
In []: extensions.keys()
\end{verbatim}
Returns a list of keys in the dictionary.
-\item \texttt{.values()} method
+\item \verb~.values()~ method
\begin{verbatim}
In []: extensions.values()
\end{verbatim}
@@ -188,42 +125,89 @@ extensions = {'jpg' : 'JPEG Image',
\end{frame}
\begin{frame}
\frametitle{Exercise 1}
-\label{sec-8}
+\label{sec-6}
- Print the keys and values in the dictionary one by one.
+\begin{itemize}
+\item Print the keys and values in the dictionary one by one.
+\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Summary}
-\label{sec-9}
+\label{sec-7}
+
+ In this tutorial, we have learnt to,
-\begin{itemize}
-\item Creating dictionaries
\begin{itemize}
+\item Create dictionaries namely --
+\begin{itemize}
\item empty dictionaries
-\item with data
+\item dictionaries with data.
+\end{itemize}
+\item Access elements in the dictionaries using the keys.
+\item Add elements to a dictionary by assigning a value to a key.
+\item Delete elements from a dictionary by using the function ``del''.
+\item Retrieve the keys and values by using the methods ``.keys()'' and
+ ``.values()'' respectively.
+\item Iterate over elements of a dictionary using a ``for'' loop.
+\end{itemize}
+\end{frame}
+\begin{frame}[fragile]
+\frametitle{Evaluation}
+\label{sec-8}
+
+
+\begin{enumerate}
+\item Container-ship of values can be checked in a python dictionary
+\begin{itemize}
+\item True
+\item False
+\vspace{5pt}
\end{itemize}
+\item Consider the python dictionary
+
+\begin{verbatim}
+ x = {'a':['a','b','c'], 'b':(1, 2, 3),
+ 1:{1:'one', 2:'two'},
+ 10:{10:'ten', 11:'eleven'}}
+\end{verbatim}
-\item \texttt{.keys()} method
-\item \texttt{.values()} method
-\item Iterating over dictionaries
+
+ What will the following code return?\\
+ \verb~(1, 2, 3) in x.values()~
+\vspace{3pt}
+\begin{itemize}
+\item True
+\item False
+\item Container-ship of values cannot be checked in dictionaries
+\item The dictionary is invalid
\end{itemize}
+\end{enumerate}
+\end{frame}
+\begin{frame}
+\frametitle{Solutions}
+\label{sec-9}
+
+
+\begin{enumerate}
+\item False
+\vspace{15pt}
+\item True
+\end{enumerate}
\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
+ \textcolor{blue}{\Large THANK YOU!}
\end{center}
+ \end{block}
+\begin{block}{}
\begin{center}
- \textcolor{blue}{National Mission on Education through \\
- Information \& Communication Technology \\
- MHRD, Govt. of India}.
+ For more Information, visit our website\\
+ \url{http://fossee.in/}
\end{center}
\end{block}
\end{frame}
-\end{document}
+\end{document} \ No newline at end of file