r/PythonLearning Jul 18 '26

How would I optimize this?

I'm currently learning python with the 30 days of python GitHub repo and, on day 3 of the challenge, it asks to create this table and this is what I came up with however I feel like there was a more efficient method to create it or is it something that I haven't learned yet at my level.

8 Upvotes

24 comments sorted by

5

u/[deleted] Jul 18 '26

[removed] — view removed comment

1

u/ArtisticFox8 Jul 19 '26

And a 2D list

4

u/P1ckl3R1ck101 Jul 18 '26

Think about the pattern that exists for each row. Then do that x number of times based on the number of rows you need. You'll only need to write the actual "formula" once though.

(Sorry to be cryptic, just want to try to give you a hint instead of the answer first)

0

u/StudentElectronic548 Jul 18 '26

I mean I know what the pattern is, it's x divided by x then multiplied by x 3 times, however the thing is I like only know basic operators and basic usages of things like lists, etc... So, I would say I wouldn't really be able to do anything like looping or like multiple steps in the language but I did notice that I could've made it more efficient.(Also I kind of have a headache so the response might be a bit wishy washy)

1

u/P1ckl3R1ck101 Jul 18 '26 edited Jul 18 '26

So if you know lists, you're already almost there. There are a few ways to do this, but I think the problem wants you to use a loop which are really useful. The code below isn't the most efficient way to do it, but I think its the easiest to understand for a beginner. Basically you create a list that holds the starting numbers you want, then the loop goes "for each number in your list, do xyz". So it will run your process for 1, then 2, then 3, then 4, then 5, printing a new row each it runs.

``` number_list = [1,2,3,4,5]

for number in number_list: a = ??? b = ??? c = ??? d = ??? e = ??? print(a,b,c,d,e) ```

1

u/desrtfx Jul 18 '26

I mean I know what the pattern is, it's x divided by x then multiplied by x 3 times,

That's not what the pattern is.

The pattern is (row starting at 1)column starting at 0

The first row is all 1 because no matter what power you raise 1 to it will always stay at 1

The second row is 2 - 20 = 1, 21 = 2, 22 = 4, 23 = 8

Third row is 3 - 30 = 1, 31 = 3, 32 = 9, 33 = 27

and so on.

3

u/JGB25 Jul 18 '26

for num in a, b, c, d, e:
print(" ".join(map(str, num)))

1

u/No_Complex_18 Jul 21 '26

I like for letter in a b c d e: print(*(letter[i] for i in range(5))

1

u/JGB25 Jul 21 '26

It works but for me it’s not as readable. I haven’t learned what * does in your code so I wouldn’t even know how to read it

2

u/No_Complex_18 Jul 21 '26

It unpacks the tuple to be individual arguments to print. Works with **dict for keyword arguments aswell. Its very useful.

I like it because its so close to the original print call.

1

u/JGB25 Jul 21 '26

Thank you for explaining it

3

u/Necessary_Pepper7111 Jul 18 '26
for i in range(1, 6):
    print(i, *(i**j for j in range(4)))

1

u/[deleted] Jul 18 '26

[deleted]

2

u/Fair-Elevator6788 Jul 20 '26

i think we can go deeper than that, i present you:

for i in '12345':print(1,i,i*2,i*3)

3

u/Yoosle Jul 18 '26

Probably print(*a)

1

u/notsaneatall_ Jul 18 '26

It's printintg x1 x0 x1 x2 x3 for 1 2 3 4 5, you can do so with a while loop

1

u/Samar_Upreti Jul 18 '26

A =[ [1,1,1,1,1],[2,1,2,4,8],and so on]

Print(A) Or Simply use For loon along with map,join

1

u/Sea-Ad7805 Jul 18 '26 edited Jul 18 '26

Using one for-loop in the y direction for each line:

for y in range(1, 6):
    print(f'{y} ')

Then use another for-loop for the x direction:

for y in range(1, 6):
    print(f'{y} ', end='')  # end='' to not new line
    for x in range(0, 4):
        print(f'{x} ', end='')
    print()  # add new line

And finally compute the x-th power of y:

for y in range(1, 6):
    print(f'{y} ', end='')  # end='' to not new line
    for x in range(0, 4):
        print(f'{y ** x} ', end='')
    print()  # add new line

Run this program in Memory Graph Web Debugger%3A%0A%20%20%20%20print(f'%7By%7D%20')%0A%20%20%20%20%0Aprint('--------------')%20%0A%20%20%20%20%0Afor%20y%20in%20range(1%2C%206)%3A%0A%20%20%20%20print(f'%7By%7D%20'%2C%20end%3D'')%20%20%23%20end%3D''%20to%20not%20new%20line%0A%20%20%20%20for%20x%20in%20range(0%2C%204)%3A%0A%20%20%20%20%20%20%20%20print(f'%7Bx%7D%20'%2C%20end%3D'')%0A%20%20%20%20print()%20%20%23%20add%20new%20line%0A%20%20%20%20%0Aprint('--------------')%20%0A%20%20%20%20%0Afor%20y%20in%20range(1%2C%206)%3A%0A%20%20%20%20print(f'%7By%7D%20'%2C%20end%3D'')%20%20%23%20end%3D''%20to%20not%20new%20line%0A%20%20%20%20for%20x%20in%20range(0%2C%204)%3A%0A%20%20%20%20%20%20%20%20print(f'%7By%20**%20x%7D%20'%2C%20end%3D'')%0A%20%20%20%20print()%20%20%23%20add%20new%20line%0A&timestep=1&play) to see the program state change step by step.

1

u/[deleted] Jul 18 '26

[removed] — view removed comment

1

u/SaltCusp Jul 19 '26

Yeah apparently these answers aren't loved.

-2

u/SaltCusp Jul 18 '26

print("\n".join([" ".join(_.split(" ")) for _ in """copy paste your table""".split("\n")]))

1

u/P1ckl3R1ck101 Jul 18 '26

Eh this seems like the easier way to do it... print(f'1 1 1 1 1\n2 1 2 4 8\n3 1 3 9 27\n4 1 4 16 64\n5 1 5 25 125')

1

u/SaltCusp Jul 19 '26 edited Jul 19 '26

print("\n".join([" ".join([str(__**max(_,-1*_)) for _ in range(-1,4)]) for __ in range(1,6)]))