Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Python 2 or Python 3? - (Finally) Understanding...
Search
yyyyyyyan
October 21, 2018
Programming
3
120
Python 2 or Python 3? - (Finally) Understanding the differences between the two main versions of the language
yyyyyyyan
October 21, 2018
Tweet
Share
More Decks by yyyyyyyan
See All by yyyyyyyan
como funcionam as senhas e por que não funcionam: tentando resolver um problema sem solução
yyyyyyyan
1
25
[PyCon APAC 2022] Writing secure code in Python
yyyyyyyan
1
150
Writing secure code in Python
yyyyyyyan
1
210
Escrevendo códigos seguros em Python
yyyyyyyan
2
330
Entendendo sockets no Python criando um bot de IRC
yyyyyyyan
1
380
Python 2 ou Python 3?
yyyyyyyan
1
120
Criando um malware com Python
yyyyyyyan
3
5.4k
Creating a malware using Python
yyyyyyyan
1
490
Other Decks in Programming
See All in Programming
アジャイルを支えるテストアーキテクチャ設計/Test Architecting for Agile
goyoki
9
3.3k
광고 소재 심사 과정에 AI를 도입하여 광고 서비스 생산성 향상시키기
kakao
PRO
0
170
CSC509 Lecture 12
javiergs
PRO
0
160
subpath importsで始めるモック生活
10tera
0
300
どうして僕の作ったクラスが手続き型と言われなきゃいけないんですか
akikogoto
1
120
ペアーズにおけるAmazon Bedrockを⽤いた障害対応⽀援 ⽣成AIツールの導⼊事例 @ 20241115配信AWSウェビナー登壇
fukubaka0825
6
1.9k
イベント駆動で成長して委員会
happymana
1
320
2024/11/8 関西Kaggler会 2024 #3 / Kaggle Kernel で Gemma 2 × vLLM を動かす。
kohecchi
5
920
NSOutlineView何もわからん:( 前編 / I Don't Understand About NSOutlineView :( Pt. 1
usagimaru
0
330
3rd party scriptでもReactを使いたい! Preact + Reactのハイブリッド開発
righttouch
PRO
1
600
よくできたテンプレート言語として TypeScript + JSX を利用する試み / Using TypeScript + JSX outside of Web Frontend #TSKaigiKansai
izumin5210
6
1.7k
「今のプロジェクトいろいろ大変なんですよ、app/services とかもあって……」/After Kaigi on Rails 2024 LT Night
junk0612
5
2.1k
Featured
See All Featured
StorybookのUI Testing Handbookを読んだ
zakiyama
27
5.3k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
Thoughts on Productivity
jonyablonski
67
4.3k
What's new in Ruby 2.0
geeforr
343
31k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
840
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
109
49k
GitHub's CSS Performance
jonrohan
1030
460k
A Modern Web Designer's Workflow
chriscoyier
693
190k
Art, The Web, and Tiny UX
lynnandtonic
297
20k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
250
21k
Six Lessons from altMBA
skipperchong
27
3.5k
Site-Speed That Sticks
csswizardry
0
23
Transcript
Hello! I'm Yan Orestes Content creator on 1 @yanorestes
Python 2 X Python 3 Which version to learn? 2
Python 2 X Python 3 Which version to learn? Which
version to code with? 3
Wait what about backward compatibility? 4
backward compatibility 5
design failures Python 2 6
design failures Python 3 7
8 Python 2 or Python 3? (Finally) Understanding the differences
between the two main versions of the language
Subtle changes 1. 9
Python 2 >>> print 'Hello, world!' Hello, world! 10
>>> print 'Hello, world!' File "<stdin>", line 1 print 'Hello,
world!' ^ SyntaxError: Missing parentheses in call to 'print'. Did you mean print('Hello, world!')? Python 3 11
Python 2 & Python 3 >>> print('Hello, world!') Hello, world!
12
raw_input X input 13
raw_input X input Python 2 Python 3 14
Python 2 >>> input() Hello! Traceback (most recent call last):
File "<stdin>", line 1, in <module> File "<string>", line 1 Hello! ^ SyntaxError: unexpected EOF while parsing 15
Lazy evaluation 2. 16
range function X class 17
range function X class list instance Python 2 Python 3
18
Python 2 >>> range(10**10) Traceback (most recent call last): File
"<stdin>", line 1, in <module> MemoryError 19
Python 3 >>> range(10000**10000) range(0,1000000000000000000000 00000000000000000000000000000 00000000000000000000000000000 00000000000000000000000000000 00000000000000000000000000000 00000000000000000000000000…)
20
Python 2 But what about xrange? 21
Python 2 >>> range(10**10) xrange(0,10000000000) 22
Python 2 >>> xrange(10000**10000) Traceback (most recent call last): File
"<stdin>", line 1, in <module> OverflowError: Python int too large to convert to C long 23
Numbers 3. 24
large numbers long X int 25
Python 2 >>> sys.maxint 9223372036854775807 >>> sys.maxint + 1 9223372036854775808L
>>> type(sys.maxint + 1) <type 'long'> 26
Python 3 >>> sys.maxsize 9223372036854775807 >>> sys.maxsize + 1 9223372036854775808
>>> type(sys.maxsize + 1) <class 'int'> 27
int division Python 2 X Python 3 int float 28
Python 2 >>> 4/2 2 >>> 3/2 1 29
Python 3 >>> 4/2 2.0 >>> 3/2 1.5 30
Python 2 e Python 3 >>> 3//2 1 >>> 3/2.0
1.5 31
"Text" 4. 32
Python 2 >>> type('Hello, world!') <type 'str'> >>> type(u'Hello, world!')
<type 'unicode'> 33
Python 2 str unicode bytes texto 34
Python 2 >>> len('Ā') 2 >>> len(u'Ā') 1 35
Python 3 bytes str bytes text 36
Python 3 >>> type('Hello, world!') <class 'str'> >>> type(u'Hello, world!')
<class 'str'> >>> type(b'Hello, world!') <class 'bytes'> 37
Python 3 >>> π = 3.14 >>> π 3.14 38
GIF identifier def is_gif(filename): with open(filename, 'rb') as f: id
= f.read(6) return id == 'GIF89a' 39
id de GIFs Python 2 True Python 3 False 40
GIF identifier def is_gif(filename): with open(filename, 'rb') as f: id
= f.read(6) return id == b'GIF89a' 41
Comparison 5. 42 • == != • > < •
>= <=
43 cmp e __cmp__ -1 (<), 0 (==), 1 (>)
44 cmp e __cmp__ -1 (<), 0 (==), 1 (>)
45 Python 2 all objects are ordinally comparable
46 Python 2 all objects are ordinally comparable ??????????
47 Python 2 >>> foo = MyClass() >>> foo <
'a' True >>> foo < 1 True >>> foo < [] True
48 Python 2 >>> 999999999 < 'a' True >>> 999999999
< [] True >>> 999999999.99 < [] True
49 Python 2 >>> ' ' > [] True >>>
'str' > 'list' True
50 Python 3 >>> ' ' > [] Traceback (most
recent call last): File "<stdin>", line 1, in <module> TypeError: '>' not supported between instances of 'str' and 'list'
Indentation 6. 51
52 Python 3 >>> while True: ... print('hello') ... print('bye')
File "<stdin>", line 3 print('bye') ^ TabError: inconsistent use of tabs and spaces in indentation
53 Which version to learn? (and which to teach?)
54 Which version to code with?
55 2014 - Python 2 78% X 22% Python 3
56 2014 - Python 2 78% X 22% Python 3
2016 - Python 2 60% X 40% Python 3
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
58 Making your program more portable
Python 2 & Python 3 >>> print('Hello, world!') Hello, world!
from sys import version_info if version_info.major == 3: input() else:
raw_input() 60
from sys import version_info if version_info.major == 3: input() else:
raw_input() 61
from sys import version_info if version_info.major > 2: input() else:
raw_input() 62
from sys import version_info if version_info.major > 2: input() else:
raw_input() 63
EAFP
Sometimes it's easier to ask for forgiveness than permission 65
- Grace Hopper
try: raw_input('Enter your name: ') except NameError: input('Enter your name:
') 66
try: raw_input('Enter your name: ') except NameError: input('Enter your name:
') 67
Thank you! Any questions? You can talk to me here:
▪ @yanorestes ▪
[email protected]
68