Python Programming Essentials: Exercise Answers
Exercise 1
Variables Multiplied | Resulting Type |
---|---|
int × int | int |
float × float | float |
complex × complex | complex |
int × float | float |
float × complex | complex |
int × complex | complex |
Python's general rule for handling mixed type operations is to covert the operands up to the type of the most complex operand, and then perform the math on same-type operands. This means that if you multiply an integer by a floating-point number, the integer is first converted to a float, the two are multiplied, and the final result is a floating-point number.
Return to Python Programming Essentials
Exercise 2
The table below shows the result of the commands explains the results.
Commands | Explanation |
---|---|
In [1]: str1="egg's"
|
Everything inside the double quotes is assigned to the string. Note that you can put a single quote inside the string. |
In [3]: str2='egg"s'
|
Everything inside the single quotes is assigned to the string. Note that you can put a double quote inside the string. |
In [5]: str='Ni!'
|
An string multiplied by an integer n causes the string to be repeated n times. |
In [7]: s='5'
|
A string multiplied by a string gives an error as does addition of a string and an integer. |
In [15]: len(str1)
|
The len() function returns the length of a string.
|
In [17]: st = "hello"
|
This is an example of a formated print() command. The %s is a stand-in for a string and %g is a stand-in for a float. The % symbol after the quoted text tells print() that the following list of objects contained in parenthesis should be substituted for the stand-ins. The number of objects and the object type must match the number and type of the stand-in symbols in quoted text.
|
In [20]: longstring = 'The knights who say "Ni!"'
|
A string is just an array of characters. You can access any element of an array using this square-bracket notation. NOTE: The first element of the array has the index 0. The notation [0:4] slices-out and returns elements 0, 1, 2, and 3 of the array. The notation [4:7] slices-out elements 4, 5, and 6.
|
Return to Python Programming Essentials
Exercise 3
Adding, subtracting, multiplying, and dividing by numbers
In [2]: from pylab import * In [3]: xary = linspace(0,10,10) In [4]: yary = ones(10) In [5]: xary Out[5]: array([ 0. , 1.11111111, 2.22222222, 3.33333333, 4.44444444, 5.55555556, 6.66666667, 7.77777778, 8.88888889, 10. ]) In [6]: yary Out[6]: array([ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]) In [7]: yary+2 Out[7]: array([ 3., 3., 3., 3., 3., 3., 3., 3., 3., 3.]) In [8]: yary*2 Out[8]: array([ 2., 2., 2., 2., 2., 2., 2., 2., 2., 2.]) In [9]: yary-5 Out[9]: array([-4., -4., -4., -4., -4., -4., -4., -4., -4., -4.]) In [10]: yary/5 Out[10]: array([ 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2])
When adding, subtracting, multiplying, and dividing arrays by constants, Numpy just applies the operation to each element of the array.
Adding, subtracting, multiplying, and dividing one array by another
In [11]: xary + yary Out[11]: array([ 1. , 2.11111111, 3.22222222, 4.33333333, 5.44444444, 6.55555556, 7.66666667, 8.77777778, 9.88888889, 11. ]) In [12]: xary - yary Out[12]: array([-1. , 0.11111111, 1.22222222, 2.33333333, 3.44444444, 4.55555556, 5.66666667, 6.77777778, 7.88888889, 9. ]) In [13]: xary*yary Out[13]: array([ 0. , 1.11111111, 2.22222222, 3.33333333, 4.44444444, 5.55555556, 6.66666667, 7.77777778, 8.88888889, 10. ]) In [14]: xary/yary Out[14]: array([ 0. , 1.11111111, 2.22222222, 3.33333333, 4.44444444, 5.55555556, 6.66666667, 7.77777778, 8.88888889, 10. ]) In [15]: yary/xary Out[15]: array([ Inf, 0.9 , 0.45 , 0.3 , 0.225 , 0.18 , 0.15 , 0.12857143, 0.1125 , 0.1 ]) In [16]: zary = rand(12) In [17]: zary Out[17]: array([ 0.44133581, 0.98794904, 0.04241822, 0.30115897, 0.33447446, 0.33279533, 0.39598572, 0.87429202, 0.09507045, 0.07532312, 0.20075777, 0.95970071]) In [18]: xary + zary --------------------------------------------------------------------------- ValueError Traceback (most recent call last) /Users/sburns/<ipython console> in <module>() ValueError: shape mismatch: objects cannot be broadcast to a single shape
When two arrays are added, subtracted, multiplied, and divided, Numpy performs the operation the operation element by element. For example, when adding two arrays the result is another array whose first element is the sum of the first elements of the two arrays, etc.
If a division of two arrays results in a division by zero, the corresponding array element is set to Inf
. This is a useful feature since all the other elements of the array may be well defined.
If the two arrays don't have the same number of elements, Python generates an error message.
Return to Python Programming Essentials
Exercise 4
In [16]: run input_test.py Enter your name: Shane Burns Your name is Shane Burns. Enter a floating point number: 137 Your number is: 137.0 In [17]: run input_test.py Enter your name: 13/2 Your name is 13/2. Enter a floating point number: 13/2 --------------------------------------------------------------------------- ValueError Traceback (most recent call last) /Users/sburns/ProgrammingPythonEssentials/Examples_and_Data/input_test.py in() 9 10 # input() outputs an object whose type is determined by the input ---> 11 x = float(input("Enter a floating point number: ")) 12 print("Your number is:") 13 print(x) ValueError: could not convert string to float: '13/2' In [18]: run input_test.py Enter your name: Shane Burns Your name is Shane Burns. Enter a floating point number: Shane Burns --------------------------------------------------------------------------- ValueError Traceback (most recent call last) /Users/sburns/ProgrammingPythonEssentials/Examples_and_Data/input_test.py in () 9 10 # input() outputs an object whose type is determined by the input ---> 11 x = float(input("Enter a floating point number: ")) 12 print("Your number is:") 13 print(x) ValueError: could not convert string to float: 'Shane Burns'
The input()
command always returns an object of type string
. If using the float(input())
syntax and the input isn't a string that can be converted to a floating-point number, float()
will generate an error.