made the script writing_python_scripts into new form
authorNishanth <nishanth@fossee.in>
Mon, 11 Oct 2010 13:41:01 +0530
changeset 296 641a6ee868c0
parent 287 d9507624eb8f
child 297 bef342784a40
made the script writing_python_scripts into new form
writing_python_scripts/questions.rst
writing_python_scripts/quickref.tex
writing_python_scripts/script.rst
writing_python_scripts/slides.tex
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/writing_python_scripts/questions.rst	Mon Oct 11 13:41:01 2010 +0530
@@ -0,0 +1,90 @@
+Objective Questions
+-------------------
+
+ 1. If ``a = [1, 1, 2, 3, 3, 5, 5, 8]``. What is set(a)
+
+   a. set([1, 1, 2, 3, 3, 5, 5, 8])
+   #. set([1, 2, 3, 5, 8])
+   #. set([1, 2, 3, 3, 5, 5])
+   #. Error
+
+   Answer: set([1, 2, 3, 5, 8])
+
+ 2. ``a = set([1, 3, 5])``. How do you find the length of a?
+
+   Answer: len(a)
+
+ 3. ``a = set([1, 3, 5])``. What does a[2] produce?
+
+   a. 1
+   #. 3
+   #. 5
+   #. Error
+
+   Answer: Error
+
+ 4. ``odd = set([1, 3, 5, 7, 9])`` and ``squares = set([1, 4, 9, 16])``. What
+    is the value of ``odd | squares``?
+
+   Answer: set([1, 3, 4, 5, 7, 9, 16])
+
+ 5. ``odd = set([1, 3, 5, 7, 9])`` and ``squares = set([1, 4, 9, 16])``. What
+    is the value of ``odd - squares``?
+
+   Answer: set([3, 5, 7])
+
+ 6. ``odd = set([1, 3, 5, 7, 9])`` and ``squares = set([1, 4, 9, 16])``. What
+    is the value of ``odd ^ squares``?
+
+   Answer: set([3, 4, 5, 7, 16])
+
+ 7. ``odd = set([1, 3, 5, 7, 9])`` and ``squares = set([1, 4, 9, 16])``. What
+    does ``odd * squares`` give?
+
+   a. set([1, 12, 45, 112, 9])
+   #. set([1, 3, 4, 5, 7, 9, 16])
+   #. set([])
+   #. Error
+
+   Answer: Error
+
+ 8. ``a = set([1, 2, 3, 4])`` and ``b = set([5, 6, 7, 8])``. What is ``a + b``
+
+   a. set([1, 2, 3, 4, 5, 6, 7, 8])
+   #. set([6, 8, 10, 12])
+   #. set([5, 12, 21, 32])
+   #. Error
+
+ 9. ``a`` is a set. how do you check if if a varaible ``b`` exists in ``a``?
+
+   Answer: b in a
+
+ 10. ``a`` and ``b`` are two sets. What is ``a ^ b == (a - b) | (b - a)``?
+
+   a. True
+   #. False
+
+   Answer: False
+
+
+Larger Questions
+----------------
+
+ 1. Given that mat_marks is a list of maths marks of a class. Find out the
+    no.of duplicates marks in the list.
+
+   Answer::
+
+     unique_marks = set(mat_marks)
+     no_of_duplicates = len(mat_marks) - len(unique_marks)
+
+ 2. Given that mat_marks is a list of maths marks of a class. Find how many
+    duplicates of each mark exist.
+
+   Answer::
+
+     marks_set = set(mat_marks)
+     for mark in marks_set:
+         occurences = mat_marks.count(mark)
+         print occurences - 1, "duplicates of", mark, "exist"
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/writing_python_scripts/quickref.tex	Mon Oct 11 13:41:01 2010 +0530
@@ -0,0 +1,11 @@
+Creating a tuple:\\
+{\ex \lstinline|    t = (1, "hello", 2.5)|}
+
+Accessing elements of tuples:\\
+{\ex \lstinline|    t[index] Ex: t[2]|}
+
+Accessing slices of tuples:\\
+{\ex \lstinline|    t[start:stop:step]|}
+
+Swapping values:\\
+{\ex \lstinline|    a, b = b, a|}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/writing_python_scripts/script.rst	Mon Oct 11 13:41:01 2010 +0530
@@ -0,0 +1,146 @@
+.. Objectives
+.. ----------
+
+.. Prerequisites
+.. -------------
+     
+.. Author              : Nishanth Amuluru
+   Internal Reviewer   : 
+   External Reviewer   :
+   Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
+
+Script
+------
+
+Hello friends and welcome to the tutorial on "Writing Python scripts"
+
+{{{ Show the slide containing title }}}
+
+{{{ Show the slide containing the outline slide }}}
+
+In this tutorial, we shall learn
+
+ * How write Python scripts 
+
+Often we will have to reuse the code that we haave written. We do that by
+writing functions. Functions are bundled into packages and are imported as and
+required in the script.
+
+Let us first write a function that computes the gcd of two numbers and save it
+in a script.
+
+{{{ Open an editor and start typing out the following code }}}
+::
+
+    def gcd(a, b):
+
+        while b:
+            a, b = b, a%b
+
+        return a
+
+We shall write an test function in the script that tests the gcd function every
+time the script is run.
+
+{{{ Add to the script }}}
+
+::
+
+    if gcd(40, 12) == 4:
+        print "Everything OK"
+    else:
+        print "The GCD function is wrong"
+
+Let us save the file as script.py in /home/fossee/gcd_script.py
+
+We shall run the script by doing
+::
+
+    $ python /home/fossee/gcd_script.py
+
+We can see that the script is executed and everything is fine.
+
+What if we want to use the gcd function in some of our later scripts. This
+is also possible since every python file can be used as a module.
+
+But first, we shall understand what happens when you import a module.
+
+Open IPython and type
+::
+
+    import sys
+    sys.path
+
+This is a list of locations where python searches for a module when it
+encounters an import statement.
+
+hence when we just did =import sys=, python searches for a file named sys.py or
+a folder named sys in all these locations one by one, until it finds one.
+
+We can place our script in any one of these locations and can import it.
+
+The first item in the list is an empty string which means the current working
+directory is also searched. 
+
+Alternatively, we can also import the module if we are working in same 
+directory where the script exists.
+
+Since we are in /home/fossee, we can simply do
+::
+
+    import gcd_script
+    
+We can see that the gcd_script is imported. But the test code that we added at
+the end of the file is also executed.
+
+But we want the test code to be executed only when the file is run as a python 
+script and not when it is imported.
+
+This is possible by using =__name__= variable.
+
+First we shall look at how to use the idiom and then understand how it works.
+
+Go to the file and add
+::
+
+    if __name__ == "__main__":
+        
+before the test code and indent the test code.
+
+Let us first run the code.
+::
+
+    $ python gcd_script.py
+
+We can see that the test runs successfully.
+
+Now we shall import the file
+::
+    
+    import gcd_script
+
+We see that now the test code is not executed.
+
+The __name__ variable is local to every module and it is equal to __main__ only
+when the file is run as a script.
+
+hence all the code that goes after __name__ == "__main__" is executed only when
+the file is run as a python script.
+
+{{{ Show summary slide }}}
+
+This brings us to the end of the tutorial.
+we have learnt
+
+ * What happens when we import a module
+ * How to use a script as a module
+ * How to write test functions using the __name__ idiom 
+
+{{{ Show the "sponsored by FOSSEE" slide }}}
+
+#[Nishanth]: Will add this line after all of us fix on one.
+This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
+
+Hope you have enjoyed and found it useful.
+Thankyou
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/writing_python_scripts/slides.tex	Mon Oct 11 13:41:01 2010 +0530
@@ -0,0 +1,104 @@
+% Created 2010-10-10 Sun 23:53
+\documentclass[presentation]{beamer}
+\usepackage[latin1]{inputenc}
+\usepackage[T1]{fontenc}
+\usepackage{fixltx2e}
+\usepackage{graphicx}
+\usepackage{longtable}
+\usepackage{float}
+\usepackage{wrapfig}
+\usepackage{soul}
+\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{Sets}
+\author{FOSSEE}
+\date{}
+
+\usetheme{Warsaw}\usecolortheme{default}\useoutertheme{infolines}\setbeamercovered{transparent}
+\begin{document}
+
+\maketitle
+
+
+
+
+
+
+
+
+
+\begin{frame}
+\frametitle{Outline}
+\label{sec-1}
+
+\begin{itemize}
+\item Defining Sets
+\item Operations on sets
+\end{itemize}
+\end{frame}
+\begin{frame}
+\frametitle{Question 1}
+\label{sec-2}
+
+  Given a list of marks, \texttt{marks = [20, 23, 22, 23, 20, 21, 23]} list
+  all the duplicates
+\end{frame}
+\begin{frame}[fragile]
+\frametitle{Solution 1}
+\label{sec-3}
+
+\lstset{language=Python}
+\begin{lstlisting}
+marks = [20, 23, 22, 23, 20, 21, 23] 
+marks_set = set(marks)
+for mark in marks_set:
+    marks.remove(mark)
+
+# we are now left with only duplicates in the list marks
+duplicates = set(marks)
+\end{lstlisting}
+\end{frame}
+\begin{frame}
+\frametitle{Summary}
+\label{sec-4}
+
+  You should now be able to --
+\begin{itemize}
+\item make sets from lists
+\item input sets directly
+\item perform operations like union, intersection, symmetric difference
+\item check if a subset of another
+\item check containership, length and other properties similar to lists
+\end{itemize}
+\end{frame}
+\begin{frame}
+\frametitle{Thank you!}
+\label{sec-5}
+
+  \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}