diff options
author | Jovina | 2011-05-18 12:39:57 +0530 |
---|---|---|
committer | Jovina | 2011-05-18 12:39:57 +0530 |
commit | d8138f5b5770f3bd3a5ad18b20c73b41e37820b6 (patch) | |
tree | d4be04b705baf8f119a1c3d4c6f6eb44dcfd728e /getting_started_with_strings/script.rst | |
parent | 5ed9ae992d68dcf4cc63b95a690cb3ffe354a54b (diff) | |
download | st-scripts-d8138f5b5770f3bd3a5ad18b20c73b41e37820b6.tar.gz st-scripts-d8138f5b5770f3bd3a5ad18b20c73b41e37820b6.tar.bz2 st-scripts-d8138f5b5770f3bd3a5ad18b20c73b41e37820b6.zip |
Modifications to the slides and script of
1.Getting started with strings
2.Getting started with files.
Diffstat (limited to 'getting_started_with_strings/script.rst')
-rw-r--r-- | getting_started_with_strings/script.rst | 293 |
1 files changed, 218 insertions, 75 deletions
diff --git a/getting_started_with_strings/script.rst b/getting_started_with_strings/script.rst index 754fede..d383dcb 100644 --- a/getting_started_with_strings/script.rst +++ b/getting_started_with_strings/script.rst @@ -24,144 +24,242 @@ Script ------ -{{{ Show the slide containing the title }}} +.. L1 -Hello friends. Welcome to this spoken tutorial on Getting started with -strings. +{{{ Show the first slide containing title, name of the production +team along with the logo of MHRD }}} -{{{ Show the slide containing the outline }}} +.. R1 -In this tutorial, we will look at what we really mean by strings, how -Python supports the use of strings and some of the operations that can -be performed on strings. +Hello friends and Welcome to the tutorial on "Getting started with +strings". + +.. L2 + +{{{ Show slide with objectives }}} + +.. R2 + +At the end of this tutorial, you will be able to, + + 1. Define strings in differnt ways. + #. Concatenate strings. + #. Print a string repeatedly. + #. Access individual elements of the string. + #. Learn immutability of strings. + +.. L3 {{{ Shift to terminal and start ipython }}} +:: -To begin with let us start ipython, by typing:: + ipython - ipython +.. R3 -on the terminal +Open the terminal and invoke the ipython interpreter by typing ipython + +.. R4 So, what are strings? In Python anything within either single quotes or double quotes or triple single quotes or triple double quotes are strings. -{{{ Type in ipython the following and read them as you type }}}:: +.. L4 + +{{{ Type in ipython the following and read them as you type }}} +:: + + 'This is a string' + "This is a string too" + '''This is a string as well''' + """This is also a string""" + 'p' + "" - 'This is a string' - "This is a string too' - '''This is a string as well''' - """This is also a string""" - 'p' - "" +.. R5 Note that it really doesn't matter how many characters are present in the string. The last example is a null string or an empty string. Having more than one control character to define strings is handy when -one of the control characters itself is part of the string. For -example:: +one of the control characters itself is part of the string. For example + +.. L5 + +:: - "Python's string manipulation functions are very useful" + "Python's string manipulation functions are very useful" + +.. R6 By having multiple control characters, we avoid the need for escaping characters -- in this case the apostrophe. -The triple quoted strings let us define multi-line strings without +Let us now move on to the triple quoted strings. Let us define multi-line strings without using any escaping. Everything within the triple quotes is a single -string no matter how many lines it extends:: +string no matter how many lines it extends + +.. L6 +:: + + """Having more than one control character to define + strings come as very handy when one of the control + characters itself is part of the string.""" - """Having more than one control character to define - strings come as very handy when one of the control - characters itself is part of the string.""" +.. R7 -We can assign this string to any variable:: +We can assign this string to any variable - a = 'Hello, World!' +.. L7 +:: + + a = 'Hello, World!' + +.. R8 Now 'a' is a string variable. String is a collection of characters. In -addition string is an immutable collection. So all the operations that -are applicable to any other immutable collection in Python works on -string as well. So we can add two strings:: +addition string is an immutable collection which means that the string cannot be modified +after it is created.So all the operations that are applicable to any other immutable +collection in Python, works on strings as well. Hence we can add two strings + +.. L8 +:: - a = 'Hello' - b = 'World' - c = a + ', ' + b + '!' + a = 'Hello' + b = 'World' + c = a + ', ' + b + '!' + print c + +.. R9 We can add string variables as well as the strings themselves all in the same statement. The addition operation performs the concatenation of two strings. -Similarly we can multiply a string with an integer:: +.. L9 + +.. R10 + +Similarly we can multiply a string with an integer + +.. L10 +:: + + a = 'Hello' + a * 5 - a = 'Hello' - a * 5 +.. R11 -gives another string in which the original string 'Hello' is repeated +It gives another string in which the original string 'Hello' is repeated 5 times. -Following is an exercise that you must do. +.. L11 + +.. L12 + +{{{ Show slide with Question 1 }}} + +.. R12 -%% %% Obtain the string ``%% -------------------- %%`` (20 hyphens) - without typing out all the twenty hyphens. +Pause the video here, try out the following exercise and resume the video. -Please, pause the video here. Do the exercise and then continue. + Obtain the string ``%% -------------------- %%`` (20 hyphens) + without typing out all the twenty hyphens. + +.. L13 + +{{{ Switch to terminal }}} :: - s = "%% " + "-"*20 + " %%" + s = "%% " + "-"*20 + " %%" + print s + +.. R13 Let's now look at accessing individual elements of strings. Since, -strings are collections we can access individual items in the string -using the subscripts:: +strings are collections, we can access individual items in the string +using the subscripts - a[0] +.. L14 +:: -gives us the first character in the string. The indexing starts from 0 -for the first character and goes up to n-1 for the last character. We -can access the strings from the end using negative indices:: + a[0] - a[-1] +.. R14 -gives us the last element of the string and +a[0] gives us the first character in the string. The indexing starts from 0 +for the first character and goes up to (n-1) for the last character,where 'n' is the total +number of characters in a string. +We can access the strings from the end using negative indices + +.. L15 :: + a[-1] a[-2] -gives us second element from the end of the string +.. R15 + +a[-1] gives us the last element of the string and +a[-2] gives us second element from the end of the string. + +.. L16 + +{{{ Show slide with Question 2 }}} + +.. R16 -Following is an exercise that you must do. +Pause the video here, try out the following exercise and resume the video. -%% %% Given a string, ``s = "Hello World"``, what is the output of:: +Given a string, ``s = "Hello World"``, what is the output of:: - s[-5] - s[-10] - s[-15] + s[-5] + s[-10] + s[-15] -Please, pause the video here. Do the exercise and then continue. +.. L17 +{{{ Switch to terminal }}} :: - s[-5] + s[-5] -gives us 'W' +.. R17 + +s[-5] gives us 'W' + +.. L18 :: - s[-10] + s[-10] + +.. R18 + +s[-10] gives us 'e' and -gives us 'e' and +.. L19 :: - s[-15] + s[-15] -gives us an ``IndexError``, as should be expected, since the string +.. R19 + +s[-15] gives us an ``IndexError``, as should be expected, since the string given to us is only 11 characters long. -Let us attempt to change one of the characters in a string:: +.. R20 + +Let us attempt to change one of the characters in a string + +.. L20 +:: + + a = 'hello' + a[0] = 'H' - a = 'hello' - a[0] = 'H' +.. R21 As said earlier, strings are immutable. We cannot manipulate a string. Although there are some methods which let us manipulate @@ -171,20 +269,65 @@ methods like split which lets us break the string on the specified separator, the join method which lets us combine the list of strings into a single string based on the specified separator. +.. L21 + +.. L22 + {{{ Show summary slide }}} -This brings us to the end of another session. In this tutorial session -we learnt +.. R22 + +Let's revise quickly what we have learnt today.In this tutorial we have learnt to, + + 1. Define strings in differnt ways. + #. Concatenate strings by performing addition. + #. Repeat a string 'n' number of times by doing multiplication. + #. Access individual elements of the string by using their subscripts. + #. Use the concept of immutability of strings. + +.. L23 + +{{{Show self assessment questions slide}}} + +.. R23 + +Here are some self assessment questions for you to solve + +1. Write code to assign s, the string ``' is called the apostrophe`` + +2. Given strings s and t, ``s = "Hello"`` and ``t = "World"`` and an + integer r, ``r = 2``. What is the output of s * r + s * t? + +3. How will you change s='hello' to s='Hello'. + + - s[0]= H + - s[0]='H' + - strings are immutable,hence cannot be manipulated + +.. L24 + +{{{ solution of self assessment questions on slide }}} + +.. R24 + +And the answers, + +1. The given string can be assigned in this manner +:: + + s = "` is called the apostrophe" + +2. The operation ``s * r + s * t`` will print each of the two words twice + + HelloHelloWorldWorld + +3. Strings are immutable.Therefore they cannot be manipulated. - * How to define strings - * Different ways of defining a string - * String concatenation and repetition - * Accessing individual elements of the string - * Immutability of strings +.. L25 -{{{ Show the "sponsored by FOSSEE" slide }}} +{{{ Show the Thankyou slide }}} -This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India +.. R25 Hope you have enjoyed and found it useful. Thank you! |