From 3eef2cebc990e689103e00ae75462e1852a446b3 Mon Sep 17 00:00:00 2001 From: Jovina Date: Tue, 21 Jun 2011 13:20:22 +0530 Subject: Major changes to script & slides of 'input_output'. --- input_output/script.rst | 284 +++++++++++++++++++++++++++++++++++++----------- input_output/slides.org | 111 ++++++++++++------- input_output/slides.tex | 160 ++++++++++++++++----------- 3 files changed, 388 insertions(+), 167 deletions(-) diff --git a/input_output/script.rst b/input_output/script.rst index 71ec8a9..f8192da 100644 --- a/input_output/script.rst +++ b/input_output/script.rst @@ -9,7 +9,7 @@ .. Prerequisites .. ------------- -.. 1. Loops +.. none .. Author : Nishanth Amuluru Internal Reviewer : Puneeth @@ -20,87 +20,132 @@ Script ------ -Hello friends and welcome to this tutorial on Input/Output +.. L1 -{{{ Show the slide containing title }}} +{{{ Show the first slide containing title, name of the production +team along with the logo of MHRD }}} -{{{ Show the slide containing the outline slide }}} +.. R1 -Input and Output are used in almost every program we use. -In this tutorial, we shall learn how to +Hello friends and welcome to this tutorial on 'Input/Output'. - * Output data - * Take input from the user +.. L2 -type +{{{ Show the slide containing the objectives }}} + +.. R2 + +At the end of this tutorial,you will be able to, + + 1. Print some value. + #. Print using modifiers. + #. Take input from user. + #. Display a prompt to the user before taking the input. + +.. L3 + +Let us first start ipython on our teminal + +.. R3 +:: + + ipython + +.. L4 + +Let us start this tutorial by typing a string + +.. R4 :: a = "This is a string" a print a +.. R5 -``print a``, obviously, is printing the value of ``a``. +``print a``, obviously, prints the value of ``a``. As you can see, even when you type just a, the value of a is shown. But there is a difference. -.. #[Amit: The next sentence does seem to be clear enough] +Typing just ``a`` displays the content of ``a`` whereas the +statement ``print a`` prints the string itself.This difference becomes +more evident when we use strings with newlines in them. -Typing a shows the value of a while print a prints the string. This difference -becomes more evident when we use strings with newlines in them. -type +.. L5 :: b = "A line \n New line" b print b -As you can see, just typing b shows that b contains a newline character. -While typing print b prints the string and hence the newline. +.. R6 -Moreover when we type just a, the value a is shown only in interactive mode and -does not have any effect on the program while running it as a script. +As you can see, just typing ``b`` shows that b contains a newline character but +While typing ``print b``,it prints the string and hence the newline. -.. #[punch: I think we could show that?] +Moreover when we type just ``a``, the value a is shown only in interactive mode and +does not have any effect on the program while running it as a script. We shall look at different ways of outputting the data. - -.. #[Amit: C's printf syntax ?? i think its better to elaborate the - idea] - print statement in python supports string formatting. Various arguments can be passed to print using modifiers. type + +.. L6 :: x = 1.5 y = 2 - z = "zed" - print "x is %2.1f y is %d z is %s"%(x,y) + z = "red" + print "x is %2.1f, y is %d, z is %s"%(x,y,z) + +.. R7 + +As you can see, the values of x, y and z are substituted in place of the modifiers +``%2.1f``, ``%d`` and ``%s`` respectively. -As you can see, the values of x and y are substituted in place of -``%2.1f`` and ``%d`` +Pause the video here, try out the following exercise and resume the video. -{{{ Pause here and try out the following exercises }}} +.. L7 -%% 1 %% What happens when you do ``print "x is %d y is %f" %(x, y)`` +.. L8 -{{{ continue from paused state }}} +{{{ Show slide with exercise 1 }}} +.. R8 + + What happens when you do ``print "x is %d, y is %f" %(x, y)`` + +.. R9 + +Switch to the terminal for solution. + +.. L9 + +{{{continue from paused state}}} +{{{ Switch to the terminal }}} +:: + + print "x is %d, y is %f" %(x, y) + +.. R10 + We see that the ``int`` value of x and ``float`` value of y are printed corresponding to the modifiers used in the print statement. -We can also see that ``print`` statement prints a new line character -at the end of the line, everytime it is called. This can be suppressed -by using a "," at the end ``print`` statement. +We have seen that ``print`` statement prints a new line character +everytime it is called. This can be suppressed +by using a "," at the end of the ``print`` statement. Let us see this by typing out following code on an editor as print_example.py +.. L10 + {{{ open an editor }}} -type :: print "Hello" @@ -109,7 +154,9 @@ type print "Hello", print "World" -Now we run the script using %run /home/fossee/print_example.py +.. R11 + +Save the script as 'print_example.py' and run it using %run /home/fossee/print_example.py As we can see, the print statement when used with comma in the end, prints a space instead of a new line. @@ -117,102 +164,213 @@ space instead of a new line. Now we shall look at taking input from the user. We will use the ~~raw_input~~ for this. type + +.. L11 :: ip = raw_input() +.. R12 + The cursor is blinking indicating that it is waiting for input -type +type something and hit enter. + +.. L12 :: an input -and hit enter. -Now let us see what is the value of ip by typing. +.. R13 + +Now let us see what is the value of ip by typing it. + +.. L13 :: ip +.. R14 + We can see that it contains the string "an input" -{{{ Pause here and try out the following exercises }}} +Pause the video here, try out the following exercise and resume the video. + +.. L14 + +.. L15 + +{{{ Show slide with exercise 2 }}} -%% 2 %% enter the number 5.6 as input and store it in a variable called c. +.. R15 -{{{ continue from paused state }}} + Enter the number 5.6 as input and store it in a variable called c. + +.. R16 + +Switch to the terminal for solution. + +.. L16 + +{{{continue from paused state}}} +{{{ Switch to the terminal }}} + +.. R17 We have to use the raw_input command with variable c. type + +.. L17 :: c = raw_input() 5.6 c +.. R18 + Now let us see the type of c. +.. L18 :: type(c) -We see that c is a string. This implies that anything you enter as input, will +.. R19 + +We see that c is a string. This implies that anything you enter as input,it will be taken as a string no matter what you enter. -{{{ Pause here and try out the following exercises }}} +Pause the video here, try out the following exercise and resume the video. + +.. L19 + +.. L20 -%% 3 %% What happens when you do not enter anything and hit enter +{{{ Show slide with exercise 3 }}} -{{{ continue from paused state }}} +.. R20 + What happens when you do not enter anything and hit enter. + +.. R21 + +Switch to the terminal for solution. + +.. L21 + +{{{continue from paused state}}} +{{{ Switch to the terminal }}} :: d = raw_input() d +.. R22 + We see that when nothing is entered, an empty string is considered as input. raw_input also can display a prompt to assist the user. + +.. L22 :: name = raw_input("Please enter your name: ") -prints the string given as argument and then waits for the user input. +.. R23 + +It prints the string given as argument and then waits for the user input. + +Let us do one more exercise. +Pause the video here, try out the following exercise and resume the video. -{{{ Pause here and try out the following exercises }}} +.. L23 -%% 4 %% How do you display a prompt and let the user enter input in next line +.. L24 -{{{ continue from paused state }}} +{{{ Show slide with exercise 3 }}} -.. #[Puneeth: We didn't talk of new-line character till now, did we?] -.. #[Puneeth: non-programmers might not know?] +.. R24 -.. #[Amit: Well there is a discussion earlier about new lines, I think its good -.. as a slight trick question. But may be next line is a more easier lexicon] + How do you display a prompt and let the user enter input in next line. +.. R25 + +Switch to the terminal for solution. The trick is to include a newline character at the end of the prompt string. + +.. L25 + +{{{continue from paused state}}} +{{{ Switch to the terminal }}} :: ip = raw_input("Please enter a number in the next line\n> ") -prints the newline character and hence the user enters input in the next line +.. R26 + +It prints the newline character and hence the user enters input in the next line + +.. L26 + +.. L27 {{{ Show summary slide }}} +.. R27 + This brings us to the end of the tutorial. -In this totorial we have learnt +In this totorial, we have learnt to, + + 1. Use the print statement. + #. Use the modifiers %d, %f, %s in the print statement. + #. Take input from user by using ``raw_input()``. + #. Display a prompt to the user before taking the input by passing + a string as an argument to ``raw_input``. + +.. L28 + +{{{Show self assessment questions slide}}} + +.. R28 + +Here are some self assessment questions for you to solve + +1. ``a = raw_input()`` and user enters ``2.5``. What is the type of a? + + - str + - int + - float + - char + +2. ``a = 2`` and ``b = 4.5``. What does ``print "a is %d and b is %2.1f" %(b, a)`` + print? + + - a is 2 and b is 4.5 + - a is 4 and b is 2 + - a is 4 and b is 2.0 + - a is 4.5 and b is 2 + +.. L29 + +{{{solution of self assessment questions on slide}}} + +.. R29 + +And the answers, + +1. No matter what you enter, it will be taken as a string.Hence 2.5 is a string. - * How to print some value - * How to print using modifiers - * How to take input from user - * How to display a prompt to the user before taking the input +2. Since 'b' is called first, It will display integer value of 'a' + because the modifier used is %d. Similarly, 'b' will get the float + value of 'a' due to it's modifier %2.1f. Hence 'a' will be 4 and 'b' 2.0 . -{{{ Show the "sponsored by FOSSEE" slide }}} +.. L30 +{{{ Show the Thankyou slide }}} -This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India +.. R30 -Hope you have enjoyed and found it useful. -Thank You. +Hope you have enjoyed this tutorial and found it useful. +Thank You! diff --git a/input_output/slides.org b/input_output/slides.org index df7e36f..8ff4989 100644 --- a/input_output/slides.org +++ b/input_output/slides.org @@ -18,7 +18,7 @@ #+LaTeX_HEADER: commentstyle=\color{red}\itshape, stringstyle=\color{darkgreen}, #+LaTeX_HEADER: showstringspaces=false, keywordstyle=\color{blue}\bfseries} -#+TITLE: I/O +#+TITLE: #+AUTHOR: FOSSEE #+EMAIL: #+DATE: @@ -29,54 +29,81 @@ #+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 - - Showing output to the user. - - Taking input from the user. -* Question 1 - What happens when you do ~print "x is %d y is %f" %(x, y)~ -* Solution 1 - ~int~ value of ~x~ and ~float~ value of ~y~ are printed corresponding to the - modifiers used in the ~print~ statement -* Question 2 - Enter the number 5.6 as input and store it in a variable called +* +#+begin_latex +\begin{center} +\vspace{12pt} +\textcolor{blue}{\huge Input/Output} +\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, + + - Print some value. + - Print using modifiers. + - Take input from user. + - Display a prompt to the user before taking the input. + +* Exercise 1 + - What happens when you do + print "x is %d, y is %f" %(x, y) +* Exercise 2 + - Enter the number 5.6 as input and store it in a variable called ~c~. -* Solution 2 - #+begin_src python - In []: c = raw_input() - 5.6 - In []: c - #+end_src -* Question 3 - What happens when you do not enter anything and hit enter -* Solution 3 - #+begin_src python - In []: c = raw_input() - - In []: c - #+end_src -* Question 4 - How do you display a prompt and let the user enter input in a new line -* Solution 4 - #+begin_src python - In []: ip = raw_input("Please enter a number in the next line\n> ") - #+end_src +* Exercise 3 + - What happens when you do not enter anything and hit enter +* Exercise 4 + - How do you display a prompt and let the user enter input in a new line * Summary - You should now be able to -- - + Print a value "as is" - + Print a value using using modifiers - + Accept input from user - + Display a prompt before accepting input -* Thank you! + In this tutorial, we have learnt to, + + - Use the print statement. + - Use the modifiers %d, %f, %s in the print statement. + - Take input from user by using ``raw\_input()''. + - Display a prompt to the user before taking the input by passing + a string as an argument to ``raw\_input''. +* Evaluation +1. ``a = raw\_input()'' and user enters ``2.5''. + What is the type of a? + + - str + - int + - float + - char + +2. ``a = 2'' and ``b = 4.5''. + What does print "a is %d and b is %2.1f" %(b, a) print? + + - a is 2 and b is 4.5 + - a is 4 and b is 2 + - a is 4 and b is 2.0 + - a is 4.5 and b is 2 + +* Solutions +1. str + +2. a is 4 and b is 2.0 +* #+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/input_output/slides.tex b/input_output/slides.tex index 79ce52b..21a6074 100644 --- a/input_output/slides.tex +++ b/input_output/slides.tex @@ -1,4 +1,4 @@ -% Created 2010-10-10 Sun 21:00 +% Created 2011-06-21 Tue 12:19 \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{I/O} +\title{} \author{FOSSEE} \date{} \usetheme{Warsaw}\usecolortheme{default}\useoutertheme{infolines}\setbeamercovered{transparent} \begin{document} -\maketitle + @@ -41,104 +41,140 @@ showstringspaces=false, keywordstyle=\color{blue}\bfseries} \begin{frame} -\frametitle{Outline} -\label{sec-1} -\begin{itemize} -\item Showing output to the user. -\item Taking input from the user. -\end{itemize} +\begin{center} +\vspace{12pt} +\textcolor{blue}{\huge Input/Output} +\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} - What happens when you do \texttt{print "x is \%d y is \%f" \%(x, y)} +At the end of this tutorial,you will be able to, + + +\begin{itemize} +\item Print some value. +\item Print using modifiers. +\item Take input from user. +\item Display a prompt to the user before taking the input. +\end{itemize} + \end{frame} \begin{frame} -\frametitle{Solution 1} +\frametitle{Exercise 1} \label{sec-3} - \texttt{int} value of \texttt{x} and \texttt{float} value of \texttt{y} are printed corresponding to the - modifiers used in the \texttt{print} statement + +\begin{itemize} +\item What happens when you do\\ + \verb~print ``x is \%d, y is \%f'' \%(x, y)~ +\end{itemize} \end{frame} \begin{frame} -\frametitle{Question 2} +\frametitle{Exercise 2} \label{sec-4} - Enter the number 5.6 as input and store it in a variable called - \texttt{c}. + +\begin{itemize} +\item Enter the number 5.6 as input and store it in a variable called + \verb~c~. +\end{itemize} \end{frame} -\begin{frame}[fragile] -\frametitle{Solution 2} +\begin{frame} +\frametitle{Exercise 3} \label{sec-5} -\lstset{language=Python} -\begin{lstlisting} -In []: c = raw_input() -5.6 -In []: c -\end{lstlisting} + +\begin{itemize} +\item What happens when you do not enter anything and hit enter. +\end{itemize} \end{frame} \begin{frame} -\frametitle{Question 3} +\frametitle{Exercise 4} \label{sec-6} - What happens when you do not enter anything and hit enter + +\begin{itemize} +\item How do you display a prompt and let the user enter input in a new line. +\end{itemize} \end{frame} -\begin{frame}[fragile] -\frametitle{Solution 3} +\begin{frame} +\frametitle{Summary} \label{sec-7} -\lstset{language=Python} -\begin{lstlisting} -In []: c = raw_input() - -In []: c -\end{lstlisting} + In this tutorial, we have learnt to, + + +\begin{itemize} +\item Use the print statement. +\item Use the modifiers \%d, \%f, \%s in the print statement. +\item Take input from user by using ``raw\_input()''. +\item Display a prompt to the user before taking the input by passing + a string as an argument to ``raw\_input''. +\end{itemize} \end{frame} \begin{frame} -\frametitle{Question 4} +\frametitle{Evaluation} \label{sec-8} - How do you display a prompt and let the user enter input in a new line -\end{frame} -\begin{frame}[fragile] -\frametitle{Solution 4} -\label{sec-9} -\lstset{language=Python} -\begin{lstlisting} -In []: ip = raw_input("Please enter a number in the next line\n> ") -\end{lstlisting} +\begin{enumerate} +\item ``a = raw\_input()'' and user enters ``2.5''.\\ + What is the type of a? +\begin{itemize} +\item str +\item int +\item float +\item char +\end{itemize} +\vspace{5pt} +\item ``a = 2'' and ``b = 4.5''. + What is the result of the following action.\\ + \verb~print ``a is \%d and b is \%2.1f'' \%(b,a)~ +\begin{itemize} +\item a is 2 and b is 4.5 +\item a is 4 and b is 2 +\item a is 4 and b is 2.0 +\item a is 4.5 and b is 2 +\end{itemize} +\end{enumerate} \end{frame} \begin{frame} -\frametitle{Summary} -\label{sec-10} +\frametitle{Solutions} +\label{sec-9} - You should now be able to -- -\begin{itemize} -\item Print a value ``as is'' -\item Print a value using using modifiers -\item Accept input from user -\item Display a prompt before accepting input -\end{itemize} + +\begin{enumerate} +\item str +\vspace{12pt} +\item a is 4 and b is 2.0 +\end{enumerate} \end{frame} \begin{frame} -\frametitle{Thank you!} -\label{sec-11} \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 -- cgit