2024-05-21_simple-python-tricks-2.html


001| #:name::Simple Python Tricks #2
002| #:link::https://thehardcorecoder.com/2024/05/21/simple-python-tricks-2/
003| #:date::2024-05-21
004| #:code::
005| Squares = []
006| 
007| for n in range(1,50):
008|     Squares.append(n*n)
009| 
010| print(Squares)
011| 
012| #:code::
013| Squares = [n*n for n in range(1,50)]
014| 
015| print(Squares)
016| 
017| #:code::
018| UpperCaseChars = [
019|     'A','B','C','D','E','F','G','H','I','J','K','L','M',
020|     'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
021| ]
022| 
023| #:code::
024| UpperCaseChars = [chr(ord('A')+cx) for cx in range(26)]
025| 
026| print(UpperCaseChars)
027| 
028| #:code::
029| alnums = [chr(cx) for cx in range(256) if chr(cx).isalnum()]
030| 
031| print(alnums)
032| 
033| #:code::
034| nums = [n for n in range(100) if (n%3)==0 if (n%5)==0]
035| 
036| print(nums)
037| 
038| #:code::
039| base = 4
040| pairs = [(x,y) for x in range(base) for y in range(base)]
041| 
042| print(pairs)
043| 
044| #:code::
045| base = 4
046| pairs = []
047| 
048| for x in range(base):
049|     for y in range(base):
050|         pairs.append((x,y))
051| 
052| print(pairs)
053| 
054| #:code::
055| base = 2
056| fives = [
057|     (x,y,z,w,u)
058|     for x in range(base)
059|         for y in range(base)
060|             for z in range(base)
061|                 for w in range(base)
062|                     for u in range(base)
063| ]
064| 
065| print(fives)
066| 
067| #:code::
068| board = [
069|     [1, 0, 0, 0, 0, 0, 0, 0],
070|     [0, 0, 0, 0, 0, 0, 0, 0],
071|     [0, 0, 0, 0, 0, 0, 0, 0],
072|     [0, 0, 0, 0, 0, 0, 0, 0],
073|     [0, 0, 0, 0, 0, 0, 0, 0],
074|     [0, 0, 0, 0, 0, 0, 0, 0],
075|     [0, 0, 0, 0, 0, 0, 0, 0],
076|     [0, 0, 0, 0, 0, 0, 0, 1],
077| ]
078| 
079| for row in board:
080|     print(row)
081| 
082| #:code::
083| board = [
084|     [0]*8,
085|     [0]*8,
086|     [0]*8,
087|     [0]*8,
088|     [0]*8,
089|     [0]*8,
090|     [0]*8,
091|     [0]*8,
092| ]
093| 
094| board[0][0] = 1
095| board[7][7] = 1
096| 
097| for row in board:
098|     print(row)
099| 
100| #:code::
101| board = [[0]*8]*8
102| 
103| board[0][0] = 1
104| board[7][7] = 1
105| 
106| for row in board:
107|     print(row)
108| 
109| #:code::
110| board = [[0 for _ in range(8)] for _ in range(8)]
111| 
112| board[0][0] = 1
113| board[7][7] = 1
114| 
115| for row in board:
116|     print(row)
117| 
118| #:code::
119| board = [[0]*8 for _ in range(8)]
120| 
121| board[0][0] = 1
122| board[7][7] = 1
123| 
124| for row in board:
125|     print(row)
126| 
127| #:code::
128| cell = lambda r,c: 1 if (r,c) in [(0,0),(7,7)] else 0
129| 
130| board = [[cell(r,c) for c in range(8)] for r in range(8)]
131| 
132| for row in board:
133|     print(row)
134| 
135| #:code::
136| matrix = [[(x,y) for y in range(3)] for x in range(4)]
137| 
138| matrix[0][0] = (8, 8)
139| matrix[3][2] = (9, 9)
140| 
141| # Output with x=row, y=col...
142| for iy,ys in enumerate(matrix):
143|     print(f'{iy+1}: {ys}')
144| print()
145| 
146| # Output with x=col, y=row...
147| for iy in range(3):
148|     row = [matrix[ix][iy] for ix in range(4)]
149|     print(f'{iy+1}: {row}')
150| print()
151| 
152| #:code::
153| size = 3
154| 
155| board = [[[[ (x,y,z,w)
156|     for x in range(size)]
157|         for y in range(size)]
158|             for z in range(size)]
159|                 for w in range(size)]
160| 
161| def print_board ():
162|     for wx,ws in enumerate(board):
163|         print(f'W: {wx}')
164|         for zx,zs in enumerate(ws):
165|             print(f'Z: {zx}')
166|             for yx,ys in enumerate(zs):
167|                 print(f'y: {yx} {ys!s}')
168|             print()
169|         print()
170|     print()
171| print()
172| 
173| print_board()
174| 
175| #:end:code::
176|