ENGR 102-213 (Socolofsky) Week 02 Python scripts Listing for script.py 1 # data_types.py 2 # 3 # Lecture examples of using various Python data types and string formatting 4 # 5 # ENGR 102-213 6 # Scott Socolofsky 7 # August 2018 8 9 import math 10 11 # In this first block, we will perform various mathematical operations on 12 # different data types and let the print () function print the raw, 13 # unformatted output so that we can see what data types result. 14 15 # Addition 16 i = 1 17 i = i + 1 18 print (' Addition of two integers :') 19 print (i) 20 21 i += 1 22 print (' Addition of two integers using +=: ') 23 print (i) 24 25 x = 7. 15 26 x = x + 2. 54 27 print (' Addition of two floats :') 28 print (x) 29 30 print (' Addition of a float and integer :') 31 x = x + i 32 print (x) 33 34 x += 1 35 print (' Addition of a float and integer using +=: ') 36 print (x) 37 print (' ') 38 39 # Subtraction 40 i = 10 41 i = i - 1
42 print (' Subtraction of two integers :') 43 print (i) 44 45 i -= 1 46 print (' Subtraction of two integers using -=: ') 47 print (i) 48 49 x = 2. 54 50 x = x - 7. 15 51 print (' Subtraction of two floats :') 52 print (x) 53 54 print (' Subtraction of a float and integer :') 55 x = 7. 15 56 i = 3 57 x = x - i 58 print (x) 59 60 print (' Subtraction of a float and integer using -=: ') 61 x = 7. 15 62 x -= 1 63 print (x) 64 print (' ') 65 66 # Multiplication 67 68 x = 7. 15 69 y = 2. 54 70 z = x * y 71 print (' Multiplication of two floats :') 72 print (z) 73 74 n = 10 75 w = z * n 76 print (' Multiplication of a float and integer :') 77 print (w) 78 79 s = 'ENGR 102 -- ' 80 heading = 5 * s 81 print (' Multiplication of an integer and string :') 82 print ( heading ) 83 print (' ') 84 85 # Division 86 i = 5 87 j = 10 88 k = 7 89 n1 = j / i 90 n2 = k / i 91 print ('Division of two integers (10 / 5): ') 92 print ( n1) 93 print (' Division of two integers ( 7 / 5)') 94 print ( n2) 2
95 96 x = 7. 15 97 y = 2. 54 98 z = x / y 99 print (' Division of two floats :') 100 print ( z) 101 102 w = z / n 103 print (' Division of float by integer :') 104 print ( w) 105 106 w = n / z 107 print (' Division of an integer by a float :') 108 print ( w) 109 print (' ') 110 111 # Powers 112 u = i ** j 113 print (' Integer to an integer power :') 114 print ( u) 115 116 v = x ** j 117 print (' Float to an integer power :') 118 print ( v) 119 120 w = x ** y 121 print (' Float to a float power :') 122 print ( w) 123 124 print (' NOTE : When possible, use integers in powers since ') 125 print (' integer powers execute faster on a computer.') 126 print (' ') 127 128 # Integer division 129 x = 7. 15 130 y = 2. 54 131 z1 = x // y 132 print (' Division of two floats ( 7. 15 // 2. 54): ') 133 print ( z1) 134 135 # Modulus operator 136 z2 = x % y 137 print (' Division of two floats ( 7. 15 % 2. 54): ') 138 print ( z2) 139 print (' ') 140 141 # In this section, we format many of the variables computed above within 142 # various print statements. 143 print (' Format an integer as an integer, part 1: % d' % i) 144 print (' Format an integer as an integer, part 2: % 5. 5d' % i) 145 print (' Format an integer as a float : % 5. 5g' % i) 146 print (' Format a float as a float ( default ): % g' % w) 147 print (' Format a float as a float ( formatted ): % 7. 7g' % w) 3
148 print (' ') 149 print (' Include an apostrophe to symbolize feet in a string :') 150 print (" The wall is %4.4g' long " % w) 151 152 # We can also include multiple strings in a print statement. 153 x = 7. 15 154 y = 3. 45 155 z = x // y 156 print (' Printing a result using multiple strings separated by comma ') 157 print ('The division of ', x, '// ', y, '=', z) 158 print (' Printing a result using string concatenation ') 159 print ('The division of ' + str (x) + '// ' + str (y) + ' = ' + str (z)) 160 print (' ') 161 162 # Finally, we demonstrate the input function 163 print (' Enter the diameter of a sphere :') 164 d = float ( input ()) 165 vol = math.pi / 6. * d**3 166 print (' The volume of your sphere is % 8. 8g units ' % vol ) 167 print (' ') 168 169 d = float ( input (' Enter the diameter of a sphere : ')) 170 vol = math.pi / 6. * d**3 171 print (' The volume of your sphere is % 8. 8g units ' % vol ) 172 print (' ') 4
Code execution for script.py yields Addition of two integers: 2 Addition of two integers using +=: 3 Addition of two floats: 9.690000000000001 Addition of a float and integer: 12.690000000000001 Addition of a float and integer using +=: 13.690000000000001 Subtraction of two integers: 9 Subtraction of two integers using -=: 8 Subtraction of two floats: -4.61 Subtraction of a float and integer: 4.15 Subtraction of a float and integer using -=: 6.15 Multiplication of two floats: 18.161 Multiplication of a float and integer: 181.61 Multiplication of an integer and string: ENGR 102 -- ENGR 102 -- ENGR 102 -- ENGR 102 -- ENGR 102 -- Division of two integers (10 / 5): 2.0 Division of two integers (7 / 5) 1.4 Division of two floats: 2.8149606299212597 Division of float by integer: 0.281496062992126 Division of an integer by a float: 3.5524475524475525 5
Integer to an integer power: 9765625 Float to an integer power: 349188890.423927 Float to a float power: 147.88950188577866 NOTE: When possible, use integers in powers since integer powers execute faster on a computer. Division of two floats (7.15 // 2.54): 2.0 Division of two floats (7.15 % 2.54): 2.0700000000000003 Format an integer as an integer, part 1: 5 Format an integer as an integer, part 2: 00005 Format an integer as a float: 5 Format a float as a float (default): 147.89 Format a float as a float (formatted): 147.8895 Include an apostrophe to symbolize feet in a string: The wall is 147.9 long Printing a result using multiple strings separated by comma The division of 7.15 // 3.45 = 2.0 Printing a result using string concatenation The division of 7.15//3.45 = 2.0 Enter the diameter of a sphere: 3.5 The volume of your sphere is 22.449298 units Enter the diameter of a sphere: 4.5 The volume of your sphere is 47.712938 units Process finished with exit code 0 6