Multiple return statement Confusion

image

Im confused & please correct me if im wrong, in above code since header=true that means if header is there in the date set we are returning data[1:], which in actual should return data without header since header’s indexing is 0.

Yes, you are correct. The default values for file_name, header are set to AppleStore.csv, True respectively. If you invoke the function as open_dataset(), the default values will be set and data[1:] will be returned.
If you invoke the function as open_dataset('googleplaystore.csv', True), the data from googleplaystore will be returned, excluding the header.
If you are passing a file without header, you’ll have to pass False as the second parameter explicitly.

Hope it’s clear now.

Thanks for the reply, however return data [1:] should return data without header

@sheresd: if the header is present, we will need to return both the header and the rows of data separately, if its not there (i.e. explicit declaration of False as a parameter), it will only return the rows of data. We need 2 return statements in this case because there are 2 conditionals that may be met (the if and else in the case that the csv file has a header and if if there is no header). If the 1st return statement is omitted, it will return None for the header parameter. Hope this clarifies.

Thanks for the reply, however return data [1:] should return data without header

@sheresd: That is correct. In the case of both the header and data being present, both the if and else statements will get executed. That is, both the header and data will get returned separately so you will need to separate variables to store them. Hope this clarifies.

image

Hi
In above code how header=all_data[1] returns only header, since headers indexing is 0 the code should have been header=all_data[0]. Please clear my doubt.

@sheresd: the above code is correct. if there is a header, the data will get returned first followed by the data. If there is no header, only the data is returned. That being said, the data will always be at index position 0 which the header will be at index 1. If there is a header, the header information will be stored at all_data[1]. If not, the value of None will be stored at the same location.

Sorry to say but im not able digest…