1.11. Solutions to Exercises#
Exercise 1.1
# You can print special characters:
print("My first message #1")
print("Another special character: one \ two")
# However, there are some special combinations such as \n that have another meaning.
# In this case, \n starts a new line.
print("Another special character: one \n two")
# If we wish to print the text \n, we should add another \ to indicate that we do not.
print("Another special character: one \\n two")
Exercise 1.2
print('The value is', 10)
# Note that a space is added in between the string and the number.
Exercise 1.3
a = 5
print(a) # this prints 5
a = 7
print(a) # this prints 7 as the current value of a is 7
Exercise 1.4 After restarting the kernel all variables, such as a, are no longer stored and do not show up in %whos.
Exercise 1.5
print('This is the first string' + 'and this is the second string.')
# The strings are joined.
Exercise 1.6
# Only 0 is False, some examples:
if 0:
print('0 is True')
else:
print('0 is False')
if 1:
print('1 is True')
else:
print('1 is False')
if -57:
print('-57 is True')
else:
print('-57 is False')
if 6/7:
print('6/7 is True')
else:
print('6/7 is False')
Exercise 1.7
Exercise 1.8
a = [2,3,5]
b = np.array([2,3,5])
print(2*a) # Multiplying the list is like adding the two lists such that it is repeated.
print(2*b) # Multiplying the numpy array multplies each value in the array.
Exercise 1.9
even_nr = np.arange(0,10,2)
print(even_nr)
odd_nr = np.arange(1,11,2)
print(odd_nr)
total = np.append(even_nr, odd_nr)
print(total)
total = np.sort(total)
print(total)
total = np.delete(total, 2)
print(total)
Exercise 1.10
Exercise 1.11
print('7*3 =', 7*3)
print('7-3 =', 7-3)
print('7/3 =', 7/3)
print('7**3 =', 7**3)
print('7//3 =', 7//3)
print('7%3 =', 7%3)
Exercise 1.12
# You can chech that the input is always a string.
a = input('type input: ')
print(type(a))
Exercise 1.13
Exercise 1.14 ZeroDivisionError: division by zero You cannot divide by zero, so this results in an error.
NameError: name ‘practicum’ is not defined You cannot use an undefined variable, use a string to print text.
TypeError: can only concatenate str (not “int”) to str You cannot add a string and an integer. We can add two strings:
print('practicum is great' + str(2))
Exercise 1.15
Exercise 1.13