Subtracting tuple from tuple?

9 minutes into this video, the lady suggests subtracting date.time(today) with date.time(date of birth) in order to get a person’s age - https://www.youtube.com/watch?v=apACNr7DC_s&list=PLxSV5eJ9ozga1-vscy4tuUehnT9_tbNVZ&index=5&t=0s

It doesn’t make sense, because it doesn’t list it over here as one of the possible tuple operations:
https://www.tutorialspoint.com/python3/python_tuples.htm

Help???

1 Like

That doesn’t look like a tuple operation, but a value operation. The result will likely be a normalization of subtracting two timestamps. That’s not a certainty, and if we have a link to the exercise we can delve further into this. Please post the URL.

1 Like

Somewhere in the video, it shows the code that they’re working with; I think around 9 minutes in

It actually starts with a string for birthday, then splits it and converts it to integers. But then (to me at least) it puts those integers into a tuple later, and then subtracts it from another tuple.

1 Like

Please relate this to a lesson, or furnish us with some real code to test. Videos are not really my thing, if you are willing to forgive me that.

1 Like
class User:
  def __init__(self, full_name, birthday):
    self.name = full_name
    self.birthday = birthday

    #extract first and last names
    name_pieces = full_name.split(" ")
    self.first_name = name_pieces[0]
    self.last_name = name_pieces[-1]

  def age(self):
    '''Return age of user in years'''
    today = datetime.date(2001, 5, 12)
    yyyy = int(self.birthday[0:4])
    mm = int(self.birthday[4:6]
    dd = int(self.birthday[6:8])
    dob = datetime.date(yyyy, mm, dd) 
    age_in_days = (today - dob).days
    age_in_years = age_in_days/365
    return int(age_in_years)

user = User("Dave Bowman", "19710315")
print(user.age())

I haven’t tested this code (gotta go to bed soon), but I don’t understand why they’re able to do math like that with tuples.

And if they aren’t tuples, can you explain to me why not?

1 Like

As mentioned earlier, the tuples are normalized timestamps . Timestamps are numbers.

The datetime object has special methods at its disposal for resolving timestamps into normalized dates and vice-versa.

February 23, 2019

How would we get that into a timestamp?

>>> import datetime
>>> now = datetime.datetime.now()
>>> now.timestamp()
1550948794.46523
>>> 
1 Like

Still, the other hard coded value is a tuple, isn’t it?

1550948794.46523

Wonder why that’s the result?

1 Like

The return value from the timestamp method is a float. It is a running count of some unit of time starting around January 1st, 1970.

>>> from datetime import datetime
>>> a = datetime(1970, 1, 1)
>>> a.timestamp()
25200.0
>>> 
1 Like

Oh so I can set any 3 item tuple to datetime and have it be treated as a float?

1 Like

That is not a tuple, but an argument list (year, month, day). Don’t be getting these confused. Just because we see parens does not make it a tuple.

1 Like

So it’s a sequence???

In that a sequence is a type of list, yes, but it is not a sequence in the classic sense since the terms have no relationship to each other.