pyhton 2とpython 3の使用の違い
2450 ワード
pythonというプログラミング言語を習い始めたばかりで、pythonの異なるバージョンのいくつかの使い方が異なることを考慮して、python 2とpython 3の違いを集めて整理しました.現在は不完全かもしれません.
エンコーディング(コアクラス)
文字列タイプ(コアクラス)
インデント形式(コアクラス)
print,execの使い方
TrueとFalseの使い方
小数と除算の使い方
比較演算子の違い
nonlocal
反復器
forサイクル変数値の違い*
round関数戻り値の違い
転載先:https://www.cnblogs.com/changyustudy/p/11468391.html
エンコーディング(コアクラス)
Python2 ascii,Python3 UTF-8, Python3 ‘# coding=utf-8’。
py2:
- ascii
:#-*- encoding:utf-8 -*-
py3:
- utf-8
:#-*- encoding:utf-8 -*-
文字列タイプ(コアクラス)
Python2 :unicode str, , , 。
Python3 ,str ,byte , 。
:
py2:
unicode v = u"root" unicode ( )
(str/bytes) v = "root"
py3:
str v = "root" unicode ( )
bytes v = b"root"
インデント形式(コアクラス)
Python2 ,1 Tab 8 Space , Tab Space , IDE 。Python3 Tab Space , :
TabError: inconsistent use of tabs and spaces in indentation.
print,execの使い方
python2 ,print , python3 ,exec :
print(“hello”,“world”)
python2 ;
pyton3 , 。
TrueとFalseの使い方
Python2 True False , 1 0, , 。
Python3 True False , , 。
小数と除算の使い方
Python2 / , ,// ;
Python3 // ,/ 。
:3/2 Python2 1, Python3 1.5
比較演算子の違い
Python2 。
Python3 。
nonlocal
Python2 global , 。 Python3, nonlocal, jubu 。
反復器
Python2 。
Python3 , 。
forサイクル変数値の違い*
python2 ,for :
i = 1
print ('comprehension: ', [i for i in range(5)])
print ('after: i =', i ) #i=4
Python3 :
i=1
for i in range(5):
print(i)
print(i) ## 4
Python3,for :
i = 1
print ('comprehension: ', [i for i in range(5)])
print ('after: i =', i ) #i=1
[i for i in range(5)]
round関数戻り値の違い
Python2,round int
isinstance(round(15.5),int) #True
Python3,round float
isinstance(round(15.5),float) #True
転載先:https://www.cnblogs.com/changyustudy/p/11468391.html