summaryrefslogtreecommitdiff
path: root/conditionals
diff options
context:
space:
mode:
authorJovina2011-06-24 10:39:59 +0530
committerJovina2011-06-24 10:39:59 +0530
commit5c48ff60aba4f0c201ae80f6ac7840caeddfd2aa (patch)
treef6795aa5f4da5b2c28ca2456caecb9ba6b7de7b7 /conditionals
parent86f5cf6694e7980a29af2e80312d904d11d408c3 (diff)
downloadst-scripts-5c48ff60aba4f0c201ae80f6ac7840caeddfd2aa.tar.gz
st-scripts-5c48ff60aba4f0c201ae80f6ac7840caeddfd2aa.tar.bz2
st-scripts-5c48ff60aba4f0c201ae80f6ac7840caeddfd2aa.zip
Major changes to script & slides of 'Conditionals'.
Diffstat (limited to 'conditionals')
-rw-r--r--conditionals/script.rst323
-rw-r--r--conditionals/slides.org124
-rw-r--r--conditionals/slides.tex173
3 files changed, 456 insertions, 164 deletions
diff --git a/conditionals/script.rst b/conditionals/script.rst
index 156102b..a9303bb 100644
--- a/conditionals/script.rst
+++ b/conditionals/script.rst
@@ -25,41 +25,66 @@
Script
------
-{{{ Show the slide containing the title }}}
+.. L1
-Hello friends. Welcome to this spoken tutorial on Conditionals
+{{{ Show the first slide containing title, name of the production
+team along with the logo of MHRD }}}
-{{{ Show the slide containing the outline }}}
+.. R1
-In this tutorial, we will learn the basic conditional constructs
-available in Python. We learn the if/else, if/elif/else and ternary
-conditional constructs available in Python.
+Hello friends and Welcome to the tutorial on 'Conditionals'.
-{{{ Shift to terminal and start ipython }}}
+.. L2
+
+{{{ Show the slide containing objectives }}}
+
+.. R2
+
+At the end of this tutorial, you will be able to,
+
+ 1. Use if/else blocks.
+ #. Use if/elif/else blocks.
+ #. Use the Ternary conditional statement - C if X else Y.
-To begin with let us start ipython, by typing::
+.. R3
- ipython
+To begin with let us start ipython,
+
+.. L3
+
+{{{ Shift to terminal and start ipython }}}
+::
-on the terminal
+ ipython
+
+.. R4
Whenever we have two possible states that can occur depending on a
-whether a certain condition we can use if/else construct in
+a certain condition, we can use if/else construct in
Python.
For example, say, we have a variable ``a`` which stores integers and
-we are required to find out whether ``a`` is even or odd. an even
-number or an odd number. Let's say the value of ``a`` is 5, now.
+we are required to find out whether ``a`` is even or odd.
+Let's say the value of ``a`` is 5.
+
+.. L4
::
- a = 5
+ a = 5
+
+.. R5
+
+In such a case we can write the if/else block as
-In such a case we can write the if/else block as::
+.. L5
+::
+
+ if a % 2 == 0:
+ print "Even"
+ else:
+ print "Odd"
- if a % 2 == 0:
- print "Even"
- else:
- print "Odd"
+.. R6
If ``a`` is divisible by 2, i.e., the result of "a modulo 2" is 0, it
prints "Even", otherwise it prints "Odd".
@@ -67,129 +92,188 @@ prints "Even", otherwise it prints "Odd".
Note that in such a case, only one of the two blocks gets executed
depending on whether the condition is ``True`` or ``False``.
-There is a very important sytactic element to understand here. Every
+There is a very important syntactic element to understand here. Every
code block begins with a line that ends with a ``:``, in this example
the ``if`` and the ``else`` lines. Also, all the statements inside a
-code block are intended by 4 spaces. Returning to the previous
-indentation level, ends the code block.
+code block are intended by 4 spaces. Hitting enter twice,
+ends the code block.
The if/else blocks work for a condition, which can take one of two
-states. What do we do for conditions, which can take more than two
+states. But what do we do for conditions, which can take more than two
states?
-Python provides if/elif/else blocks, for such conditions. Let us take
-an example. We have a variable ``a`` which holds integer values. We
+.. L6
+
+.. R7
+
+Python provides if/elif/else blocks, for such conditions.
+For example. We have a variable ``a`` which holds integer values. We
need to print "positive" if ``a`` is positive, "negative" if
it is negative or "zero" if it is 0.
Let us use if/elif/else ladder for it. For the purposes of testing our
-code let us assume that the value of a is -3::
+code let us assume that the value of a is -3
- a = -3
+.. L7
+::
+
+ a = -3
+
+ if a > 0:
+ print "positive"
+ elif a < 0:
+ print "negative"
+ else:
+ print "zero"
- if a > 0:
- print "positive"
- elif a < 0:
- print "negative"
- else:
- print "zero"
+.. R8
-All the syntax and rules as said for if/else statements hold. The only
+All the syntax and rules as said for if/else statements hold the same. The only
addition here is the ``elif`` statement which can have another
condition of its own.
Here too, exactly one block of code is executed -- the block of code
which first evaluates to ``True``. Even if there is a situation where
-multiple conditions evaluate to True all the subsequent conditions
-other than the first one which evaluates to True are neglected.
+multiple conditions evaluate to True, all the subsequent conditions
+other than the first one, which evaluates to True, are neglected.
Consequently, the else block gets executed if and only if all the
conditions evaluate to False.
+.. L8
+
+.. R9
+
Also, the ``else`` block in both if/else statement and if/elif/else is
optional. We can have a single if statement or just if/elif statements
without having else block at all. Also, there can be any number of
elif's within an if/elif/else ladder. For example
-{{{ Show slide for this }}}
+.. L9
- if user == 'admin':
- # Do admin operations
- elif user == 'moderator':
- # Do moderator operations
- elif user == 'client':
- # Do customer operations
+{{{ Show slide ~if/elif~ ladder }}}
+
+ if user == 'admin':
+ # Do admin operations
+ elif user == 'moderator':
+ # Do moderator operations
+ elif user == 'client':
+ # Do customer operations
-{{{ end of slide switch to ipython }}}
+.. R10
-is completely valid. Note that there are multiple elif blocks and there
+Note that there are multiple elif blocks and there
is no else block.
-Following is an exercise that you must do.
+Pause the video here, try out the following exercise and resume the video.
+
+.. L10
+
+.. L11
+
+{{{ Show slide with exercise 1 }}}
+
+.. R11
-%% %% Given a number, num. Write an if else block to print num, as is,
- if it is divisible by 10, else print 10 * num.
+ Given a number, num. Write an if else block to print num, as is,
+ if it is divisible by 10, else print 10 * num.
-Please, pause the video here. Do the exercise and then continue.
+.. R12
-::
+The solution is on your screen.
- if num%10 == 0:
- print num
- else:
- print 10*num
+.. L12
+{{{ Show slide with solution 1 }}}
+
+ if num%10 == 0:
+ print num
+ else:
+ print 10*num
+
+.. R13
In addition to these conditional statements, Python provides a very
convenient ternary conditional operator. Let us take the following
-example where we read the marks data from a data file which is
+example where we read the marks from a data file which is
obtained as a string as we read a file. The marks can be in the range
-of 0 to 100 or 'AA' if the student is absent. In such a case to obtain
-the marks as an integer we can use the ternary conditional
+of 0 to 100 or 'AA' if the student is absent. In such a case, to obtain
+the marks as an integer, we can use the ternary conditional
operator. Let us say the string score is stored in score_str
-variable::
+variable
+
+.. L13
+::
- score_str = 'AA'
+ score_str = 'AA'
+
+.. R14
+
+Now let us use the ternary conditional operator
+
+.. L14
+::
-Now let us use the ternary conditional operator::
+ score = int(score_str) if score_str != 'AA' else 0
- score = int(score_str) if score_str != 'AA' else 0
+.. R15
This is just the if/else statement block which written in a more
convenient form and is very helpful when we have only one statement
-for each block. This conditional statement effectively means as we
-would have exactly specified in the English language which will be
-like score is integer of score_str is score_str is not 'AA' otherwise
-it is 0. This means that we make the scores of the students who were
+for each block. In simple terms,this conditional statement effectively means that
+score is integer of ``score_str`` if score_str is not 'AA'; otherwise it is 0.
+This means that we make the scores of the students who were
absent for the exam 0.
-Following is an exercise that you must do.
+Pause the video here, try out the following exercise and resume the video.
-%% %% Given a number, num. Write a ternary operator to print num, as is,
- if it is divisible by 10, else print 10 * num.
+.. L15
-Please, pause the video here. Do the exercise and then continue.
+{{{ Show slide with exercise 2 }}}
-::
+.. R15
- print num if num%10 == 0 else 10*num
+ Given a number, num. Write a ternary operator to print num, as is,
+ if it is divisible by 10, else print 10 * num.
+
+.. L16
+
+{{{ Show slide with Solution 2 }}}
+
+.. R16
+
+The solution is on your screen.
+
+ print num if num%10 == 0 else 10*num
+
+.. R17
Moving on, there are certain situations where we will have no
operations or statements within a block of code. For example, we have
a code where we are waiting for the keyboard input. If the user enters
-"c", "d" or "x" as the input we would perform some operation nothing
-otherwise. In such cases "pass" statement comes very handy::
+"c", "d" or "x" as the input, we would perform some operation; nothing
+otherwise. In such cases "pass" statement comes very handy
+
+.. L17
+
+.. L18
+
+{{{ Show slide with pass statement }}}
- a = raw_input("Enter 'c' to calculate and exit, 'd' to display the existing
- results exit and 'x' to exit and any other key to continue: ")
+.. R18
- if a == 'c':
- # Calculate the marks and exit
- elif a == 'd':
- # Display the results and exit
- elif a == 'x':
- # Exit the program
- else:
- pass
+ a = raw_input("Enter 'c' to calculate and exit, 'd' to display the existing
+ results exit and 'x' to exit and any other key to continue: ")
+
+ if a == 'c':
+ # Calculate the marks and exit
+ elif a == 'd':
+ # Display the results and exit
+ elif a == 'x':
+ # Exit the program
+ else:
+ pass
+
+.. R19
In this case "pass" statement acts as a place holder for the block of
code. It is equivalent to a null operation. It literally does
@@ -197,21 +281,74 @@ nothing. It can used as a place holder when the actual code
implementation for a particular block of code is not known yet but has
to be filled up later.
+.. L19
+
+.. L20
+
{{{ Show summary slide }}}
-This brings us to the end of the tutorial session on conditional
-statements in Python. In this tutorial session we learnt
+.. R20
+
+This brings us to the end of the tutorial.In this tutorial, we have learnt to,
+
+ 1. Understand the conditional statements in Python.
+ #. Use if/else statement.
+ #. Use if/elif/else statement.
+ #. Apply the ternary conditional statement - C if X else Y.
+ #. Use "pass" statement.
+
+.. L21
+
+{{{Show self assessment questions slide}}}
+
+.. R21
+
+Here are some self assessment questions for you to solve
+
+1. Use conditional statements for the following.
+ Given a variable ``time``, print ``Good Morning`` if it is less
+ than 12, otherwise ``Hello``.
+
+
+#. Convert the if else ladder below into a ternary conditional
+ statement.
+::
+
+ x = 20
+
+ if x > 10:
+ print x * 100
+ else:
+ print x
+
+.. L22
+
+{{{solution of self assessment questions on slide}}}
+
+.. R22
+
+And the answers,
+
+1. We can use the if/else statements as
+::
+
+ if time < 12:
+ print "Good Morning"
+ else:
+ print "Hello"
+
+2. The if else ladder can be converted to a ternary conditional
+ statement as
+::
+
+ print x * 100 if x > 10 else x
- * What are conditional statements
- * if/else statement
- * if/elif/else statement
- * Ternary conditional statement - C if X else Y
- * and the "pass" statement
+.. L23
-{{{ Show the "sponsored by FOSSEE" slide }}}
+{{{ Show the Thankyou slide }}}
-This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
+.. R23
-Hope you have enjoyed and found it useful.
+Hope you have enjoyed this tutorial and found it useful.
Thank you!
diff --git a/conditionals/slides.org b/conditionals/slides.org
index 0f17b45..feb271e 100644
--- a/conditionals/slides.org
+++ b/conditionals/slides.org
@@ -18,7 +18,7 @@
#+LaTeX_HEADER: commentstyle=\color{red}\itshape, stringstyle=\color{darkgreen},
#+LaTeX_HEADER: showstringspaces=false, keywordstyle=\color{blue}\bfseries}
-#+TITLE: Conditionals
+#+TITLE:
#+AUTHOR: FOSSEE
#+EMAIL:
#+DATE:
@@ -29,13 +29,43 @@
#+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
- In this tutorial, we shall look at
- + Using if/else blocks
- + Using if/elif/else blocks
- + Using the Ternary conditional statement
+*
+#+begin_latex
+\begin{center}
+\vspace{12pt}
+\textcolor{blue}{\huge Conditionals}
+\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
+At the end of this tutorial, you will be able to,
-* Question 1
+ - Use if/else blocks.
+ - Use if/elif/else blocks.
+ - Use the Ternary conditional statement - C if X else Y.
+
+* Pre-requisite
+Spoken tutorial on -
+- Basic Datatypes and Operators
+* ~if/elif~ ladder
+ #+begin_src python
+ if user == 'admin':
+ # Do admin operations
+ elif user == 'moderator':
+ # Do moderator operations
+ elif user == 'client':
+ # Do customer operations
+ #+end_src
+* Exercise 1
Given a number, num. Write an if else block to print num, as is, if
it is divisible by 10, else print 10 * num.
* Solution 1
@@ -46,44 +76,78 @@
print 10*num
#+end_src
-* ~if/elif~ ladder
- #+begin_src python
- if user == 'admin':
- # Do admin operations
- elif user == 'moderator':
- # Do moderator operations
- elif user == 'client':
- # Do customer operations
- #+end_src
-* Question 2
+* Exercise 2
Given a number, num. Write a ternary operator to print num, as is,
if it is divisible by 10, else print 10 * num.
* Solution 2
#+begin_src python
print num if num%10 == 0 else 10*num
#+end_src
+* Pass statement
+ #+begin_src python
+ a = raw_input("Enter 'c' to calculate and exit, 'd' to display the existing
+ results exit and 'x' to exit and any other key to continue: ")
+
+ if a == 'c':
+ # Calculate the marks and exit
+ elif a == 'd':
+ # Display the results and exit
+ elif a == 'x':
+ # Exit the program
+ else:
+ pass
+ #+end_src
* Summary
- In this tutorial session we learnt
+ In this tutorial, we have learnt to,
- + What are conditional statements
- + if/else statement
- + if/elif/else statement
- + Ternary conditional statement - ~C if X else Y~
- + and the ~pass~ statement
+ - Understand the conditional statements in Python.
+ - Use if/else statement.
+ - Use if/elif/else statement.
+ - Apply the ternary conditional statement - C if X else Y.
+ - Use "pass" statement.
-* Thank you!
+* Evaluation
+1. Use conditional statements for the following.
+ Given a variable ``time``, print ``Good Morning`` if it is less
+ than 12, otherwise ``Hello``.
+
+2. Convert the if else ladder below into a ternary conditional
+ statement.
+#+begin_src python
+ x = 20
+
+ if x > 10:
+ print x * 100
+ else:
+ print x
+#+end_src
+
+* Solutions
+1.
+#+begin_src python
+ if time < 12:
+ print "Good Morning"
+ else:
+ print "Hello"
+#+end_src
+
+2.
+#+begin_src python
+ print x * 100 if x > 10 else x
+#+end_src
+*
#+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{frame}
#+end_latex
-
diff --git a/conditionals/slides.tex b/conditionals/slides.tex
index 6991f39..43f899a 100644
--- a/conditionals/slides.tex
+++ b/conditionals/slides.tex
@@ -1,4 +1,4 @@
-% Created 2010-11-10 Wed 13:25
+% Created 2011-06-24 Fri 10:08
\documentclass[presentation]{beamer}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
@@ -23,14 +23,14 @@ commentstyle=\color{red}\itshape, stringstyle=\color{darkgreen},
showstringspaces=false, keywordstyle=\color{blue}\bfseries}
\providecommand{\alert}[1]{\textbf{#1}}
-\title{Conditionals}
+\title{}
\author{FOSSEE}
\date{}
\usetheme{Warsaw}\usecolortheme{default}\useoutertheme{infolines}\setbeamercovered{transparent}
\begin{document}
-\maketitle
+
@@ -41,37 +41,48 @@ showstringspaces=false, keywordstyle=\color{blue}\bfseries}
\begin{frame}
-\frametitle{Outline}
-\label{sec-1}
- In this tutorial, we shall look at
-\begin{itemize}
-\item Using if/else blocks
-\item Using if/elif/else blocks
-\item Using the Ternary conditional statement
-\end{itemize}
+\begin{center}
+\vspace{12pt}
+\textcolor{blue}{\huge Conditionals}
+\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{Question 1}
+\frametitle{Objectives}
\label{sec-2}
- Given a number, num. Write an if else block to print num, as is, if
- it is divisible by 10, else print 10 * num.
+At the end of this tutorial, you will be able to,
+
+
+\begin{itemize}
+\item Use if/else blocks.
+\item Use if/elif/else blocks.
+\item Use the Ternary conditional statement - C if X else Y.
+\end{itemize}
+
\end{frame}
-\begin{frame}[fragile]
-\frametitle{Solution 1}
+\begin{frame}
+\frametitle{Pre-requisite}
\label{sec-3}
-\lstset{language=Python}
-\begin{lstlisting}
-if num%10 == 0:
- print num
-else:
- print 10*num
-\end{lstlisting}
+Spoken tutorial on -
+
+\begin{itemize}
+\item Basic Datatypes and Operators
+\end{itemize}
\end{frame}
\begin{frame}[fragile]
-\frametitle{\texttt{if/elif} ladder}
+\frametitle{\verb~if/elif~ ladder}
\label{sec-4}
\lstset{language=Python}
@@ -85,50 +96,130 @@ elif user == 'client':
\end{lstlisting}
\end{frame}
\begin{frame}
-\frametitle{Question 2}
+\frametitle{Exercise 1}
\label{sec-5}
+ Given a number, num. Write an if else block to print num, as is, if
+ it is divisible by 10, else print 10 * num.
+\end{frame}
+\begin{frame}[fragile]
+\frametitle{Solution 1}
+\label{sec-6}
+
+\lstset{language=Python}
+\begin{lstlisting}
+if num%10 == 0:
+ print num
+else:
+ print 10*num
+\end{lstlisting}
+\end{frame}
+\begin{frame}
+\frametitle{Exercise 2}
+\label{sec-7}
+
Given a number, num. Write a ternary operator to print num, as is,
if it is divisible by 10, else print 10 * num.
\end{frame}
\begin{frame}[fragile]
\frametitle{Solution 2}
-\label{sec-6}
+\label{sec-8}
\lstset{language=Python}
\begin{lstlisting}
print num if num%10 == 0 else 10*num
\end{lstlisting}
\end{frame}
+\begin{frame}[fragile]
+\frametitle{Pass statement}
+\label{sec-9}
+
+a = raw\_input("Enter `c' to calculate and exit, `d' to display the existing results exit and `x' to exit and any other key to continue: ")
+\lstset{language=Python}
+\begin{lstlisting}
+if a == 'c':
+ # Calculate the marks and exit
+elif a == 'd':
+ # Display the results and exit
+elif a == 'x':
+ # Exit the program
+else:
+ pass
+\end{lstlisting}
+\end{frame}
\begin{frame}
\frametitle{Summary}
-\label{sec-7}
+\label{sec-10}
+
+ In this tutorial, we have learnt to,
- In this tutorial session we learnt
\begin{itemize}
-\item What are conditional statements
-\item if/else statement
-\item if/elif/else statement
-\item Ternary conditional statement - \texttt{C if X else Y}
-\item and the \texttt{pass} statement
+\item Understand the conditional statements in Python.
+\item Use if/else statement.
+\item Use if/elif/else statement.
+\item Apply the ternary conditional statement - C if X else Y.
+\item Use ``pass'' statement.
\end{itemize}
\end{frame}
+\begin{frame}[fragile]
+\frametitle{Evaluation}
+\label{sec-11}
+
+
+\begin{enumerate}
+\item Use conditional statements for the following.
+ Given a variable ``time'', print ``Good Morning'' if it is less
+ than 12, otherwise ``Hello''.
+\vspace{10pt}
+\item Convert the if else ladder below into a ternary conditional
+ statement.
+\lstset{language=Python}
+\begin{lstlisting}
+x = 20
+
+if x > 10:
+ print x * 100
+else:
+ print x
+\end{lstlisting}
+\end{enumerate}
+\end{frame}
+\begin{frame}[fragile]
+\frametitle{Solutions}
+\label{sec-12}
+
+
+\begin{enumerate}
+\item \lstset{language=Python}
+\begin{lstlisting}
+if time < 12:
+ print "Good Morning"
+else:
+ print "Hello"
+\end{lstlisting}
+
+\vspace{9pt}
+\item \lstset{language=Python}
+\begin{lstlisting}
+print x * 100 if x > 10 else x
+\end{lstlisting}
+\end{enumerate}
+\end{frame}
\begin{frame}
-\frametitle{Thank you!}
-\label{sec-8}
\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{frame}
-\end{document}
+\end{document} \ No newline at end of file