0
Good night! Sunday I discovered that I must learn Python, that the subject I will attend will be in that language and there is no way out. I studied well this week and managed to get to matrices, but I came across subtleties that I am still trying to understand. One of the exercises I did today asked me to do a function that returned if a given object belonged to a list or not, 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, as I have as language "mother" C, (if I don’t do a function that traverses a list (vector), I even have an itch), came almost automatic imagine a loop that traverses a list and compares 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), simpler, objective, fulfilled what the exercise asked, but I found in a way, very simple and even obscure, thinking of a huge code I understand how much more useful the first one would be. I do not know if with C I have learned that a good code, consistent, needs to be robust, hard, with kkk pointers. By analogy between English and Portuguese, I feel that if the requirement to learn another language was just vocabulary, it would be enough to memorize the words, but that’s not the case, there are epistemological subtleties between describing the same object in two languages, even if the logic is the same to describe. I’d like your opinion on.
Nor will I use the answer space because, as you say, at the end of the question, the answers will be based on opinion and I’d rather not risk being outside the scope of the site, but my opinion is that, contrary to what you think, the first one is much more elegant and, most important, most pythonica.
– Sidon
And yes, this is not an opinion, in the first function the list is, tb, "traversed" to find the element (pq go through all?). If you think the first one is inelegant pq is very simple, I suggest you open a Pyton terminal and type:
import this <enter>
, read very carefully and fall in love with python. :-)– Sidon
I believe that understanding if, for, while, basic functions and variables you will already have the basics, comparing both languages you will not get anywhere, what you need to learn (as much as you believe you already know) is programming logic and the python language, compare with C will only get you more upset.
– Guilherme Nascimento
@Sidon I think I expressed myself wrong to classify as inelegant, deep down I wanted to say that it is obscure, like, what is the execution time of this call? Does it behave the same way with ordered lists? I still do not know the functioning of these intrinsic "functions", I will follow your advice and read what you proposed.
– Marcelo de Sousa
@Guilhermenascimento As my only reference in programming is C, I think it was kind of inevitable not to compare, so I tried to raise this question already to avoid thinking that C is better than Python and vice versa, I think that this question doesn’t even make sense. was a shock to find this persistence call where I don’t need to write a code snippet that does something exhaustively.
– Marcelo de Sousa
Gostaria da opinião de vocês sobre.
No. We are morally obligated to close every question that asks opinions.– Oralista de Sistemas