# HG changeset patch # User amit # Date 1286970038 -19800 # Node ID 223044cf254fa4d5842b827ae3aa6ca7721aae1d # Parent e78c284d644ba382eab00a7bc848b56ab32b44dc Adding new format st-scripts with questions etc for basic-data-type and getting started with lists diff -r e78c284d644b -r 223044cf254f 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:10:38 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 e78c284d644b -r 223044cf254f 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:10:38 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 e78c284d644b -r 223044cf254f 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:10:38 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 e78c284d644b -r 223044cf254f 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:10:38 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 e78c284d644b -r 223044cf254f basicdatatype.rst --- a/basicdatatype.rst Thu Oct 07 10:57:15 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 e78c284d644b -r 223044cf254f 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:10:38 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 e78c284d644b -r 223044cf254f 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:10:38 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 e78c284d644b -r 223044cf254f 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:10:38 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 e78c284d644b -r 223044cf254f 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:10:38 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}