Python Tutorial - How to make Text-Based Tables

preview_player
Показать описание
(See below for better code)
A note from 2022: ok so notice how I'm 15 in this video, and how i'm clearly not taking the video seriously; these are indicators that the code is going to be low quality too;)
= Why is it bad code? =
- The header row is hard-coded (i.e. there's one string that represents the whole header row, rather than one string for each column name)
- The lengths of each header row is hard coded (e.g. the numbers 9, 13, 4 in the big print line). This is bad because if we ever need to change the column names, we'll also need to update these, and the relationship between 'Subject Chosen' and 13 is a bit unclear.
- The number of columns themselves is hard-coded. Better code would be able to handle more columns automatically.
- The table just looks a bit ugly...?
- Also why are the entries left-aligned instead of right-aligned?! that looks silly for the column with numbers in.
- It's not in a function, so it can't be reused.
- Using camel case for variable names (i.e. namingYourVariablesLikeThis) is a bit ugly too...

= Updated code =
def print_table(nested_list, column_names):
num_cols = len(column_names)
header_row = (
'| '
+ ' | '.join(column_names)
+ ' |'
)
nice_horizontal_rule = ('|'+'-' * (len(header_row)-2)+'|')
print(nice_horizontal_rule)
print(header_row)
print(nice_horizontal_rule)

for item in nested_list:
# writing each row to a string,
# then printing the string, is better for performance:)
s = "|"
for i in range(num_cols):
entry = str(item[i])
s += (" "*(len(column_names[i]) - len(entry)+2) +
entry + "|")
print(s)
print(nice_horizontal_rule)

my_nested_list = [
["Francis", "English", 435],
["Larry","Maths", 234],
["Nicole", "Biology", 986],
["Joey", "Physics", 562],
["Sam", "Computing", 12],
]

my_column_names = ["First Name", "Subject Chosen", "Score"]

print_table(my_nested_list, my_column_names)
Рекомендации по теме
Комментарии
Автор

Finally somebody actually made a list using base python functionality instead of trying to import modules. Thanks for the vid, was the only helpful thing I've found all day

dmnck
Автор

thank you for not importing random modules into this, what I was looking for exactly. Great work!

KeanuReevolution
Автор

thank you soooo much for your video! I was trying to find how to do this kind of table for a long time) Couldn't get it before, but you made it so clear) Thank you a lot!

cookie_
Автор

Thanks, learning Python for school and can't find anything else that doesn't import modules, and of course my professor didn't show us how to make tables before giving the assignment. Appreciate it a lot!

jasmine.
Автор

For people still looking at this, there is a much more efficient way to do this

for item in nestedlist:
print('| %-11s | %-14s | %-5s |'%tuple(item))

There are other ways to do this if you want to use the new .format() method instead.

josh___
Автор

Great video. I've been working with dictionaries trying to get this sort of functionality, but no more!

jurdendurden
Автор

Amazing video. Found it really useful for my IGCSE´s controlled assessment

davidyang
Автор

let we want to make a function for displaying table and print the rows dynamically and *if the length of next item is bigger than the previous one. how will be setup how many spaces to print?*

adviksingh
Автор

Very good and I took some things from it... the only issue is that by converting the numbers to a string they are then left justified rather than right justified as numbers would be.

tomwasser
Автор

I'm just learning Python now, and this stuff is identical to what i need to accomplish in chapter 6 of Automate the Boring stuff in Python. appreciate it a lot. The book didn't even cover an example close to this and expected it. Do you want to be a mentor ? lol (actually not kidding) Great vid fine sir

paulcoffee
Автор

Can you help me as I need set up a numerical tables in Python from 1-49.

Cheers

mralimorley
Автор

Holy shit thank you so much for this video. Helped me tremendously with a programming project for school.

wubologist
Автор

thanks mate for this video...but i'm getting error as ' list index out of range '. what might be the problem?

nehmaheshnair
Автор

Can somebody tell me how I get that dotted line in print statement

rajeevjadon
Автор

instead of using index like [0], [1] ... how can we use a loop to do that? In other words, if we have a long list what would be another way to do this?

moeal
Автор

thanks a lot, it surely is very helpful for me

nrcsalluop
Автор

can you do this but with user-inputs as items?

incognito
Автор

Help alot thanxx dude, keep up the good work!!

ktmyname
Автор

does anyone know what the code would look like if i used a text file isntead of inputting the data on python

nahmir
Автор

hi, can i ask, if i were to have new students to add to the list, what should i do?

janlyn