%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %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 { \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}}} } %%% This is from Fernando's setup. % \usepackage{color} % \definecolor{orange}{cmyk}{0,0.4,0.8,0.2} % % Use and configure listings package for nicely formatted code % \usepackage{listings} % \lstset{ % language=Python, % basicstyle=\small\ttfamily, % commentstyle=\ttfamily\color{blue}, % stringstyle=\ttfamily\color{orange}, % showstringspaces=false, % breaklines=true, % postbreak = \space\dots % } %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Title page \title[Basic Python]{Python language: Basics} \author[FOSSEE Team] {The FOSSEE Group} \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} \date[] {SciPy 2010, Introductory tutorials\\Day 2, Session 1} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} %\logo{\pgfuseimage{iitmlogo}} %% Delete this, if you do not want the table of contents to pop up at %% the beginning of each subsection: \AtBeginSubsection[] { \begin{frame} \frametitle{Outline} \tableofcontents[currentsection,currentsubsection] \end{frame} } \AtBeginSection[] { \begin{frame} \frametitle{Outline} \tableofcontents[currentsection,currentsubsection] \end{frame} } % If you wish to uncover everything in a step-wise fashion, uncomment % the following command: %\beamerdefaultoverlayspecification{<+->} %\includeonlyframes{current,current1,current2,current3,current4,current5,current6} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % DOCUMENT STARTS \begin{document} \begin{frame} \titlepage \end{frame} \begin{frame} \frametitle{Outline} \tableofcontents % You might wish to add the option [pausesections] \end{frame} \section{Data types} \begin{frame} \frametitle{Primitive Data types} \begin{itemize} \item Numbers: float, int, complex \item Strings \item Booleans \end{itemize} \end{frame} \subsection{Numbers} \begin{frame}[fragile] \frametitle{Numbers} \begin{itemize} \item \kwrd{int}\\ whole number, no matter what the size! \begin{lstlisting} In []: a = 13 In []: b = 99999999999999999999 \end{lstlisting} \item \kwrd{float} \begin{lstlisting} In []: p = 3.141592 \end{lstlisting} \end{itemize} \end{frame} \begin{frame}[fragile] \frametitle{Complex numbers} \begin{lstlisting} In []: c = 3+4j In []: abs(c) Out[]: 5.0 In []: c.imag Out[]: 4.0 In []: c.real Out[]: 3.0 \end{lstlisting} \end{frame} \subsection{Booleans} \begin{frame}[fragile] \frametitle{Booleans} \begin{lstlisting} In []: t = True In []: F = not t In []: F or t Out[]: True In []: F and t Out[]: False \end{lstlisting} \inctime{5} \end{frame} \begin{frame}[fragile] \frametitle{( ) for precedence} \begin{lstlisting} In []: a = False In []: b = True In []: c = True In []: (a and b) or c Out[]: True In []: a and (b or c) Out[]: False \end{lstlisting} \inctime{5} \end{frame} \subsection{Strings} \begin{frame}[fragile] \frametitle{Strings} Strings were introduced previously, let us now look at them in a little more detail. \begin{lstlisting} In []: w = "hello" In []: print w[0] + w[2] + w[-1] Out[]: hlo In []: len(w) Out[]: 5 \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Strings \ldots} \emphbar{Strings are immutable} \begin{lstlisting} In []: w[0] = 'H' \end{lstlisting} \pause \begin{lstlisting} -------------------------------------------- TypeError Traceback (most recent call last) in () TypeError: 'str' object does not support item assignment \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{String methods} \begin{lstlisting} In []: a = 'Hello World' In []: a.startswith('Hell') Out[]: True In []: a.endswith('ld') Out[]: True In []: a.upper() Out[]: 'HELLO WORLD' In []: a.lower() Out[]: 'hello world' \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{A bit about IPython} Recall, we showed a few features of IPython, here is one more: \begin{itemize} \item IPython provides better help \item object.function? \begin{lstlisting} In []: a = 'Hello World' In []: a.lower? \end{lstlisting} \end{itemize} \end{frame} \begin{frame}[fragile] \frametitle{Still with strings} \begin{itemize} \item We saw split() yesterday \item join() is the opposite of split() \end{itemize} \begin{lstlisting} In []: ''.join(['a', 'b', 'c']) Out[]: 'abc' In []: ', '.join(['a', 'b', 'c']) Out[]: 'a, b, c' \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{String formatting} \begin{lstlisting} In []: x, y = 1, 1.234 In []: 'x is %s, y is %s' %(x, y) Out[]: 'x is 1, y is 1.234' \end{lstlisting} \begin{itemize} \item \emph{\%d}, \emph{\%f} etc. available \end{itemize} \emphbar{\url{http://docs.python.org/library/stdtypes.html}} \inctime{10} \end{frame} \section{Operators} \begin{frame}[fragile] \frametitle{Arithmetic operators} \small \begin{lstlisting} In []: 1786 % 12 Out[]: 10 In []: 45 % 2 Out[]: 1 In []: 864675 % 10 Out[]: 5 In []: 3124 * 126789 Out[]: 396088836 In []: big = 1234567891234567890 ** 3 In []: verybig = big * big * big * big \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Arithmetic operators} \begin{lstlisting} In []: 17 / 2 Out[]: 8 In []: 17 / 2.0 Out[]: 8.5 In []: 17.0 / 2 Out[]: 8.5 In []: 17.0 / 8.5 Out[]: 2.0 \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Arithmetic operators} \begin{lstlisting} In []: a = 7546 In []: a += 1 In []: a Out[]: 7547 In []: a -= 5 In []: a In []: a *= 2 In []: a /= 5 \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{String operations} \begin{lstlisting} In []: s = 'Hello' In []: p = 'World' In []: s + p Out[]: 'HelloWorld' In []: s * 4 Out[]: 'HelloHelloHelloHello' \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{String operations \ldots} \begin{lstlisting} In []: s * s \end{lstlisting} \pause \begin{lstlisting} -------------------------------------------- TypeError Traceback (most recent call last) in () TypeError: can`t multiply sequence by non-int of type `str` \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Relational and logical operators} \begin{lstlisting} In []: p, z, n = 1, 0, -1 In []: p == n Out[]: False In []: p >= n Out[]: True In []: n < z < p Out[]: True In []: p + n != z Out[]: False \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Built-ins} \begin{lstlisting} In []: int(17 / 2.0) Out[]: 8 In []: float(17 / 2) Out[]: 8.0 In []: str(17 / 2.0) Out[]: '8.5' In []: round( 7.5 ) Out[]: 8.0 \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Odds and ends} \begin{itemize} \item Case sensitive \item Dynamically typed $\Rightarrow$ need not specify a type \begin{lstlisting} In []: a = 1 In []: a = 1.1 In []: a = "Now I am a string!" \end{lstlisting} \item Comments: \begin{lstlisting} In []: a = 1 # In-line comments In []: # A comment line. In []: a = "# Not a comment!" \end{lstlisting} \end{itemize} \inctime{15} \end{frame} \section{Simple IO} \begin{frame}[fragile] \frametitle{Simple IO: Console Input} \small \begin{itemize} \item raw\_input() waits for user input. \begin{lstlisting} In []: a = raw_input() 5 In []: a Out[]: '5' In []: a = raw_input('Enter a value: ') Enter a value: 5 \end{lstlisting} \item Prompt string is optional. \item All keystrokes are strings! \item \texttt{int()} converts string to int. \end{itemize} \end{frame} \begin{frame}[fragile] \frametitle{Simple IO: Console output} \begin{itemize} \item \typ{print} is straight forward \item Put the following code snippet in a file \typ{hello1.py} \end{itemize} \begin{lstlisting} print "Hello" print "World" In []: %run -i hello1.py Hello World \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Simple IO: Console output \ldots} Put the following code snippet in a file \typ{hello2.py} \begin{lstlisting} print "Hello", print "World" In []: %run -i hello2.py Hello World \end{lstlisting} \emphbar{Note the distinction between \typ{print x} and \typ{print x,}} \end{frame} \section{Control flow} \begin{frame} \frametitle{Control flow constructs} \begin{itemize} \item \kwrd{if/elif/else}: branching \item \kwrd{C if X else D}: Ternary conditional operator \item \kwrd{while}: looping \item \kwrd{for}: iterating \item \kwrd{break, continue}: modify loop \item \kwrd{pass}: syntactic filler \end{itemize} \end{frame} \subsection{Basic Conditional flow} \begin{frame}[fragile] \frametitle{\typ{If...elif...else} example} Type the following code in an editor \& save as \alert{ladder.py} \small \begin{lstlisting} x = int(raw_input("Enter an integer:")) if x < 0: print 'Be positive!' elif x == 0: print 'Zero' elif x == 1: print 'Single' else: print 'More' \end{lstlisting} \inctime{10} \end{frame} \begin{frame}[fragile] \frametitle{Ternary conditional operator} \begin{lstlisting} ... a = raw_input('Enter number(Q to quit):') num = int(a) if a != 'Q' else 0 ... \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{What did we learn?} \begin{itemize} \item Data types: int, float, complex, boolean, string \item Operators: +, -, *, /, \%, **, +=, -=, *=, /=, >, <, <=, >=, ==, !=, a < b < c \item Simple IO: \kwrd{raw\_input} and \kwrd{print} \item Conditional structures: \kwrd{if/elif/else},\\ \kwrd{C if X else D} \end{itemize} \end{frame} \end{document} %% Questions for Quiz %% %% ------------------ %% \begin{frame} \frametitle{\incqno } What is the largest integer value that can be represented natively by Python? \end{frame} \begin{frame} \frametitle{\incqno } What is the result of 17.0 / 2? \end{frame} \begin{frame} \frametitle{\incqno } Which of the following is not a type in Python? \begin{enumerate} \item int \item float \item char \item string \end{enumerate} \end{frame} \begin{frame} \frametitle{\incqno } How do you create a complex number with real part 2 and imaginary part 0.5. \end{frame} \begin{frame} \frametitle{\incqno } What is the difference between \kwrd{print} \emph{x} and \kwrd{print} \emph{x,} ? \end{frame} \begin{frame} \frametitle{\incqno } What does '*' * 40 produce? \end{frame} \begin{frame}[fragile] \frametitle{\incqno } What is the output of: \begin{lstlisting} In []: ', '.join(['a', 'b', 'c']) \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{\incqno} \begin{lstlisting} In []: 47 % 3 \end{lstlisting} What is the output? \end{frame}