Skip to content

Instantly share code, notes, and snippets.

@demidovakatya
Last active February 21, 2016 11:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save demidovakatya/cba20afb3256ef158a08 to your computer and use it in GitHub Desktop.
Save demidovakatya/cba20afb3256ef158a08 to your computer and use it in GitHub Desktop.

Из основного:

  • print в 3.х не keyword, а функция.
Feature Python 2.x Python 3.x
print print "a string" или print("a string") print("a string")
integer division 3 / 2 # 1 3 // 2 # 1
float division 3.0 / 2 # 1.5 3.0 / 2 # 1.5
user input raw_input("> ") input("> ")

Dictionaries

Далее d — это словарь (dictionary).

Feature Python 2.x Python 3.x
iterating through dict for key in d.iterkeys(): for key in d:
iterating through dict for value in d.itervalues(): for value in d.values():
iterating through dict for (key, value) in d.iteritems(): for (key, value) in d.items():
dict as a list keylist = d.keys() keylist = list(d)
dict as a list valuelist = d.values() valuelist = list(d.values())

Iterators

Feature Python 2.x Python 3.x
xrange for i in xrange(10): for i in range(10):
range for i in range(10): for i in list(range(10)):
map new = map(f, old) new = list(map(f, old))
filter new = filter(f, old) new = list(filter(f, old))

Exceptions

Feature Python 2.x Python 3.x
raising exceptions raise ValueError, "something is wrong" raise ValueError("something is wrong")
catching exceptions except ValueError, e: except ValueError as e:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment