Python Intermediate 5/12 quick question

Screen Link:

Hi guys, in 5/12 of Python Functions Intermediate, I don’t understand why the clause that says:
if header:
data = data[1:]
return data
return data

Specifically, why is “if header” enough? Why can’t it be “if header = True” which makes more sense to me. Full code of the excercise can be seen below. Thanks.

Your Code:```# INITIAL CODE
def open_dataset(file_name=‘AppleStore.csv’,header = True):
opened_file = open(file_name)
from csv import reader
read_file = reader(opened_file)
data = list(read_file)

if header:
    data = data[1:]
    return data
    return data

apps_data = open_dataset(‘AppleStore.csv’)```

both statements are correct i.e.
if header == True
is the same as
if header

Let’s put the variable value in the code and take a look.

  1. When header = True
    if True:
    data = data[1:]
    return data
    And script goes into if section.

  2. When header = False
    if False:
    data = data[1:]
    return data
    and script will not go into if, just straight to else clause

Thanks, I was looking this up as well but still a bit confused. It seems “header=True/False” is a built-in attribute (if that’s the term) of a list object.
Otherwise, how does it know what a header is?

Thanks for your time

When you define function in this excercise you pass ‘header’ parameter that by default has ‘True’ assigned (so it becomes bool).
def open_dataset(file_name=‘AppleStore.csv’,header = True)

Then when you call function
open_dataset(), without parameters it opens AppleStore.csv with header as True. When executing function it goes to if clause becasue header = True

If you called the function specifing parameters
open_dataset(header=False) that would make the header = False and hence skip the if part within function, just go to else clause.


(I think) I have the same question here.

How can the computer know what the ‘header’ is unless it is defined? Is a ‘header’ a property that is generally built into csv files (and therefore one that would be known to the computer without definition)?

I can see why in the ‘if’ statement, we can just put ‘header’ (as ‘header’ is previously assigned to be ‘true’, so I believe this really means ‘if true’).
But how can the computer know what a header is (and therefore whether it can be ‘true’ or ‘false’) if a ‘header’ is not defined?



in this case header is just a function parameter that is by default set as True:

def open_dataset(file_name=‘AppleStore.csv’,header = True)


Ah ok I’ve got it now.

Thank you! :slight_smile:

I’m also thinking this lesson and explanation could be better presented.

In the solution code, the default argument

header = True)

to then be called with the conditional
if header:

It works sufficiently well without naming the variable “header”. It could be anything such as
i = True)
and then later
if i:

Which means this function only really ‘works’ as intended if we know that the dataset does indeed contain a header row. It’s not the case that the function actually evaluates the first row and determines that it is a header, so then ‘decides’ to return the second row onward in based on that conditional.