简体   繁体   中英

how to calculate age form list of dates in python

How to calculate age from list. Is this possible?

name= ['Jonas','Miko','Jenelyn']
birthdate=['02/26/2000','12/02/2001','04/21/1999']

 expected output:
student Jonas age is 20 y.o
student Miko age is 19 y.o
student Jenelyn age is 21 y.o

You will need to format the dates appropriately, so research the datetime date function and figure out where to go from here.

from datetime import date
today = date.today()

for n, bday in zip(name, birthdate):
  age = (today - date(bday)).years
  print("Student ", n, "age is ", age, "years old.")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM