diff options
author | Jovina | 2011-06-20 16:10:49 +0530 |
---|---|---|
committer | Jovina | 2011-06-20 16:10:49 +0530 |
commit | 1c0eee692f6c5f2b2f13de3b76baa257713b0d59 (patch) | |
tree | 0d5bcff3d41aac9ddbff2700ebe617fee77f5af5 | |
parent | fe92a5886e01d3a4a05d7a7b9d0d06602693c496 (diff) | |
download | st-scripts-1c0eee692f6c5f2b2f13de3b76baa257713b0d59.tar.gz st-scripts-1c0eee692f6c5f2b2f13de3b76baa257713b0d59.tar.bz2 st-scripts-1c0eee692f6c5f2b2f13de3b76baa257713b0d59.zip |
Major changes to script & slides of 'basic_datatypes_and_operators.
-rw-r--r-- | basic_datatypes_and_operators/script.rst | 871 | ||||
-rw-r--r-- | basic_datatypes_and_operators/slides.org | 120 | ||||
-rw-r--r-- | basic_datatypes_and_operators/slides.tex | 233 |
3 files changed, 791 insertions, 433 deletions
diff --git a/basic_datatypes_and_operators/script.rst b/basic_datatypes_and_operators/script.rst index 22d4f3a..8554cf8 100644 --- a/basic_datatypes_and_operators/script.rst +++ b/basic_datatypes_and_operators/script.rst @@ -16,285 +16,516 @@ External Reviewer : Checklist OK? : <put date stamp here, if OK> [2010-10-05] -Hello friends and welcome to the tutorial on Basic Data types and operators +------- +Script +------- + +.. L1 + +{{{ Show the first slide containing title, name of the production +team along with the logo of MHRD }}} + +.. R1 + +Hello friends and welcome to the tutorial on 'Basic Data types and operators' in Python. -{{{ Show the slide containing title }}} +.. L2 -{{{ Show the slide containing the outline slide }}} +{{{ Show the slide containing the objectives }}} -In this tutorial, we shall look at +.. R2 -* Datatypes in Python - * Numbers - * Boolean - * Sequence -* Operators in Python - * Arithmetic Operators - * Boolean Operators +At the end of this tutorial, you will be able to, -* Python Sequence Data types - * list - * string - * tuple +1. Know the Datatypes in Python + - Numbers + - Boolean + - Sequence +#. Learn about the Operators in Python + - Arithmetic Operators + - Boolean Operators +#. Know the Python Sequence Data types + - list + - string + - tuple + +.. R3 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 the different datatypes }}} +.. L3 + +.. L4 + +{{{ Show the slide 'numbers' }}} + +.. R4 These are: - * int - * float - * complex + - int + - float + - complex + +.. R5 -Lets first talk about int. :: +Let us first invoke our ipython interpreter - a = 13 - a +.. L5 +:: + ipython -Now, we have our first int variable a. +.. R6 + +Lets first talk about int. + +.. L6 +:: + a = 13 + a -If we now see :: +.. R7 + +Now, we have our first int variable a. +If we now see + +.. L7 +:: - type(a) - <type 'int'> + type(a) + <type 'int'> -This means that a is a type of int. There are lot of functions associated +.. R8 + +This means that 'a' is a type of int. There are lot of functions associated with the int datatype, to manipulate it in different ways. These can be -explored by doing, :: +explored by doing, + +.. L8 +:: - a.<Tab> + a.<Tab> + +.. R9 *int* datatype can hold integers of any size lets see this by an example. + +.. L9 :: - b = 99999999999999999999 - b + b = 99999999999999999999 + b + +.. R10 -As you can see even when we put a value of 9 repeated 20 times python did +As you can see, even when we put a value of 9 repeated 20 times, python did not complain. This is because python's int data-type can hold integers of any size. Let us now look at the float data-type. +Decimal numbers in python are represented by the float data-type -Decimal numbers in python are represented by the float data-type :: +.. L10 +:: + + p = 3.141592 + p - p = 3.141592 - p +.. R11 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 approximation. 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 :: +The last data type in the list is complex number + +.. L11 +:: + + c = 3.2+4.6j - c = 3.2+4.6j +.. R12 -as simple as that so essentialy its just a combination of two floats the +it's 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. Let us look at these :: +have a lot of functions specific to them. Let us look at these + +.. L12 +:: + + c.<Tab> + +.. R13 + +Lets try some of them - c.<Tab> +.. L13 +:: -Lets try some of them :: + c.real + c.imag - c.real - c.imag +.. R14 c.real gives the real part of the number and c.imag the imaginary. -We can get the absolute value using the function :: +We can get the absolute value using the function + +.. L14 +:: - abs(c) + abs(c) +.. R15 -Following is are exercises that you must do. +Pause the video here, try out the following exercise and resume the video. -%% %% Find the absolute value of 3+4j -:: +.. L15 + +.. L16 + +{{{ Show slide with exercise 1 }}} + +.. R16 - abs(3+4j) + Find the absolute value of 3+4j +<pause> +Switch to your terminal for solution -%% %% What is the datatype of number 999999999999999999? Is it -not int? +.. L17 + +{{{continue from paused state}}} +{{{ Switch to the terminal }}} :: - Long - Big integers are internally stored in python - as Long datatype. + abs(3+4j) + +.. R17 + +Thus we get the absolute value of the expression. + +Let us do 1 more exercise of a similar type. +Pause the video here, try out the following exercise and resume the video. + +.. L18 + +{{{ Show slide with exercise 2 }}} + +.. R18 -Please, pause the video here. Do the exercises and then continue. + What is the datatype of number 999999999999999999? Is it not int? +.. L19 -{{ Slide for showing Boolean datatypes }} +{{{ Switch to slide solution 2 }}} + +.. R19 + +The solution is on your screen. +The data type of this number is long though it is an integer. +Big integers are internally stored in python as Long datatype. Python also has Boolean as a built-in type. +To Try it out, just type + +.. L20 -Try it out just type :: +{{{ Switch to terminal }}} +:: - t = True + t = True + +.. R20 note that T in true is capitalized. -You can apply different Boolean operations on t now for example :: +.. R21 + +You can apply different Boolean operations on t now for example + +.. L21 +:: - f = not t - f - f or t - f and t + f = not t + f + f or t + f and t +.. R22 The results are self explanatory. 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.:: +Lets write some piece of code to check this out. + +.. L22 +:: - a=False - b=True - c=True + a=False + b=True + c=True +.. R23 To check how precedence changes with parenthesis, we will try two -expressions and their evaluation. +expressions and their evaluation.The first one -one :: +.. L23 +:: - (a and b) or c + (a and b) or c + +.. R24 This expression gives the value True +where as the expression -where as the expression :: +.. L24 +:: - a and (b or c) + a and (b or c) -gives the value False. +.. R25 +gives the value False. Let's now look at some operators available in Python to manipulate these data types. -Python uses '+' for addition :: +.. L25 + +.. R26 - 23 + 74 +Python uses '+' sign for addition + +.. L26 +:: -'-' for subtraction :: + 23 + 74 - 23 - 56 +.. R27 + +'-' sign for subtraction + +.. L27 +:: -'*' for multiplication :: + 23 - 56 + +.. R28 + +'*' (star) sign for multiplication + +.. L28 +:: - 45*76 + 45*76 -'/' for division :: +.. R29 + +'/'(back slash) for division + +.. L29 +:: - 384/16 - 8/3 - 8.0/3 + 384/16 + 8/3 + 8.0/3 -When we did 8/3 the first case results in am integer +.. R30 + +Note that, when we did 8/3 the first case results in an integer output as both the operands are integer however when 8.0/3 is used the answer is float as one of the operands is float. +.. L30 + +.. R31 + +Let us move ahead with the operators. +'%' (percentage) sign for modulo operation -'%' for modulo operation :: +.. L31 +:: 87 % 6 -and two stars for a exponent. :: +.. R32 + +and two stars for a exponent. + +.. L32 +:: 7**8 +.. R33 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`. :: +is stored in the expression, one can do that by putting the operator before +`equal to`. + +.. L33 +:: + + a=73 + a*=34 - a=73 - a*=34 +.. R34 -is same as :: +The above expression is same as + +.. L34 +:: - a=a*34 + a=a*34 + +.. R35 -and :: +and + +.. L35 +:: a/=23 -is same as :: +.. R36 - a=a/23 +is same as -Following is are exercises that you must do. +.. L36 +:: -%% %% Using python find sqaure root of 3? + a=a/23 -%% %% Is 3**1/2 and 3**0.5 same +.. R37 -Please, pause the video here. Do the exercises and then continue. +Pause the video here, try out the following exercise and resume the video. -:: +.. L37 - 3**0.5 +.. L38 + +{{{ Show slide with exercise 3 }}} + +.. R38 + + Using python find sqaure root of 3. +.. L39 + +{{{ Switch to slide solution 3 }}} + +.. R39 + +The solution is on your screen. +3**0.5 gives the square root of 3. + +.. L40 + +{{{ Show slide with exercise 4 }}} + +.. R40 + + Now, Is 3**1/2 and 3**0.5 same? +<Pause> + +.. R41 + +Switch to your terminal for solution +Let us try both these operations. + +.. L41 + +{{{continue from paused state}}} +{{{ Switch to the terminal }}} :: - No,One gives an int answer and the other float + 3**0.5 + 3**1/2 + +.. R42 + +As you can see,the first operation gives an integer,whereas the second one gives a float. +Hence,though both mean the same,they give different outputs. -Lets now discuss sequence data types in Python. Sequence data types +Let us now discuss sequence data types in Python. Sequence data types are those in which elements are kept in a sequential order and all the elements are accessed using index numbers. +.. L42 + +.. L43 + {{{ slide introducing sequence datatype }}} -The sequence datatypes in Python are :: +.. R43 - * list - * string - * tuple +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 :: +.. R44 + +We create our first list by typing + +.. L44 + +{{{ Switch to terminal }}} +:: - num_list = [1, 2, 3, 4] - num_list + num_list = [1, 2, 3, 4] + num_list +.. R45 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 :: +We can have a list something like - var_list = [1, 1.2, [1,2]] - var_list +.. L45 +:: + + var_list = [1, 1.2, [1,2]] + var_list + +.. R46 Lets look at another sequence data type, strings -type :: +.. L46 +:: - greeting_string="hello" + greeting_string="hello" +.. R47 greeting_string is now a string variable with the value "hello" -{{{ All the different types of strings shown }}} +Python strings can actually be defined in three different ways -Python strings can actually be defined in three different ways :: +.. L47 +:: - k='Single quote' - l="Let's see how to include a single quote" - m='''"Let's see how to include both"''' + k='Single quote' + l="Let's see how to include a single quote" + m='''"Let's see how to include both"''' + +.. R48 As you can see, single quotes are used as delimiters usually. @@ -304,250 +535,408 @@ 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.:: +To create a tuple we use normal brackets '(' unlike '[' for lists. + +.. L48 +:: - num_tuple = (1, 2, 3, 4, 5, 6, 7, 8) + num_tuple = (1, 2, 3, 4, 5, 6, 7, 8) + +.. R49 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 :: +They can be accessed using index numbers - num_list[2] - num_list[-1] - greeting_string[1] - greeting_string[3] - greeting_string[-2] - num_tuple[2] - num_tuple[-3] +.. L49 +:: + + num_list[2] + num_list[-1] + greeting_string[1] + greeting_string[3] + greeting_string[-2] + num_tuple[2] + num_tuple[-3] +.. R50 -Indexing starts from 0 from left to right and from -1 when accessing lists +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 +.. L50 +:: -Addition gives a new sequence containing both sequences :: + num_list+var_list + a_string="another string" + greeting_string+a_string + t2=(3,4,6,7) + num_tuple+t2 - num_list+var_list - a_string="another string" - greeting_string+a_string - t2=(3,4,6,7) - num_tuple+t2 +.. R51 -len function gives the length :: +len function gives the length - len(num_list) - len(greeting_string) - len(num_tuple) +.. L51 +:: + + len(num_list) + len(greeting_string) + len(num_tuple) + +.. R52 -Prints the length the variable. +We can check the containership of an element using the 'in' keyword -We can check the containership of an element using the 'in' keyword :: +.. L52 +:: + + 3 in num_list + 'H' in greeting_string + 2 in num_tuple - 3 in num_list - 'H' in greeting_string - 2 in num_tuple +.. R53 We see that it gives True and False accordingly. -Find maximum using max function and minimum using min:: +Find maximum using max function and minimum using min + +.. L53 +:: - max(num_tuple) - min(greeting_string) + max(num_tuple) + min(greeting_string) -Get a sorted list :: +.. R54 - sorted(num_list) - +Get a sorted list + +.. L54 +:: + sorted(num_list) + +.. R55 + As a consequence of their order, we can access a group of elements in a sequence, together. This is called slicing and striding. First lets discuss Slicing, -Given a list :: +Given a list + +.. L55 +:: + + j=[1,2,3,4,5,6] - j=[1,2,3,4,5,6] +.. R56 Lets say we want elements starting from 2 and ending in 5. -For this we can do :: +For this we can do + +.. L56 +:: - j[1:4] + j[1:4] -The syntax for slicing is, sequence variable name square bracket first +.. R57 + +The syntax for slicing is, sequence variable name, square bracket, first element index, colon, second element index. The last element however is not -included in the resultant list:: +included in the resultant list + +.. L57 +:: + j[:4] - j[:4] +.. R58 If first element is left blank default is from beginning and if last element is left blank it means till the end. +.. L58 :: - j[1:] - - j[:] + j[1:] + j[:] +.. R59 This effectively is the whole list. Striding is similar to slicing except that the step size here is not one. -Lets see by example :: +Let us see an example + +.. L59 +:: + + new_num_list=[1,2,3,4,5,6,7,8,9,10] + new_num_list[1:8:2] + [2, 4, 6, 8] - new_num_list=[1,2,3,4,5,6,7,8,9,10] - new_num_list[1:8:2] - [2, 4, 6, 8] +.. R60 -The colon two added in the end signifies all the alternate elements. This +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.:: +tuples. Lets see this by example. - new_num_list[1]=9 - greeting_string[1]='k' - -{{{ slide to show the error }}} +.. L60 +:: + new_num_list[1]=9 + greeting_string[1]='k' +.. R61 As you can see while the first command executes with out a problem there is an error on the second one. -Now lets try :: +Now lets try - new_tuple[1]=5 +.. L61 +:: + + new_tuple[1]=5 + +.. R62 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: +one data type to other -We can convert all the number data types to one another :: +.. L62 +:: + + i=34 + d=float(i) + d - i=34 - d=float(i) - d +.. R63 Python has built in functions int, float and complex to convert one number type data structure to another. +.. L63 :: - dec=2.34 - dec_con=int(dec) - dec_con + dec=2.34 + dec_con=int(dec) + dec_con +.. R64 As you can see the decimal part of the number is simply stripped to get the -integer.:: +integer. + +.. L64 +:: + + com=2.3+4.2j + float(com) + com - com=2.3+4.2j - float(com) - com +.. R65 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 :: +Similarly we can convert list to tuple and tuple to list + +.. L65 +:: - lst=[3,4,5,6] - tup=tuple(lst) - tupl=(3,23,4,56) - lst=list(tuple) + lst=[3,4,5,6] + tup=tuple(lst) + lst + tupl=(3,23,4,56) + lst=list(tupl) + tupl + +.. R66 However converting a string to a list and a list to a string is an -interesting problem. Let's say we have a string :: +interesting problem. Let's say we have a string + +.. L66 +:: - In: somestring="Is there a way to split on these spaces." - In: somestring.split() + somestring="Is there a way to split on these spaces." + somestring.split() +.. R67 This produces a list with the string split at whitespace. Similarly we can split on some other character. +.. L67 :: - In: otherstring="Tim,Amy,Stewy,Boss" + otherstring="Tim,Amy,Stewy,Boss" -How do we split on comma , simply pass it as argument :: +.. R68 - In: otherstring.split(',') +How do we split on comma , simply pass it as argument -join function does the opposite. Joins a list to make a string.:: +.. L68 +:: + + otherstring.split(',') - ','.join['List','joined','on','commas'] +.. R69 -Thus we get a list joined on commas. Similarly we can do spaces.:: +join function does the opposite. Joins a list to make a string. + +.. L69 +:: - ' '.join['Now','on','spaces'] + l1=['List','joined','on','commas'] + ','.join(l1) + +.. R70 + +Thus we get a list joined on commas. Similarly we can do spaces. + +.. L70 +:: + + l2=['Now','on','spaces'] + ' '.join(l2) + +.. R71 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 . +Pause the video here, try out the following exercise and resume the video. + +.. L71 + +.. L72 + +{{{ Show slide with exercise 5 }}} + +.. R72 -Following is an (are) exercise(s) that you must do. + Check if 3 is an element of the list [1,7,5,3,4]. In case + it is change it to 21. +.. R73 +Switch to the terminal for solution -%% %% Check if 3 is an element of the list [1,7,5,3,4]. In case -it is change it to 21. +.. L73 + +{{{continue from paused state}}} +{{{ Switch to the terminal }}} :: - l=[1,7,5,3,4] - 3 in l - l[3]=21 - l + + l=[1,7,5,3,4] + 3 in l + l[3]=21 + l + +.. R74 + +Let us solve one more exercise. +Pause the video here, do the exercise and resume the video. + +.. L74 + +.. L75 + +{{{ Show slide with exercise 6 }}} -%% %% Convert the string "Elizabeth is queen of england" to +.. R75 + + Convert the string "Elizabeth is queen of england" to "Elizabeth is queen" + +.. R76 + +Switch to the terminal for solution + +.. L76 + +{{{continue from paused state}}} +{{{ Switch to the terminal }}} :: - s="Elizabeth is queen of england" - stemp=s.split() - ' '.join(stemp[:3]) - -Please, pause the video here. Do the exercise(s) and then continue. + s="Elizabeth is queen of england" + stemp=s.split() + ' '.join(stemp[:3]) +.. R77 -This brings us to the end of the tutorial. In this tutorial we have -discussed +As you can see, we have easily removed the unwanted words. -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. +.. L77 -{{{ Show the "sponsored by FOSSEE" slide }}} +.. L78 -This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India +{{{ Show summary slide }}} -Hope you have enjoyed and found it useful. +.. R78 -Thank You. +This brings us to the end of the tutorial. In this tutorial, we have +learnt to, +1. Understand the number Datatypes -- integer,float and complex. +#. Know the boolean datatype and operators -- +, *, /, **, % . +#. use the sequence data types -- List,String and Tuple. +#. Slice sequences by using the row and column numbers. +#. Split and join a list using ``split()`` and ``join()`` function respectively. +#. Convert to string to tuple and vice-versa. + +.. L79 + +{{{Show self assessment questions slide}}} + +.. R79 + +Here are some self assessment questions for you to solve + +1. What is the major diffence between tuples and lists? + +2. Split this string on whitespaces +:: -.. - Local Variables: - mode: rst - indent-tabs-mode: nil - sentence-end-double-space: nil - fill-column: 75 - End: + string="Split this string on whitespaces" + +.. L80 + +{{{solution of self assessment questions on slide}}} + +.. R80 + +And the answers, + +1. The major diffence between tuples and lists is that Tuples are immutable while lists are not. + +2. To split the string on whitespace, we use the function `` split`` without any argument +:: + + string.split() + +.. L81 + +{{{ Show the thankyou slide }}} + +.. R81 + +Hope you have enjoyed this tutorial and found it useful. +Thank You. diff --git a/basic_datatypes_and_operators/slides.org b/basic_datatypes_and_operators/slides.org index b78549f..9041f30 100644 --- a/basic_datatypes_and_operators/slides.org +++ b/basic_datatypes_and_operators/slides.org @@ -18,7 +18,7 @@ #+LaTeX_HEADER: commentstyle=\color{red}\itshape, stringstyle=\color{darkgreen}, #+LaTeX_HEADER: showstringspaces=false, keywordstyle=\color{blue}\bfseries} -#+TITLE: Plotting Data +#+TITLE: #+AUTHOR: FOSSEE #+DATE: 2010-09-14 Tue #+EMAIL: info@fossee.in @@ -30,7 +30,25 @@ #+OPTIONS: TeX:t LaTeX:nil skip:nil d:nil todo:nil pri:nil tags:not-in-toc #+STARTUP: align fold nodlcheck hidestars oddeven lognotestate -* Outline +* +#+begin_latex +\begin{center} +\vspace{12pt} +\textcolor{blue}{\huge Basic Datatypes and Operators} +\end{center} +\vspace{18pt} +\begin{center} +\vspace{10pt} +\includegraphics[scale=0.95]{../images/fossee-logo.png}\\ +\vspace{5pt} +\scriptsize Developed by FOSSEE Team, IIT-Bombay. \\ +\scriptsize Funded by National Mission on Education through ICT\\ +\scriptsize MHRD,Govt. of India\\ +\includegraphics[scale=0.30]{../images/iitb-logo.png}\\ +\end{center} +#+end_latex +* Objectives + At the end of the tutorial,you will be able to, ** Datatypes in Python *** Numbers *** Boolean @@ -47,13 +65,9 @@ - int - float - complex -* Question 1 +* Exercise 1 - Find the absolute value of 3+4j -* Solution 1 - #+begin_src python - abs(3+4j) - #+end_src python -* Question 2 +* Exercise 2 - What is the datatype of number 999999999999999999? Is it not int? @@ -62,24 +76,15 @@ not int? - Large integers numbers are internally stored in python as Long datatype. - -* Boolean - #+begin_src python - In []: t=True - In []: f=False - #+end_src - -* Question 3 +* Exercise 3 - Using python find sqaure root of 3? * Solution 3 - 3**0.5 -* Question 4 - - Is 3**1/2 and 3**0.5 same -* Solution 4 - - No,One gives an int answer and the other float +* Exercise 4 + - Is 3**1/2 and 3**0.5 same * Sequence Data types ** Properties @@ -90,64 +95,44 @@ not int? - String - Tuple -* All are Strings - #+begin_src python - k = 'Single quote' - l = "Double quote contain's single quote" - m = '''"Contain's both"''' +* Exercise 5 + - Check if 3 is an element of the list [1,7,5,3,4]. In case it is +change it to 21. - #+end_src -* Immutabilty Error - #+begin_src python - In []: greeting_string[1]='k' - ------------------------------------------------------- - TypeError Traceback (most recent call last) +* Exercise 6 + - Convert the string ~"Elizabeth is queen of england"~ to ~"Elizabeth is +queen"~ - /home/fossee/<ipython console> in <module>() +* Summary + In this tutorial, we have learnt to, - TypeError: 'str' object does not support item assignment - #+end_src + - Understand the number Datatypes -- integer,float and complex. + - Know the boolean datatype and operators -- +, *, /, **, % . + - use the sequence data types -- List,String and Tuple. + - Slice sequences by using the row and column numbers. + - Split and join a list using ``split()`` and ``join()`` function respectively. + - Convert to string to tuple and vice-versa. +* Evaluation +1. What is the major diffence between tuples and lists? -* Question 5 - Check if 3 is an element of the list [1,7,5,3,4]. In case it is -change it to 21. +2. Split this string on whitespaces -* Solution 5 - #+begin_src python - l=[1,7,5,3,4] - 3 in l - l[3]=21 - l - #+end_src -* Question 6 - Convert the string ~"Elizabeth is queen of england"~ to ~"Elizabeth is -queen"~ + - string="Split this string on whitespaces" +* Solutions +1. Tuples are immutable while lists are not. -* Solution 6 - #+begin_src python - s = "Elizabeth is queen of england" - stemp = s.split() - ' '.join(stemp[:3]) - #+end_src -* Summary - - Number Datatypes -- integer,float and complex - - Boolean and datatype and operators - - Sequence data types -- List, String and Tuple - - Accesing sequence - - Slicing sequences - - Finding length, sorting and reversing operations on sequences - - Immutability -* Thank you! +2. string.split() +* #+begin_latex \begin{block}{} \begin{center} - This spoken tutorial has been produced by the - \textcolor{blue}{FOSSEE} team, which is funded by the + \textcolor{blue}{\Large THANK YOU!} \end{center} + \end{block} +\begin{block}{} \begin{center} - \textcolor{blue}{National Mission on Education through \\ - Information \& Communication Technology \\ - MHRD, Govt. of India}. + For more Information, visit our website\\ + \url{http://fossee.in/} \end{center} \end{block} #+end_latex @@ -156,3 +141,4 @@ queen"~ + diff --git a/basic_datatypes_and_operators/slides.tex b/basic_datatypes_and_operators/slides.tex index 1f149b1..c8a5f8f 100644 --- a/basic_datatypes_and_operators/slides.tex +++ b/basic_datatypes_and_operators/slides.tex @@ -1,4 +1,4 @@ -% Created 2010-11-09 Tue 15:26 +% Created 2011-06-20 Mon 16:04 \documentclass[presentation]{beamer} \usepackage[latin1]{inputenc} \usepackage[T1]{fontenc} @@ -8,7 +8,6 @@ \usepackage{float} \usepackage{wrapfig} \usepackage{soul} -\usepackage{t1enc} \usepackage{textcomp} \usepackage{marvosym} \usepackage{wasysym} @@ -24,14 +23,14 @@ commentstyle=\color{red}\itshape, stringstyle=\color{darkgreen}, showstringspaces=false, keywordstyle=\color{blue}\bfseries} \providecommand{\alert}[1]{\textbf{#1}} -\title{Plotting Data } +\title{} \author{FOSSEE} \date{2010-09-14 Tue} \usetheme{Warsaw}\usecolortheme{default}\useoutertheme{infolines}\setbeamercovered{transparent} \begin{document} -\maketitle + @@ -42,48 +41,68 @@ showstringspaces=false, keywordstyle=\color{blue}\bfseries} \begin{frame} -\frametitle{Outline} -\label{sec-1} + +\begin{center} +\vspace{12pt} +\textcolor{blue}{\huge Basic Datatypes and Operators} +\end{center} +\vspace{18pt} +\begin{center} +\vspace{10pt} +\includegraphics[scale=0.95]{../images/fossee-logo.png}\\ +\vspace{5pt} +\scriptsize Developed by FOSSEE Team, IIT-Bombay. \\ +\scriptsize Funded by National Mission on Education through ICT\\ +\scriptsize MHRD,Govt. of India\\ +\includegraphics[scale=0.30]{../images/iitb-logo.png}\\ +\end{center} +\end{frame} +\begin{frame} +\frametitle{Objectives} +\label{sec-2} + + At the end of the tutorial,you will be able to, \begin{itemize} \item Datatypes in Python -\label{sec-1_1}% +\label{sec-2_1}% \begin{itemize} \item Numbers\\ -\label{sec-1_1_1}% +\label{sec-2_1_1}% \item Boolean\\ -\label{sec-1_1_2}% +\label{sec-2_1_2}% \item Sequence\\ -\label{sec-1_1_3}% +\label{sec-2_1_3}% \end{itemize} % ends low level \item Operators in Python -\label{sec-1_2}% +\label{sec-2_2}% \begin{itemize} \item Arithmetic Operators\\ -\label{sec-1_2_1}% +\label{sec-2_2_1}% \item Boolean Operators\\ -\label{sec-1_2_2}% +\label{sec-2_2_2}% \end{itemize} % ends low level \item Python Sequence Datatypes -\label{sec-1_3}% +\label{sec-2_3}% \begin{itemize} \item list\\ -\label{sec-1_3_1}% +\label{sec-2_3_1}% \item string\\ -\label{sec-1_3_2}% +\label{sec-2_3_2}% \item tuple\\ -\label{sec-1_3_3}% +\label{sec-2_3_3}% \end{itemize} % ends low level \end{itemize} % ends low level \end{frame} \begin{frame} \frametitle{Numbers} -\label{sec-2} +\label{sec-3} + \begin{itemize} \item int @@ -92,53 +111,39 @@ showstringspaces=false, keywordstyle=\color{blue}\bfseries} \end{itemize} \end{frame} \begin{frame} -\frametitle{Question 1} -\label{sec-3} +\frametitle{Exercise 1} +\label{sec-4} + \begin{itemize} \item Find the absolute value of 3+4j \end{itemize} \end{frame} -\begin{frame}[fragile] -\frametitle{Solution 1} -\label{sec-4} - -\begin{verbatim} -abs(3+4j) -\end{verbatim} -\end{frame} \begin{frame} -\frametitle{Question 2} +\frametitle{Exercise 2} \label{sec-5} + \begin{itemize} \item What is the datatype of number 999999999999999999? Is it \end{itemize} - not int? \end{frame} \begin{frame} \frametitle{Solution 2} \label{sec-6} + \begin{itemize} \item Long \item Large integers numbers are internally stored in python as Long datatype. \end{itemize} \end{frame} -\begin{frame}[fragile] -\frametitle{Boolean} +\begin{frame} +\frametitle{Exercise 3} \label{sec-7} -\begin{verbatim} -In []: t=True -In []: f=False -\end{verbatim} -\end{frame} -\begin{frame} -\frametitle{Question 3} -\label{sec-8} \begin{itemize} \item Using python find sqaure root of 3? @@ -146,7 +151,8 @@ In []: f=False \end{frame} \begin{frame} \frametitle{Solution 3} -\label{sec-9} +\label{sec-8} + \begin{itemize} @@ -154,133 +160,110 @@ In []: f=False \end{itemize} \end{frame} \begin{frame} -\frametitle{Question 4} -\label{sec-10} +\frametitle{Exercise 4} +\label{sec-9} -\begin{itemize} -\item Is 3**1/2 and 3**0.5 same -\end{itemize} -\end{frame} -\begin{frame} -\frametitle{Solution 4} -\label{sec-11} \begin{itemize} -\item No,One gives an int answer and the other float +\item Is 3**1/2 and 3**0.5 same? \end{itemize} \end{frame} \begin{frame} \frametitle{Sequence Data types} -\label{sec-12} +\label{sec-10} \begin{itemize} -\item Properties -\label{sec-12_1}% +\item Properties\\ +\label{sec-10_1}% \begin{itemize} \item Data in Sequence \item Accessed using Index \end{itemize} - -\item Type -\label{sec-12_2}% +\item Type\\ +\label{sec-10_2}% \begin{itemize} \item list \item String \item Tuple \end{itemize} - \end{itemize} % ends low level \end{frame} -\begin{frame}[fragile] -\frametitle{All are Strings} -\label{sec-13} - -\begin{verbatim} -k = 'Single quote' -l = "Double quote contain's single quote" -m = '''"Contain's both"''' -\end{verbatim} -\end{frame} -\begin{frame}[fragile] -\frametitle{Immutabilty Error} -\label{sec-14} - -\begin{verbatim} -In []: greeting_string[1]='k' -------------------------------------------------------- -TypeError Traceback (most recent call last) - -/home/fossee/<ipython console> in <module>() - -TypeError: 'str' object does not support item assignment -\end{verbatim} -\end{frame} \begin{frame} -\frametitle{Question 5} -\label{sec-15} +\frametitle{Exercise 5} +\label{sec-11} + - Check if 3 is an element of the list [1,7,5,3,4]. In case it is +\begin{itemize} +\item Check if 3 is an element of the list [1,7,5,3,4].\\ In case it is, +\end{itemize} change it to 21. \end{frame} -\begin{frame}[fragile] -\frametitle{Solution 5} -\label{sec-16} - -\begin{verbatim} -l=[1,7,5,3,4] -3 in l -l[3]=21 -l -\end{verbatim} -\end{frame} \begin{frame} -\frametitle{Question 6} -\label{sec-17} +\frametitle{Exercise 6} +\label{sec-12} + - Convert the string \~{}''Elizabeth is queen of england''\~{} to \~{}''Elizabeth is +\begin{itemize} +\item Convert the string \~{}``Elizabeth is queen of england''\~{} to \~{}``Elizabeth is +\end{itemize} queen''\~{} \end{frame} -\begin{frame}[fragile] -\frametitle{Solution 6} -\label{sec-18} - -\begin{verbatim} -s = "Elizabeth is queen of england" -stemp = s.split() -' '.join(stemp[:3]) -\end{verbatim} -\end{frame} \begin{frame} \frametitle{Summary} -\label{sec-19} +\label{sec-13} + + In this tutorial, we have learnt to, + \begin{itemize} -\item Number Datatypes -- integer,float and complex -\item Boolean and datatype and operators -\item Sequence data types -- List, String and Tuple -\item Accesing sequence -\item Slicing sequences -\item Finding length, sorting and reversing operations on sequences -\item Immutability +\item Understand the number Datatypes -- integer,float and complex. +\item Know the boolean datatype and operators -- +, *, /, **, \% . +\item use the sequence data types -- List,String and Tuple. +\item Slice sequences by using the row and column numbers. +\item Split and join a list using ``split()'' and ``join()'' function respectively. +\item Convert to string to tuple and vice-versa. \end{itemize} \end{frame} \begin{frame} -\frametitle{Thank you!} -\label{sec-20} +\frametitle{Evaluation} +\label{sec-14} + + +\begin{enumerate} +\item What is the major diffence between tuples and lists? +\vspace{12pt} +\item Split this string on whitespaces +\begin{itemize} +\item string=``Split this string on whitespaces'' +\end{itemize} +\end{enumerate} +\end{frame} +\begin{frame} +\frametitle{Solutions} +\label{sec-15} + + +\begin{enumerate} +\item Tuples are immutable while lists are not. +\vspace{12pt} +\item string.split() +\end{enumerate} +\end{frame} +\begin{frame} \begin{block}{} \begin{center} - This spoken tutorial has been produced by the - \textcolor{blue}{FOSSEE} team, which is funded by the + \textcolor{blue}{\Large THANK YOU!} \end{center} + \end{block} +\begin{block}{} \begin{center} - \textcolor{blue}{National Mission on Education through \\ - Information \& Communication Technology \\ - MHRD, Govt. of India}. + For more Information, visit our website\\ + \url{http://fossee.in/} \end{center} \end{block} \end{frame} -\end{document} +\end{document}
\ No newline at end of file |