ในส่วนนี้ เราจะตรวจสอบการพิมพ์เอาต์พุตตัวแปรเดี่ยวและหลายตัวแปรในเวอร์ชันหลามสองเวอร์ชันที่แตกต่างกัน
# Python 2.7
พิมพ์ตัวแปรเดี่ยว
>>> #Python 2.7
>>> #Print single variable
>>> print 27
27
>>> print "Rahul"
Rahul
>>> #Print single variable, single brackets
>>> print(27)
27
>>> print("Rahul")
Rahul Python 3.6
>>> #Python 3.6 >>> #Print single variable without brackets >>> print 27 SyntaxError: Missing parentheses in call to 'print' >>> print "Rahul" SyntaxError: Missing parentheses in call to 'print'
ไวยากรณ์ด้านบนใน 3.6 เกิดจาก:ใน python 3.x พิมพ์ไม่ใช่คำสั่ง แต่เป็นฟังก์ชัน (print()) ดังนั้นการพิมพ์จึงเปลี่ยนเป็น print()
>>> print (27)
27
>>> print("Rahul")
Rahul พิมพ์ตัวแปรหลายตัว
Python 2.x (เช่น python 2.7)
>>> #Python 2.7
>>> #Print multiple variables
>>> print 27, 54, 81
27 54 81
>>> #Print multiple variables inside brackets
>>> print (27, 54, 81)
(27, 54, 81)
>>> #With () brackets, above is treating it as a tuple, and hence generating the
>>> #tuple of 3 variables
>>> print ("Rahul", "Raj", "Rajesh")
('Rahul', 'Raj', 'Rajesh')
>>> จากผลลัพธ์ข้างต้น เราสามารถเห็นได้ใน python 2.x การส่งตัวแปรหลายตัวภายในวงเล็บ () จะถือว่าเป็นทูเพิลของหลายรายการ
Python 3.x (เช่น:python 3.6)
#Python 3.6
#Print multiple variables
>>> print(27, 54, 81)
27 54 81
>>> print ("Rahul", "Raj", "Rajesh")
Rahul Raj Rajesh ลองใช้ตัวอย่างอื่นของหลายคำสั่งใน python 2.x และ python 3.x