(00) How to run python? > python Python 2.4.3 (#1, Aug 2 2006, 22:24:22) [GCC 3.3.4] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> (01) Let's do the followings: >>> 1 + 5 6 >>> 2 * 5 10 >>> 1 / 2 0 >>> (02) Is it the right answer? Then, do the followings: >>> float(1 / 2) 0.0 >>> 1 / 2.0 0.5 >>> float(1)/2 0.5 >>> 1 / float(2) 0.5 >>> (03) How about these? >>> 'aaa' 'aaa' >>> len('aaa') 3 >>> (04) What happened? Read carefully the error message (if you have), and explain it. >>> len('aaa') + len('bbb') 6 >>> len('aaa') + len('bbb') + 4 10 >>> 'aaa' + 'bbb' 'aaabbb' >>> 'aaa' + 5 Traceback (most recent call last): File "", line 1, in ? TypeError: cannot concatenate 'str' and 'int' objects >>> (05) How to protect you from this kind of problem? >>> type(1) >>> type('1') >>> type(1.0) >>> (06) You can associate a name to a value: >>> a = 10 >>> a 10 >>> (07) The interpreter displays the value (10) of the variable (a). Read carefully the error message (if you have), and explain it. >>> myVar = 'one sentence' >>> myVar 'one sentence' >>> 1string = 'one string' File "", line 1 1string = 'one string' ^ SyntaxError: invalid syntax >>> >>> myvar Traceback (most recent call last): File "", line 1, in ? NameError: name 'myvar' is not defined >>> (08) What appended? >>> a = 2 >>> a 2 >>> a * 5 10 >>> b = a * 5 >>> b 10 >>> a = 1 >>> b 10 >>> (09) Why hasn't b changed? What is the difference between the followings: >>> b = a * 5 >>> >>> b = 5 >>> (10) What is the differerence between the followings: What do you conclude about the type of a variable? >>> a = 1 #in this case a is a number >>> a + 2 3 >>> a = '1' #in this case a is a string >>> a + 1 Traceback (most recent call last): File "", line 1, in ? TypeError: cannot concatenate 'str' and 'int' objects >>> (11) The following will be explained later: >>> from string import * >>> (12) Can we perform calculus on strings? >>> myString='abc' >>> myString * 3 'abcabcabc' >>> myString1 = 'abcdefghijklmnopqr' >>> myString2 = 'ggghhhiiijjjkkklll' >>> (13) How do you concatenate myString1 and myString2 in a single string? >>> myString = myString1 + myString2 'abcdefghijklmnopqrggghhhiiijjjkkklll' >>> (14) What is the length of the string myString? >>> len(myString) 36 >> (15) Does the string myString contain the character 'c'? >>> 'c' in myString True >>> (16) Does it contain 'm'? >>> 'm' in myString False >>> (17) Do the followings: And explain why. >>> myString[0] 'a' >>> myString[1] 'b' (18) Display the 10th character. >>> myString[9] 'j' (19) Find the index of the last character. >>> len(myString) 36 >>> myString[35] 'l' >>> (20) Find a more generic way to do it. >>> myString[len(myString) - 1] 'l' (21) Python provides a special form to get the characters from the end of a string: >>> myString1[-1] 'r' >>> myString1[-4] 'o' >>> (22) Do the following, then what do you get? >>> myString1[0] + myString1[5] + myString1[10] 'afk' >>> (23) Python provides a form to get 'slices' from strings: >>> myString[0:3] 'abc' >>> myString[3:6] 'def' (24) How many of each character does this string contain? >>> count(myString, 'a') 1 >>> count(myString, 'c') 1 >>> count(myString, 'k') 4 >>> count(myString, 'l') r >>> (25) Count the percentage of 'k' on the string myString. >>> totalCount = len(myString) >>> localCount = count(myString, 'k') >>> percentage = (localCount / totalCount) * 100 >>> percentage >>> 0 (26) What happened? Can you correct this? >>> float(localCount) / totalCount * 100 (27) How to find specific pattern on a string: Do the following and explain why? >>> myString = """abcdefghijklmnopqrstuvwxyz zyxwvutsrqponmlkjihgfedcba""" >>> myString1 = 'ABCDEF' >>> myString2 = 'FEDCAB' >>> count(myString, myString1) 0 >>> >>> 'abcdef' == 'abcdef' True >>> 'abcedf' == 'ABCDEF' False >>> (28) How to change the case of a string? >>> myString11 = lower(myString1) >>> myString11 'abcdef' >>> count(myString, myString11) 1 >>> (29) Display the string myString. Do you see a character that you don't expect to have in myString? Then, how to remove it? >>> myString 'abcdefghijklmnopqrstuvwxyz\nzyxwvutsrqponmlkjihgfedcba' >>> len(myString) >>> 53 >>> myString = replace(myString, '\n', '') >>> myString 'abcdefghijklmnopqrstuvwxyzzyxwvutsrqponmlkjihgfedcba' >>> len(myString) >>> 52 (30) How to exit Python shell? >>> ^D