diff options
author | Santosh G. Vattam | 2010-05-05 17:31:32 +0530 |
---|---|---|
committer | Santosh G. Vattam | 2010-05-05 17:31:32 +0530 |
commit | 81aa5044a1ca757df4f7ec48e56cee814a8d7340 (patch) | |
tree | 5ddf727f4bba2647d427b1d35fb925260f4f6ed9 | |
parent | 80e14a5cada5f8b655fa9b2e01c5287235b74a5d (diff) | |
download | st-scripts-81aa5044a1ca757df4f7ec48e56cee814a8d7340.tar.gz st-scripts-81aa5044a1ca757df4f7ec48e56cee814a8d7340.tar.bz2 st-scripts-81aa5044a1ca757df4f7ec48e56cee814a8d7340.zip |
Completed functions.org and added functions.tex file.
-rw-r--r-- | functions.org | 25 | ||||
-rw-r--r-- | ipython-tut.txt | 8 | ||||
-rw-r--r-- | presentations/functions.tex | 138 |
3 files changed, 162 insertions, 9 deletions
diff --git a/functions.org b/functions.org index 12dca14..5675cc3 100644 --- a/functions.org +++ b/functions.org @@ -60,8 +60,8 @@ We shall now look at default arguments. [show slide with examples of functions with default arguments] The split function has been used in two different ways in the - previous tutorials - one for splitting on spaces and the other for - splitting on semicolons. + given example - one for splitting on spaces and the other for + splitting on commas. The function split is being called with no arguments and one argument, respectively. In the first case, white space is being @@ -83,7 +83,7 @@ We have already looked at functions and keyword arguments in these examples. loc, linewidth, xy, labels are all keywords. - Let's now customize our function so that it displays a custom + Let's now edit our function so that it displays a custom greeting message as well. def welcome( greet = 'Hello', name = 'World!'): @@ -99,12 +99,25 @@ in any order and removes the need to remember the order of arguments in the function definition. - + Let's now write a new function + + def per_square(n): + i = 1 + while ( i*i < n ): + i += 1 + return i*i == n, i + + What does this function do? It checks if the given number is a perfect square. + If it is, then the function returns True along with the square root of + the given number. If the number is not a perfect square it returns + False and the square root of the next perfect square. + + Please observe that this function returns 2 values. In Python there is no restriction on the number of values returned by - a function. When a function returns more than one value, the multiple + a function. Whenever a function has to return more than one value, the multiple values are packed into one single tuple and that single tuple is returned. - We come to the end of this tutorial on functions. In this tutorial + With this we come to the end of this tutorial on functions. In this tutorial we have learnt about functions in a greater detail. We looked at how to define functions, calling them, default and keyword arguments. diff --git a/ipython-tut.txt b/ipython-tut.txt index cdd2644..76a5556 100644 --- a/ipython-tut.txt +++ b/ipython-tut.txt @@ -1,7 +1,9 @@ Hello friends and welcome to this tutorial on IPython and its features. IPython is an enhanced interactive Python interpreter. IPython provides various advanced -features that the vanilla Python interpreter does not. We have shown some of these -features in the previous tutorials. In this tutorial we shall look at a few more -of them. +features that the vanilla Python interpreter does not. We have seen some of these +features in the previous tutorials. In this tutorial we shall recall them and +look at a few more new ones. + + diff --git a/presentations/functions.tex b/presentations/functions.tex new file mode 100644 index 0000000..9300141 --- /dev/null +++ b/presentations/functions.tex @@ -0,0 +1,138 @@ +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%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} + +% Taken from Fernando's slides. +\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{Python for Scientific Computing : Functions} + +\author[FOSSEE] {FOSSEE} + +\institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} +\date{} + +% DOCUMENT STARTS +\begin{document} + +\begin{frame} + \titlepage +\end{frame} + +\begin{frame} + \frametitle{About the Session} + \begin{block}{Functions in Python} + \begin{itemize} + \item Function definition + \item Function call + \end{itemize} + \end{block} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Functions: default arguments} + \begin{lstlisting} +In []: greet = 'hello world' + +In []: greet.split() +Out[]: ['hello', 'world'] + +In []: line = 'Rossum, Guido, 54, 46, 55' + +In []: line.split(',') +Out[]: ['Rossum', ' Guido', ' 54', + ' 46', ' 55'] + \end{lstlisting} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Functions: Keyword arguments} +We have seen the following +\begin{lstlisting} +legend(['sin(2y)'], loc = 'center') + +plot(y, sin(y), 'g', linewidth = 2) + +annotate('local max', xy = (1.5, 1)) + +pie(science.values(), + labels = science.keys()) + \end{lstlisting} +\end{frame} + +\begin{frame} + \frametitle{Summary} + \begin{itemize} + \item The \typ{def} keyword + \item Docstrings + \item Function arguments + \begin{itemize} + \item Default arguments + \item Keyword arguments + \end{itemize} + \item Return values + \end{itemize} +\end{frame} + +\begin{frame} + \frametitle{Thank you!} + \begin{block}{} + This session is part of \textcolor{blue}{FOSSEE} project funded by: + \begin{center} + \textcolor{blue}{NME through ICT from MHRD, Govt. of India}. + \end{center} + \end{block} +\end{frame} + +\end{document} |