Python Fundamentals: Guided Project

Hello there :slight_smile:

In this project there was already written a function to visualize the dataset.
May I ask: What exactly happens in the program when we write:

(rows_and_columns=False)

and further

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

What I don’t understand is how the programm realize what we mean with “rows_and_columns”, we haven’t predefined it. And how the “Conditional Statement” helps the programm to understand what we mean.

Thank you in advance.

Cheers, Horigome

Here’s the function:

1 Like

Hi @Horigome. In Python you can define a function that takes any arguments. You can also set a default value for the argument. Python doesn’t know what the words mean – it only knows to expect something to be there as an input. In the body of the code, we tell Python what to do with those inputs. If we put in something it didn’t expect, or if it can’t be used in the way we wrote in the body of the function, then Python will give an error.

Let’s have a look at the explore_data() function in more detail.

def explore_data(dataset, start, end, rows_and_columns=False):

Because of how we’re defining the function, anything we put in the dataset slot can be refered to as dataset in the body of the function. Likewise for start, end, and rows_and_columns. So when we call the function with, say, explore_data(android_apps, 0, 4), we are telling Python these 4 things:

  • dataset = android_apps
  • start = 0
  • end = 4
  • rows_and_columns = False (because that was the default in the function definition)

Then with that information, Python replaces the variables in the function with their values from the input. As long as we, the people using the function, are using it properly, Python will happily process it.

The if-statement if rows_and_columns: means the same thing as if rows_and_columns = True. This part of the code will only be run if we add True to our function input (such as explore_data(android_apps, 0, 4, True). If we don’t enter anything, then automatically this will be false and the rest of the code won’t be processed (it won’t print the number of rows and columns of your dataset). We want to know ahead of time if we want it to print the number of rows and columns so we know whether to add the True or not.

I hope that makes it a little clearer for you. Let me know if anything else is confusing and I’ll try my best to help.

2 Likes

Hi @april.g.
Yeah, I got it. Many thanks :wink:
I know I paid for it but the time it takes you moderators to answer and the clarity of the responses are just phenomenal.
I really appreciate.

1 Like

I’m just a volunteer and a fellow student! It helps me to help you. :grin: Good luck on the rest of your project.

3 Likes

:grin: :+1:t3: Then keep doing it, cauz I keep posting questions.

2 Likes