Applying Primer¶
Numbers¶
The interpreter acts as a simple calculator: you can type an expression at it and it will write the value. Expression syntax is straightforward: the operators +, -, * and / work just like in most other languages (for example, Pascal or C); parentheses (()) can be used for grouping. For example:
>>> 2+2
4
>>> 50 - 5*6
20
>>> (50 - 5*6) / 4
5.0
>>> 8 / 5 # division always returns a floating point number
1.6
The integer numbers (e.g. 2, 4, 20) have type int
, the ones with a fractional part (e.g. 5.0, 1.6) have type float
.
>>> 17 // 3 # floor division discards the fractional part
5
>>> 17 % 3 # the % operator returns the remainder of the division
2
With Python, it is possible to use the **
operator to calculate powers
>>> 5 ** 2 # 5 squared
25
>>> 2 ** 7 # 2 to the power of 7
128
The equal sign (=
) is used to assign a value to a variable. Afterwards, no result is displayed before the next interactive prompt:
>>> width = 20
>>> height = 5 * 9
>>> width * height
900
If a variable is not “defined” (assigned a value), trying to use it will give you an error:
>>> variable_not_exists # try to access an undefined variable
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-11-087486976af0> in <module>
----> 1 variable_not_exists # try to access an undefined variable
NameError: name 'variable_not_exists' is not defined
In interactive mode, the last printed expression is assigned to the variable _
. This means that when you are using Python as a desk calculator, it is somewhat easier to continue calculations, for example:
>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax # Result is 12.5625
>>> price + _ # Result is 113.0625
>>> round(_, 2)
900
This variable should be treated as read-only by the user. Don’t explicitly assign a value to it _
you would create an independent local variable with the same name masking the built-in variable with its magic behavior.
Strings¶
Besides numbers, Python can also manipulate strings, which can be expressed in several ways. They can be enclosed in single quotes (‘…’) or double quotes (“…”) with the same result
\
can be used to escape quotes:
>>>'spam eggs' # single quotes
'spam eggs'
>>> 'doesn\'t' # use \' to escape the single quote...
"doesn't"
>>> "doesn't" # ...or use double quotes instead
"doesn't"
'"Yes," they said.'
>>> '"Yes," they said.'
>>> "\"Yes,\" they said."
'"Yes," they said.'
>>> '"Isn\'t," they said.'
'"Isn\'t," they said.'
The print()
function produces a more readable output, by omitting the enclosing quotes and by printing escaped and special characters:
>>> '"Isn\'t," they said.'
'"Isn\'t," they said.'
>>> print('"Isn\'t," they said.')
"Isn't," they said.
s = 'First line.\nSecond line.' # \n means 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.
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:
>>> print('C:\some\name') # here \n means newline!
C:\some
ame
>>> print(r'C:\some\name') # note the r before the quote
C:\some\name
String literals can span multiple lines. One way is using triple-quotes: “””…””” or ‘’’…’’’. End of lines are automatically included in the string, but it’s possible to prevent this by adding a \ at the end of the line. The following example:
>>> print("""\
... Usage: thingy [OPTIONS]
... -hDisplay this usage message
... -H hostname Hostname to connect to
... """)
Usage: thingy [OPTIONS]
-hDisplay this usage message
-H hostname Hostname to connect to
Strings can be concatenated (glued together) with the +
operator, and repeated with *
:
>>> 3 * 'un' + 'ium'
'unununium'
>>> 'Py' 'thon' # Two or more string literals next to each other are automatically concatenated.
'Python'
>>> text = ('Put several strings within parentheses '
... 'to have them joined together.')
>>> print(text)
Put several strings within parentheses to have them joined together.
>>> prefix = 'Py'
>>> refix 'thon' # can't concatenate a variable and a string literal
File "<ipython-input-30-7c051b293f22>", line 2
refix 'thon' # can't concatenate a variable and a string literal
^
SyntaxError: invalid syntax
>>> prefix = 'Py'
>>> prefix + 'thon' # If you want to concatenate variables or a variable and a literal, use +
'Python'
Strings can be indexed (subscripted), with the first character having index 0
. There is no separate character type; a character is simply a string of size one:
>>> word = 'Python'
>>> word[0] # character in position 0
'P'
>>> word[-1] # last character
'n'
>>> word[-2] # second-last character
'o'
Note that since -0 is the same as 0, negative indices start from -1.
>>> word[0:2] # characters from position 0 (included) to 2 (excluded)
'Py'
>>> word[2:5] # characters from position 2 (included) to 5 (excluded)
'tho'
Note how the start is always included, and the end always excluded. This makes sure that s[:i] + s[i:]
is always equal to s:
>>> word[:2] + word[2:]
'Python'
word[:4] + word[4:]
'Python'
>>> word[:2] # character from the beginning to position 2 (excluded)
'Py'
>>> word[4:] # characters from position 4 (included) to the end
'on'
+—+—+—+—+—+—+ | P | y | t | h | o | n | +—+—+—+—+—+—+ 0 1 2 3 4 5 6 -6 -5 -4 -3 -2 -1
>>> word[42] # the word only has 6 characters
---------------------------------------------------------------------------
IndexErrorTraceback (most recent call last)
<ipython-input-41-469c6d99b5b2> in <module>
----> 1 word[42] # the word only has 6 characters
IndexError: string index out of range
word[4:42] # out of range slice indexes are handled gracefully when used for slicing
'on'
The built-in function len()
returns the length of a string:
>>> len(word)
6
Lists¶
Python knows a number of compound data types, used to group together other values. The most versatile is the list, which can be written as a list of comma-separated values (items) between square brackets. Lists might contain items of different types, but usually the items all have the same type.
>>> squares = [1, 4, 9, 16, 25]
>>> squares
[1, 4, 9, 16, 25]
Like strings
(and all other built-in sequence types), lists
can be indexed and sliced:
>>> squares[0] # indexing returns the item
1
>>> squares[-3:] # slicing returns a new list
[9, 16, 25]
>>> squares + [36, 49, 64, 81, 100] # Lists also support operations like concatenation
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Unlike strings, which are immutable, lists are a mutable type, i.e. it is possible to change their content:
>>> cubes = [1, 8, 27, 65, 125] # something's wrong here\
>>> cubes[3] = 64 # replace the wrong value
>>> cubes
[1, 8, 27, 64, 125]
You can also add new items at the end of the list, by using the append()
method:
>>> cubes.append(216) # add the cube of 6
>>> cubes.append(7 ** 3) # and the cube of 7
>>> cubes
[1, 8, 27, 64, 125, 216, 343]
>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> letters
['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> letters[2:5] = ['C', 'D', 'E'] # replace some values
>>> letters
['a', 'b', 'C', 'D', 'E', 'f', 'g']
>>> letters[2:5] = [] # now remove them
>>> letters
['a', 'b', 'f', 'g']
>>> letters[:] = [] # clear the list
>>> letters
[]
# It is possible to nest lists (create lists containing other lists), for example:
>>> a = ['a', 'b', 'c']
>>> n = [1, 2, 3]
>>> x = [a, n]
>>> x[0]
['a', 'b', 'c']
>>> x[0][1]
'b'
# Fibonacci series:
>>> a, b = 0, 1
... while a < 10:
... print(a)
... a, b = b, a+b
0
1
1
2
3
5
8
>>> a, b = 0, 1
... while a < 20:
... print(a, end=',')
... a, b = b, a+b
0,1,1,2,3,5,8,13,