Installation Python2 Python3 interpreter and the Windows10

 

 

Python now has two versions, one is Python2, a Python3, now basically mainstream are based Python3, but in order to learn to install two versions of the interpreter. About interpreter, Java also has a similar concept, Java using virtual machine (JVM) to control the interpreter (java.exe), on the compiled byte code file using the compiler (javac.exe), namely .class file It is interpreted as the machine code to perform specific reference blog: https://www.cnblogs.com/youngchaolin/p/10930182.html .

Python2 official website to download and python3

Official website address: https://www.python.org/

(1) Open official website, select the download button below choose to install the system, select Windows

(2) There are two versions of the interpreter, the first one is the latest release of the interpreter, as python3, followed by a python2 is the latest version, now no longer updated 2020 Official will no longer be supported. Taking into account the subsequent compatibility with other plug-ins, python3 choose to install the 3.6.8 version, and Python2 could choose.

python2, click the Windows x86 MSI Installer to download and install the package:

python3, select the 3.6.8 version, select Window x86-64 executable installer to install.

Create two installation directory

Create two installation directory, a python2 installation, an installation python3, and the name of the interpreter installation directory python2 change the python27.exe, and python3 installation directory under the name of the interpreter modify python36.exe, so that the terminal selects behind you can switch between different versions of the free time interpreter.

python2 after installation:

After installation python3

Add the system environment variables

安装完解释器后,如果不安装系统环境变量,cmd终端中如果想运行解释器需要输入python.exe所在的完整路径名,如果想只输入python就可以执行,需要将完整路径添加到系统环境变量,这样系统就可以识别可执行文件名。

添加系统环境变量需要将刚才安装的解释器的文件夹完整路径添加到系统环境变量path下,添加python2和python3的安装包,win7略有不同,其用分号" : "隔开。

运行一个简单的python脚本

在电脑文件目录下写一个简单的txt文件,里面执行输出语句,打印一个"hello python",具体代码,其中头部代表指定编码采用UTF-8:

print('hello world 一期一会')

然后使用python3和python2的解释器直接执行这个python脚本,具体如下图所示:

  

发现用python3可以正常输出中文,但是python2无法正常输出,这是由于python3默认使用UTF-8的编码方案,而python2默认使用ASCII的编码方法。如果需要指定python2的编码为UTF-8,需要在脚本头部加上# -*- coding:utf-8 -*-。

编码

ASCII:1个字节编码(8位),主要显示英文、标点符号和其他西欧语言,格式为 0XXXXXXXX。

Unicode:4个字节编码(32位),也叫作万国码,可以显示世界上所有的符号,目前已用到10万+。

UTF-8:1~4字节变长字节编码,其中中文日文韩文等使用3个字节表示,格式为1110XXXX 10XXXXXX 10XXXXXX。

比如'中'这个字符,对应的unicode编码为0x4e2d,转化成二进制为0100 1110 0010 1101 然后需要使用三个字节编码,将二进制格式按顺序填充到1110XXXX 10XXXXXX 10XXXXXX中,变成11100100 10111000 10101101,这个就是UTF-8编码。这种将字符的

unicode索引添加到UTF-8的字节位置上的过程,叫做编码,编码后的数据通过网络一个字节一个字节的传输,如果将网络上传输的字节一个一个的再重新组合起来,再和unicode索引对照找到对应的字符,这个过程就做解码。

输出

输出语句非常简单,直接写print即可,其中python2和python3略有区别,如下所示:

# python2写法

print 'hello python'

# python3写法

print('hello python')

数据类型

字符串 str:凡是用引号包起来的都是字符串类型,一般使用单引号、双引号和三引号,java中一般就是双引号。此外python中字符串可以和数字相乘,这个比较特殊。

整形 int :就是整数,在32位机器上int的范围是: -2**31~2**31-1,即-2147483648~2147483647,在64位机器上int的范围是: -2**63~2**64即-9223372036854775808~9223372036854775807。

布尔型 bool :就是true和false

变量

变量就是一个代号,将字符串、数字或者布尔型先用这个代号保存起来,保存到内存中,通过代号就可以找到指向的内容。

变量一般使用字母数字和下划线组合,不能以数字开头,不能使用python中的关键字。另外也可以使用驼峰命名,但是一般python中不用,java用驼峰多些。

字符串直接转换成数字,使用number=int("数字")。

输入

输入就是程序接收外部数据,Java中使用Scanner的方法输入,可以区分整数和字符串,但是python中只要是输入的,就是字符串,并且语法较为简单。

# python2的写法

name=raw_input('请输入名字')
print name

# python3的写法
name=input('请输入名字')
print(name)

注释

使用#开头进行单行注释,使用三个引号'''进行多行注释。

条件判断

参考代码,需要注意条件判断的第二行首行缩进4个空格

# -*- coding: utf-8 -*-

# input输入的都是str
str=input('请输入数字')

#将str转换为数字
number=int(str)

if number>30:
    print('you are old boy')
else:
    print('you are not old boy')

测试结果,其中输入的都是字符串类型,需要使用int('数字')进行转换。

总结

(1)python目前有python2和python3两种解释器,其中前者默认使用ASCII编码,后者默认使用UTF-8编码

(2)python中没有数据类型的概念,定义一个变量直接写变量名=值

 

参考博文:https://www.cnblogs.com/youngchaolin/p/10463887.html

Guess you like

Origin www.cnblogs.com/youngchaolin/p/10927325.html