Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python - getting 'none' at the end of printing a list [duplicate]

newbie here. can someone please explain to me why 'none' is printed at the end of this code, but only when called inside a function?

background: I have a variable (share_data) which contains some lists:

share_data = 
[['Date', 'Ticker', 'Company', 'Mkt Cap', 'VM Rank', 'Value Rank', 'Momentum Rank'],  
['2016-08-27', 'BEZ', 'Beazley', '2,063', '89', '72', '76'], 
['2016-08-30', 'BEZ', 'Beazley', '2,063', '89', '72', '76'], 
['2016-08-31', 'BEZ', 'Beazley', '2,050', '89', '72', '75'], 
['2016-09-01', 'BEZ', 'Beazley', '2,039', '96', '73', '93'], 
['2016-09-02', 'BEZ', 'Beazley', '2,069', '90', '72', '77'], 
['2016-09-03', 'BEZ', 'Beazley', '2,120', '96', '70', '94'], 
['2016-09-06', 'BEZ', 'Beazley', '2,106', '90', '71', '77'], 
['2016-09-07', 'BEZ', 'Beazley', '2,085', '89', '71', '76'], 
['2016-09-08', 'BEZ', 'Beazley', '2,091', '89', '72', '77'], 
['2016-09-09', 'BEZ', 'Beazley', '2,114', '89', '71', '77'], 
['2016-09-10', 'BEZ', 'Beazley', '2,084', '94', '71', '89'], 
['2016-09-12', 'BEZ', 'Beazley', '2,084', '94', '71', '89']]

I am interested in printing the last 5 lines.

If I use this in the main program:

for row in share_data[-5:]:
    print(row)

I get the correct data:

['2016-09-07', 'BEZ', 'Beazley', '2,085', '89', '71', '76']
['2016-09-08', 'BEZ', 'Beazley', '2,091', '89', '72', '77']
['2016-09-09', 'BEZ', 'Beazley', '2,114', '89', '71', '77']
['2016-09-10', 'BEZ', 'Beazley', '2,084', '94', '71', '89']
['2016-09-12', 'BEZ', 'Beazley', '2,084', '94', '71', '89']

...however when I created a function to do this:

def share_details(share_data, n=5):
    ''' Prints the last n rows of a share's records'''
    for row in share_data[-n:]:
        print(row)
    return

and called the function this way:

print(share_details(share_data))

...what I get is this (note the 'None' at the end):

['2016-09-07', 'BEZ', 'Beazley', '2,085', '89', '71', '76']
['2016-09-08', 'BEZ', 'Beazley', '2,091', '89', '72', '77']
['2016-09-09', 'BEZ', 'Beazley', '2,114', '89', '71', '77']
['2016-09-10', 'BEZ', 'Beazley', '2,084', '94', '71', '89']
['2016-09-12', 'BEZ', 'Beazley', '2,084', '94', '71', '89']
None

I think it's the 'return' statement at the end of the function that triggers it, but don't know how/why.

EDIT - now that's it's clear what my error was (ie. printing inside the function, and then also the return value outside) can I follow up with an additional question? Is it good practice to delegate all the printing to a function? Maybe a function called, for extra readability:

print_share_details(share_data)

Or there is a better way which is more readable/pythonic?

like image 907
Patrick Avatar asked Dec 28 '25 00:12

Patrick


1 Answers

Every function in Python returns something, with a default return value of None. So

print(share_details(share_data))

calls share_details, share_details prints the last 5 rows of share_data, share_details returns None (by default), then print prints that return value.

like image 174
Craig Burgler Avatar answered Dec 30 '25 12:12

Craig Burgler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!