Why doesn't this course translate over to python?


Taking some of the basic things I've learned from this, and even copy&paste, but I get all kinds of syntax errors and such in Python 3.5, it makes no sense to teach it in a way it's not used


Three changes you will encounter almost immediately are,

  1. raw_input() no longer exists
  2. print is now a function
  3. 11 / 5 returns a float

Apart from these, not much else is different from the perspective of this introductory course. Python is still Python. help() still works.


as a mater of fact python 3.5 does not recognize datetime.now
I wonder why datetime.now returns a value that is 4 hours ahead of computer time ???


>>> import datetime
>>> now = datetime.datetime.now()
>>> now.time().strftime("%H:%M:%S")

This is my local time at the present moment.

>>> utc_now = datetime.datetime.utcnow()
>>> loc_now = datetime.datetime.now()
>>> tz_offset = int(loc_now.strftime("%H")) - int(utc_now.strftime("%H"))
>>> tz_offset


ahh, sounds fun to learn one thing then have to learn the differences between the course and the real thing :////


Good to absorb as much 2.x as you can so differences get learned while you learn each concept. Some of the differences are more profound than others.


print map(lambda (x, y): x * y, [(2,4), (3,6), (4, 8)])
[8, 18, 32]

That is what we can do in 2.x. It converts a map object to a list. It also unpacks a tuple.

>>> print map(lambda (x,y): x * y, [(2,4), (3,6), (4, 8)])
SyntaxError: invalid syntax

map is highlighted, meaing the error is before that point. Oh, print is now a function.

>>> print (map(lambda (x,y): x * y, [(2,4), (3,6), (4, 8)]))
SyntaxError: invalid syntax

The opening parenthesis on the (x,y) is highlighted. Okay.... Now what? Guess we remove the parens.

>>> map(lambda x,y: x * y, [(2,4), (3,6), (4, 8)])
<map object at 0x02EAD710>

Now we see that 3.x does not convert the map object to a list, but leaves it as it is.

print (map(lambda x,y: x * y, [(2,4), (3,6), (4, 8)]))
<map object at 0x02EAD5B0>

That means we have to perfom the coversion manually.

>>> print (list(map(lambda x,y: x * y, [(2,4), (3,6), (4, 8)])))
Traceback (most recent call last):
  File "<pyshell#284>", line 1, in <module>
    print (list(map(lambda x,y: x * y, [(2,4), (3,6), (4, 8)])))
TypeError: <lambda>() missing 1 required positional argument: 'y'

Okay... Another hurdle.

>>> print (list(map(lambda x: x[0]*x[1], [(2,4), (3,6), (4, 8)])))
[8, 18, 32]

This is just a sample. There are many differences that will surface. We can read about them, that's one way of finding out. Or we can drop some 2.x in the 3.x interpreter and go to town discovering the new nuances (and reading up on them). If you are given the assignment of repairing or updating code, you will be equipped to work in both realms.

In case you're wondering, the above code runs fine in 2.x.

     > print (list(map(lambda x: x[0] * x[1], [(2,4), (3,6), (4, 8)])))
     [8, 18, 32]


Another example. reduce has been moved to the new functools module.


def factorial(x):
    return 1 if x == 0 else \
    reduce(lambda a, b: a * b, range(1, abs(x) + 1))


>>> from functools import reduce
>>> def factorial(x):
    return 1 if x == 0 else \
    reduce(lambda a, b: a * b, range(1, abs(x) + 1))

>>> factorial(1)


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.