Learning by doing: Reading books and trying to understand the (code) examples
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

22 lines
648 B

  1. import datetime
  2. from collections import namedtuple
  3. def middle(stock, date):
  4. symbol, current, high, low = stock
  5. return ((high + low) / 2), date
  6. tuple = ("GOOG", 613.30, 625.86, 610.50)
  7. mid_value, date = middle(tuple, datetime.datetime.now())
  8. print("middle value: {} at date {}".format(mid_value, date))
  9. print("high value: {}".format(tuple[2]))
  10. print(tuple[1:3])
  11. # named tuples
  12. Stock = namedtuple("Stock", "symbol current high low")
  13. stock = Stock("GOOG", 613.30, high=625.86, low=610.50)
  14. print("{}: current={}, high={}, low={}".format(stock.symbol, stock.current,
  15. stock.high, stock.low))