What does the` 'if people:'` statement do in this function? it doesn't seem to specify any conditions?

what does the 'if people:' statement do in this function? it doesn’t seem to specify any conditions?

def find_duplicates(birthdays_list, birthday, index):
 people = []
  for i in range(len(birthdays_list)):
    if birthdays_list[i] == birthday and i != index:
      people.append(i + 1)
  if people:
    people.append(index + 1)
    print("\n\nIn our simulation, the following people have the same birthdays: ")
    for person in people:
      print("Person {0}".format(person))
    return True
  else:
    return False

Hi,
In this case the condition is ‘people’, and the piece of code is checking if it’s empty or not.
While you’re probably used to conditions evaluating explicitly to true or false, but you can also use the state of a variable.
Certain values will evaluate to true and others false.
e.g
an empty list,
an empty string,
a null value,
an undefined value,
the number 0

would all evaluate as False (may vary between languages)
but, a variable with valid content in it would be True.

Hopefully that’s cleared it up a little, and if not try looking up ‘truthy falsy values’ and you should hit a better explanation (should no-one else offer one here).
Hope that helps.

2 Likes

Have a look at:
https://docs.python.org/3/library/stdtypes.html#truth-value-testing
to see which objects evaluate as False.
A few examples:

# These evaluate as True
if "abc":
if " ":    
if [1, 5]:
if 6:
if [""]:     
if [None]: 
if [[]]:  

# These evaluate as False
if "":  # Empty String
if None:
if []:    # Empty List
if 0:
1 Like

The if people: statement checks if the people list is not empty.

In the context of the find_duplicates function, the people list is populated with the index values of other people who have the same birthday as the person at the specified index in the birthdays_list. If there are no other people with the same birthday, then the people list will be empty.

The if people: statement serves as a condition for printing out a message and returning a value. If there are other people with the same birthday as the person at the specified index, then the people list will not be empty, and the function will print out a message listing the people with the same birthday and return a value of True. If there are no other people with the same birthday, then the people list will be empty, and the function will return a value of False without printing out a message.

2 Likes