第一章 Python 之初探

Python3 初探

Python 起源、基本介绍

Python 诞生于191年(比java还早,而且一直是最流行的十门语言之一。Python 之父 吉多*范*罗素姆java

Python 能够应用到许多计算环境下,以下所示:
  * 命令行窗口
  * 图形用户界面,包括 Web
  * 客户端和服务器端 Web
  * 大型网站后端
  * 云
  * 移动设备
  * 嵌入式设备python

Python 的特性、及缺点

Python 是一门很是通用的高级语言有如下优势:
  1. 代码可读性强
  2. 语法简洁
  3. 想对于其余语言,较容易学习
  4. 开源,免费
  5. 开发速度快,效率高linux

  6. 使用于多种平台,如Linux、Mac、Windowssql

Python 不适用如下场景数据库

1. 须要用大量时间进行运算时,建议用C或者C++ 编写你的程序macos

2. Python 并不属于某一个公司,因此它的发展更新会 相对更缓慢一些django

3. 假如你的项目要求很是高,不管如何努力,Python都没法达到项目的要求。vim

   那么能够去选择其余语言,如C、C++、JAVA,不过新的语言GO也是不错的选择,windows

   由于它写起来像Python的风格,性能却像C后端

安装Python3

官方网站地址:

  http://www.python.org

进入官网后,将鼠标指针放到 downloads 处,随后会自动显示对应的平台,如Mac、windows、Linux,点击相应的版本便可进入对应的下载页面。

 

Linux 版本

Linux 下载地址:

  https://www.python.org/downloads/source/

  打开网址后,选择相应版本的源码压缩包便可。下载后进行解压,编译安装便可。编译时会须要一些开发环境,请自行 Yum 安装便可。

同时,请注意:在编译安装Python时,务必加上--enable-shared编译参数,在有些系统下,操做系统不会建立分享库,centos系统即是如此的,会报找不到模块的错误。

 好比你的环境可能会用到pytho 的共享库,好比编译安装 postgressql 时,使用了 --with-python 选项,那么在编译安装 python 时就必须使用 --enable-shared 

可是这样会致使编译后 python 会没法使用,缘由是须要手动设置一下 环境变量

不然会报错:

python: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory

  

首先找到共享库文件:

[root@ansible lib]# find /  -name libpython2.7.so\*
/usr/local/lib/libpython2.7.so.1.0
/usr/local/lib/libpython2.7.so
/root/Python-2.7.8/libpython2.7.so.1.0
/root/Python-2.7.8/libpython2.7.so


# 上面的 /usr/local/lib 路径就是变量须要的值

设置环境变量:

在用户的家目录下 .bash_profile 文件中添加下面的代码, 或者是系统的 /etc/profile  文件中添加,懒得在每一个用户家目录下改

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib

# 使其生效
. .bash_profile

  

[root@localhost Python-3.6.0]# yum –y install  openssl-devel zlib  readline-devel sqlite-devel  # sqlit-devel 用于 django 的自带数据库
[root@localhost a]# ls
Python-3.6.0.tgz
[root@localhost a]# tar -xf Python-3.6.0.tgz -C /usr/local/src/
[root@localhost a]# cd /usr/local/src/
[root@localhost src]# ls
Python-3.6.0
[root@localhost src]# cd Python-3.6.0/
[root@localhost Python-3.6.0]# sed -i 's/^#readline/readline/' Modules/Setup.dist
[root@localhost Python-3.6.0]# sed -ri 's/^#(_ssl)/\1/p' Modules/Setup.dist
[root@localhost Python-3.6.0]# sed -ri 's/^#([\t]*-DUSE)/\1/p' Modules/Setup.dist
[root@localhost Python-3.6.0]# sed -ri 's/^#([\t]*-L\$\(SSL\))/\1/p' Modules/Setup.dist   
[root@localhost Python-3.6.0]# ./configure --enable-shared && make -j 4 && make install    # -j 后面跟线程数,假如你是4核cpu ,建议 -j 4

View Code

 

Python 3.5.3rc1 - 2017-01-03

Mac 版本

  Python 3.5.3rc1 - 2017-01-03

Windows 版本

Python 2.7.12 - 2016-06-25

Python 3.5.2 - 2016-06-27

运行 Python

1. 在交互式解释器中运行

[root@x201 ~]# python3
Python 3.6.0 (default, Jan 12 2017, 21:20:19)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print('Hello world!!!')
Hello world!!!
>>> exit()
[root@x201 ~]#

  在交互式解释器中运行就是所输几所见,执行结果也是及时输出到屏幕上的。符号 >>> 是 cpython 解释器的提示符。固然好有其余的解释器,如Ipython等。

退出输入  exit() 回车便可

2. 使用 Python 文件,执行 Python 程序

  首先用文本编辑器(如:vi、vim、office软件)编辑一个文件,写入 Python 的代码。以后再在命令行中执行相应的命令运行它

[root@x201 ~]# cat hello.py 
print('Hello world!!!')
[root@x201 ~]# python3 hello.py 
Hello world!!!
[root@x201 ~]# 

禅定一刻

每种语言都有本身的风格。在 Python 中内置了一些自由体诗歌,它们简单明了的说明了 Python 的哲学。

在交互式解释器中输入  import  this  ,以后回车就是显示它们。

 

 1 >>> import this
 2 The Zen of Python, by Tim Peters
 3 
 4 Beautiful is better than ugly.
 5 优美胜于丑陋
 6 Explicit is better than implicit.
 7 明了胜于隐晦
 8 Simple is better than complex.
 9 简洁胜于复杂
10 Complex is better than complicated.
11 复杂胜于混乱
12 Flat is better than nested.
13 扁平胜于嵌套
14 Sparse is better than dense.
15 宽松胜于紧凑
16 Readability counts.
17 可读性很重要
18 Special cases aren't special enough to break the rules.
19 即使是特例,也不可违背这些规则
20 Although practicality beats purity.
21 虽然现实不那么完美
22 Errors should never pass silently.
23 可是不该该放过任何异常
24 Unless explicitly silenced.
25 除非你肯定须要如此
26 In the face of ambiguity, refuse the temptation to guess.
27 若是存在多种可能,不要猜想
28 There should be one-- and preferably only one --obvious way to do it.
29 确定有一种——一般也是惟一一种——最佳的解决方案
30 Although that way may not be obvious at first unless you're Dutch.
31 虽然这并不容易,由于你不是 Python 之父
32 Now is better than never.
33 动手比不动手要好
34 Although never is often better than *right* now.
35 但不假思索就动手,还不如不作
36 If the implementation is hard to explain, it's a bad idea.
37 若是你的方案很难懂,那确定不是一个好方案
38 If the implementation is easy to explain, it may be a good idea.
39 若是你的方案很好懂,那确定是一个好方案
40 Namespaces are one honking great idea -- let's do more of those!
41 命名空间很是有用,咱们应当多加利用
42 >>> 
View Code

这些哲学思想,但愿能伴随你在 Python 之路上。

相关文章
相关标签/搜索