Basics of Python 2

Source: Internet
Author: User

1.python2 or Python3?

py2.7 is the last version of the 2.x series and has stopped developing without adding new features. 2020 termination of support.

Updates to all the latest standard libraries will only be available in the 3.x version.

Uncle Turtle decided to clean up the python2.x and was no longer compatible with the old version. One of the biggest changes is to use Unicode as the default encoding. pyhton2.x directly write in Chinese will error, Python3 can directly write in Chinese.

From open source projects, the proportion of support py3 has been greatly improved, and well-known projects generally support py2.7 and py3+.

PY3 is more standardized than py2, removing unnecessary keywords

Python3.x continues to improve

2. Character encoding

In python2.x, the default encoding ==assic code, if required to support Chinese

#!-*-Coding:utf-8-*-

#coding: Utf-8

In python3.x, the default encoding ==unicode directly supports Chinese

Let's write down the character code in detail.

At the beginning of the development of computers, human-computer interaction uses ASSIC encoding, which is mainly used to display modern English and other Western European languages, which can only be represented by a maximum of 8 bits (one byte), i.e.: 2**8 = 256-1, so that ASCII code can represent up to 255 symbols

The first table that supports Chinese is GB2312 (1980,6700+), which is then extended to gbk1.0 (1995,20000), Gb18030 (2000,27000)

Unicode universal code appears, in order to solve the limitations of the traditional character encoding scheme, it has a unified and unique binary encoding for each character in each language, 2 **16 = 65536 one character in uniform 2 bytes

Utf-8= is an extended set of Unicode that uses a variable-length set of characters, that is, the original English characters still account for 8 bits, the European language occupies 2 bytes, and the Asian language occupies 3 bytes

3. User input

Name = input ("Your name:") print ("Hello", name)

Execute:
C:\test>python input.py
Your Name:miao
Hello Miao

the data type entered by the user defaults to string, if the following program is executed

death_age=100= input ("")print("", Death_ Age-age,"years")

The error will be as follows:
C:\test>python input.py
Your age:1
Traceback (most recent):
File "input.py", line 3, <module>
Print ("Can Live for", Death_age-age, "years")
typeerror:unsupported operand type (s) for-: ' int ' and ' str '

C:\test>

Need to cast the data type

death_age=100= Int (input (""))print("  ", Death_age-age,"years")

can be executed successfully
C:\test>python input.py
Your age:1
You can live for years

When entering a password, if you want to be invisible, you need to take advantage of the Getpass method in the Getpass module

Basics of Python 2

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.