Kernel: Python 2 (SageMath)
Issue 1: Referencing the counter of a loop
This is problematic. Each time the loop is completed the variable reverts to its value as a counter.
In [3]:
Out[3]:
0
100
1
100
2
100
3
100
4
100
Issue 2: functions that do not return anything
You can create functions which do not return anything:
In [12]:
In [13]:
Out[13]:
5
In [14]:
In [15]:
Out[15]:
None
In [19]:
In [21]:
Out[21]:
None
What happened here?
In [2]:
In [4]:
Out[4]:
[1, 2]
None
The reverse method for lists reverses the list in-place. It does not return any value.
Lists and generators:
A binary list if length n is a list of length n, all of whose elements are 0 or 1. Recursion is a convenient method to generate a list of all such strings:
In [17]:
In [19]:
Out[19]:
[[0, 0, 0],
[0, 0, 1],
[0, 1, 0],
[0, 1, 1],
[1, 0, 0],
[1, 0, 1],
[1, 1, 0],
[1, 1, 1]]
We now look at a generator for such lists:
In [24]:
In [25]:
Out[25]:
[[0, 0, 0, 0],
[0, 0, 0, 1],
[0, 0, 1, 0],
[0, 0, 1, 1],
[0, 1, 0, 0],
[0, 1, 0, 1],
[0, 1, 1, 0],
[0, 1, 1, 1],
[1, 0, 0, 0],
[1, 0, 0, 1],
[1, 0, 1, 0],
[1, 0, 1, 1],
[1, 1, 0, 0],
[1, 1, 0, 1],
[1, 1, 1, 0],
[1, 1, 1, 1]]
In [35]:
In [36]:
Out[36]:
{0: 1,
1: 20,
2: 190,
3: 1140,
4: 4845,
5: 15504,
6: 38760,
7: 77520,
8: 125970,
9: 167960,
10: 184756,
11: 167960,
12: 125970,
13: 77520,
14: 38760,
15: 15504,
16: 4845,
17: 1140,
18: 190,
19: 20,
20: 1}
In [37]:
In [38]:
Out[38]:
{0: 1,
1: 20,
2: 190,
3: 1140,
4: 4845,
5: 15504,
6: 38760,
7: 77520,
8: 125970,
9: 167960,
10: 184756,
11: 167960,
12: 125970,
13: 77520,
14: 38760,
15: 15504,
16: 4845,
17: 1140,
18: 190,
19: 20,
20: 1}
Generating all the square numbers
In [47]:
Find the smallest square number that is greater than n
In [45]:
In [46]:
Out[46]:
4
In [ ]: