How does python know the usage of flag , rows_and_columns = False

Screen Link:
App Profile Recommendation | Dataquest

My Code:

def explore_data(dataset, start, end, rows_and_columns=False):
    dataset_slice = dataset[start:end]    
    for row in dataset_slice:
        print('\n') # adds a new (empty) line after each row

    if rows_and_columns:
        print('Number of rows:', len(dataset))
        print('Number of columns:', len(dataset[0]))

What I expected to happen:

The code is part of the example given. What I don’t understand is how does python know using the flag that headers are to be excluded ? It is not intuitive.

What actually happened:

No error. Just the doubt
What does this indicate "rows_and_columns = False" and how does python understand it without the code ? Secondly, if rows_and_columns= False, then how does it relate to "if rows_and columns" ( which is assumed to be "True") . Could I have excluded header without using the two booleans ?

since posting this, I read upon the concept of flags. I do think dataquest should mention it ! I understand now that flags indicate if a condition is met to run the code or not run it. My query still remains:

  1. when we initialize that rows and columns = False, which condition are we trying to meet ? Exclusion of header ?
  2. If it is exclusion of header, how did Python know if that’s what we are talking about ?
  3. When is flag important ? Can we usually code without it ?

this works equally well without a flag, so why the flag !!:

def explore_data(dataset, start, end):
dataset_slice = dataset[start:end]
for row in dataset_slice:
print(’\n’) # adds a new (empty) line after each row
print(‘Number of rows:’, len(dataset))
print(‘Number of columns:’, len(dataset[0]))

The condition is if we want to print the number of rows and columns in the dataset. It only adds that choice and can be useful to only print out things we want to display when we call thatt function.

No, it has nothing to do with the exclusion of header. It’s just a flag, if when True, will print out the number of rows and columns in the dataset.

It’s important if you don’t want to print that information out and that’s the extent of it.

The purpose here is not so much as “this has a very important reason so you need it”, but more about helping students get comfortable with how functions can be implemented and used.

For this project, you can code without it as per me. It’s use is under your control as well. Feel free to experiment with the function as you think might be necessary. But be mindful of those changes for any future instructions that ask you to call that function.

Understood, thanks a lot!

1 Like