![]() |
Readability issues - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Readability issues (/thread-41177.html) |
Readability issues - Mark17 - Nov-22-2023 What's the best way to separate out a line of code that runs past the right margin? For example: """#practicepython.org exercise #12: Write a program that takes a list of numbers (for example, a = [5, 10, 15, 20, 25]) and makes a new list of only the first and last elements of the given list. For practice, write this code inside a function. Concepts to practice: Lists and properties of lists, List comprehensions (maybe), Functions.""" from numpy import random list_len = random.randint(2, 100) unorg_list = [random.randint(0, 100) for i in range(list_len)] print(f'This unorganized list is {list_len} numbers long: {unorg_list}') org_list = list(set(unorg_list)) print(f'\nThe organized version (sorted and duplicates eliminated) of the same list is ',end='') print(f'{len(org_list)} numbers long: {org_list}')The last two lines I separated out and used end='' to avoid a line break. This works, but can't I somehow use a backslash and continue on the next line? My problem with doing it this way is that I sometimes get EOL errors or indentation errors. If there's a proper way to do this, then please let me know. The output is also a bit confusing: Actually, it looks fine here in the forum. In my JN, 84 and 14 print with the tens digit at the end of one line and the ones digit starting off the very next line. I'd rather it not split numbers like that.Thanks! RE: Readability issues - rob101 - Nov-22-2023 How about: org_list_msg = """ The organized version (sorted and duplicates eliminated) of the same list is""" print(f"{org_list_msg} {len(org_list)} numbers long: {org_list}")Or, if you don't want to see a list object, but rather the items therein: print(f"{org_list_msg} {len(org_list)} numbers long:", *org_list)Or, if you'd like to see the numbers as two digits long, so that the number take up the same about of space, when displayed e.g: 00 ... 99 :org_list = [f"{n:02d}" for n in org_list] print(f"{org_list_msg} {len(org_list)} numbers long:", *org_list)So, what I'm saying here, is that you can display data in whatever way you choose. |