Slide 1

Slide 1 text

Hello! I'm Yan Orestes Content creator on 1 @yanorestes

Slide 2

Slide 2 text

Python 2 X Python 3 Which version to learn? 2

Slide 3

Slide 3 text

Python 2 X Python 3 Which version to learn? Which version to code with? 3

Slide 4

Slide 4 text

Wait what about backward compatibility? 4

Slide 5

Slide 5 text

backward compatibility 5

Slide 6

Slide 6 text

design failures Python 2 6

Slide 7

Slide 7 text

design failures Python 3 7

Slide 8

Slide 8 text

8 Python 2 or Python 3? (Finally) Understanding the differences between the two main versions of the language

Slide 9

Slide 9 text

Subtle changes 1. 9

Slide 10

Slide 10 text

Python 2 >>> print 'Hello, world!' Hello, world! 10

Slide 11

Slide 11 text

>>> print 'Hello, world!' File "", line 1 print 'Hello, world!' ^ SyntaxError: Missing parentheses in call to 'print'. Did you mean print('Hello, world!')? Python 3 11

Slide 12

Slide 12 text

Python 2 & Python 3 >>> print('Hello, world!') Hello, world! 12

Slide 13

Slide 13 text

raw_input X input 13

Slide 14

Slide 14 text

raw_input X input Python 2 Python 3 14

Slide 15

Slide 15 text

Python 2 >>> input() Hello! Traceback (most recent call last): File "", line 1, in File "", line 1 Hello! ^ SyntaxError: unexpected EOF while parsing 15

Slide 16

Slide 16 text

Lazy evaluation 2. 16

Slide 17

Slide 17 text

range function X class 17

Slide 18

Slide 18 text

range function X class list instance Python 2 Python 3 18

Slide 19

Slide 19 text

Python 2 >>> range(10**10) Traceback (most recent call last): File "", line 1, in MemoryError 19

Slide 20

Slide 20 text

Python 3 >>> range(10000**10000) range(0,1000000000000000000000 00000000000000000000000000000 00000000000000000000000000000 00000000000000000000000000000 00000000000000000000000000000 00000000000000000000000000…) 20

Slide 21

Slide 21 text

Python 2 But what about xrange? 21

Slide 22

Slide 22 text

Python 2 >>> range(10**10) xrange(0,10000000000) 22

Slide 23

Slide 23 text

Python 2 >>> xrange(10000**10000) Traceback (most recent call last): File "", line 1, in OverflowError: Python int too large to convert to C long 23

Slide 24

Slide 24 text

Numbers 3. 24

Slide 25

Slide 25 text

large numbers long X int 25

Slide 26

Slide 26 text

Python 2 >>> sys.maxint 9223372036854775807 >>> sys.maxint + 1 9223372036854775808L >>> type(sys.maxint + 1) 26

Slide 27

Slide 27 text

Python 3 >>> sys.maxsize 9223372036854775807 >>> sys.maxsize + 1 9223372036854775808 >>> type(sys.maxsize + 1) 27

Slide 28

Slide 28 text

int division Python 2 X Python 3 int float 28

Slide 29

Slide 29 text

Python 2 >>> 4/2 2 >>> 3/2 1 29

Slide 30

Slide 30 text

Python 3 >>> 4/2 2.0 >>> 3/2 1.5 30

Slide 31

Slide 31 text

Python 2 e Python 3 >>> 3//2 1 >>> 3/2.0 1.5 31

Slide 32

Slide 32 text

"Text" 4. 32

Slide 33

Slide 33 text

Python 2 >>> type('Hello, world!') >>> type(u'Hello, world!') 33

Slide 34

Slide 34 text

Python 2 str unicode bytes texto 34

Slide 35

Slide 35 text

Python 2 >>> len('Ā') 2 >>> len(u'Ā') 1 35

Slide 36

Slide 36 text

Python 3 bytes str bytes text 36

Slide 37

Slide 37 text

Python 3 >>> type('Hello, world!') >>> type(u'Hello, world!') >>> type(b'Hello, world!') 37

Slide 38

Slide 38 text

Python 3 >>> π = 3.14 >>> π 3.14 38

Slide 39

Slide 39 text

GIF identifier def is_gif(filename): with open(filename, 'rb') as f: id = f.read(6) return id == 'GIF89a' 39

Slide 40

Slide 40 text

id de GIFs Python 2 True Python 3 False 40

Slide 41

Slide 41 text

GIF identifier def is_gif(filename): with open(filename, 'rb') as f: id = f.read(6) return id == b'GIF89a' 41

Slide 42

Slide 42 text

Comparison 5. 42 ● == != ● > < ● >= <=

Slide 43

Slide 43 text

43 cmp e __cmp__ -1 (<), 0 (==), 1 (>)

Slide 44

Slide 44 text

44 cmp e __cmp__ -1 (<), 0 (==), 1 (>)

Slide 45

Slide 45 text

45 Python 2 all objects are ordinally comparable

Slide 46

Slide 46 text

46 Python 2 all objects are ordinally comparable ??????????

Slide 47

Slide 47 text

47 Python 2 >>> foo = MyClass() >>> foo < 'a' True >>> foo < 1 True >>> foo < [] True

Slide 48

Slide 48 text

48 Python 2 >>> 999999999 < 'a' True >>> 999999999 < [] True >>> 999999999.99 < [] True

Slide 49

Slide 49 text

49 Python 2 >>> ' ' > [] True >>> 'str' > 'list' True

Slide 50

Slide 50 text

50 Python 3 >>> ' ' > [] Traceback (most recent call last): File "", line 1, in TypeError: '>' not supported between instances of 'str' and 'list'

Slide 51

Slide 51 text

Indentation 6. 51

Slide 52

Slide 52 text

52 Python 3 >>> while True: ... print('hello') ... print('bye') File "", line 3 print('bye') ^ TabError: inconsistent use of tabs and spaces in indentation

Slide 53

Slide 53 text

53 Which version to learn? (and which to teach?)

Slide 54

Slide 54 text

54 Which version to code with?

Slide 55

Slide 55 text

55 2014 - Python 2 78% X 22% Python 3

Slide 56

Slide 56 text

56 2014 - Python 2 78% X 22% Python 3 2016 - Python 2 60% X 40% Python 3

Slide 57

Slide 57 text

57 2014 - Python 2 78% X 22% Python 3 2016 - Python 2 60% X 40% Python 3 2017 - Python 2 25% X 75% Python 3

Slide 58

Slide 58 text

58 Making your program more portable

Slide 59

Slide 59 text

Python 2 & Python 3 >>> print('Hello, world!') Hello, world!

Slide 60

Slide 60 text

from sys import version_info if version_info.major == 3: input() else: raw_input() 60

Slide 61

Slide 61 text

from sys import version_info if version_info.major == 3: input() else: raw_input() 61

Slide 62

Slide 62 text

from sys import version_info if version_info.major > 2: input() else: raw_input() 62

Slide 63

Slide 63 text

from sys import version_info if version_info.major > 2: input() else: raw_input() 63

Slide 64

Slide 64 text

EAFP

Slide 65

Slide 65 text

Sometimes it's easier to ask for forgiveness than permission 65 - Grace Hopper

Slide 66

Slide 66 text

try: raw_input('Enter your name: ') except NameError: input('Enter your name: ') 66

Slide 67

Slide 67 text

try: raw_input('Enter your name: ') except NameError: input('Enter your name: ') 67

Slide 68

Slide 68 text

Thank you! Any questions? You can talk to me here: ▪ @yanorestes ▪ [email protected] 68