Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

ความแตกต่างระหว่าง Python 2.x และ 3.x

Python 2.x เป็น legacy Python 3.x เป็นปัจจุบันและอนาคตของภาษา

ฟีเจอร์เฉพาะ 3.x (ไม่มีใน 2.x):

  • Strings เป็น Unicode by default
  • Unicode/bytes separation ชัดเจน
  • Exception chaining
  • Function annotations
  • Syntax สำหรับ keyword-only arguments
  • Extended tuple unpacking
  • Non-local variable declarations

ความแตกต่างสำคัญ

  • print: Python 2 เป็น statement ไม่ต้อง parentheses Python 3 เป็น function ต้องใช้ parentheses
  • Variables in List Comprehension: Python 2.x ตัวแปรใน list comprehension ชื่อซ้ำ global variable อาจเปลี่ยนค่า global
  • Integer Division: Python 2 ตัวเลขไม่มีทศนิยมเป็น int 3/2 = 1 Python 3 3/2 = 1.5 Python 3 ใช้ // สำหรับ integer division Python 2 ใส่ .0 เพื่อ float division

รายละเอียดเพิ่มเติม: https://docs.python.org/3/whatsnew/