1
class Person:
    def __ini‎t__(sel‎f, age):
        self._age = age

    @prop‎‎er‎t‎y
    def age(self):
        return self._a‎g‎e

person‎s = [Pe‎r‎son(20), Pe‎‎r‎s‎o‎n(30), Person‎(19), Pers‎on(17), Per‎son(15), Per‎‎son(25)]  

result = persons[0]

for person in persons:
    if person.age < result.age:
        result = person

Can someone please explain to me, what happens in this for loop? As far as I understand it checks, if the input of age is smaller than the result and if so, it "puts" it into the result variable. The solution states 15 but how do we get there?

8
  • 1
    I don't understand what is unclear to you. The code literally says what it's doing. You have already described it correctly. Commented Apr 15, 2022 at 19:35
  • Just to say you could achieve this with min(persons, key=attrgetter('age')) Commented Apr 15, 2022 at 19:35
  • Did you expect a different result? Why? Commented Apr 15, 2022 at 19:36
  • result is just a name for an object. The name result is assigned to the Person object with the lowest age attribute. Commented Apr 15, 2022 at 19:37
  • I just don't know why its 15...I mean if the code iterates through persons, the first parameter already fullfills the for-loop, so why is the result not "20"? Commented Apr 15, 2022 at 19:41

2 Answers 2

2

The loop cycles though the persons list and then compares their age Person.age to the Person's age in results result.age. The result at the start is set to persons[0] or Person(20), if it finds someone who is younger aka person.age < result.age the result becomes the "person". So the loop is finding the youngest person in the list.

Sign up to request clarification or add additional context in comments.

2 Comments

Ahhh...thats it...I didn't follow through on the logic...sure..so it compares every array and only stores the smalles age in the variable result. Alright, got it! Thanks a lot to all of you
list. There is no array here, it is a list
0

For things like this it is sometimes easier to step through the problem by writing out what is happening.

So we know we're using a list of Person objects, which have an assigned age that we can retrieve.

Starting the loop if we plug in what we have:

result = persons[0] # first person in the list, Person.age = 20

for person in persons:
   if person.age < result.age:
       result = person.age

Iteration 1:

result = 20
if 20 < 20: # this is false
    result = 20

Iteration 2:

result = 20
if 30 < 20: # this is false
    result = 30

Iteration 3:

result = 20
if 19 < 20: # this is true, result is reassigned
    result = 19

Etc. for all items in the list.

3 Comments

It's not quite right, I'm afraid - in the original code result was a Person instance, not the Person.age property value. OP will end up with the youngest Person.
This was meant to show the substitution of values, I agree that it is a Person instance that exists; however, they were expressing difficulty in following what the code was comparing which was why I rewrote it with the values instead.
Understood -- your code sample will throw an exception if anyone were to run it, which I worried has potential to cause confusion (result becomes an integer, so result.age fails on the second loop), you may just want to include a note in your answer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.