CH0: Different of python 2.X and python 3.X

The task of CHO is to find out the different of Py2 and Py3.

How to tell the different of python 2.X and python 3.X ? In the task of CHO, we will alter the py2 to py3 script to see what happens. Which one is better? That's not a good question. ALL is well, sweetie. Just pick one and then learn how to use it, don't waste your time for comparing.

1. Print is a function

Modify the ex1.py, add "()" around the str.

print is no moreDifference: a statement,but a function in py3. print "Hello world!" in python2.X become print("Hello world!") in python3.X.

2. Chinese can be display in py3.

Modify the ex1.py,saved as ex101.py Difference: Source code acquiescence utf-8 coding in py3,so Chinese can display correctly. You can difine the variable as in chinese directly,such as:

汉字="test",print(汉字)`

Moreover, bracket the str in Python2.X are not equivalent to python3.X:

In python2.X:

print(1,2)

---output--- (1, 2)

In python3.X:

print(1,2)

---output--- 1 2

Description: Print isn't a function in python2.If there is no comma in brackets,the str will be considered mathematical expression of the brackets, ignored; if there is a comma, will be considered as the type of tuple.

3. Float

Modify ex3.py

In python2:

print ("Is it true that 3 + 2 < 5 - 7?")
print (3 + 2 < 5 - 7)

print ("What is 3 + 2?", 3 + 2)
print ("What is 5 - 7?", 5 - 7)

print ("Oh,that's why it's False.")

In python3, converte to this way:

print ("Is it true that 3 / 2 < 3 // 2 ?")
print ( 3 / 2 < 3 // 2)

print ("What is 3 / 2 ?", 3 / 2)
print ("What is  3 // 2 ?",  3 // 2 )

print ("Oh,that's why it's False.")

Difference: The integer division is automatically converted to float. In python 2.X, 3 / 2 = 1, 3 // 2 = 1; INpython 3.X, 3 / 2 = 1.5, 3 // 2 = 1.

4. Only input

Modify ex11.py

Difference: raw_input became input funciton in python 3.X, no more raw_input.

5.Something aobut the unicode

Modify ex4.py

Difference: The script : (-2)**0.5 can be calculated while in Py2 it will be error, the reason I don't know yet, I have to read the office docs to find out.


SUMMARY:

Of course, to read the office docs is the best way to know the whole situtaion:What’s New In Python 3.5 and this may help you to make better choice python 2 or 3?

My learning is based on Python 3.

results matching ""

    No results matching ""