Run each commands and try to understand why certain operations behave the way they do before looking at the answers.
There are 3 parts to this exercise: Numbers, Strings, and Lists, with the answers written under each section.
2 + 2
10 / 4
50 - 5*6
(50 - 5)*6
5 ** 2
7 % 2
7 // 2
3.14 + 2
Assign the integer 20 to the variable width
Assign the float 5 to the variable height
Save width * height
to the variable area
What is the area?
What happens if you assign 30 to width
and print area
again?
Re-assign width * height
to area
and see if the results changes now.
Assign area * 2
to 2timesArea
Assign area + 2
to return
2 + 2
4
10 / 4 # Note that division of two integers becomes a float
2.5
50 - 5*6 # multiplication is evaluated before the minus
20
(50 - 5)*6 # Order of operations, parenthesis first
270
5 ** 2 # To the power of
25
7 % 2 # Modulo yields the remainder of the division of 7 by 2 (6/2 has the remainder 1)
1
7 // 2 # Floor division returns the highest integer
3
3.14 + 2
5.140000000000001
So why is the example above not 5.14? Python represents floats as base 2 (binary) fractions. This causes some of the numbers to having to be approximated instead of exact. If you need very precise calculations you need to be aware that python behaves this way. To read more about this, click here
width = 20
height = 5.0
area = width * height
area # Multiplication of a float and an integer results in a float
100.0
width = 30
area
100.0
When assigning width * height to area, python stores the actual value assigned to area in memory, ie area = 100.0. It does not store the pointer to width * height. To update area with the new inputs we have to actively assign area a value again.
area = width * height
area
150.0
2timesArea = area * 2
File "<ipython-input-14-4bdf2ca84b6b>", line 1 2timesArea = area * 2 ^ SyntaxError: invalid syntax
return = area + 2
File "<ipython-input-15-da7854f8d5e4>", line 1 return = area + 2 ^ SyntaxError: invalid syntax
Why does the above fail? In the first case we are trying to assign a value to a variable name starting with a number, which is not allowed. In the second case we are trying to assign a value to a reserved keyword. However, both of them results in the same error message, a SyntaxError, which sometimes can be tricky to track, so be aware.
'Hello World!'
"Hello World!"
'Oh, no, you didn\\'t...'
"Oh, no, you didn't..."
"\\"Yes\\", I did!"
'"Yes", I did'
'Hello' ' ' 'World' '!'
s = 'Hello\tWorld'
s
print(s)
s = 'First line.\nSecond line.'
s
print(s)
folder = 'C:\some\name'
print(folder)
folder = r'C:\some\name'
print(folder)
'hmm ' + 3 * 'miam'
'Py' 'thon'
prefix = 'Py'
prefix + 'thon'
text = ('Put several strings within parentheses ' +
'to have them joined together, ' +
'with the use of ' +
'concatenation')
text
'Hello World!' # "" and '' can be used interchangeably
'Hello World!'
"Hello World!"
'Hello World!'
'Oh, no, you didn\'t...'
"Oh, no, you didn't..."
In the example above we have to escape the special character ' to prevent python as interpreting it as end of the string. Another way of inserting ' into a string would be to enclose the string in " instead, as in the example below. That way the ' is interpreted as a normal character.
"Oh, no, you didn't..."
"Oh, no, you didn't..."
"\"Yes\", I did!"
'"Yes", I did!'
'"Yes", I did'
'"Yes", I did'
'Hello' ' ' 'World' '!' # Ignores spaces between strings
'Hello World!'
s = 'Hello\tWorld' # Adds a tab between the words
s # Notice that s only shows the content
'Hello\tWorld'
print(s) # While print() renders the content of s
Hello World
s = 'First line.\nSecond line.' # \n adds a newline
s # Without print(), \n is included in the output
'First line.\nSecond line.'
print(s) # With print(), \n produces a new line
First line. Second line.
folder = 'C:\some\name' # Here the \n will be interpreted as a newline
print(folder)
C:\some ame
folder = r'C:\some\name'
print(folder)
C:\some\name
If you don’t want characters prefaced by \ to be interpreted as special characters, you can use raw strings by adding an r before the first quote as above
'hmm ' + 3 * 'miam'
'hmm miammiammiam'
'Py' 'thon'
'Python'
prefix = 'Py'
prefix + 'thon'
'Python'
text = ('Put several strings within parentheses ' +
'to have them joined together, ' +
'with the use of ' +
'concatenation')
text
'Put several strings within parentheses to have them joined together, with the use of concatenation'
squares = [1, 4, 9, 16, 25]
cubes = [1, 8, 27, 64, 125]
squares + cubes
squares * 2
squares * cubes
Lists are a collection of items grouped together. A list is denoted using comma-separated values between square brackets. A list might contain items of different types, but usually the items all have the same type.
squares = [1, 4, 9, 16, 25]
cubes = [1, 8, 27, 64, 125]
squares + cubes # Even though both contains integers and are of equal length they cannot be added together, only concatenated
[1, 4, 9, 16, 25, 1, 8, 27, 64, 125]
squares * 2 # Multiplication with 2 will produce a new list with a duplicate of the first list concatenated to the end
[1, 4, 9, 16, 25, 1, 4, 9, 16, 25]
squares * cubes # Multiplication of lists can only be done with integers, not lists
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-35-27f4ae718739> in <module>() ----> 1 squares * cubes # Multiplication of lists can only be done with integers, not lists TypeError: can't multiply sequence by non-int of type 'list'