# Code for chapter 18

#### For instructors:

Below are listed all the scripts as shown in chapter 18 of the book. These are free to copy and paste into your code editor.

## Script 18.1

 script_18_1.py ```# Script 18.1mass = 3.4volume = 1.8density = mass/volume       # Divisiondensity = round(density, 3) # Round to three decimal placesprint(density) ``` script_18_1.out ```[ah@hobbes Documents]\$ python script_18_1.py 1.889[ah@hobbes Documents]\$ ```

## Script 18.2

 script_18_2.py ```# Script 18.2x = 2     # Integery = 5     # Integerz = 3.0   # Floating pointprint(x * y) # 10  - Integerprint(x / y) # 0.4 - Floating point due to divisionprint(x + z) # 5.0 - Floating pointt = type(x * y - z)  # Get a value's data typeprint(t)             # float ``` script_18_2.out ```[ah@hobbes Documents]\$ python script_18_2.py 1005.0[ah@hobbes Documents]\$ ```

## Script 18.3

 script_18_3.py ```# Script 18.3x = 1            # An arbitrary integerprint(x.__doc__) # Print Python documentation for this object ``` script_18_3.out `[ah@hobbes Documents]\$ python script_18_3.py int(x=0) -> int or longint(x, base=10) -> int or longConvert a number or string to an integer, or return 0 if no argumentsare given.  If x is floating point, the conversion truncates towards zero.If x is outside the integer range, the function returns a long instead.If x is not a number or if base is given, then x must be a string orUnicode object representing an integer literal in the given base.  Theliteral can be preceded by '+' or '-' and be surrounded by whitespace.The base defaults to 10.  Valid bases are 0 and 2-36.  Base 0 means tointerpret the base from the string as an integer literal.>>> int('0b100', base=0)4[ah@hobbes Documents]\$ `

## Script 18.4

 script_18_4.py ```# Script 18.4x = Trueprint(x)print(bool(0.0)) # Falseprint(bool(7))   # True ``` script_18_4.out `[ah@hobbes Documents]\$ python script_18_4.py TrueFalseTrue[ah@hobbes Documents]\$`

## Script 18.5

 script_18_5.py ```# Script 18.5x = 2print(x > 5) # Falsey = x < 3print(y)     # True ``` script_18_5.out `[ah@hobbes Documents]\$ python script_18_5.py FalseTrue[ah@hobbes Documents]\$ `

## Script 18.6

 script_18_6.py ```# Script 18.6x = 1y = -1x > y and y > 1   # False - second comparison is Falseprint(x > y and y > 1)x != 2 or y == 1  # True - first comparison is True; x does not equal 2print(x != 2 or y == 1)not (y > x)       # True - comparison is False; not False is Trueprint(not (y > x)) ``` script_18_6.out `[ah@hobbes Documents]\$ python script_18_6.py FalseTrueTrue[ah@hobbes Documents]\$ `

## Script 18.7

 script_18_7.py ```# Script 18.7x = 'Hello'y = "world"print(x,y)  # Print both values to screen ``` script_18_7.out `[ah@hobbes Documents]\$ python script_18_7.py ('Hello', 'world')[ah@hobbes Documents]\$ `

## Script 18.8

 script_18_8.py ```# Script 18.8x = '''This text flows from one lineto the next line inside triple quotes'''print(x) ``` script_18_8.out `[ah@hobbes Documents]\$ python script_18_8.py This text flows from one lineto the next line inside triple quotes[ah@hobbes Documents]\$ `

## Script 18.9

 script_18_9.py ```# Script 18.9x = 'abcde'print(len(x)) # 5 print(x[0])   # First character 'a' at index 0print(x[-1])  # Last character 'e'print(x[1:3]) # 'bcd' range from index 1, up to, but not including 3 ``` script_18_9.out `[ah@hobbes Documents]\$ python script_18_9.py 5aebc[ah@hobbes Documents]\$ `

## Script 18.10

 script_18_10.py ```# Script 18.10name = 'Mary'x = 0.12y = 34121.0a = 'Hello {}'.format(name)    # name replaces bracketsprint(a)                       # 'Hello Mary'b = 'X {:.5f} Y {:.2e}'.format(x, y)   # 5 dp float 2 dp sciprint(b)                               # 'X 0.12000 Y 3.41e4' c = '{:3d},{:3d}'.format(12,7)   # pad digits to 3 charactersprint(c)                         # ' 12,  7' ``` script_18_10.out `[ah@hobbes Documents]\$ python script_18_10.py Hello MaryX 0.12000 Y 3.41e+04 12,  7[ah@hobbes Documents]\$ `

## Script 18.11

 script_18_11.py ```# Script 18.11x = ['a', 'b', 'c', 'd'] print(x[2])   # 'c' - the character at index 2print(x[1:])  # ['b', 'c', 'd'] - from index 1 to the endx[0] = 'z'    # character at index 0 is set to 'z'del x[2]      # Deletes item at index 2print(x)      # ['z', 'b', 'd']y = [[4,7], [9,6]] # A list containing two other listsprint(y[1])        # [9,6] - the sub-list at y index 1print(y[1][0])     # 9 - item 0 from sub-list 1   ``` script_18_11.out `[ah@hobbes Documents]\$ python script_18_11.py c['b', 'c', 'd']['z', 'b', 'd'][9, 6]9[ah@hobbes Documents]\$ `

## Script 18.12

 script_18_12.py ```# Script 18.12x = list('PQRST')     # Create list from text stringprint(x)              # ['P','Q','R','S','T'] - list of charactersy = list(range(10))   # A list of the range from 0 up to <10 print(y)              # [0,1,2,3,4,5,6,7,8,9] ``` script_18_12.out `[ah@hobbes Documents]\$ python script_18_12.py ['P', 'Q', 'R', 'S', 'T'][0, 1, 2, 3, 4, 5, 6, 7, 8, 9][ah@hobbes Documents]\$ `

## Script 18.13

 script_18_13.py ```# Script 18.13x = [4, 1, 0] # Three items a, b, c = x   # a is 4, b is 1, c is 0x = ['a', 'b', 'c', 'd']print(len(x))     # 4 - number of items in listprint('c' in x)   # True - string 'c' is in the listprint(2 in x)     # False - number 2 is not in list ``` script_18_13.out `[ah@hobbes Documents]\$ python script_18_13.py 4TrueFalse[ah@hobbes Documents]\$ `

## Script 18.14

 script_18_14.py ```# Script 18.14x = (9, 3, 1, 0)  # Create a tupley = (2,)          # Tuple with one item (needs trailing comma)z = ()            # Empty tupleprint(x[-1])      # Print the last item - 0#x[0] = 6          # Does not work! Tuples cannot be changed!w = list(x)       # Create equivalent list from a tuplew[0] = 6          # List copy can be changed print('x',x)print('w',w) ``` script_18_14.out `[ah@hobbes Documents]\$ python script_18_14.py 0('x', (9, 3, 1, 0))('w', [6, 3, 1, 0])[ah@hobbes Documents]\$ `

## Script 18.15

 script_18_15.py ```# Script 18.15x = {1,2,3,4,3,2,1}  # x is {1,2,3,4} - duplicates ignoredy = set([3,4,5,6])   # y created from a listprint(len(y))        # 4print(1 in y)        # False; 1 is not in yprint(x & y)         # {3,4} - intersection, items in bothprint(x | y)         # {1,2,3,4,5,6} - union, items in either ``` script_18_15.out `[ah@hobbes Documents]\$ python script_18_15.py 4Falseset([3, 4])set([1, 2, 3, 4, 5, 6])[ah@hobbes Documents]\$ `

## Script 18.16

 script_18_16.py ```# Script 18.16d = {"G":329.21, "C":289.18, "A":313.21, "T":314.19}print(d['A'])      # 313.21 -  value associated with 'A'print(len(d))      # 4 - number of key:value pairsprint(d.keys())    # Just keys 'G', 'A', 'T', 'C'print(d.values())  # Just values 329.21, 313.21, 314.19, 289.18 ``` script_18_16.out `[ah@hobbes Documents]\$ python script_18_16.py 313.214['A', 'C', 'T', 'G'][313.21, 289.18, 314.19, 329.21][ah@hobbes Documents]\$ `

## Script 18.17

 script_18_17.py ```# Script 18.17d = {"G":329.21, "C":289.18, "A":313.21, "T":314.19}d['T'] = 304.19   # Change the value of an existing itemd['U'] = 291.08   # Add a new key:value pairprint(len(d))     # 5 - dict is larger  del d['U']        # Delete a key and its value from the dictionary ``` script_18_17.out `[ah@hobbes Documents]\$ python script_18_17.py 5[ah@hobbes Documents]\$ `

## Script 18.18

 script_18_18.py ```# Script 18.18x = ['Mon', 'Tue', 'Wed'] # A list of stringsy = ['Fri', 'Sat', 'Sun'] # And anotherx.append('Thu')  # Add a single new item to endprint(x)x.extend(y)       # Extend with items from another collection print(x)x.sort()         # Sort contents alphabeticallyprint(x)x.remove('Sun')  # Remove an itemprint(x)x.index('Sat')   # Positional index of an itemprint(x) ``` script_18_18.out `[ah@hobbes Documents]\$ python script_18_18.py ['Mon', 'Tue', 'Wed', 'Thu']['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']['Fri', 'Mon', 'Sat', 'Sun', 'Thu', 'Tue', 'Wed']['Fri', 'Mon', 'Sat', 'Thu', 'Tue', 'Wed']['Fri', 'Mon', 'Sat', 'Thu', 'Tue', 'Wed'][ah@hobbes Documents]\$ `

## Script 18.19

 script_18_19.py ```# Script 18.19s = {'G', 'C', 'A', 'T'}   # A set with 4 stringst = {'N', 'R', 'Y'}print(s)s.add('U')       # Add a single item (if not present) print(s)s.update(t)      # Add any new items from another collectionprint(s) ``` script_18_19.out `[ah@hobbes Documents]\$ python script_18_19.py set(['A', 'C', 'T', 'G'])set(['A', 'C', 'U', 'T', 'G'])set(['A', 'C', 'G', 'N', 'R', 'U', 'T', 'Y'])[ah@hobbes Documents]\$`