diff options
Diffstat (limited to 'additional_ipython')
-rw-r--r-- | additional_ipython/quickref.tex | 11 | ||||
-rw-r--r-- | additional_ipython/script.rst | 214 | ||||
-rw-r--r-- | additional_ipython/slides.tex | 106 |
3 files changed, 331 insertions, 0 deletions
diff --git a/additional_ipython/quickref.tex b/additional_ipython/quickref.tex new file mode 100644 index 0000000..a0212b3 --- /dev/null +++ b/additional_ipython/quickref.tex @@ -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|} diff --git a/additional_ipython/script.rst b/additional_ipython/script.rst new file mode 100644 index 0000000..1a00973 --- /dev/null +++ b/additional_ipython/script.rst @@ -0,0 +1,214 @@ +.. Objectives +.. ---------- + +.. A - Students and teachers from Science and engineering backgrounds + B - + C - + D - + +.. Prerequisites +.. ------------- + +.. 1. Embellishing Plots + +.. 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 Additional Features of IPython + +{{{ Show the slide containing title }}} + +{{{ Show the slide containing the outline slide }}} + +In this tutorial, we shall look at additional features of IPython that help us +to retreive the commands that we type on the interpreter and then save them +into a file and run it. + +Let us start ipython with pylab loaded, by typing +:: + + $ ipython -pylab + +on the terminal + +{{{ shit to terminal and type ipython -pylab }}} + +We shall first make a plot and then view the history and save it. +:: + + x = linspace(-2*pi, 2*pi, 100) + plot(x, xsinx(x)) + +xsin(x) is actually x * sin(x) +:: + + plot(x, x*sin(x)) + plot(x, sin(x)) + xlabel("x") + ylabel("$f(x)$") + title("x and xsin") + +We now have the plot. Let us look at the commands that we have typed in. The +history can be retreived by using =%hist= command. Type +:: + + %hist + +As you can see, it displays a list of recent commands that we typed. Every +command has a number in front, to specify in which order and when it was typed. + +Please note that there is a % sign before the hist command. This implies that +%hist is a command that is specific to IPython and not available in vannila +Python interpreter. These type of commands are called as magic commands. + +Also note that, the =%hist= itself is a command and is displayed as the most +recent command. This implies that anything we type in is stored as history, +irrespective of whether it is command or an error or IPython magic command. + +If we want only the recent 5 to be displayed, we pass the number as an argument +to =%hist= command. Hence +:: + + %hist 5 + +displays the recent 5 commands, inclusive of the =%hist= command. +The default number is 40. + +{{{ Pause here and try out the following exercises }}} + +%% 1 %% Read through the %hist documenatation and find out how can we list all + the commands between 5 and 10 + +{{{ continue from paused state }}} + +As we can see from =%hist= documentation, +:: + + %hist 5 10 + +displays the commands from 5 to 10 + +Now that we have the history, we would like to save the required line of code +from history. This is possible by using the =%save= command. + +Before we do that, let us first look at history and identify what lines of code +we require.Type +:: + + %hist + + +{{{ point to the lines }}} + +The first command is linspace. But second command is a command that gave us an +error. Hence we do not need seconf. The commands from third to sixth are +required. The seventh command although is correct, we do not need it since we +are setting the title correctly in the eigthth command. + +So we need first third to sixth and the eigthth command for our program. +Hence the syntax of =%save= is +:: + + %save /home/fossee/plot_script.py 1 3-6 8 + +{{{ point to the output of the command }}} + +The command saves first and then third to sixth and eighth lines of code into +the specified file. + +The first argument to %save is the path of file to save the commands and the +arguments there after are the commands to be saved in the given order. + +{{{ goto the file and open it and show it }}} + +{{{ Pause here and try out the following exercises }}} + +%% 2 %% change the label on y-axis to "y" and save the lines of code + accordingly + +{{{ continue from paused state }}} + +we use the command =ylabel= on interpreter as +:: + + ylabel("y") + +and then do +:: + + %save /home/fossee/example_plot.py 1 3-6 10 + +Now that we have the required lines of code in a file, let us learn how to run +the file as a python script. + +We use the IPython magic command =%run= to do this. Type +:: + + %run -i /home/fossee/plot_script.py + +The script runs but we do not see the plot. This happens because we are running +a script and we are not in interactive mode anymore. + +Hence on your terminal type +:: + + show() + +to show the plot. + +{{{ Pause here and try out the following exercises }}} + +%% 3 %% Use %hist and %save and create a script that has show in it and run it + to produce and show the plot. + +{{{ continue from paused state }}} + +We first look at the history using +:: + + %hist 20 + +Then save the script using +:: + + %save /home/fossee/show_included.py 1 3-6 8 10 13 + %run -i /home/fossee/show_included.py + +We get the desired plot. + +The reason for including a -i after run is to tell the interpreter that if any +name is not found in script, search for it in the interpreter. Hence all these +sin, plot, pi and show which are not available in script, are taken from the +interpreter and used to run the script. + +{{{ Pause here and try out the following exercises }}} + +%% 4 %% Run the script without using the -i option. Do you find any difference? + +{{{ continue from paused state }}} + +We see that it raises nameerror saying the name linspace is not found. + +{{{ Show summary slide }}} + +This brings us to the end of the tutorial. +we have looked at + + * Retreiving history using =%hist= command + * Vieweing only a part of history by passing an argument to %hist + * saving the required lines of code in required order using %save + * using %run -i command to run the saved script + +{{{ 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 + diff --git a/additional_ipython/slides.tex b/additional_ipython/slides.tex new file mode 100644 index 0000000..df1462c --- /dev/null +++ b/additional_ipython/slides.tex @@ -0,0 +1,106 @@ +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%Tutorial slides on Python. +% +% Author: FOSSEE +% Copyright (c) 2009, FOSSEE, IIT Bombay +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +\documentclass[14pt,compress]{beamer} +%\documentclass[draft]{beamer} +%\documentclass[compress,handout]{beamer} +%\usepackage{pgfpages} +%\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm] + +% Modified from: generic-ornate-15min-45min.de.tex +\mode<presentation> +{ + \usetheme{Warsaw} + \useoutertheme{infolines} + \setbeamercovered{transparent} +} + +\usepackage[english]{babel} +\usepackage[latin1]{inputenc} +%\usepackage{times} +\usepackage[T1]{fontenc} + +\usepackage{ae,aecompl} +\usepackage{mathpazo,courier,euler} +\usepackage[scaled=.95]{helvet} + +\definecolor{darkgreen}{rgb}{0,0.5,0} + +\usepackage{listings} +\lstset{language=Python, + basicstyle=\ttfamily\bfseries, + commentstyle=\color{red}\itshape, + stringstyle=\color{darkgreen}, + showstringspaces=false, + keywordstyle=\color{blue}\bfseries} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Macros +\setbeamercolor{emphbar}{bg=blue!20, fg=black} +\newcommand{\emphbar}[1] +{\begin{beamercolorbox}[rounded=true]{emphbar} + {#1} + \end{beamercolorbox} +} +\newcounter{time} +\setcounter{time}{0} +\newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}} + +\newcommand{\typ}[1]{\lstinline{#1}} + +\newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } + +% Title page +\title{Your Title Here} + +\author[FOSSEE] {FOSSEE} + +\institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} +\date{} + +% DOCUMENT STARTS +\begin{document} + +\begin{frame} + \maketitle +\end{frame} + +\begin{frame}[fragile] + \frametitle{Outline} + \begin{itemize} + \item + \end{itemize} +\end{frame} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% All other slides here. %% +%% The same slides will be used in a classroom setting. %% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +\begin{frame}[fragile] + \frametitle{Summary} + \begin{itemize} + \item + \end{itemize} +\end{frame} + +\begin{frame} + \frametitle{Thank you!} + \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} |