Python basics Flashcards
What is Subsetting in Python and how do you do it?
Subsetting Python lists is a piece of cake. Take the code sample below, which creates a list x and then selects “b” from it. Remember that this is the second element, so it has index 1. You can also use negative indexing.
x = [“a”, “b”, “c”, “d”]
x[1]
x[-3] # same result!
After you’ve extracted values from a list, you can use them to perform additional calculations. Take this example, where the second and fourth element of a list x are extracted. The strings that result are pasted together using the + operator
x = [“a”, “b”, “c”, “d”]
print(x[1] + x[3])
Subsetting lists of list
You saw before that a Python list can contain practically anything; even other lists! To subset lists of lists, you can use the same technique as before: square brackets. Try out the commands in the following code sample in the IPython Shell:
x = [[“a”, “b”, “c”],
[“d”, “e”, “f”],
[“g”, “h”, “i”]]
x[2][0]
x[2][:2]
x[2] results in a list, that you can subset again by adding additional square brackets.
How do you Slice and dice data in python lists?
Selecting single values from a list is just one part of the story. It’s also possible to slice your list, which means selecting multiple elements from your list. Use the following syntax:
my_list[start:end]
Please note:The start index will be included, while the end index is not.
The code sample below shows an example. A list with “b” and “c”, corresponding to indexes 1 and 2, are selected from a list x
x = [“a”, “b”, “c”, “d”]
x[1:3]
The elements with index 1 and 2 are included, while the element with index 3 is not.
Earlier we saw syntax where you specify both where to begin and end the slice of your list:
my_list[begin:end]
However, it’s also possible not to specify these indexes. If you don’t specify the begin index, Python figures out that you want to start your slice at the beginning of your list. If you don’t specify the end index, the slice will go all the way to the last element of your list. To experiment with this, try the following commands in the IPython Shell:
x = [“a”, “b”, “c”, “d”]
x[:2]
x[2:]
x[:]
How do you do List Manipulation in Python? like replacing list elements,
Replacing List Elements
Replacing list elements is pretty easy. Simply subset the list and assign new values to the subset. You can select single elements or you can change entire list slices at once.
Use the IPython Shell to experiment with the commands below. Can you tell what’s happening and why?
x = ["a", "b", "c", "d"] x[1] = "r" x[2:] = ["s", "t"]
Output
In [1]: x = [“a”, “b”, “c”, “d”]
In [2]: x[1] = “r”
In [3]: x[2:] = [”s”, “t”]
In [4]: x
Out[4]: [‘a’, ‘r’, ‘s’, ‘t’]
command 2 x[1] = “r” basically replaces the value at index 1 in the list x to “r” from “b”
command 3 x[2:] = [”s”,”t”] basically replaces the value in index 2 till the end of the list x with values “s” and “t”.
Extend a list
If you can change elements in a list, you sure want to be able to add elements to it, right? You can use the + operator:
x = ["a", "b", "c", "d"] y = x + ["e", "f"]
The above command adds the list [“e”,”f”] to the list x by using the + operator. so the output of y would include all the elements in list x as well as the additional elements “e” and “f”
In [1]: x = [“a”, “b”, “c”, “d”]
In [2]: y = x + [“e”, “f”]
In [3]: x
Out[3]: [‘a’, ‘b’, ‘c’, ‘d’]
In [4]: y
Out[4]: [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’]
Delete List Elements
Finally, you can also remove elements from your list. You can do this with the del statement:
x = [“a”, “b”, “c”, “d”]
del(x[1])
Pay attention here: as soon as you remove an element from a list, the indexes of the elements that come after the deleted element all change!
What is the inner workings of list in Python?
Lets say that you have the following list
areas = [11.25, 18.0, 20.0, 10.75, 9.50]
and you run the following command
areas_copy = areas
In the above command you have copied the contents of the areas list into areas_copy. Now lets say you update the areas_copy list index 0 to value 5.0 by running the following command
areas_copy[0] = 5.0
If you see the output you would find that the value is changed for areas list as well. Now why did this happen?
This is because when you create a list in Python by using the “=” by assigning values from another list then internally python is not copying the actual values. instead it is just copying the pointer of the assigned list. So when you update the copy you are still pointing to the assigned list and changing its value.
In order to avoid this and to make sure that you are copying the contents of the list instead of the pointer you would need to use the list() function or by using [:]
for example in the sample code above you could do the copy using the following code
areas_copy = list(areas)
Or you could use the following
areas_copy = areas[:]
about functions in Python
Out of the box, Python offers a bunch of built-in functions to make your life as a data scientist easier. You already know two such functions: print() and type(). You’ve also used the functions str(), int(), bool() and float() to switch between data types. These are built-in functions as well.
Calling a function is easy. To get the type of 3.0 and store the output as a new variable, result, you can use the following:
result = type(3.0)
The general recipe for calling functions and saving the result to a variable is thus:
output = function_name(input)
How do you access help in Python?
you have to ask for information about a function with another function: help(). In IPython specifically, you can also use ? before the function name.
To get help on the max() function, for example, you can use one of these calls:
help(max)
?max
What is the use of / , // and % in Python?
Division (/) always returns a float. To do floor division and get an integer result (discarding any fractional result) you can use the // operator; to calculate the remainder you can use %:
\>\>\> 17 / 3 # classic division returns a float 5.666666666666667 \>\>\> \>\>\> 17 // 3 # floor division discards the fractional part 5 \>\>\> 17 % 3 # the % operator returns the remainder of the division 2 \>\>\> 5 \* 3 + 2 # result \* divisor + remainder 17
What is the use of ** operator?
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
What is the use of = operator?
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
what is the use of _ in “Interactive mode”?
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 12.5625 \>\>\> price + _ 113.0625 \>\>\> round(\_, 2) 113.06
How are strings expressed in Python?
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 string is enclosed in double quotes if the string contains a single quote and no double quotes, otherwise it is enclosed in single quotes.
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] -h Display this usage message -H hostname Hostname to connect to """)
produces the following output (note that the initial newline is not included):
Usage: thingy [OPTIONS] -h Display this usage message -H hostname Hostname to connect to
Strings can be concatenated (glued together) with the +
operator, and repeated with *
:
>>>
~~~
>>> # 3 times ‘un’, followed by ‘ium’ >>> 3 * ‘un’ + ‘ium’ ‘unununium’
~~~
Two or more string literals (i.e. the ones enclosed between quotes) next to each other are automatically concatenated.
>>>
~~~
>>> ‘Py’ ‘thon’ ‘Python’
~~~
This feature is particularly useful when you want to break long strings:
>>>
~~~
>>> text = (‘Put several strings within parentheses ‘ … ‘to have them joined together.’) >>> text ‘Put several strings within parentheses to have them joined together.’
~~~
This only works with two literals though, not with variables or expressions:
>>>
~~~
>>> prefix = ‘Py’ >>> prefix ‘thon’ # can’t concatenate a variable and a string literal File “<stdin>", line 1
prefix 'thon'
^
SyntaxError: invalid syntax
>>> ('un' * 3) 'ium'
File "<stdin>", line 1
('un' * 3) 'ium'
^
SyntaxError: invalid syntax
</stdin></stdin>
~~~
If you want to concatenate variables or a variable and a literal, use +
:
>>>
~~~
>>> prefix + ‘thon’ ‘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[5] # character in position 5 ‘n’
~~~
Indices may also be negative numbers, to start counting from the right:
>>>
~~~
>>> word[-1] # last character ‘n’ >>> word[-2] # second-last character ‘o’ >>> word[-6] ‘P’
~~~
Note that since -0 is the same as 0, negative indices start from -1.
In addition to indexing, slicing is also supported. While indexing is used to obtain individual characters, slicingallows you to obtain substring:
>>>
~~~
>>> 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’
~~~
Slice indices have useful defaults; an omitted first index defaults to zero, an omitted second index defaults to the size of the string being sliced.
>>>
~~~
>>> word[:2] # character from the beginning to position 2 (excluded) ‘Py’ >>> word[4:] # characters from position 4 (included) to the end ‘on’ >>> word[-2:] # characters from the second-last (included) to the end ‘on’
~~~
One way to remember how slices work is to think of the indices as pointing between characters, with the left edge of the first character numbered 0. Then the right edge of the last character of a string of n characters has index n, for example:
\+---+---+---+---+---+---+ | P | y | t | h | o | n | +---+---+---+---+---+---+ 0 1 2 3 4 5 6 -6 -5 -4 -3 -2 -1
The first row of numbers gives the position of the indices 0…6 in the string; the second row gives the corresponding negative indices. The slice from i to j consists of all characters between the edges labeled i and j, respectively.
For non-negative indices, the length of a slice is the difference of the indices, if both are within bounds. For example, the length of word[1:3]
is 2.
Attempting to use an index that is too large will result in an error:
>>>
~~~
>>> word[42] # the word only has 6 characters Traceback (most recent call last): File “<stdin>", line 1, in <module>
IndexError: string index out of range
</module></stdin>
~~~
However, out of range slice indexes are handled gracefully when used for slicing:
>>>
~~~
>>> word[4:42] ‘on’ >>> word[42:] ‘’
~~~
Python strings cannot be changed — they are immutable. Therefore, assigning to an indexed position in the string results in an error:
>>>
~~~
>>> word[0] = ‘J’ Traceback (most recent call last): File “<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>> word[2:] = 'py'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
</module></stdin></module></stdin>
~~~
If you need a different string, you should create a new one:
>>>
~~~
>>> ‘J’ + word[1:] ‘Jython’ >>> word[:2] + ‘py’ ‘Pypy’
~~~
The built-in function len()
returns the length of a string:
>>>
~~~
>>> s = ‘supercalifragilisticexpialidocious’ >>> len(s) 34
~~~
What are raw string literals in Python?
In raw string literals, backslashes have no special meaning as an escape character. This is useful for writing strings that contain a lot of backslashes. Not having to escape each backslash makes the string more readable.
#notice the r prefixe indicates a raw string literal less\_caput = r'C:\Program Files\^%(x86)\Internet Explorer\iexplore.exe'
Can python strings be changed?
Python strings cannot be changed — they are immutable. Therefore, assigning to an indexed position in the string results in an error:
\>\>\> word[0] = 'J' Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'str' object does not support item assignment >>> word[2:] = 'py' Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'str' object does not support item assignment </module></stdin></module></stdin>
What is IMMUTABLE in Python?
An object with a fixed value. Immutable objects include numbers, strings and tuples. Such an object cannot be altered. A new object has to be created if a different value has to be stored. They play an important role in places where a constant hash value is needed, for example as a key in a dictionary.
What are lists in python?
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[-1] 25 >>> squares[-3:] # slicing returns a new list [9, 16, 25]
~~~
All slice operations return a new list containing the requested elements. This means that the following slice returns a new (shallow) copy of the list:
>>>
~~~
>>> squares[:] [1, 4, 9, 16, 25]
~~~
Lists also support operations like concatenation:
>>>
~~~
>>> squares + [36, 49, 64, 81, 100] [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 >>> 4 ** 3 # the cube of 4 is 64, not 65! 64 >>> 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 (we will see more about methods later):
>>>
~~~
>>> 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]
~~~
Assignment to slices is also possible, and this can even change the size of the list or clear it entirely:
>>>
~~~
>>> letters = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’] >>> letters [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’] >>> # replace some values >>> letters[2:5] = [‘C’, ‘D’, ‘E’] >>> letters [‘a’, ‘b’, ‘C’, ‘D’, ‘E’, ‘f’, ‘g’] >>> # now remove them >>> letters[2:5] = [] >>> letters [‘a’, ‘b’, ‘f’, ‘g’] >>> # clear the list by replacing all the elements with an empty list >>> letters[:] = [] >>> letters []
~~~
The built-in function len()
also applies to lists:
>>>
~~~
>>> letters = [‘a’, ‘b’, ‘c’, ‘d’] >>> len(letters) 4
~~~
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 [[‘a’, ‘b’, ‘c’], [1, 2, 3]] >>> x[0] [‘a’, ‘b’, ‘c’] >>> x[0][1] ‘b’
~~~