Four sides of square: Difference between revisions

→‎Procedural: added another version showing interesting Python features
m (→‎Procedural: fix naming; remove unnecessary parentheses)
(→‎Procedural: added another version showing interesting Python features)
Line 477:
print("0", end=" ")
print()</lang>
 
{{out}}
See Raku output.
 
 
The following version illustrates several features of Python, such as default arguments, nested functions with lexical scoping, generators, and convenient syntax for creating sets and performing set operations such as intersection.
 
<lang python>def square(size=9):
 
def is_at_border(row, col):
# `&` is set intersection: if the set {row, col} intersects the set
# {0, size-1}, then at least one of row, col is either 0 or size-1
return {row, col} & {0, size-1}
 
for row in range(size):
print(' '.join(
'1' if is_at_border(row, col) else '0'
for col in range(size)
))
 
suqare()
</lang>
 
===Functional===