# HG changeset patch # User amit # Date 1286971379 -19800 # Node ID 4054b1a6392da437d6f5d5598459227ede3f288a # Parent e675f9208b91e8e5f4556d439b3921a0d37ad9c3# Parent e8c02b3c51aca4b45a260677014278d6288d4acf merging heads diff -r e8c02b3c51ac -r 4054b1a6392d basic-data-type/questions.rst --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/basic-data-type/questions.rst Wed Oct 13 17:32:59 2010 +0530 @@ -0,0 +1,83 @@ +Objective Questions +------------------- + +.. A mininum of 8 questions here (along with answers) + +1. How large can an integer in Python be? + + Any Size. + + +2. How do you define a complex number in Python? + + Using the following notation. + + [Real part] + [Imaginary part] j + example :: + + c= 3.2 + 4.6j + + + +3. Look at the following piece of code :: + + In []: f or t + Out[]:True + + What can you comment about the data type of f and t ? + +4. One major diffence between tuples and lists? + + Tuples are immutable while lists are not. + + +5. Look at the following sequence :: + + In []:t=true + NameError: name 'true' is not defined + + What might be the reason for error here? + + In this scenario , it seems the programmer wanted to create a variable t with the boolean value True with a capital T. Since no variable by the name true(small t) is known to the interpreter it gives a NameError. + + +6. Put the following string in a variable quotation. + "God doesn't play dice" -Albert Einstein + + quotation='''"God doesn't play dice" -Albert Einstein''' + +7. Given a tuple :: + + tup=(7,4,2,1,3,6,5,8) + tup[-2] + + 5 + +8. What is the syntax for checking containership in Python?:: + + element in sequence + 'l' in "Hello" + True + +9. Split this string on whitespaces? :: + + string="Split this string on whitespaces?" + + string.split() + +10. What is the answer of 5/2 and 5.0/2 . If yes , why. + + Yes, There is a difference. + Because one is integer division and other is float division. + +Larger Questions +---------------- + +.. A minimum of 2 questions here (along with answers) + +1. Given two lists for example, + list1=[1,2,3,4] and list2=[1,2,3,4,5,6,7] write a program to remove one list from the other. + + +#. Write a program to check if a string is palindrome? + diff -r e8c02b3c51ac -r 4054b1a6392d basic-data-type/quickref.tex --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/basic-data-type/quickref.tex Wed Oct 13 17:32:59 2010 +0530 @@ -0,0 +1,49 @@ +\documentclass{article} +\begin{Document} +\begin{center} +\textbf{Basic DataType Quick Reference}\\ +\end{center} +Declaring an Integer:\\ +{\ex \lstinline| b=9999999999999999999 |} + +Declaring a float:\\ +{\ex \lstinline| p=3.141592 |} + +Declaring a Complex number:\\ +{\ex \lstinline| c = 3.2+4.6j |} + +Modulo Operator:\\ +{\ex \lstinline| 87 % 6 |} + +Exponent Operator:\\ +{\ex \lstinline| 7**8 |} + +Declaring a list:\\ +{\ex \lstinline| var_list = [1, 1.2, [1,2]] |} + +Declaring a string:\\ +{\ex \lstinline| k='Single quote' |} +{\ex \lstinline| l="Double quote contain's single quote" |} +{\ex \lstinline| m='''"Contain's both"''' |} + +Declaring a tuple:\\ +{\ex \lstinline| var_tup = (1,2,3,4) |} + + +Accessing Lists, string and tuples:\\ +{\ex \lstinline| seq[-1] |} + +Interconversion of number datatype:\\ +{\ex \lstinline| float(2.3+4.2j) |} + + +Interconversion of sequences:\\ +{\ex \lstinline| tup=tuple([1,2,3,4,5]) |} + +Spliting string into lists:\\ +{\ex \lstinline| ''split this sting''.split() |} + +Join lists to create strings:\\ +{\ex \lstinline| ','.join['List','joined','on','commas'] |} + +\end{Document} diff -r e8c02b3c51ac -r 4054b1a6392d basic-data-type/script.rst --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/basic-data-type/script.rst Wed Oct 13 17:32:59 2010 +0530 @@ -0,0 +1,467 @@ +.. Objectives +.. ---------- + +.. Learn about Python Data Structures and Operators.(Remembering) +.. Use them to do basic operations.(Applying) + +.. Prerequisites +.. ------------- + + + +.. Author : Amit Sethi + Internal Reviewer : + External Reviewer : + Checklist OK? : [2010-10-05] +Hello friends and welcome to the tutorial on Basic Data types and operators in Python. +{{{ Show the slide containing title }}} + +{{{ Show the slide containing the outline slide }}} + +In this tutorial, we shall look at:: + + * Datatypes in Python + * Operators in Python + +with a little hands-on on how they can be applied to the different data types. + + + +First we will explore python data structures in the domain of numbers. +There are three built-in data types in python to represent numbers. + +{{{ A slide to make a memory note of this }}} + +These are: + + * Integers + * float and + * Complex + +Lets first talk about integers. :: + + a = 13 + a + + +Thats it, there we have our first integer variable a. + + + +If we now see :: + + type(a) + + +This means that a is a type of int. Being an int data structure +in python means that there are various functions that this variable +has to manipulate it different ways. You can explore these by doing, + + a. + + + +Lets see the limits of this int. + + b = 99999999999999999999 + b + +As you can see even when we put a value of 9 repeated 20 times +python did not complain. However when you asked python to print +the number again it put a capital L at the end. Now if you check +the type of this variable b, :: + + type(b) + + + +The reason for this is that python recognizes large integer numbers +by the data type long. However long type and integer type share there +functions and properties. + +Lets now try out the second type in list called float. + +Decimal numbers in python are recognized by the term float :: + + p = 3.141592 + p + +If you notice the value of output of p isn't exactly equal to p. This +is because computer saves floating point values in a specific +format. There is always an aproximationation. This is why we should +never rely on equality of floating point numbers in a program. + +The last data type in the list is complex number :: + + c = 3.2+4.6j + +as simple as that so essentialy its just a combination of two floats the +imaginary part being defined by j notation instead of i. Complex numbers have a lot of functions specific to them. +Lets check these :: + + c. + +Lets try some of them :: + + c.real + c.imag + +c.real gives the real part of the number and c.imag the imaginary. + +We can get the absolute value using the function :: + + abs(c) + + + +{{ Slide for memory aid }} + +Python also has Boolean as a built-in type. + +Try it out just type :: + + t = True + +note that T in true is capitalized. + +You can apply different Boolean operations on t now for example :: + + f = not t + f + f or t + f and t + + + +The results are explanotary in themselves. + +The usage of boolean brings us to an interesting question of precendence. +What if you want to apply one operator before another. + +Well you can use parenthesis for precedence. + +Lets write some piece of code to check this out. + + In[]: a=False + In[]: b=True + In[]: c=True + +To check how precedence changes with parenthesis. We will try two +expressions and their evaluation. + +one :: + + (a and b) or c + +This expression gives the value True + +where as the expression :: + + a and (b or c) + +gives the value False. + + +Lets now look at some operators available in Python to manipulate these data types. + + + +Python uses % for modulo operation :: + + 87 % 6 +and two stars for a exponent. :: + + 7**8 + + +In case one wishes to use the current value of variable in which the result is stored in the expression one can do that by putting the operator before `equal to`. :: + + a=73 + a*=34 + +is same as :: + + a=a*34 + +and :: + + a/=23 + +is same as :: + + a=a/23 + + +Lets now discuss sequence data stypes in python. Sequence +datatypes are those in which elements are kept in a sequential +order. All the elements accessed using index. + + +{{{ slide to for memory aid }}} + +The sequence datatypes in python are :: + + * list + * string + * tuple + +The list type is a container that holds a number of other +objects, in the given order. + +We create our first list by typing :: + + num_list = [1, 2, 3, 4] + num_list + + +Items enclosed in square brackets separated by comma +constitutes a list. + +Lists can store data of any type in them. + +We can have a list something like :: + + var_list = [1, 1.2, [1,2]] + var_list + + + +Now we will have a look at strings + +type :: + + In[]: greeting_string="hello" + + +greeting_string is now a string variable with the value "hello" + +{{{ Memory Aid Slide }}} + +Python strings can actually be defined in three different ways :: + + In[]: k='Single quote' + In[]: l="Double quote contain's single quote" + In[]: m='''"Contain's both"''' + +Thus, single quotes are used as delimiters usually. +When a string contains a single quote, double quotes are used as delimiters. +When a string quote contains both single and double quotes, triple quotes are +used as delimiters. + +The last in the list of sequence data types is tuple. + +To create a tuple we use normal brackets '(' +unlike '[' for lists.:: + + In[]: num_tuple = (1, 2, 3, 4, 5, 6, 7, 8) + +Because of their sequential property there are certain functions and +operations we can apply to all of them. + + + +The first one is accessing. + +They can be accessed using index numbers :: + + In[]: num_list[2] + In[]: num_list[-1] + In[]: greeting_string[1] + In[]: greeting_string[3] + In[]: greeting_string[-2] + In[]: num_tuple[2] + In[]: num_tuple[-3] + + +Indexing starts from 0 from left to right and from -1 when accessing +lists in reverse. Thus num_list[2] refers to the third element 3. +and greetings [-2] is the second element from the end , that is 'l'. + + + +Addition gives a new sequence containing both sequences :: + + In[]: num_list+var_list + In[]: a_string="another string" + In[]: greeting_string+a_string + In[]: t2=(3,4,6,7) + In[]: num_tuple+t2 + +len function gives the length :: + + In[]: len(num_list) + In[]: len(greeting_string) + In[]: len(num_tuple) + +Prints the length the variable. + +We can check the containership of an element using the 'in' keyword :: + + In[]: 3 in num_list + In[]: 'H' in greeting_string + In[]: 2 in num_tuple + +We see that it gives True and False accordingly. + +Find maximum using max function and minimum using min:: + + In[]: max(num_tuple) + In[]: min(greeting_string) + +Get a sorted list and reversed list using sorted and reversed function :: + + In[]: sorted(num_list) + In[]: reversed(greeting_string) + +As a consequence of the order one we access a group of elements together. +This is called slicing and striding. + +First Slicing + +Given a list :: + + In[]:j=[1,2,3,4,5,6] + +Lets say we want elements starting from 2 and ending in 5. + +For this we can do :: + + In[]: j[1:4] + +The syntax for slicing is sequence variable name square bracket +first element index, colon, second element index.The last element however is notincluded in the resultant list:: + + + In[]: j[:4] + +If first element is left blank default is from beginning and if last +element is left blank it means till the end. + + In[]: j[1:] + + In[]: j[:] + +This effectively is the whole list. + +Striding is similar to slicing except that the step size here is not one. + +Lets see by example :: + + new_num_list=[1,2,3,4,5,6,7,8,9,10] + new_num_list[1:8:2] + [2, 4, 6, 8] + +The colon two added in the end signifies all the alternate elements. This is why we call this concept +striding because we move through the list with a particular stride or step. The step in this example +being 2. + +We have talked about many similar features of lists, strings and tuples. But there are many important +features in lists that differ from strings and tuples. Lets see this by example.:: + + In[]: new_num_list[1]=9 + In[]: greeting_string[1]='k' + +{{{ slide to show the error }}} + + + +As you can see while the first command executes with out a problem there is an error on the second one. + +Now lets try :: + + In[]: new_tuple[1]=5 + +Its the same error. This is because strings and tuples share the property of being immutable. +We cannot change the value at a particular index just by assigning a new value at that position. + + +We have looked at different types but we need to convert one data type into another. Well lets one +by one go through methods by which we can convert one data type to other: + +We can convert all the number data types to one another :: + + i=34 + d=float(i) + d + +Python has built in functions int, float and complex to convert one number type +data structure to another. + + dec=2.34 + dec_con=int(dec) + dec_con + + +As you can see the decimal part of the number is simply stripped to get the integer.:: + + com=2.3+4.2j + float(com) + com + +In case of complex number to floating point only the real value of complex number is taken. + +Similarly we can convert list to tuple and tuple to list :: + + lst=[3,4,5,6] + tup=tuple(lst) + tupl=(3,23,4,56) + lst=list(tuple) + +However string to list and list to string is an interesting problem. +Lets say we have a string :: + + In: somestring="Is there a way to split on these spaces." + In: somestring.split() + + +This produces a list with the string split at whitespace. +similarly we can split on some other character. + + In: otherstring="Tim,Amy,Stewy,Boss" + +How do we split on comma , simply pass it as argument :: + + In: otherstring.split(',') + +join function does the opposite. Joins a list to make a string.:: + + In[]:','.join['List','joined','on','commas'] + +Thus we get a list joined on commas. Similarly we can do spaces.:: + + In[]:' '.join['Now','on','spaces'] + +Note that the list has to be a list of strings to apply join operation. + +With this we come to the end of this tutorial . + +In this tutorial we have discussed + +1. Number Datatypes , integer,float and complex +2. Boolean and datatype and operators +3. Sequence data types ,List,String and Tuple +4. Accesing sequence +5. Slicing sequences +6. Finding length , sorting and reversing operations on sequences. +7. Immutability. + + + + +.. #[Nishanth]: string to list is fine. But list to string can be left for + string manipulations. Just say it requires some string + manipulations and leave it there. + +.. #[Nishanth]: Where is the summary + There are no exercises in the script + +{{{ Show the "sponsored by FOSSEE" slide }}} + +This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India + +Hope you have enjoyed and found it useful. + +Thank You. + + + diff -r e8c02b3c51ac -r 4054b1a6392d basic-data-type/slides.org --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/basic-data-type/slides.org Wed Oct 13 17:32:59 2010 +0530 @@ -0,0 +1,72 @@ +#+LaTeX_CLASS: beamer +#+LaTeX_CLASS_OPTIONS: [presentation] +#+BEAMER_FRAME_LEVEL: 1 + +#+BEAMER_HEADER_EXTRA: \usetheme{Warsaw}\useoutertheme{infolines}\usecolortheme{default}\setbeamercovered{transparent} +#+COLUMNS: %45ITEM %10BEAMER_env(Env) %10BEAMER_envargs(Env Args) %4BEAMER_col(Col) %8BEAMER_extra(Extra) +#+PROPERTY: BEAMER_col_ALL 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 :ETC +#+OPTIONS: H:5 num:t toc:nil \n:nil @:t ::t |:t ^:t -:t f:t *:t <:t + +#+TITLE: Plotting Data +#+AUTHOR: FOSSEE +#+DATE: 2010-09-14 Tue +#+EMAIL: info@fossee.in + +# \author[FOSSEE] {FOSSEE} + +# \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} +# \date{} + +* Tutorial Plan +** Datatypes in Python +** Operators in Python + +* Numbers +** Integers +** Float +** Complex + +* Boolean +** True +** False + +* Sequence Data types +** Data in Sequence +** Accessed using Index +*** list +*** String +*** Tuple + +* All are Strings + +** k='Single quote' +** l="Double quote contain's single quote" +** m='''"Contain's both"''' + +* Summary +** a=73 +** b=3.14 +** c=3+4j + +* Summary Contd. + +** t=True +** f=False +** t and f + +* Summary Contd. +** l= [2,1,4,3] +** s='hello' +** tu=(1,2,3,4) + +* Summary Contd. +** tu[-1] +** s[1:-1] + +* Summary Contd. + +** Sorted(l) +** reversed(s) + + + diff -r e8c02b3c51ac -r 4054b1a6392d basic-data-type/slides.tex --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/basic-data-type/slides.tex Wed Oct 13 17:32:59 2010 +0530 @@ -0,0 +1,158 @@ +% Created 2010-10-13 Wed 17:08 +\documentclass[presentation]{beamer} +\usetheme{Warsaw}\useoutertheme{infolines}\usecolortheme{default}\setbeamercovered{transparent} +\usepackage[latin1]{inputenc} +\usepackage[T1]{fontenc} +\usepackage{graphicx} +\usepackage{longtable} +\usepackage{float} +\usepackage{wrapfig} +\usepackage{soul} +\usepackage{amssymb} +\usepackage{hyperref} + + +\title{Plotting Data } +\author{FOSSEE} +\date{2010-09-14 Tue} + +\begin{document} + +\maketitle + + + + + + +\begin{frame} +\frametitle{Tutorial Plan} +\label{sec-1} +\begin{itemize} + +\item Datatypes in Python\\ +\label{sec-1.1}% +\item Operators in Python\\ +\label{sec-1.2}% +\end{itemize} % ends low level +\end{frame} +\begin{frame} +\frametitle{Numbers} +\label{sec-2} +\begin{itemize} + +\item Integers\\ +\label{sec-2.1}% +\item Float\\ +\label{sec-2.2}% +\item Complex\\ +\label{sec-2.3}% +\end{itemize} % ends low level +\end{frame} +\begin{frame} +\frametitle{Boolean} +\label{sec-3} +\begin{itemize} + +\item True\\ +\label{sec-3.1}% +\item False\\ +\label{sec-3.2}% +\end{itemize} % ends low level +\end{frame} +\begin{frame} +\frametitle{Sequence Data types} +\label{sec-4} +\begin{itemize} + +\item Data in Sequence\\ +\label{sec-4.1}% +\item Accessed using Index +\label{sec-4.2}% +\begin{itemize} + +\item list\\ +\label{sec-4.2.1}% +\item String\\ +\label{sec-4.2.2}% +\item Tuple\\ +\label{sec-4.2.3}% +\end{itemize} % ends low level +\end{itemize} % ends low level +\end{frame} +\begin{frame} +\frametitle{All are Strings} +\label{sec-5} +\begin{itemize} + +\item k='Single quote'\\ +\label{sec-5.1}% +\item l="Double quote contain's single quote"\\ +\label{sec-5.2}% +\item m='''"Contain's both"'''\\ +\label{sec-5.3}% +\end{itemize} % ends low level +\end{frame} +\begin{frame} +\frametitle{Summary} +\label{sec-6} +\begin{itemize} + +\item a=73\\ +\label{sec-6.1}% +\item b=3.14\\ +\label{sec-6.2}% +\item c=3+4j\\ +\label{sec-6.3}% +\end{itemize} % ends low level +\end{frame} +\begin{frame} +\frametitle{Summary Contd.} +\label{sec-7} +\begin{itemize} + +\item t=True\\ +\label{sec-7.1}% +\item f=False\\ +\label{sec-7.2}% +\item t and f\\ +\label{sec-7.3}% +\end{itemize} % ends low level +\end{frame} +\begin{frame} +\frametitle{Summary Contd.} +\label{sec-8} +\begin{itemize} + +\item l= [2,1,4,3]\\ +\label{sec-8.1}% +\item s='hello'\\ +\label{sec-8.2}% +\item tu=(1,2,3,4)\\ +\label{sec-8.3}% +\end{itemize} % ends low level +\end{frame} +\begin{frame} +\frametitle{Summary Contd.} +\label{sec-9} +\begin{itemize} + +\item tu[-1]\\ +\label{sec-9.1}% +\item s[1:-1]\\ +\label{sec-9.2}% +\end{itemize} % ends low level +\end{frame} +\begin{frame} +\frametitle{Summary Contd.} +\label{sec-10} +\begin{itemize} + +\item Sorted(l)\\ +\label{sec-10.1}% +\item reversed(s)\\ +\label{sec-10.2}% +\end{itemize} % ends low level +\end{frame} + +\end{document} diff -r e8c02b3c51ac -r 4054b1a6392d basicdatatype.rst --- a/basicdatatype.rst Wed Oct 13 14:00:33 2010 +0530 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,405 +0,0 @@ -Hello friends and welcome to the tutorial on Basic Data types and -operators in Python. -{{{ Show the slide containing title }}} - -{{{ Show the slide containing the outline slide }}} - -In this tutorial, we shall look at:: - - * Various Datatypes in Python - * Operators with a little hands-on on how they can be applied to - the different data types. - - - -First we will explore python data structures in the domain of numbers. -There are three built-in data types in python to represent numbers. - -{{{ A slide to make a memory note of this }}} - -These are: - - * Integers - * Complex and - * Boolean - -Lets first talk about integers. :: - - a = 13 - a - - -Thats it, there we have our first integer variable a. - - - -If we now see :: - - type(a) - - -This means that a is a type of int. Being an int data structure -in python means that there are various functions that this variable -has to manipulate it different ways. You can explore these by doing, - - a. - - - -Lets see the limits of this int. - - b = 99999999999999999999 - b - -As you can see even when we put a value of 9 repeated 20 times -python did not complain. However when you asked python to print -the number again it put a capital L at the end. Now if you check -the type of this variable b, :: - - type(b) - - - -The reason for this is that python recognizes large integer numbers -by the data type long. However long type and integer type share there -functions and properties. - -Lets now try out the second type in list called float. - -Decimal numbers in python are recognized by the term float :: - - p = 3.141592 - p - -If you notice the value of output of p isn't exactly equal to p. This -is because computer saves floating point values in a specific -format. There is always an aproximationation. This is why we should -never rely on equality of floating point numbers in a program. - -The last data type in the list is complex number :: - - c = 3.2+4.6j - -as simple as that so essentialy its just a combination of two floats the -imaginary part being define by j notation instead of i. Complex numbers have a lot of functions specific to them. -Lets check these :: - - c. - -Lets try some of them :: - - c.real - c.imag - -c.real gives the real part of the number and c.imag the imaginary. - -We can get the absolute value using the function :: - - abs(c) - -Python also has Boolean as a built-in type. - -Try it out just type :: - - t = True - -note that T in true is capitalized. - -You can apply different Boolean operations on t now for example :: - - f = not t - f - f or t - f and t - - - -The results are explanotary in themselves. - -The usage of boolean brings us to an interesting question of precendence. -What if you want to apply one operator before another. - -Well you can use parenthesis for precedence. - -Lets write some piece of code to check this out. - - In[]: a=False - In[]: b=True - In[]: c=True - -To check how precedence changes with parenthesis. We will try two -expressions and their evaluation. - -one :: - - (a and b) or c - -This expression gives the value True - -where as the expression :: - - a and (b or c) - -gives the value False. - -Lets now discuss sequence data structures in python. Sequence -datatypes are those in which elements are kept in a sequential -order. All the elements accessed using index. - -{{{ slide to for memory aid }}} - -The sequence datatypes in python are :: - - * list - * string - * tuple - -The list type is a container that holds a number of other -objects, in the given order. - -We create our first list by typing :: - - num_list = [1, 2, 3, 4] - num_list - - -Items enclosed in square brackets separated by comma -constitutes a list. - -Lists can store data of any type in them. - -We can have a list something like :: - - var_list = [1, 1.2, [1,2]] - var_list - - - -Now we will have a look at strings - -type :: - - In[]: greeting_string="hello" - - -greeting_string is now a string variable with the value "hello" - -{{{ Memory Aid Slide }}} - -Python strings can actually be defined in three different ways :: - - In[]: k='Single quote' - In[]: l="Double quote contain's single quote" - In[]: m='''"Contain's both"''' - -Thus, single quotes are used as delimiters usually. -When a string contains a single quote, double quotes are used as delimiters. -When a string quote contains both single and double quotes, triple quotes are -used as delimiters. - -The last in the list of sequence data types is tuple. - -To create a tuple we use normal brackets '(' -unlike '[' for lists.:: - - In[]: num_tuple = (1, 2, 3, 4, 5, 6, 7, 8) - -Because of their sequential property there are certain functions and -operations we can apply to all of them. - -{{{ Slide for memory aid }}} - -The first one is accessing. - -They can be accessed using index numbers :: - - In[]: num_list[2] - In[]: num_list[-1] - In[]: greeting_string[1] - In[]: greeting_string[3] - In[]: greeting_string[-2] - In[]: num_tuple[2] - In[]: num_tuple[-3] - - -Indexing starts from 0 from left to right and from -1 when accessing -lists in reverse. Thus num_list[2] refers to the third element 3. -and greetings [-2] is the second element from the end , that is 'l'. - - - -Addition gives a new sequence containing both sequences :: - - In[]: num_list+var_list - In[]: a_string="another string" - In[]: greeting_string+a_string - In[]: t2=(3,4,6,7) - In[]: num_tuple+t2 - -len function gives the length :: - - In[]: len(num_list) - In[]: len(greeting_string) - In[]: len(num_tuple) - -Prints the length the variable. - -We can check the containership of an element using the 'in' keyword :: - - In[]: 3 in num_list - In[]: 'H' in greeting_string - In[]: 2 in num_tuple - -We see that it gives True and False accordingly. - -Find maximum using max function and minimum using min:: - - In[]: max(num_tuple) - In[]: min(greeting_string) - -Get a sorted list and reversed list using sorted and reversed function :: - - In[]: sorted(num_list) - In[]: reversed(greeting_string) - -As a consequence of the order one we access a group of elements together. -This is called slicing and striding. - -First Slicing - -Given a list :: - - In[]:j=[1,2,3,4,5,6] - -Lets say we want elements starting from 2 and ending in 5. - -For this we can do :: - - In[]: j[1:4] - -The syntax for slicing is sequence variable name square bracket -first element index, colon, second element index.The last element however is notincluded in the resultant list:: - - - In[]: j[:4] - -If first element is left blank default is from beginning and if last -element is left blank it means till the end. - - In[]: j[1:] - - In[]: j[:] - -This effectively is the whole list. - -Striding is similar to slicing except that the step size here is not one. - -Lets see by example :: - - new_num_list=[1,2,3,4,5,6,7,8,9,10] - new_num_list[1:8:2] - [2, 4, 6, 8] - -The colon two added in the end signifies all the alternate elements. This is why we call this concept -striding because we move through the list with a particular stride or step. The step in this example -being 2. - -We have talked about many similar features of lists, strings and tuples. But there are many important -features in lists that differ from strings and tuples. Lets see this by example.:: - - In[]: new_num_list[1]=9 - In[]: greeting_string[1]='k' - -{{{ slide to show the error }}} - - - -As you can see while the first command executes with out a problem there is an error on the second one. - -Now lets try :: - - In[]: new_tuple[1]=5 - -Its the same error. This is because strings and tuples share the property of being immutable. -We cannot change the value at a particular index just by assigning a new value at that position. - - -We have looked at different types but we need to convert one data type into another. Well lets one -by one go through methods by which we can convert one data type to other: - -We can convert all the number data types to one another :: - - i=34 - d=float(i) - d - -Python has built in functions int, float and complex to convert one number type -data structure to another. - - dec=2.34 - dec_con=int(dec) - dec_con - - -As you can see the decimal part of the number is simply stripped to get the integer.:: - - com=2.3+4.2j - float(com) - com - -In case of complex number to floating point only the real value of complex number is taken. - -Similarly we can convert list to tuple and tuple to list :: - - lst=[3,4,5,6] - tup=tuple(lst) - tupl=(3,23,4,56) - lst=list(tuple) - -However string to list and list to string is an interesting problem. -Lets say we have a string :: - - In: somestring="Is there a way to split on these spaces." - In: somestring.split() - - -This produces a list with the string split at whitespace. -similarly we can split on some other character. - - In: otherstring="Tim,Amy,Stewy,Boss" - -How do we split on comma , simply pass it as argument :: - - In: otherstring.split(',') - -join function does the opposite. Joins a list to make a string.:: - - In[]:','.join['List','joined','on','commas'] - -Thus we get a list joined on commas. Similarly we can do spaces.:: - - In[]:' '.join['Now','on','spaces'] - -Note that the list has to be a list of strings to apply join operation. - -.. #[Nishanth]: string to list is fine. But list to string can be left for - string manipulations. Just say it requires some string - manipulations and leave it there. - -.. #[Nishanth]: Where is the summary - There are no exercises in the script - -{{{ Show the "sponsored by FOSSEE" slide }}} - -This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India - -Hope you have enjoyed and found it useful. - -Thank You. - - - -Author : Amit Sethi -Internal Reviewer 1 : Nishanth -Internal Reviewer 2 : -External Reviewer diff -r e8c02b3c51ac -r 4054b1a6392d getting-started-with-lists/getting_started_with_lists.rst --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/getting-started-with-lists/getting_started_with_lists.rst Wed Oct 13 17:32:59 2010 +0530 @@ -0,0 +1,137 @@ +Hello friends and welcome to the tutorial on getting started with +lists. + + {{{ Show the slide containing title }}} + + {{{ Show the slide containing the outline slide }}} + +In this tutorial we will be getting acquainted with a python data +structure called lists. We will learn :: + + * How to create lists + * Structure of lists + * Access list elements + * Append elements to lists + * Deleting elements from lists + +List is a compound data type, it can contain data of other data +types. List is also a sequence data type, all the elements are in +order and there order has a meaning. + +We will first create an empty list with no elements. On your IPython +shell type :: + + empty = [] + type(empty) + + +This is an empty list without any elements. + +* Filled lists + +Lets now define a list, nonempty and fill it with some random elements. + +nonempty = ['spam', 'eggs', 100, 1.234] + +Thus the simplest way of creating a list is typing out a sequence +of comma-separated values (items) between square brackets. +All the list items need not have the same data type. + + + +As we can see lists can contain different kinds of data. In the +previous example 'spam' and 'eggs' are strings and 100 and 1.234 +integer and float. Thus we can put elements of heterogenous types in +lists. Thus list themselves can be one of the element types possible +in lists. Thus lists can also contain other lists. Example :: + + list_in_list=[[4,2,3,4],'and', 1, 2, 3, 4] + +We access list elements using the number of index. The +index begins from 0. So for list nonempty, nonempty[0] gives the +first element, nonempty[1] the second element and so on and +nonempty[3] the last element. :: + + nonempty[0] + nonempty[1] + nonempty[3] + +We can also access the elememts from the end using negative indices :: + + nonempty[-1] + nonempty[-2] + nonempty[-4] + +-1 gives the last element which is the 4th element , -2 second to last and -4 gives the fourth +from last element which is first element. + +We can append elements to the end of a list using append command. :: + + nonempty.append('onemore') + nonempty + nonempty.append(6) + nonempty + +As we can see non empty appends 'onemore' and 6 at the end. + + + +Using len function we can check the number of elements in the list +nonempty. In this case it being 6 :: + + len(nonempty) + + + +Just like we can append elements to a list we can also remove them. +There are two ways of doing it. One is by using index. :: + + del(nonempty[1]) + + + +deletes the element at index 1, i.e the second element of the +list, 'eggs'. The other way is removing element by content. Lets say +one wishes to delete 100 from nonempty list the syntax of the command +should be :: + + a.remove(100) + +but what if their were two 100's. To check that lets do a small +experiment. :: + + a.append('spam') + a + a.remove('spam') + a + +If we check a now we will see that the first occurence 'spam' is removed +thus remove removes the first occurence of the element in the sequence +and leaves others untouched. + + +{{{Slide for Summary }}} + + +In this tutorial we came across a sequence data type called lists. :: + + * We learned how to create lists. + * How to access lists. + * Append elements to list. + * Delete Element from list. + * And Checking list length. + + + +{{{ Sponsored by Fossee Slide }}} + +This tutorial was created as a part of FOSSEE project. + +I hope you found this tutorial useful. + +Thank You + + + * Author : Amit Sethi + * First Reviewer : + * Second Reviewer : Nishanth diff -r e8c02b3c51ac -r 4054b1a6392d getting-started-with-lists/questions.rst --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/getting-started-with-lists/questions.rst Wed Oct 13 17:32:59 2010 +0530 @@ -0,0 +1,47 @@ +Objective Questions +------------------- + +.. A mininum of 8 questions here (along with answers) + +1. How do you create an empty list? :: + + empty=[] + +2. What is the most important property of sequence data types like lists? + + The elements are in order and can be accessed by index numbers. + +3. Can you have a list inside a list ? + + Yes,List can contain all the other data types, including list. + + Example: + list_in_list=[2.3,[2,4,6],'string,'all datatypes can be there'] + +4. What is the index number of the first element in a list? + + 0 + nonempty = ['spam', 'eggs', 100, 1.234] + nonempty[0] + +5. How would you access the end of a list without finding its length? + + Using negative indices. We can the list from the end using negative indices. + + :: + nonempty = ['spam', 'eggs', 100, 1.234] + nonempty[-1] + +6. What is the function to find the length of a list? + + len + + 7. + +Larger Questions +---------------- + +.. A minimum of 2 questions here (along with answers) + +1. Question 1 +2. Question 2 diff -r e8c02b3c51ac -r 4054b1a6392d getting-started-with-lists/quickref.tex --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/getting-started-with-lists/quickref.tex Wed Oct 13 17:32:59 2010 +0530 @@ -0,0 +1,8 @@ +Creating a linear array:\\ +{\ex \lstinline| x = linspace(0, 2*pi, 50)|} + +Plotting two variables:\\ +{\ex \lstinline| plot(x, sin(x))|} + +Plotting two lists of equal length x, y:\\ +{\ex \lstinline| plot(x, y)|} diff -r e8c02b3c51ac -r 4054b1a6392d getting-started-with-lists/slides.org --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/getting-started-with-lists/slides.org Wed Oct 13 17:32:59 2010 +0530 @@ -0,0 +1,35 @@ +#+LaTeX_CLASS: beamer +#+LaTeX_CLASS_OPTIONS: [presentation] +#+BEAMER_FRAME_LEVEL: 1 + +#+BEAMER_HEADER_EXTRA: \usetheme{Warsaw}\useoutertheme{infolines}\usecolortheme{default}\setbeamercovered{transparent} +#+COLUMNS: %45ITEM %10BEAMER_env(Env) %10BEAMER_envargs(Env Args) %4BEAMER_col(Col) %8BEAMER_extra(Extra) +#+PROPERTY: BEAMER_col_ALL 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 :ETC +#+OPTIONS: H:5 num:t toc:nil \n:nil @:t ::t |:t ^:t -:t f:t *:t <:t + +#+TITLE: Plotting Data +#+AUTHOR: FOSSEE +#+DATE: 2010-09-14 Tue +#+EMAIL: info@fossee.in + +# \author[FOSSEE] {FOSSEE} + +# \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} +# \date{} + +* Tutorial Plan +** How to create lists +** Structure of lists +** Access list elements +** Append elements to lists +** Deleting elements from lists + + +* Summary + + l=[1,2,3,4] + l[-1] + l.append(5) + del(l[2]) + len(l) + diff -r e8c02b3c51ac -r 4054b1a6392d getting-started-with-lists/slides.tex --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/getting-started-with-lists/slides.tex Wed Oct 13 17:32:59 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} diff -r e8c02b3c51ac -r 4054b1a6392d plotting-data/plotting-data.rst --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plotting-data/plotting-data.rst Wed Oct 13 17:32:59 2010 +0530 @@ -0,0 +1,136 @@ +Plotting Experimental Data +============================= +Hello and welcome , this tutorial on Plotting Experimental data is +presented by the fossee team. + +{{{ Show the slide containing title }}} + + +{{{ Show the Outline Slide }}} + +Here we will discuss plotting Experimental data. + +1. We will see how we can represent a sequence of numbers in Python. + +2. We will also become fimiliar with elementwise squaring of such a +sequence. + +3. We will also see how we can use our graph to indicate Error. + +One needs to be fimiliar with the concepts of plotting +mathematical functions in Python. + +We will use data from a Simple Pendulum Experiment to illustrate our +points. + +{{{ Simple Pendulum data Slide }}} + + + + +As we know for a simple pendulum length,L is directly proportional to +the square of time,T. We shall be plotting L and T^2 values. + + +First we will have to initiate L and T values. We initiate them as sequence +of values. To tell ipython a sequence of values we write the sequence in +comma seperated values inside two square brackets. This is also called List +so to create two sequences + +L,t type in ipython shell. :: + + In []: L = [0.1, 0.2, 0.3, 0.4, 0.5,0.6, 0.7, 0.8, 0.9] + + In []: t= [0.69, 0.90, 1.19,1.30, 1.47, 1.58, 1.77, 1.83, 1.94] + + + +To obtain the square of sequence t we will use the function square +with argument t.This is saved into the variable tsquare.:: + + In []: tsquare=square(t) + + array([ 0.4761, 0.81 , 1.4161, 1.69 , 2.1609, 2.4964, 3.1329, + 3.3489, 3.7636]) + + +Now to plot L vs T^2 we will simply type :: + + In []: plot(L,t,.) + +'.' here represents to plot use small dots for the point. :: + + In []: clf() + +You can also specify 'o' for big dots.:: + + In []: plot(L,t,o) + + In []: clf() + + +{{{ Slide with Error data included }}} + + +Now we shall try and take into account error into our plots . The +Error values for L and T are on your screen.We shall again intialize +the sequence values in the same manner as we did for L and t :: + + In []: delta_L= [0.08,0.09,0.07,0.05,0.06,0.00,0.06,0.06,0.01] + + In []: delta_T= [0.04,0.08,0.11,0.05,0.03,0.03,0.01,0.07,0.01] + + + +Now to plot L vs T^2 with an error bar we use the function errorbar() + +The syntax of the command is as given on the screen. :: + + + In []: errorbar(L,tsquare,xerr=delta_L, yerr=delta_T, fmt='b.') + +This gives a plot with error bar for x and y axis. The dots are of blue color. The parameters xerr and yerr are error on x and y axis and fmt is the format of the plot. + + +similarly we can draw the same error bar with big red dots just change +the parameters to fmt to 'ro'. :: + + In []: clf() + In []: errorbar(L,tsquare,xerr=delta_L, yerr=delta_T, fmt='ro') + + + +thats it. you can explore other options to errorbar using the documentation +of errorbar.:: + + In []: errorbar? + + +{{{ Summary Slides }}} + +In this tutorial we have learnt : + +1. How to declare a sequence of number , specifically the kind of sequence we learned was a list. + +2. Plotting experimental data extending our knowledge from mathematical functions. + +3. The various options available for plotting dots instead of lines. + +4. Plotting experimental data such that we can also represent error. We did this using the errorbar() function. + + + {{{ Show the "sponsored by FOSSEE" slide }}} + + + +This tutorial was created as a part of FOSSEE project. + +Hope you have enjoyed and found it useful. + + Thankyou + + + +Author : Amit Sethi +Internal Reviewer : +Internal Reviewer 2 : diff -r e8c02b3c51ac -r 4054b1a6392d plotting-data/questions.rst --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plotting-data/questions.rst Wed Oct 13 17:32:59 2010 +0530 @@ -0,0 +1,56 @@ +Objective Questions +------------------- + +.. A mininum of 8 questions here (along with answers) + +1. How do you declare a sequence of numbers in python? + Give example . + + Comma seperated numbers inside two square brackets. + + seq=[1.5,3.2,8.7] + + +2. Square the following sequence? + + distance_values=[2.1,4.6,8.72,9.03]. + + square(distance_values) + + + +3. How do you plot points ? + + By passing an extra parameter '.'. + +4. What does the parameter 'o' do ? + + It plots large points. + +5. How do you plot error in Python? + + Using the function error bar. + +6. How do I get large red colour dots on a plot? + + By passing the paramter 'ro'. + +7. What are the parameters 'xerr' and 'yerr' in errorbar function for? + + xerr - List of error values of variable on x axis. + yerr - List of error values of variable on y ayis. + +8. How would you plot error bar with a line? + + The fmt parameter for a line will be '-'. + + + + +Larger Questions +---------------- + +.. A minimum of 2 questions here (along with answers) + +1. Question 1 +2. Question 2 diff -r e8c02b3c51ac -r 4054b1a6392d plotting-data/slides.org --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plotting-data/slides.org Wed Oct 13 17:32:59 2010 +0530 @@ -0,0 +1,84 @@ +#+LaTeX_CLASS: beamer +#+LaTeX_CLASS_OPTIONS: [presentation] +#+BEAMER_FRAME_LEVEL: 1 + +#+BEAMER_HEADER_EXTRA: \usetheme{Warsaw}\useoutertheme{infolines}\usecolortheme{default}\setbeamercovered{transparent} +#+COLUMNS: %45ITEM %10BEAMER_env(Env) %10BEAMER_envargs(Env Args) %4BEAMER_col(Col) %8BEAMER_extra(Extra) +#+PROPERTY: BEAMER_col_ALL 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 :ETC +#+OPTIONS: H:5 num:t toc:nil \n:nil @:t ::t |:t ^:t -:t f:t *:t <:t + +#+TITLE: Plotting Experimental Data +#+AUTHOR: FOSSEE +#+DATE: 2010-09-14 Tue +#+EMAIL: info@fossee.in + +# \author[FOSSEE] {FOSSEE} + +# \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} +# \date{} + +* Tutorial Plan +** Plotting Experiment Data and Error Bars +* Pre-requisites +** Plotting simple analytical Functions +* plot L vs. T^2 + +#+ORGTBL: L vs T^2 orgtbl-to-latex + + | L | T | + | 0.1 | 0.69 | + | 0.2 | 0.90 | + | 0.3 | 1.19 | + | 0.4 | 1.30 | + | 0.5 | 1.47 | + | 0.6 | 1.58 | + | 0.7 | 1.77 | + | 0.8 | 1.83 | + | 0.9 | 1.94 | + + + + +* Initializing L & T + : In []: L = [0.1, 0.2, 0.3, 0.4, 0.5, + : 0.6, 0.7, 0.8, 0.9] + : In []: t = [0.69, 0.90, 1.19, + : 1.30, 1.47, 1.58, + : 1.77, 1.83, 1.94] +* square() + : In []: tsquare=square(t) + + : array([ 0.4761, 0.81 , 1.4161, 1.69 , 2.1609, 2.4964, 3.1329, + : 3.3489, 3.7636]) + + +* Plotting + : In[]: plot(L,t,.) + + + : In[]: plot(L,t,o) + +* Adding an Error Column + + + | L | T | /Delta L | /Delta T | + | 0.1 | 0.69 | 0.08 | 0.04 | + | 0.2 | 0.90 | 0.09 | 0.08 | + | 0.3 | 1.19 | 0.07 | 0.11 | + | 0.4 | 1.30 | 0.05 | 0.05 | + | 0.5 | 1.47 | 0.06 | 0.03 | + | 0.6 | 1.58 | 0.00 | 0.03 | + | 0.7 | 1.77 | 0.06 | 0.01 | + | 0.8 | 1.83 | 0.06 | 0.07 | + | 0.9 | 1.94 | 0.01 | 0.01 | + + +* Plotting Error bar + + : In[]: delta_L= [0.08,0.09,0.07,0.05,0.16, + : 0.00,0.06,0.06,0.01] + : In[]: delta_T= [0.04,0.08,0.11,0.05,0.03, + : 0.03,0.01,0.07,0.01] + + + diff -r e8c02b3c51ac -r 4054b1a6392d plotui/questions.rst --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plotui/questions.rst Wed Oct 13 17:32:59 2010 +0530 @@ -0,0 +1,51 @@ +Objective Questions +------------------- + +.. A mininum of 8 questions here (along with answers) + +1. Create 100 equally spaced points between -pi/2 and pi/2? + + Answer: linspace(-pi/2,pi/2,100) + +2. How do you clear a figure in ipython? + + Answer: clf() + +3. How do find the length of a sequence? + + Answer: len(sequence_name) + +4. Create a plot of x and e^x where x is 100 equally spaced points between 0,pi. Hint: e^x -> exp(x) for ipython + + Answer: x=linspace(0,pi,100) + plot(x,exp(x)) + +5. List four formats in which you can save a plot in ipython? + + Answer: png,eps,pdf,ps + +6. List the kind of buttons available in plotui to study the plot better ? + + Zoom button to Zoom In to a region. + Pan button to move it around. + +7. What are the left and right arrow buttons for? + + Answer: These buttons take you to the states that the plot has been. Much like a browser left and right arrow button. + + + +8. What is the home button for in the Plot UI? + + Initial State of the plot. + + + + +Larger Questions +---------------- + +.. A minimum of 2 questions here (along with answers) + +1. Question 1 +2. Question 2 diff -r e8c02b3c51ac -r 4054b1a6392d plotui/quickref.tex --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plotui/quickref.tex Wed Oct 13 17:32:59 2010 +0530 @@ -0,0 +1,8 @@ +Creating a linear array:\\ +{\ex \lstinline| x = linspace(0, 2*pi, 50)|} + +Plotting two variables:\\ +{\ex \lstinline| plot(x, sin(x))|} + +Plotting two lists of equal length x, y:\\ +{\ex \lstinline| plot(x, y)|} diff -r e8c02b3c51ac -r 4054b1a6392d plotui/script.rst --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plotui/script.rst Wed Oct 13 17:32:59 2010 +0530 @@ -0,0 +1,182 @@ +Hello and welcome to the tutorial on creating simple plots using +Python.This tutorial is presented by the Fossee group. +{{{ Show the Title Slide }}} + +I hope you have IPython running on your computer. + +In this tutorial we will look at plot command and also how to study +the plot using the UI. + +{{{ Show Outline Slide }}} + +Lets start ipython on your shell, type :: + + $ipython -pylab + + +Pylab is a python library which provides plotting functionality.It +also provides many other important mathematical and scientific +functions. After running IPython -pylab in your shell if at the top of +the result of this command, you see something like :: + + + `ERROR: matplotlib could NOT be imported! Starting normal + IPython.` + + +{{{ Slide with Error written on it }}} + +Then you have to install matplotlib and run this command again. + +Now type in your ipython shell :: + + In[]: linpace? + + + +as the documentation says, it returns `num` evenly spaced samples, +calculated over the interval start and stop. To illustrate this, lets +do it form 1 to 100 and try 100 points. :: + + In[]: linspace(1,100,100) + +As you can see a sequence of numbers from 1 to 100 appears. + +Now lets try 200 points between 0 and 1 you do this by typing :: + + + In[]: linspace(0,1,200) + +0 for start , 1 for stop and 200 for no of points. In linspace +the start and stop points can be integers, decimals , or +constants. Let's try and get 100 points between -pi to pi. Type :: + + In[]: p = linspace(-pi,pi,100) + + +'pi' here is constant defined by pylab. Save this to the variable, p +. + +If you now :: + + In[]: len(p) + +You will get the no. of points. len function gives the no of elements +of a sequence. + + +Let's try and plot a cosine curve between -pi and pi using these +points. Simply type :: + + + In[]: plot(p,cos(points)) + +Here cos(points) gets the cosine value at every corresponding point to +p. + + +We can also save cos(points) to variable cosine and plot it using +plot.:: + + In[]: cosine=cos(points) + + In[]: plot(p,cosine) + + + +Now do :: + + In[]: clf() + +this will clear the plot. + +This is done because any other plot we try to make shall come on the +same drawing area. As we do not wish to clutter the area with +overlaid plots , we just clear it with clf(). Now lets try a sine +plot. :: + + + In []: plot(p,sin(p)) + + + + +The Window on which the plot appears can be used to study it better. + +First of all moving the mouse around gives us the point where mouse +points at. + +Also we have some buttons the right most among them is +for saving the file. + +Just click on it specifying the name of the file. We will save the plot +by the name sin_curve in pdf format. + + + +{{{ Action corelating with the words }}} + +As you can see I can specify format of file from the dropdown. + +Formats like png ,eps ,pdf, ps are available. + +Left to the save button is the slider button to specify the margins. + +{{{ Action corelating with the words }}} + +Left to this is zoom button to zoom into the plot. Just specify the +region to zoom into. +The button left to it can be used to move the axes of the plot. + +{{{ Action corelating with the words }}} + +The next two buttons with a left and right arrow icons change the state of the +plot and take it to the previous state it was in. It more or less acts like a +back and forward button in the browser. + +{{{ Action corelating with the words }}} + +The last one is 'home' referring to the initial plot. + +{{{ Action corelating with the words}}} + + + +{{{ Summary Slide }}} + + +In this tutorial we have looked at + +1. Starting Ipython with pylab + +2. Using linspace function to create `num` equaly spaced points in a region. + +3. Finding length of sequnces using len. + +4. Plotting mathematical functions using plot. + +4. Clearing drawing area using clf + +5. Using the UI of plot for studying it better . Using functionalities like save , zoom , moving the plots on x and y axis + +etc .. + + + +{{{ Show the "sponsored by FOSSEE" slide }}} + + + +This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India + + + + Hope you have enjoyed and found it useful. + + Thankyou + + + +Author : Amit Sethi +Internal Reviewer : +Internal Reviewer 2 : diff -r e8c02b3c51ac -r 4054b1a6392d plotui/slides.tex --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plotui/slides.tex Wed Oct 13 17:32:59 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} diff -r e8c02b3c51ac -r 4054b1a6392d statistics.rst --- a/statistics.rst Wed Oct 13 14:00:33 2010 +0530 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,165 +0,0 @@ -Hello friends and welcome to the tutorial on statistics using Python - -{{{ Show the slide containing title }}} - -{{{ Show the slide containing the outline slide }}} - -In this tutorial, we shall learn - * Doing simple statistical operations in Python - * Applying these to real world problems - -You will need Ipython with pylab running on your computer -to use this tutorial. - -Also you will need to know about loading data using loadtxt to be -able to follow the real world application. - -We will first start with the most necessary statistical -operation i.e finding mean. - -We have a list of ages of a random group of people :: - - age_list=[4,45,23,34,34,38,65,42,32,7] - -One way of getting the mean could be getting sum of -all the elements and dividing by length of the list.:: - - sum_age_list =sum(age_list) - -sum function gives us the sum of the elements.:: - - mean_using_sum=sum_age_list/len(age_list) - -This obviously gives the mean age but python has another -method for getting the mean. This is the mean function:: - - mean(age_list) - -Mean can be used in more ways in case of 2 dimensional lists. -Take a two dimensional list :: - - two_dimension=[[1,5,6,8],[1,3,4,5]] - -the mean function used in default manner will give the mean of the -flattened sequence. Flattened sequence means the two lists taken -as if it was a single list of elements :: - - mean(two_dimension) - flattened_seq=[1,5,6,8,1,3,4,5] - mean(flattened_seq) - -As you can see both the results are same. The other is mean -of each column.:: - - mean(two_dimension,0) - array([ 1. , 4. , 5. , 6.5]) - -or along the two rows seperately.:: - - mean(two_dimension,1) - array([ 5. , 3.25]) - -We can see more option of mean using :: - - mean? - -Similarly we can calculate median and stanard deviation of a list -using the functions median and std:: - - median(age_list) - std(age_list) - - - -Now lets apply this to a real world example :: - -We will a data file that is at the a path -``/home/fossee/sslc2.txt``.It contains record of students and their -performance in one of the State Secondary Board Examination. It has -180, 000 lines of record. We are going to read it and process this -data. We can see the content of file by double clicking on it. It -might take some time to open since it is quite a large file. Please -don't edit the data. This file has a particular structure. - -We can do :: - - cat /home/fossee/sslc2.txt - -to check the contents of the file. - -Each line in the file is a set of 11 fields separated -by semi-colons Consider a sample line from this file. -A;015163;JOSEPH RAJ S;083;042;47;00;72;244;;; - -The following are the fields in any given line. -* Region Code which is 'A' -* Roll Number 015163 -* Name JOSEPH RAJ S -* Marks of 5 subjects: ** English 083 ** Hindi 042 ** Maths 47 ** -Science AA (Absent) ** Social 72 -* Total marks 244 -* - -Now lets try and find the mean of English marks of all students. - -For this we do. :: - - L=loadtxt('/home/fossee/sslc2.txt',usecols=(3,),delimiter=';') - L - mean(L) - -loadtxt function loads data from an external file.Delimiter specifies -the kind of character are the fields of data seperated by. -usecols specifies the columns to be used so (3,). The 'comma' is added -because usecols is a sequence. - -To get the median marks. :: - - median(L) - -Standard deviation. :: - - std(L) - - -Now lets try and and get the mean for all the subjects :: - - L=loadtxt('sslc2.txt',usecols=(3,4,5,6,7),delimiter=';') - mean(L,0) - array([ 73.55452504, 53.79828941, 62.83342759, 50.69806158, 63.17056881]) - -As we can see from the result mean(L,0). The resultant sequence -is the mean marks of all students that gave the exam for the five subjects. - -and :: - - mean(L,1) - - -is the average accumalative marks of individual students. Clearly, mean(L,0) -was a row wise calcultaion while mean(L,1) was a column wise calculation. - - -{{{ Show summary slide }}} - -This brings us to the end of the tutorial. -we have learnt - - * How to do the standard statistical operations sum , mean - median and standard deviation in Python. - * Combine text loading and the statistical operation to solve - real world problems. - -{{{ Show the "sponsored by FOSSEE" slide }}} - - -This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India - -Hope you have enjoyed and found it useful. -Thankyou - -.. Author : Amit Sethi - Internal Reviewer 1 : - Internal Reviewer 2 : - External Reviewer : - diff -r e8c02b3c51ac -r 4054b1a6392d statistics/questions.rst --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/statistics/questions.rst Wed Oct 13 17:32:59 2010 +0530 @@ -0,0 +1,54 @@ +Objective Questions +------------------- + +.. A mininum of 8 questions here (along with answers) + +1. What is the function for calculating sum of a list? + + sum + +2. Calcutate the mean of the given list? + + student_marks=[74,78,56,87,91,82] + + mean(student_marks) + + +3. Given a two dimensional list,:: + two_dimensional_list=[[3,5,8,2,1],[4,3,6,2,1]] + + how do we calculate the mean of each row? + + + mean(two_dimensinal_list,1) + +4. What is the function for calculating standard deviation of a list? + + std + +5. Calcutate the median of the given list? + + student_marks=[74,78,56,87,91,82] + + median(age_list) + +6. How do you calculate median along the columns of two dimensional array? + + median(two_dimensional_list,0) + + +7. What is the name of the function to load text from an external file? + + loadtxt + +8. I have a file with 6 columns but I wish to load only text in column 2,3,4,5. How do I specify that? + + Using the parameter usecols=(2,3,4,5) + +Larger Questions +---------------- + +.. A minimum of 2 questions here (along with answers) + +1. Question 1 +2. Question 2 diff -r e8c02b3c51ac -r 4054b1a6392d statistics/quickref.tex --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/statistics/quickref.tex Wed Oct 13 17:32:59 2010 +0530 @@ -0,0 +1,8 @@ +Creating a linear array:\\ +{\ex \lstinline| x = linspace(0, 2*pi, 50)|} + +Plotting two variables:\\ +{\ex \lstinline| plot(x, sin(x))|} + +Plotting two lists of equal length x, y:\\ +{\ex \lstinline| plot(x, y)|} diff -r e8c02b3c51ac -r 4054b1a6392d statistics/script.rst --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/statistics/script.rst Wed Oct 13 17:32:59 2010 +0530 @@ -0,0 +1,174 @@ +Hello friends and welcome to the tutorial on statistics using Python + +{{{ Show the slide containing title }}} + +{{{ Show the slide containing the outline slide }}} + +In this tutorial, we shall learn + * Doing simple statistical operations in Python + * Applying these to real world problems + +You will need Ipython with pylab running on your computer +to use this tutorial. + +Also you will need to know about loading data using loadtxt to be +able to follow the real world application. + +We will first start with the most necessary statistical +operation i.e finding mean. + +We have a list of ages of a random group of people :: + + age_list=[4,45,23,34,34,38,65,42,32,7] + +One way of getting the mean could be getting sum of +all the elements and dividing by length of the list.:: + + sum_age_list =sum(age_list) + +sum function gives us the sum of the elements.:: + + mean_using_sum=float(sum_age_list)/len(age_list) + +This obviously gives the mean age but python has another +method for getting the mean. This is the mean function:: + + mean(age_list) + +Mean can be used in more ways in case of 2 dimensional lists. +Take a two dimensional list :: + + two_dimension=[[1,5,6,8],[1,3,4,5]] + +the mean function used in default manner will give the mean of the +flattened sequence. Flattened sequence means the two lists taken +as if it was a single list of elements :: + + mean(two_dimension) + flattened_seq=[1,5,6,8,1,3,4,5] + mean(flattened_seq) + +As you can see both the results are same. The other way is mean +of each column.:: + + mean(two_dimension,0) + array([ 1. , 4. , 5. , 6.5]) + +we pass an extra argument 0 in that case. + +In case of getting mean along the rows the argument is 1:: + + mean(two_dimension,1) + array([ 5. , 3.25]) + +We can see more option of mean using :: + + mean? + +Similarly we can calculate median and stanard deviation of a list +using the functions median and std:: + + median(age_list) + std(age_list) + +Median and std can also be calculated for two dimensional arrays along columns and rows just like mean. + + For example :: + + median(two_dimension,0) + std(two_dimension,1) + +This gives us the median along the colums and standard devition along the rows. + +Now lets apply this to a real world example + +We will a data file that is at the a path +``/home/fossee/sslc2.txt``.It contains record of students and their +performance in one of the State Secondary Board Examination. It has +180, 000 lines of record. We are going to read it and process this +data. We can see the content of file by double clicking on it. It +might take some time to open since it is quite a large file. Please +don't edit the data. This file has a particular structure. + +We can do :: + + cat /home/fossee/sslc2.txt + +to check the contents of the file. + +Each line in the file is a set of 11 fields separated +by semi-colons Consider a sample line from this file. +A;015163;JOSEPH RAJ S;083;042;47;00;72;244;;; + +The following are the fields in any given line. +* Region Code which is 'A' +* Roll Number 015163 +* Name JOSEPH RAJ S +* Marks of 5 subjects: ** English 083 ** Hindi 042 ** Maths 47 ** +Science AA (Absent) ** Social 72 +* Total marks 244 +* + +Now lets try and find the mean of English marks of all students. + +For this we do. :: + + L=loadtxt('/home/fossee/sslc2.txt',usecols=(3,),delimiter=';') + L + mean(L) + +loadtxt function loads data from an external file.Delimiter specifies +the kind of character are the fields of data seperated by. +usecols specifies the columns to be used so (3,). The 'comma' is added +because usecols is a sequence. + +To get the median marks. :: + + median(L) + +Standard deviation. :: + + std(L) + + +Now lets try and and get the mean for all the subjects :: + + L=loadtxt('/home/fossee/sslc2.txt',usecols=(3,4,5,6,7),delimiter=';') + mean(L,0) + array([ 73.55452504, 53.79828941, 62.83342759, 50.69806158, 63.17056881]) + +As we can see from the result mean(L,0). The resultant sequence +is the mean marks of all students that gave the exam for the five subjects. + +and :: + + mean(L,1) + + +is the average accumalative marks of individual students. Clearly, mean(L,0) +was a row wise calcultaion while mean(L,1) was a column wise calculation. + + +{{{ Show summary slide }}} + +This brings us to the end of the tutorial. +we have learnt + + * How to do the standard statistical operations sum , mean + median and standard deviation in Python. + * Combine text loading and the statistical operation to solve + real world problems. + +{{{ Show the "sponsored by FOSSEE" slide }}} + + +This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India + +Hope you have enjoyed and found it useful. +Thankyou + +.. Author : Amit Sethi + Internal Reviewer 1 : + Internal Reviewer 2 : + External Reviewer : + diff -r e8c02b3c51ac -r 4054b1a6392d statistics/slides.org --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/statistics/slides.org Wed Oct 13 17:32:59 2010 +0530 @@ -0,0 +1,33 @@ +#+LaTeX_CLASS: beamer +#+LaTeX_CLASS_OPTIONS: [presentation] +#+BEAMER_FRAME_LEVEL: 1 + +#+BEAMER_HEADER_EXTRA: \usetheme{Warsaw}\useoutertheme{infolines}\usecolortheme{default}\setbeamercovered{transparent} +#+COLUMNS: %45ITEM %10BEAMER_env(Env) %10BEAMER_envargs(Env Args) %4BEAMER_col(Col) %8BEAMER_extra(Extra) +#+PROPERTY: BEAMER_col_ALL 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 :ETC +#+OPTIONS: H:5 num:t toc:nil \n:nil @:t ::t |:t ^:t -:t f:t *:t <:t + +#+TITLE: Statistics +#+AUTHOR: FOSSEE +#+DATE: 2010-09-14 Tue +#+EMAIL: info@fossee.in + +# \author[FOSSEE] {FOSSEE} + +# \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} +# \date{} + +* Tutorial Plan +** Doing simple statistical operations in Python +** Using loadtxt to solve statistics problem + +* Summary +** seq=[1,5,6,8,1,3,4,5] +** sum(seq) +** mean(seq) +** median(seq) +** std(seq) + +* Summary + +** loadtxt diff -r e8c02b3c51ac -r 4054b1a6392d statistics/slides.tex --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/statistics/slides.tex Wed Oct 13 17:32:59 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} diff -r e8c02b3c51ac -r 4054b1a6392d symbolics.rst --- a/symbolics.rst Wed Oct 13 14:00:33 2010 +0530 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,343 +0,0 @@ -Symbolics with Sage -------------------- - -This tutorial on using Sage for symbolic calculation is brought to you -by Fossee group. - -.. #[Madhu: Sounds more or less like an ad!] - -{{{ Part of Notebook with title }}} - -.. #[Madhu: Please make your instructions, instructional. While - recording if I have to read this, think what you are actually - meaning it will take a lot of time] - -We would be using simple mathematical functions on the sage notebook -for this tutorial. - -.. #[Madhu: What is this line doing here. I don't see much use of it] - -During the course of the tutorial we will learn - -{{{ Part of Notebook with outline }}} - -To define symbolic expressions in sage. Use built-in costants and -function. Integration, differentiation using sage. Defining -matrices. Defining Symbolic functions. Simplifying and solving -symbolic expressions and functions. - -.. #[Nishanth]: The formatting is all messed up - First fix the formatting and compile the rst - The I shall review -.. #[Madhu: Please make the above items full english sentences, not - the slides like points. The person recording should be able to - read your script as is. It can read something like "we will learn - how to define symbolic expressions in Sage, using built-in ..."] - -Using sage we can perform mathematical operations on symbols. - -.. #[Madhu: Same mistake with period symbols! Please get the - punctuation right. Also you may have to rephrase the above - sentence as "We can use Sage to perform sybmolic mathematical - operations" or such] - -On the sage notebook type:: - - sin(y) - -It raises a name error saying that y is not defined. But in sage we -can declare y as a symbol using var function. - -.. #[Madhu: But is not required] -:: - var('y') - -Now if you type:: - - sin(y) - - sage simply returns the expression . - -.. #[Madhu: Why is this line indented? Also full stop. When will you - learn? Yes we can correct you. But corrections are for you to - learn. If you don't learn from your mistakes, I don't know what - to say] - -thus now sage treats sin(y) as a symbolic expression . You can use -this to do a lot of symbolic maths using sage's built-in constants and -expressions . - -.. #[Madhu: "Thus now"? It sounds like Dus and Nou, i.e 10 and 9 in - Hindi! Full stop again. "a lot" doesn't mean anything until you - quantify it or give examples.] - -Try out - -.. #[Madhu: "So let us try" sounds better] - :: - - var('x,alpha,y,beta') x^2/alpha^2+y^2/beta^2 - -Similarly , we can define many algebraic and trigonometric expressions -using sage . - -.. #[Madhu: comma again. Show some more examples?] - - -Sage also provides a few built-in constants which are commonly used in -mathematics . - -example : pi,e,oo , Function n gives the numerical values of all these - constants. - -.. #[Madhu: This doesn't sound like scripts. How will I read this - while recording. Also if I were recording I would have read your - third constant as Oh-Oh i.e. double O. It took me at least 30 - seconds to figure out it is infinity] - -For instance:: - - n(e) - - 2.71828182845905 - -gives numerical value of e. - -If you look into the documentation of n by doing - -.. #[Madhu: "documentation of the function "n"?] - -:: - n( - -You will see what all arguments it can take etc .. It will be very -helpful if you look at the documentation of all functions introduced - -.. #[Madhu: What does etc .. mean in a script?] - -Also we can define the no of digits we wish to use in the numerical -value . For this we have to pass an argument digits. Type - -.. #[Madhu: "no of digits"? Also "We wish to obtain" than "we wish to - use"?] -:: - - n(pi, digits = 10) - -Apart from the constants sage also has a lot of builtin functions like -sin,cos,sinh,cosh,log,factorial,gamma,exp,arcsin,arccos,arctan etc ... -lets try some out on the sage notebook. - -.. #[Madhu: Here "a lot" makes sense] -:: - - sin(pi/2) - - arctan(oo) - - log(e,e) - - -Given that we have defined variables like x,y etc .. , We can define -an arbitrary function with desired name in the following way.:: - - var('x') function( {{{ Just to show the documentation - extend this line }}} function('f',x) - -.. #[Madhu: What will the person recording show in the documentation - without a script for it? Please don't assume recorder can cook up - things while recording. It is impractical] - -Here f is the name of the function and x is the independent variable . -Now we can define f(x) to be :: - - f(x) = x/2 + sin(x) - -Evaluating this function f for the value x=pi returns pi/2.:: - - f(pi) - -We can also define functions that are not continuous but defined -piecewise. We will be using a function which is a parabola between 0 -to 1 and a constant from 1 to 2 . type the following as given on the -screen - -.. #[Madhu: Instead of "We will be using ..." how about "Let us define - a function ..."] -:: - - - var('x') h(x)=x^2 g(x)=1 f=Piecewise( {{{ Just to show the - documentation extend this line }}} - f=Piecewise([[(0,1),h(x)],[(1,2),g(x)]],x) f - -Checking f at 0.4, 1.4 and 3 :: f(0.4) f(1.4) f(3) - -.. #[Madhu: Again this doesn't sound like a script] - -for f(3) it raises a value not defined in domain error . - - -Apart from operations on expressions and functions one can also use -them for series . - -.. #[Madhu: I am not able to understand this line. "Use them as -.. series". Use what as series?] - -We first define a function f(n) in the way discussed above.:: - - var('n') function('f', n) - -.. #[Madhu: Shouldn't this be on 2 separate lines?] - -To sum the function for a range of discrete values of n, we use the -sage function sum. - -For a convergent series , f(n)=1/n^2 we can say :: - - var('n') function('f', n) - - f(n) = 1/n^2 - - sum(f(n), n, 1, oo) - -For the famous Madhava series :: var('n') function('f', n) - -.. #[Madhu: What is this? your double colon says it must be code block - but where is the indentation and other things. How will the - recorder know about it?] - - f(n) = (-1)^(n-1)*1/(2*n - 1) - -This series converges to pi/4. It was used by ancient Indians to -interpret pi. - -.. #[Madhu: I am losing the context. Please add something to bring - this thing to the context] - -For a divergent series, sum would raise a an error 'Sum is -divergent' :: - - var('n') - function('f', n) - f(n) = 1/n sum(f(n), n,1, oo) - - - - -We can perform simple calculus operation using sage - -.. #[Madhu: When you switch to irrelevant topics make sure you use - some connectors in English like "Moving on let us see how to - perform simple calculus operations using Sage" or something like - that] -For example lets try an expression first :: - - diff(x**2+sin(x),x) 2x+cos(x) - -The diff function differentiates an expression or a function . Its -first argument is expression or function and second argument is the -independent variable . - -.. #[Madhu: Full stop, Full stop, Full stop] - -We have already tried an expression now lets try a function :: - - f=exp(x^2)+arcsin(x) diff(f(x),x) - -To get a higher order differentiation we need to add an extra argument -for order :: - - diff( diff(f(x),x,3) - -.. #[Madhu: Please try to be more explicit saying third argument] - -in this case it is 3. - - -Just like differentiation of expression you can also integrate them :: - - x = var('x') s = integral(1/(1 + (tan(x))**2),x) s - -.. #[Madhu: Two separate lines.] - -To find the factors of an expression use the "factor" function - -.. #[Madhu: See the diff] - -:: - factor( y = (x^100 - x^70)*(cos(x)^2 + cos(x)^2*tan(x)^2) f = - factor(y) - -One can also simplify complicated expression using sage :: - f.simplify_full() - -This simplifies the expression fully . You can also do simplification -of just the algebraic part and the trigonometric part :: - - f.simplify_exp() f.simplify_trig() - -.. #[Madhu: Separate lines?] - -One can also find roots of an equation by using find_root function:: - - phi = var('phi') find_root(cos(phi)==sin(phi),0,pi/2) - -.. #[Madhu: Separate lines?] - -Lets substitute this solution into the equation and see we were -correct :: - - var('phi') f(phi)=cos(phi)-sin(phi) - root=find_root(f(phi)==0,0,pi/2) f.substitute(phi=root) - -.. #[Madhu: Separate lines?] - -as we can see the solution is almost equal to zero . - -.. #[Madhu: So what?] - -We can also define symbolic matrices :: - - - - var('a,b,c,d') A=matrix([[a,1,0],[0,b,0],[0,c,d]]) A - -.. #[Madhu: Why don't you break the lines?] - -Now lets do some of the matrix operations on this matrix - -.. #[Madhu: Why don't you break the lines? Also how do you connect - this up? Use some transformation keywords in English] -:: - A.det() A.inverse() - -.. #[Madhu: Why don't you break the lines?] - -You can do :: - - A. - -To see what all operations are available - -.. #[Madhu: Sounds very abrupt] - -{{{ Part of the notebook with summary }}} - -So in this tutorial we learnt how to - - -We learnt about defining symbolic expression and functions . -And some built-in constants and functions . -Getting value of built-in constants using n function. -Using Tab to see the documentation. -Also we learnt how to sum a series using sum function. -diff() and integrate() for calculus operations . -Finding roots , factors and simplifying expression using find_root(), -factor() , simplify_full, simplify_exp , simplify_trig . -Substituting values in expression using substitute function. -And finally creating symbolic matrices and performing operation on them . - -.. #[Madhu: See what Nishanth is doing. He has written this as - points. So easy to read out while recording. You may want to - reorganize like that] diff -r e8c02b3c51ac -r 4054b1a6392d symbolics/questions.rst --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/symbolics/questions.rst Wed Oct 13 17:32:59 2010 +0530 @@ -0,0 +1,53 @@ +Objective Questions +------------------- + +.. A mininum of 8 questions here (along with answers) + +1. How do you define a name 'y' as a symbol? + + + Answer: var('y') + +2. List out some constants pre-defined in sage? + + Answer: pi, e ,euler_gamma + +3. List the functions for differentiation and integration in sage? + + Answer: diff and integral + +4. Get the value of pi upto precision 5 digits using sage? + + Answer: n(pi,5) + +5. Find third order differential of function. + + f(x)=sin(x^2)+exp(x^3) + + Answer: diff(f(x),x,3) + +6. What is the function to find factors of an expression? + + Answer: factor + +7. What is syntax for simplifying a function f? + + Answer f.simplify_full() + +8. Find the solution for x between pi/2 to pi for the given equation? + + sin(x)==cos(x^3)+exp(x^4) + find_root(sin(x)==cos(x^3)+exp(x^4),pi/2,pi) + +9. Create a simple two dimensional matrix with two symbolic variables? + + var('a,b') + A=matrix([[a,1],[2,b]]) + +Larger Questions +---------------- + +.. A minimum of 2 questions here (along with answers) + + +2. Question 2 diff -r e8c02b3c51ac -r 4054b1a6392d symbolics/quickref.tex --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/symbolics/quickref.tex Wed Oct 13 17:32:59 2010 +0530 @@ -0,0 +1,8 @@ +Creating a linear array:\\ +{\ex \lstinline| x = linspace(0, 2*pi, 50)|} + +Plotting two variables:\\ +{\ex \lstinline| plot(x, sin(x))|} + +Plotting two lists of equal length x, y:\\ +{\ex \lstinline| plot(x, y)|} diff -r e8c02b3c51ac -r 4054b1a6392d symbolics/script.rst --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/symbolics/script.rst Wed Oct 13 17:32:59 2010 +0530 @@ -0,0 +1,343 @@ +Symbolics with Sage +------------------- + +Hello friends and welcome to the tutorial on symbolics with sage. + + +.. #[Madhu: Sounds more or less like an ad!] + +{{{ Part of Notebook with title }}} + +.. #[Madhu: Please make your instructions, instructional. While + recording if I have to read this, think what you are actually + meaning it will take a lot of time] + +We would be using simple mathematical functions on the sage notebook +for this tutorial. + +.. #[Madhu: What is this line doing here. I don't see much use of it] + +During the course of the tutorial we will learn + +{{{ Part of Notebook with outline }}} + +To define symbolic expressions in sage. Use built-in costants and +function. Integration, differentiation using sage. Defining +matrices. Defining Symbolic functions. Simplifying and solving +symbolic expressions and functions. + +.. #[Nishanth]: The formatting is all messed up + First fix the formatting and compile the rst + The I shall review +.. #[Madhu: Please make the above items full english sentences, not + the slides like points. The person recording should be able to + read your script as is. It can read something like "we will learn + how to define symbolic expressions in Sage, using built-in ..."] + +Using sage we can perform mathematical operations on symbols. + +.. #[Madhu: Same mistake with period symbols! Please get the + punctuation right. Also you may have to rephrase the above + sentence as "We can use Sage to perform sybmolic mathematical + operations" or such] + +On the sage notebook type:: + + sin(y) + +It raises a name error saying that y is not defined. But in sage we +can declare y as a symbol using var function. + +.. #[Madhu: But is not required] +:: + var('y') + +Now if you type:: + + sin(y) + + sage simply returns the expression . + +.. #[Madhu: Why is this line indented? Also full stop. When will you + learn? Yes we can correct you. But corrections are for you to + learn. If you don't learn from your mistakes, I don't know what + to say] + +thus now sage treats sin(y) as a symbolic expression . You can use +this to do a lot of symbolic maths using sage's built-in constants and +expressions . + +.. #[Madhu: "Thus now"? It sounds like Dus and Nou, i.e 10 and 9 in + Hindi! Full stop again. "a lot" doesn't mean anything until you + quantify it or give examples.] + +Try out + +.. #[Madhu: "So let us try" sounds better] + :: + + var('x,alpha,y,beta') x^2/alpha^2+y^2/beta^2 + +Similarly , we can define many algebraic and trigonometric expressions +using sage . + +.. #[Madhu: comma again. Show some more examples?] + + +Sage also provides a few built-in constants which are commonly used in +mathematics . + +example : pi,e,oo , Function n gives the numerical values of all these + constants. + +.. #[Madhu: This doesn't sound like scripts. How will I read this + while recording. Also if I were recording I would have read your + third constant as Oh-Oh i.e. double O. It took me at least 30 + seconds to figure out it is infinity] + +For instance:: + + n(e) + + 2.71828182845905 + +gives numerical value of e. + +If you look into the documentation of n by doing + +.. #[Madhu: "documentation of the function "n"?] + +:: + n( + +You will see what all arguments it can take etc .. It will be very +helpful if you look at the documentation of all functions introduced + +.. #[Madhu: What does etc .. mean in a script?] + +Also we can define the no of digits we wish to use in the numerical +value . For this we have to pass an argument digits. Type + +.. #[Madhu: "no of digits"? Also "We wish to obtain" than "we wish to + use"?] +:: + + n(pi, digits = 10) + +Apart from the constants sage also has a lot of builtin functions like +sin,cos,sinh,cosh,log,factorial,gamma,exp,arcsin,arccos,arctan etc ... +lets try some out on the sage notebook. + +.. #[Madhu: Here "a lot" makes sense] +:: + + sin(pi/2) + + arctan(oo) + + log(e,e) + + +Given that we have defined variables like x,y etc .. , We can define +an arbitrary function with desired name in the following way.:: + + var('x') function( {{{ Just to show the documentation + extend this line }}} function('f',x) + +.. #[Madhu: What will the person recording show in the documentation + without a script for it? Please don't assume recorder can cook up + things while recording. It is impractical] + +Here f is the name of the function and x is the independent variable . +Now we can define f(x) to be :: + + f(x) = x/2 + sin(x) + +Evaluating this function f for the value x=pi returns pi/2.:: + + f(pi) + +We can also define functions that are not continuous but defined +piecewise. We will be using a function which is a parabola between 0 +to 1 and a constant from 1 to 2 . type the following as given on the +screen + +.. #[Madhu: Instead of "We will be using ..." how about "Let us define + a function ..."] +:: + + + var('x') h(x)=x^2 g(x)=1 f=Piecewise( {{{ Just to show the + documentation extend this line }}} + f=Piecewise([[(0,1),h(x)],[(1,2),g(x)]],x) f + +Checking f at 0.4, 1.4 and 3 :: f(0.4) f(1.4) f(3) + +.. #[Madhu: Again this doesn't sound like a script] + +for f(3) it raises a value not defined in domain error . + + +Apart from operations on expressions and functions one can also use +them for series . + +.. #[Madhu: I am not able to understand this line. "Use them as +.. series". Use what as series?] + +We first define a function f(n) in the way discussed above.:: + + var('n') function('f', n) + +.. #[Madhu: Shouldn't this be on 2 separate lines?] + +To sum the function for a range of discrete values of n, we use the +sage function sum. + +For a convergent series , f(n)=1/n^2 we can say :: + + var('n') function('f', n) + + f(n) = 1/n^2 + + sum(f(n), n, 1, oo) + +For the famous Madhava series :: var('n') function('f', n) + +.. #[Madhu: What is this? your double colon says it must be code block + but where is the indentation and other things. How will the + recorder know about it?] + + f(n) = (-1)^(n-1)*1/(2*n - 1) + +This series converges to pi/4. It was used by ancient Indians to +interpret pi. + +.. #[Madhu: I am losing the context. Please add something to bring + this thing to the context] + +For a divergent series, sum would raise a an error 'Sum is +divergent' :: + + var('n') + function('f', n) + f(n) = 1/n sum(f(n), n,1, oo) + + + + +We can perform simple calculus operation using sage + +.. #[Madhu: When you switch to irrelevant topics make sure you use + some connectors in English like "Moving on let us see how to + perform simple calculus operations using Sage" or something like + that] +For example lets try an expression first :: + + diff(x**2+sin(x),x) 2x+cos(x) + +The diff function differentiates an expression or a function . Its +first argument is expression or function and second argument is the +independent variable . + +.. #[Madhu: Full stop, Full stop, Full stop] + +We have already tried an expression now lets try a function :: + + f=exp(x^2)+arcsin(x) diff(f(x),x) + +To get a higher order differentiation we need to add an extra argument +for order :: + + diff( diff(f(x),x,3) + +.. #[Madhu: Please try to be more explicit saying third argument] + +in this case it is 3. + + +Just like differentiation of expression you can also integrate them :: + + x = var('x') s = integral(1/(1 + (tan(x))**2),x) s + +.. #[Madhu: Two separate lines.] + +To find the factors of an expression use the "factor" function + +.. #[Madhu: See the diff] + +:: + factor( y = (x^100 - x^70)*(cos(x)^2 + cos(x)^2*tan(x)^2) f = + factor(y) + +One can also simplify complicated expression using sage :: + f.simplify_full() + +This simplifies the expression fully . You can also do simplification +of just the algebraic part and the trigonometric part :: + + f.simplify_exp() f.simplify_trig() + +.. #[Madhu: Separate lines?] + +One can also find roots of an equation by using find_root function:: + + phi = var('phi') find_root(cos(phi)==sin(phi),0,pi/2) + +.. #[Madhu: Separate lines?] + +Lets substitute this solution into the equation and see we were +correct :: + + var('phi') f(phi)=cos(phi)-sin(phi) + root=find_root(f(phi)==0,0,pi/2) f.substitute(phi=root) + +.. #[Madhu: Separate lines?] + +as we can see the solution is almost equal to zero . + +.. #[Madhu: So what?] + +We can also define symbolic matrices :: + + + + var('a,b,c,d') A=matrix([[a,1,0],[0,b,0],[0,c,d]]) A + +.. #[Madhu: Why don't you break the lines?] + +Now lets do some of the matrix operations on this matrix + +.. #[Madhu: Why don't you break the lines? Also how do you connect + this up? Use some transformation keywords in English] +:: + A.det() A.inverse() + +.. #[Madhu: Why don't you break the lines?] + +You can do :: + + A. + +To see what all operations are available + +.. #[Madhu: Sounds very abrupt] + +{{{ Part of the notebook with summary }}} + +So in this tutorial we learnt how to + + +We learnt about defining symbolic expression and functions . +And some built-in constants and functions . +Getting value of built-in constants using n function. +Using Tab to see the documentation. +Also we learnt how to sum a series using sum function. +diff() and integrate() for calculus operations . +Finding roots , factors and simplifying expression using find_root(), +factor() , simplify_full, simplify_exp , simplify_trig . +Substituting values in expression using substitute function. +And finally creating symbolic matrices and performing operation on them . + +.. #[Madhu: See what Nishanth is doing. He has written this as + points. So easy to read out while recording. You may want to + reorganize like that] diff -r e8c02b3c51ac -r 4054b1a6392d symbolics/slides.tex --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/symbolics/slides.tex Wed Oct 13 17:32:59 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}