part1: Python language features and installation, Ubuntu 19.04 Server system installation, Windows 7 + RHEL7 dual system installation

Python is an excellent programming language, is characterized by simple, easy to use and powerful. It can be used to develop desktop, network programming, development of Web applications. Can also be used and large data field of artificial intelligence. 

A, Introduction to Python

1.1, Python's history
in 1989, Guido van Rossum developed Python language, Python language based on language ABC, ABC language is very powerful, it is designed for non-professional programmers. ABC language is not widely used because it is not open.

Python is simple to use, much like natural language. Non-professional software, Python choose low cost, and therefore practitioners in various fields, both with Python.

In December 2008, Python has released version 3.0, also called Python3000 or Py3k. Python 3.0 is not compatible with Python 2.x version. It is not compatible with this reason, the impact of Python applications to some extent.

1.2, Python features of
Python is object oriented, interpreted, weakly typed scripting language is powerful and perfect function universal language. Compared with other languages, Python code is simple, easy to get started.

Pros: clean syntax and scalability. In indention grammatical code block. In addition, scalability refers to its modules, the most rich and powerful class libraries , covering file I / O, GUI, network programming, database access, text manipulation and other scenarios. In the Python community, some uncommon scenario, but also open source modules provide a solution.

In addition, Python is a cross-platform programming language.

Disadvantages: slow, Python program compared to Java, C, C ++ program to be slow; source code encryption difficult, Python is an interpreted language, unlike compiled language source code is compiled into object files.

For the disadvantage of slow, can make up for hardware upgrades, the development process is more concerned with efficiency and reliability, is not running efficiency.

For difficult source code encryption shortcomings, and now this trend is the open source software industry, Java program also can be decompiled.

Second, the program operating mechanism

computer program is a set of instruction set, and truly let the machine running the machine instructions , ordinary difficult for developers to write machine instructions. Programs written in high-level computer language eventually be translated into machine instructions to execute. High-level language by way of an executable program can be divided into compiled and interpreted two kinds.

A special compiler language compiler, the compiler for a specific platform level language source code into executable machine code on the hardware platform (comprising machine instructions and operands), and packaged as the internet can recognize executable program format, which is compiled (compile) process. Compiled executable program from the development environment, run independently on a specific platform.

After compilation, a link may also need other compiled object code, the assembly object code block two or more generate the final executable program code for using low level in this way.

Compiled language advantages : one-time compiled to machine code can be run independently from the development environment, and high operating efficiency. Drawback : compiled executable program can not be ported to other platforms
running, if you want to run on other platforms, you need to modify the code for a specific platform, and recompile. C, C ++, Objective-C are all high-level compiled language.

Interpreted languages: interpreter for specific source language into machine code line by line explanation of a specific platform and immediately executed. Without integrity compile and link process, interpreted language is a compiled language in the compilation and interpretation process mixed together simultaneously.

The implementation of the interpreted language program, need to be compiled once. Shortcomings : low efficiency, and can not operate independently from the interpreter. Advantages : easy cross-platform, as long as the platform has the appropriate
interpreter can be. Interpreter on the platform will be responsible for the cost of the platform to explain the source machine instructions. Cross-platform interpreted language easy, efficiency is sacrificed in exchange for program execution.

Summary:
a compiled language: source program is compiled into machine instructions, machine instructions to run the drive machine.
Interpreted language: source code is read line by line, progressive conversion, line by line.


Third, install Python

1, installed on Windows Python
download page: https: //www.python.org/downloads/windows/
download file is divided into 32-bit versions and 64-bit versions, Windows system has 32-bit and 64-bit versions, in Python download installation file also needs to download the corresponding 32-bit or 64-bit version of the software installed. It has expressed Windows x86-64 64-bit version on the download link, Windows x86 32-bit version.

You can install version 3.x and 2.x version of Python at the same time. During installation, check the "Add Python 3.x to PATH" check box to add to the system Path environment variable Python command tool. Further, you may also choose a custom installation.

2, install Python on Linux
installation command on Ubuntu:
sudo APT-GET Update
sudo APT-GET install python3.7
The first command to specify the source address and update /etc/apt/sources.list /etc/apt/sources.list.d listed, thus ensuring access to the latest software packages. Mounting a second of the command specified Python 3.7.

Installation command on RHEL / CentOS
(1), download the Python source package, download the latest version-3.7.4 Python
wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4. the tgz
(2), extract the source packet
the tar-3.7.4.tgz -zxvf the Python
(. 3), the installation
create installation directory
mkdir / opt / python374
arranged
./configure --enable-optimizations --prefix == / opt / python374
compiled and installation of
the make
the make install

3, Ubuntu Server system installed
Linux system versions: Ubuntu 19.04 Server
(1), create a virtual machine, and specify the file system installation
(2), start the virtual machine, enter the installation step
(3), select the installation language
图一  选择安装语言
                      FIG Select a language installation 
(4), select the server version Installation
图二  选择安装服务器版本
                  Figure II choose to install the server version 
(5), the network is provided, temporarily provided
图三  设置网络,暂时不设置
                 图三  设置网络,暂时不设置
(6)、设置代理,不设置
图四  设置代理,不设置
                  图四  设置代理,不设置
(7)、设置镜像,默认即可
图五  设置镜像,默认即可
                 图五  设置镜像,默认即可
(8)、设置分区,选择 Manual,人工分区
图六  设置分区,选择 Manual
                 图六  设置分区,选择 Manual
(9)、进入分区界面,开始分区
图七  进入分区界面
                           图七  进入分区界面
最终的分区信息如下
图八  最终的分区信息
                         图八  最终的分区信息
(10)、创建用户名和密码
图九  创建用户名和密码
                         图九  创建用户名和密码
(11)、选择是否安装OpenSSH Server
图十  选择是否安装OpenSSH Server
                    图十  选择是否安装OpenSSH Server

在正式安装的时候,需要退出安装光盘
图十一  开始安装
                       图十一  开始安装
(12)、修改 root 账户密码
系统安装完成后,使用安装时创建的账户和密码登录系统后修改 ubuntu 的 root 账户密码,也即启用 root 账户
sudo passwd root
首先输入当前账户的密码,之后输入要设置的 root 账户的密码,设置成功后就可切换到 root 用户进行操作:
su - root
输入root账户密码,开始使用 root 账户

(13)、配置网络
配置文件路径:/etc/netplan/*.yaml
新版的 Ubuntu 系统网卡的配置是以 yaml 结尾的文件。进入这个配置文件,配置网卡。配置信息如下:
图十二  配置网卡
                             图十二  配置网卡
配置完成后运行下面的命令,如果运行命令网络仍然不通,就重启系统。
netplan apply

(14)、在Ubuntu Server 上安装最新版的 Python
下载源码包:
curl -O https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tar.xz
解压源码包:
tar -Jxvf Python-3.7.4.xz
安装所需要的编译器和依赖
apt-get update
apt-get install -y gcc make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev \
libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev


编译安装
# 创建安装目录
mkdir /usr/python3

# 进入解压后的源码文件夹
cd Python-3.7.4

# 配置检查安装环境,生成 Makefile 文件,指定安装到 /usr/python3
./configure --enable-optimizations --prefix=/usr/python3

如果有报错没有生成 Makefile 文件,根据报错信息安装相应的依赖或编译器,再重新执行

# 安装,安装完成后如果有报错,同样需要解决报错信息,如某些依赖未安装等
make && make install

添加 Python 环境变量
# 打开环境变量配置文件
vim /etc/profile
# 在这个文件的最后面添加 /usr/python3/bin
export PATH=/usr/python3/bin:$PATH
# 让配置文件立即生效
source /etc/profile

到此,python3 和 pip3 均也可用。接下来安装 ipython 和 jupyter。
# 安装 ipython
pip3 install ipython

# 安装 jupyter
pip3 install jupyter

四、 Windows 7 + RHEL7双系统安装

1、在 Windows 7 中下载 UltralISO 软件
2、准备U盘,制作启动盘
3、在 UltraISO 软件上,选择文件-> 打开 -> 找到linux的 ISO 文件,选择 -> 打开
4、启动 -> 写入硬盘映像
5、磁盘分区,将磁盘的空间空出一部分给linux系统使用:
步骤:右键电脑选择管理,在窗口中选择磁盘管理,这里显示的是所有系统分区,选择一个空闲容量大的磁盘,在磁盘上右键选择压缩卷,设置磁盘大小,点击压缩。压缩完成后,磁盘会多出一个未分配的空间,这就是从磁盘中压缩出来的,给 Linux 系统安装使用。
6、重启电脑按F12进入BIOS界面,选择U盘启动
7、进入安装页面,在安装时进行分区划分,基本的分区有:swap、boot、home、根分区(/)等。
8、设置双系统启动
在电脑Windows7系统上安装RHEL7,安装后找不到Windows7引导菜单。
因为RHEL7已采用新式的grub2系统,所以需要进入/boot/grub2目录后使用 vim 编辑 grub.cfg 文件
修改RHEL7的Grub2引导,添加 Windows 的启动项
(1)vim /boot/grub2/grub.cfg
(2)找到这一行(注意看清楚) ### BEGIN /etc/grub.d/30_os-prober ###
在后面添加
menuentry "Windows 7 (loader) (on /dev/sda1)" {
insmod ntfs
set root=(hd0,1) #由于windows安装在硬盘的C盘,故(hd0,1)
chainloader +1
}

另外,不想用上面的方法,还可以让系统自动检测是否有其他系统,命令是:
grub2-mkconfig -o /boot/grub2/grub.cfg

到此,双系统安装完成。

RHEL7上使用yum安装时,如果未注册,不能在官方源下载安装,为此重新安装 yum 软件
rpm –qa | grep yum # 查看 RedHat 自带的 yum 包
rpm –qa | grep yum | xargs rpm -e --nodeps #卸载red hat自带的yum
rpm –qa | grep yum # 查看是否卸载干净
下载yum安装需要的包(有六个),下载网址:
http://mirrors.163.com/centos/7/os/x86_64/Packages/
六个包分别是:
yum-metadata-parser-1.1.4-10.el7.x86_64.rpm
yum-3.4.3-161.el7.centos.noarch.rpm
yum-plugin-fastestmirror-1.1.31-50.el7.noarch.rpm
python-urlgrabber-3.10-9.el7.noarch.rpm
python-iniparse-0.4-9.el7.noarch.rpm
rpm-4.11.3-35.el7.x86_64.rpm

开始安装
rpm -ivh yum-metadata-parser-1.1.4-10.el7.x86_64.rpm
rpm -Uvh rpm-4.11.3-35.el7.x86_64.rpm --nodeps # 还需要安装这个依赖包
vim /usr/libexec/urlgrabber-ext-down 将首行的 /usr/bin/python 改为 /usr/bin/python2.7
rpm -ivh python-iniparse-0.4-9.el7.noarch.rpm
rpm -Uvh python-urlgrabber-3.10-9.el7.noarch.rpm --nodeps
rpm -ivh yum-3.4.3-161.el7.centos.noarch.rpm yum-plugin-fastestmirror-1.1.31-50.el7.noarch.rpm


备份Redhat自带的repo文件
cd /etc/yum.repos.d/
mv epel.repo backup_epel.repo.bak


下载repo文件
wget http://mirrors.163.com/.help/CentOS7-Base-163.repo

替换CentOS7-Base-163.repo文件中的$releasever为版本号7
vim CentOS7-Base-163.repo
执行下面命令
:1,$s/$releasever/7
替换完成之后输入 :wq 保存并退出

生成缓存
yum clean all
yum makecache

到此,yum 软件重新安装完成

五、编写Python程序

在编写Python程序时,要注意单词的大小写,Python程序是严格区分大写的。另外在每一行代码的末尾可以加分号,但没有什么作用,除非同一行有更多的代码,在Python中不建议这种做法。

编写Python程序可使用自带的 IDLE 工具,也可使用第三方工具,如:Notepad++、PyCharm、vim、Sublime Text等。不能使用Word、 写字板等做为编辑器,因为这些编辑器产生的文档包含有隐藏的格式化字符,这些字符会导致程序不能正常编译、运行。

运行Python程序,可以使用代码编辑工具自带的运行命令入口。也可以在命令行运行Python程序,命令行运行方式如下:
python <python 源程序路径>

1、 在Sublime Text工具中集成运行Python程序的“python”命令,步骤如下:
(1)、在Sublime Text 主菜单中选择 Tools / Build System / New Build System... 菜单,此时Sublime Text 会打开
一个新的配置文件,修改这个配置,将内容修改如下:
{
"cmd": ["python", "-u", "$file"]
}

这个文件指定运行python命令来解释运行当前文件($file表示当前文件),将这个配置文件命令为 Python.sublime-build,并保存在默认目录下。Python.sublime-build 是 Sublime Text 自定义构建系统的配置文件。

(2)、重启Sublime Text 工具,在 Tools / Build System 菜单中可看到 Python 菜单项,选择该菜单项,表示 Sublime Text将使用前一步定义的配置文件。

(3)、选择Sublime Text 的 Tools / Build 菜单项即可运行当前打开的Python程序,也可按快捷键 Ctrl + B 运行。运行后在底部显示运行结果。

如果同时装有 python2 和 python3 版本,在第1步的配置方法可以进行区分,对应的配置文件名也要进行区分。

2、 使用交互式解释器

python 命令可运行 Python 程序,也可进入一个交互式解释器,进入交互式解释器后,可看到 Python 版本号、系统平台、查看帮助等信息。

交互式解释器可学习各种新语法和测试各种新功能。在交互式解释器中可以直接进行加、减、乘、除运算,因此可以当做计算器使用。整数的加减运算结果是整数,若其中一个运算数带小数时,运算结果是小数,例如:
5 + 2 # 输出是 7
5 + 2.0 # 输出是 7.0
5 - 2 # 输出是 3
5 - 2.0 # 输出是 3.0

整数的乘法运算结果是整数,若其中一个运算数是小数时,运算结果是小数,例如:
2 * 5 # 输出是 10
2 * 5.0 # 输出是 10.0

除法的运算结果都是小数,整除的运算结果只取运算结果的商,即整除的运算结果是整数,例如:
6 / 2 # 普通除法运算结果是小数,输出是 3.0
5 // 2 # 整除的运算结果只取商,输出是整数,输出是 2

math模块可以执行三角函数运算,示例如下:
import math
math.sin(math.pi/2) # 输出是 1.0

Guess you like

Origin www.cnblogs.com/Micro0623/p/11408364.html