The logic of arguments and parameters


#1

I hope I'm not infringing any rule by posting a question not related specifically to any CodeCademy lesson byt Python's syntax.

During my learning I noticed some loops and functions taking arguments or parameters that have never been defined anywhere else in the code. Yet Python understands exactly what data they refer to.

Let's take for example the supermarket lesson.
In Keeping track of your produce we have this bit of code to figure out :

for key in prices:
    print key
    print "price: %s" % prices[key]
    print "stock: %s" % stock[key]

I get that key would easily be interpreted as the key of that dictionary. Yet I can't understand how Python figures out what we mean by prices. Especially when it seems very meticulous on defining values and variables.

Another example, very similar but this time, it's a whole function that takes a paremeter never defined anywhere else and then assign it to a for loop function inside of it:

def compute_bill(food):
    total = 0
    for item in food:
        if stock[item] >0:
            total += prices[item] 
            stock[item] -=1
            return total

I feel that I'm not yet understanding the mechanism of a parameter and I would love if someone would explain it to me or point me to some reading material on the subject.


#2

prices is the name of our dictionary, as defined at the top of our code.

With the compute_bill function, the name of the argument isn't something we've defined, just a placeholder that let's you know what kind of data might be passed to it. In that lesson, the compute_bill function was passed our list called shopping_list.


#3

@netrunner10450: That's a good question and you're not infringing any Codecademy rules, the topic is related to Python and a lesson 10. A Day at the Supermarket .

Let's go!

In Python, whenever we want to iterate over a list/dictionary/tuple/etc, we first need to declare and, obviously, fill those structures before using them.
In this example, prices is one of those structures, I think it's a list, might be wrong tho, so Python already know it exists.

for key in prices:
    print key
    print "price: %s" % prices[key]
    print "stock: %s" % stock[key]

In case you didn't declare it, you'd get a syntax error, like this one:

But in case you declared it, you'll see this:

Edit: it was a dictionary, not a list that OP was mentioning about! So just ignore the error, otherwise I'll explain it later.

Note: don't mind the sad face and error, we'll get to that soon!

So yeah, Python knows how your loop works because it goes by this way:

for iterableVariable in what_I_will_iterate:

Python knows the elements in the left side of in are the "variables" it will use to iterate over the structure in the right side of in.

def compute_bill(food):
    total = 0
    for item in food:
        if stock[item] >0:
            total += prices[item] 
            stock[item] -=1
            return total

When we are talking about arguments and Python, we don't have to worry about declaration.
As long as you are either using one of your arguments, or all of them, or using a variable you've declared in your global scope or in your local scope, everything will be just fine.

I haven't found something that goes into the syntax and how for loops are verified in Python, sorry!

Now back to the sad face:
Why the sad face? Because I thought it was a list, not a dictionary, my bad! :sweat_smile:


#4

Thank you so much. As you and @zh30 mentioned, in the prices' case, the parameter is actually a wholly defined variable in which we are going to use a loop to ouput data conditionally.

But now I understand something important :

for some_kind_of_cursor in some_range_of_data
    do this

So if I understand correctly : Python lets you use any argument in a function as long as you explain its nature in subsequent instructions inside that function ?

For example :

def compute_bill(food):
    total = 0
    for item in food:
        if stock[item] >0:
            total += prices[item] 
            stock[item] -=1
            return total

We didn't define food, before, and we could've used any word we wanted to (though food is contextual in this instance) but Python knew what to do with it since we hinted what it is in the subsequent for loop, right ?


#5

@netrunner10450: Yes, as long as you don't use some specific words as arguments, like for , while, etc, and follow the rules about your variable's name (no special characters and etc).
I think whenever we are working with for loops, the compiler, or whatever, will check if the element we want to iterate over it .. can be iterated!

Because, how can you iterate over an integer without using the range function?

Hope things are clear now! :sunny:


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.