# HG changeset patch # User Nishanth # Date 1286440832 -19800 # Node ID da873a5ac918d38c5c49d8dddbec134bb92b2459 # Parent e78c284d644ba382eab00a7bc848b56ab32b44dc made the tuples.rst into new template form diff -r e78c284d644b -r da873a5ac918 tuples.rst --- a/tuples.rst Thu Oct 07 10:57:15 2010 +0530 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,206 +0,0 @@ -Hello friends and welcome to the tutorial on Tuples - -{{{ Show the slide containing title }}} - -{{{ Show the slide containing the outline slide }}} - -In this tutorial, we shall learn - - * what are tuples - * their similarities and dissimilarities with lists - * why are they needed - -Let`s get started by defining a tuple. A tuple is defined by enclosing -parantheses around a sequence of items seperated by commas. It is similar to -defining a list except that parantheses are used instead of square brackets. -:: - - t = (1, 2.5, "hello", -4, "world", 1.24, 5) - t - -defines a tuple. The items in the tuple are indexed using numbers and can be -accessed by using their position. -:: - - t[3] - -prints -4 which is the fourth item of the tuple. - -:: - - t[1:5:2] - -prints the corresponding slice - -This is the behaviour similar as to lists. But the difference can be seen when -we try to change an element in the tuple. -:: - - t[2] = "Hello" - -We can see that, it raises an error saying tuple does not support item -assignment. It only implies that tuples are immutable or in simple words, -tuples cannot be changed. - -But what is the use of tuples!!! - -We shall understand that soon. But let us look at a simple problem of swapping -values. - -{{{ Pause here and try out the following exercises }}} - -%% 1 %% a = 5 and b = 7. swap the values of a and b - -{{{ continue from paused state }}} -:: - - a = 5 - b = 7 - - a - b - -We define the two values -:: - - temp = a - a = b - b = temp - - a - b - -This is the traditional approach - -Now let us do it the python way -:: - - a - b - - a, b = b, a - - a - b - -We see that the values are swapped. -This idiom works for different datatypes also. -:: - - a = 2.5 - b = "hello" - - a - b - -Moreover this type of behaviour is straight forward and what you would expect -should happen naturally. - -This is possible because of the immutability of tuples. This process is called -tuple packing and unpacking. - -Let us first see what is tuple packing. Type -:: - - 5, - -What we see is a tuple with one element. -:: - - 5, "hello", 2.5 - -Now it is a tuple with two elements. - -So when we are actually typing two or more elements seperated by commas, those -elements are packed and a tuple is made from them. - -When you type -:: - - a, b = b, a - -First the values of b and a are packed into a tuple on the right side and then -unpacked into the variables a and b. - -Immutability of tuples ensures that the values are not changed during the -packing and unpacking. - -{{{ Show summary slide }}} - -This brings us to the end of the tutorial. -we have learnt - - * How to define tuples - * The similarities of tuples with lists, like indexing and iterability - * The immutability of tuples - * The value swapping idiom in Python - * packing and unpacking of tuples - -{{{ 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 - -.. Author : Nishanth - Internal Reviewer 1 : - Internal Reviewer 2 : - External Reviewer : - -Questions -========= - - 1. Define a tuple containing two values. The first being integer 4 and second - is a float 2.5 - - Answer: (4, 2.5) - - 2. If ``a = (5, "Hello", 3.2)``. what is the value of a[2] - - Answer: 3.2 - - 3. If ``a = 5,`` then what is the type of a - - a. int - #. float - #. tuple - #. string - - Answer: tuple - - 4. if ``a = (2, 3)``. What does ``a[0], a[1] = (3, 4)`` produce - - Answer: Error - - 5. If ``a = ([2, 3], 4, 5)``. What is the value of ``a`` after doing - ``a[0].append(6)`` - - a. ([2, 3, 6], 4, 5) - #. Raises an error - #. ([2, 3], 4, 5) - #. [2, 3, 4, 5, 6] - - Answer: ([2, 3, 6], 4, 5) - - 6. What does the following code produce:: - - a = 5 - b = "Hello" - a, b = b, a - print a - print b - - Answer: Hello - 5 - - 7. ``a = ("hello", "world", 5, 6, 8)``. What is the value of a[1:4] - - Answer: ("world", 5, 6) - - 8. ``a = (1, 2, 3, 4, 5, 6, 7, 8)``. What is the value of a[1::3] - - Answer: (2, 5, 8) - - diff -r e78c284d644b -r da873a5ac918 tuples/questions.rst --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tuples/questions.rst Thu Oct 07 14:10:32 2010 +0530 @@ -0,0 +1,60 @@ +Objective Questions +------------------- + + 1. Define a tuple containing two values. The first being integer 4 and second + is a float 2.5 + + Answer: (4, 2.5) + + 2. If ``a = (5, "Hello", 3.2)``. what is the value of a[2] + + Answer: 3.2 + + 3. If ``a = 5,`` then what is the type of a + + a. int + #. float + #. tuple + #. string + + Answer: tuple + + 4. if ``a = (2, 3)``. What does ``a[0], a[1] = (3, 4)`` produce + + Answer: Error + + 5. If ``a = ([2, 3], 4, 5)``. What is the value of ``a`` after doing + ``a[0].append(6)`` + + a. ([2, 3, 6], 4, 5) + #. Raises an error + #. ([2, 3], 4, 5) + #. [2, 3, 4, 5, 6] + + Answer: ([2, 3, 6], 4, 5) + + 6. What does the following code produce:: + + a = 5 + b = "Hello" + a, b = b, a + print a + print b + + Answer:: + + Hello + 5 + + 7. ``a = ("hello", "world", 5, 6, 8)``. What is the value of a[1:4] + + Answer: ("world", 5, 6) + + 8. ``a = (1, 2, 3, 4, 5, 6, 7, 8)``. What is the value of a[1::3] + + Answer: (2, 5, 8) + + 9. ``a = (1, 2, 3, 4, 5, 6, 7, 8)``. What is the value of a[-3::-1] + + Answer: (6, 5, 4, 3, 2, 1) + diff -r e78c284d644b -r da873a5ac918 tuples/quickref.tex --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tuples/quickref.tex Thu Oct 07 14:10:32 2010 +0530 @@ -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 -r e78c284d644b -r da873a5ac918 tuples/script.rst --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tuples/script.rst Thu Oct 07 14:10:32 2010 +0530 @@ -0,0 +1,168 @@ +.. Objectives +.. ---------- + +.. A - Students and teachers from Science and engineering backgrounds + B - Will learn what are tuples and why they are needed + Will learn the various methods of accessing elements in tuples + C - + D - + +.. Prerequisites +.. ------------- + +.. 1. Getting started with lists + +.. Author : Nishanth Amuluru + Internal Reviewer : + External Reviewer : + Checklist OK? : [2010-10-05] + +Script +------ + +Hello friends and welcome to the tutorial on Tuples + +{{{ Show the slide containing title }}} + +{{{ Show the slide containing the outline slide }}} + +In this tutorial, we shall learn + + * what are tuples + * their similarities and dissimilarities with lists + * why are they needed + +Let`s get started by defining a tuple. A tuple is defined by enclosing +parantheses around a sequence of items seperated by commas. It is similar to +defining a list except that parantheses are used instead of square brackets. +:: + + t = (1, 2.5, "hello", -4, "world", 1.24, 5) + t + +defines a tuple. The items in the tuple are indexed using numbers and can be +accessed by using their position. +:: + + t[3] + +prints -4 which is the fourth item of the tuple. + +:: + + t[1:5:2] + +prints the corresponding slice + +This is the behaviour similar as to lists. But the difference can be seen when +we try to change an element in the tuple. +:: + + t[2] = "Hello" + +We can see that, it raises an error saying tuple does not support item +assignment. It only implies that tuples are immutable or in simple words, +tuples cannot be changed. + +But what is the use of tuples!!! + +We shall understand that soon. But let us look at a simple problem of swapping +values. + +{{{ Pause here and try out the following exercises }}} + +%% 1 %% a = 5 and b = 7. swap the values of a and b + +{{{ continue from paused state }}} +:: + + a = 5 + b = 7 + + a + b + +We define the two values +:: + + temp = a + a = b + b = temp + + a + b + +This is the traditional approach + +Now let us do it the python way +:: + + a + b + + a, b = b, a + + a + b + +We see that the values are swapped. +This idiom works for different datatypes also. +:: + + a = 2.5 + b = "hello" + + a + b + +Moreover this type of behaviour is straight forward and what you would expect +should happen naturally. + +This is possible because of the immutability of tuples. This process is called +tuple packing and unpacking. + +Let us first see what is tuple packing. Type +:: + + 5, + +What we see is a tuple with one element. +:: + + 5, "hello", 2.5 + +Now it is a tuple with two elements. + +So when we are actually typing two or more elements seperated by commas, those +elements are packed and a tuple is made from them. + +When you type +:: + + a, b = b, a + +First the values of b and a are packed into a tuple on the right side and then +unpacked into the variables a and b. + +Immutability of tuples ensures that the values are not changed during the +packing and unpacking. + +{{{ Show summary slide }}} + +This brings us to the end of the tutorial. +we have learnt + + * How to define tuples + * The similarities of tuples with lists, like indexing and iterability + * The immutability of tuples + * The value swapping idiom in Python + * packing and unpacking of tuples + +{{{ 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 -r e78c284d644b -r da873a5ac918 tuples/slides.tex --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tuples/slides.tex Thu Oct 07 14:10:32 2010 +0530 @@ -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 +{ + \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}