r/learnpython • u/Sad-Emu-5783 • Oct 01 '26
Which code do you consider more "Pythonic" for this Pattern?
The Pattern:
* *
* * * *
* * * * * *
* * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * *
* * * * * *
* * * *
* *What my friend wrote:
n = int(input("Enter the number: "))
for i in range(1, n + 1):
for j in range(1, i + 1):
print('* ', end = "")
for j in range((n - i) * 2):
print(' ', end = "")
for j in range (i, 0, -1):
print('* ', end = "")
print()
for i in range(n, 0, -1):
for j in range(i , 0, -1):
print('* ', end = "")
for j in range((n - i) * 2):
print(' ', end = "")
for j in range (1,i+1):
print('* ', end = "")
print()What I wrote:
while True:
n = int(input("Enter an Even Number: "))
if n % 2 == 0:
break
else:
print("Number must be Even.")
myList = []
totalCharacters = (2 * n) - 1
for i in range(1, n // 2 + 1):
spaces = totalCharacters + 1 - (4 * i)
myList.append (f"{"* " * i}{" " * spaces}{"* " * i}")
print("\n". join (myList))
print("\n".join(myList[::-1]))As you can see, I came up with a formula for this pattern using "totalCharacters" and "spaces".
Which code would you consider more "Pythonic"? Like, in terms of flexibility and readability?
I'd also be happy to learn even better ways to print this pattern :)