The difference between python2.x and python3.x __python

Source: Internet
Author: User
Tags integer division

Recently always encountered some annoying things, such as using Python3.5 to write some code, some do not understand the place, go online to find the answer, but found that many are based on python2.x.


Today I'm going to come up with some problems.


Unicode string
In Python2, normal strings are stored in 8-bit ASCII, while Unicode strings are stored as 16-bit Unicode strings, which can represent more character sets. The syntax used is to precede the string with the prefix U.
In Python3, all strings are Unicode strings.

Urllib

Urllib is a python-brought library of network data, and the most common way to get network data in Python 2 is urllib.urlopen (), whereas in Python 3 the method no longer exists, where Urllib.urlopen () in Python The equivalent expression in 3 is Urllib.request.urlopen ().


Py2 inside input with raw_input ()
Input in Py3 ()


Python3 inside Input The default received is the STR type
Python2 inside is considered int type


num = input ("Please input a number:")
Please input a number:45
Type (num)
OUT[54]: Str


Here are some common differences:

0x00
Filter (), encountering a similar <filter object at 0x000001e2ce37f390> problem

The object returned by the filter function in 3.x is changed from the list to filter object (iterator), in order to return a list you can add list () and also support iterative operations, such as for loops.


0x01
Range (), in 3.x the filter function returns an object that is Range object, such as screenshot plus list () to resolve.



0x02
Map (), encountered a similar <map at 0x1e2ce323160>.
This is because in Python3, the return value of map () is no longer a list, but a iterators.
Simply convert the iterator to a list, such as a list (map ()).



0X03:
Reduce (), encountered Nameerror:name ' reduce ' is not defined problem.
Because after entering Python 3.x, reduce is no longer in the built-in function.
Just: From functools import reduce



The integer division operation of Python 2.7 is still an integer, and the Python 3.x has improved the division of integers, "/" except to get floating-point numbers, "//" is still an integer.


In python2.x, Dict.keys () returns a list, in python3.x, Dict.keys () returns a Dict_keys object, which behaves more like set than the list, so it does not support indexing. Solution: List (Dict.keys ()) [index]


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.