linux下安装python

-去官网下载源码安装包
或者lftp 172.25.254.250
cd pub/software/python
get Python-3.6.4.tg
python3.6
-解压安装包到/opt目录
tar xf Python-3.6.4.tgz -C /opt
-安装编译过程中需要的依赖包:gcc,zlib,zlib-devel
-进入解压的安装包进行编译
cd /opt/Python3-*/
./configure --prefix=/usr/local/python --with-ssl

-安装:make && make install
-添加python3的命令到环境变量里
echo $PATH
#临时添加
export PATH='python3所在的路径:$PATH'
#永久添加
vim ~/.bashrc
添加 export PATH="/usr/local/python/bin/:$PATH"
或者  echo  PATH="/usr/local/python/bin/:$PATH" >> ~/.bashrc

测试:

另开一个SHELL输入python3
[root@sheen ~]# python3
Python 3.6.4 (default, Aug  6 2018, 22:54:20) 
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

~/ #表示当前用户家目录

python代码编写

python打印

python2中的print既是特殊字符又是函数

-python2:
    >>> print("hello")
    hello
    >>> print "hello "
    hello 

    
-python3
    >>> print('hello')  
    hello
-python2向python3
    >>> from __future__ import print_function    #导入python2向python3打印的转换模块
    >>> print "hello"
      File "<stdin>", line 1
        print "hello"
                ^
    SyntaxError: invalid syntax
    >>> print("hello")
    hello

图片描述

用命令编译文件

vim hello.py

#!/usr/local/python/bin/python3
context="hello sheen"
print(context)

chmod +x hello.py
cp hello.py /bin/welcome
测试:

[root@sheen bin]# welcome
hello sheen

python的编码格式

python2:ASCII
python3:Unicode

ASCII编码:

1字节=8bit,1英文字符=1字节,-----00000000(2^8-1)

Unicode编码:

一个字符代表两个字节,(2^16-1)

utf-8:

如果是英文,一个字节存储;如果是中文,用三个字节存储


SheenStar
168 声望26 粉丝

祝你坚强