Subtleties between Python and C [closed]

0

Good evening! Sunday I discovered that I will have to learn python, that the subject that I will study will be in this language and can not escape. I studied well this week and managed to get to matrices, but I came across subtleties that I'm still trying to understand. One of the exercises I did today asked me to do a function that would return if a given object belonged to a list, I did "two" functions that follow:

def Pertence1 (item, lista):
    return item in lista

def Pertence2 (item, lista):
    for i in range(len(lista)):
        if (item == lista[i]): return True
    return False

This second function was the first one I thought of, as I have as "mother" language C, (if I do not do a function that goes through a list (vector), I even have itching), it almost came automatically to imagine a loop that runs through a list and compare item by item until I find, in a second moment came the first function, which I feel is not exactly a "function" (I am still understanding this concept in python), more simple, objective, fulfilled what the exercise requested, I found in a way, very simple and even obscure, thinking of an immense code, I understand how useful the first one would be. I do not know if with C I learned that a good code, consistent, needs to be robust, hard, with kkk pointers. By making an analogy between English and Portuguese, I feel that if the requirement to learn another language was just vocabulary, it would be enough to decorate the words and that's it, but there is epistemological subtlety between describing the same object in two languages, even though logic is the same to describe. I would like your opinion on.

    
asked by anonymous 10.08.2017 / 03:25

1 answer

4

What you describe is what we call the idiomatic code.

What is "idiom expression" in programming?

Obviously there will be several ways to create a function that returns the desired result, but not all of them will be idiomatic; that is, not all will apply the philosophy that language preaches. In C, it makes sense to have to scroll through the list until you find the desired value; it was designed to be this way and works very well. Python, in turn, preaches that the code should read the most readable to humans; you write your program almost as if you were writing a text. There is no way to compare solutions in different languages, let alone choose which one is best. Also, in Python, the idiomatic solutions we call pythonic .

What is a "pythonic" code?

Now, by answering your question, for sure, in Python, the first solution makes a lot more sense than the second. Simple is better than complex. If there is a simple way, why complicate? And I still say that if it was not as an exercise proposal, it would not make sense for you to define a function to execute this logic. It would be much simpler and readable to directly use the value in list condition than to need to call a function for it. Readability counts.

    
10.08.2017 / 05:23