Python 3.8.12 | packaged by conda-forge | (default, Oct 12 2021, 21:50:38)
Type "copyright", "credits" or "license" for more information.
IPython 7.29.0 -- An enhanced Interactive Python.
In [1]: 3*5
Out[1]: 15
In [2]: 3^5
Out[2]: 6
In [3]: 3**5
Out[3]: 243
In [4]: 51%5
Out[4]: 1
In [5]: 3
Out[5]: 3
In [6]: 51%3
Out[6]: 0
In [7]: aa=[[8,2],[42,42]] #list of lists - like a matrix
In [8]: aa+[4] # concatenates the two arrays
Out[8]: [[8, 2], [42, 42], 4]
In [9]: aa + 4
Traceback (most recent call last):
File "/var/folders/nj/tkk5x_7s3yn33fpszmyt7_d0p7__8q/T/ipykernel_12585/3168112346.py", line 1, in <module>
aa + 4
TypeError: can only concatenate list (not "int") to list
In [10]: cc = [j**2 for j in range(100)] # range(100) means 0 thru 99; list comprehension
In [11]: cc
Out[11]:
[0,
1,
4,
9,
16,
25,
36,
49,
64,
81,
100,
121,
144,
169,
196,
225,
256,
289,
324,
361,
400,
441,
484,
529,
576,
625,
676,
729,
784,
841,
900,
961,
1024,
1089,
1156,
1225,
1296,
1369,
1444,
1521,
1600,
1681,
1764,
1849,
1936,
2025,
2116,
2209,
2304,
2401,
2500,
2601,
2704,
2809,
2916,
3025,
3136,
3249,
3364,
3481,
3600,
3721,
3844,
3969,
4096,
4225,
4356,
4489,
4624,
4761,
4900,
5041,
5184,
5329,
5476,
5625,
5776,
5929,
6084,
6241,
6400,
6561,
6724,
6889,
7056,
7225,
7396,
7569,
7744,
7921,
8100,
8281,
8464,
8649,
8836,
9025,
9216,
9409,
9604,
9801]
In [12]: cc[0] # first element
Out[12]: 0
In [13]: aa[1] #second element (I mean row)
Out[13]: [42, 42]
In [14]: cc[-5:]
Out[14]: [9025, 9216, 9409, 9604, 9801]
In [15]: # last 5 elements of the list
In [16]: cc[90:]
Out[16]: [8100, 8281, 8464, 8649, 8836, 9025, 9216, 9409, 9604, 9801]
In [17]: cc[1:3]
Out[17]: [1, 4]
In [18]: cc[0:5]
Out[18]: [0, 1, 4, 9, 16]
In [19]: sum(cc)
Out[19]: 328350
In [20]: len(cc)
Out[20]: 100
In [21]: len(cc+cc+cc)
Out[21]: 300
In [22]: aa[1][0] means 2nd row, 1st column of our list of lists
File "/var/folders/nj/tkk5x_7s3yn33fpszmyt7_d0p7__8q/T/ipykernel_12585/401568039.py", line 1
aa[1][0] means 2nd row, 1st column of our list of lists
^
SyntaxError: invalid syntax
In [23]: aa[1][0] #means 2nd row, 1st column of our list of lists
Out[23]: 42
In [24]: def funTr(X):
...: if len(X)!=len(X[0]):
...: return "not a square matrix, dude"
...: else:
...: return sum([X[j][j] for j in range(len(X))])
...:
In [25]: # the if checks to make sure the #rows = #columns
In [26]: funTr(aa)
Out[26]: 50
In [27]: funTr(cc)
Traceback (most recent call last):
File "/var/folders/nj/tkk5x_7s3yn33fpszmyt7_d0p7__8q/T/ipykernel_12585/2722038734.py", line 1, in <module>
funTr(cc)
File "/var/folders/nj/tkk5x_7s3yn33fpszmyt7_d0p7__8q/T/ipykernel_12585/1446683790.py", line 2, in funTr
if len(X)!=len(X[0]):
TypeError: object of type 'int' has no len()
In [28]: bb=[[4,3,2,2],[3,2,2,1]]
In [29]: funTr(bb)
Out[29]: 'not a square matrix, dude'
In [30]: funTr(bb+bb)
Out[30]: 9
In [31]: runfile('/Users/richardson/Documents/2022S_na/python/classPy2022_01_20.py', wdir='/Users/richardson/Documents/2022S_na/python')
27
In [32]: runfile('/Users/richardson/Documents/2022S_na/python/classPy2022_01_20.py', wdir='/Users/richardson/Documents/2022S_na/python')
27
825
In [33]: