Python 学习从入门到放弃(一)-Python 基础介绍
写在前面
为什么要写这个东西呢?
一方面希望对之后学习Python的小伙伴能有帮助,另一方面希望通过这种方式激励自己,避免半途而废。
Python的基础介绍
Python is a programming language that lets you work quickly
and integrate systems more effectively.
这是Python官网上对Python的介绍,翻译一下就是“一种编程语言,用来帮助你快速工作,并整合系统以提高效率”。
按我的理解,Python有很多文件操作、网络、数学相关的库,因此在进行数据处理、网络爬虫等方面确实是有优势的。
Python的安装
1. 下载与安装
Python针对Windows、Mac都提供了安装包,你可以通过Python的官网选择想要的版本下载安装文件。
下载后直接点击安装即可。
对于Linux用户可能需要下载源码并通过命令行来安装。
2. 安装检查
可以通过打开shell/terminal执行python --version
来查看python的版本信息
Python的两个版本
Python有两个主要版本,Python2和Python3。应该如何选择,官网中也给出了建议,以下摘自官网
Short version: Python 2.x is legacy, Python 3.x is the present and future of the language
…
Which version you ought to use is mostly dependent on what you want to get done.
…
…there are some key issues that may require you to use Python 2 rather than Python 3.
* Firstly, if you’re deploying to an environment you don’t control, that may impose a specific version, rather than allowing you a free selection from the available versions.
* Secondly, if you want to use a specific third party package or utility that doesn’t yet have a released version that is compatible with Python 3, and porting that package is a non-trivial task, you may choose to use Python 2 in order to retain access to that package.
简单地说,2.x是遗产,2.x是现在和未来语言
…
使用那个版本取决于你想做什么…
…
不过某些情况下python2可能更合适一些:
1. 你在部署一个你无法控制的环境,这个环境已经使用了某个特定版本,你无法
2. 你需要的第三方库目前还不支持Python3
还是啊
所以能用Python3,还是用Python3吧。
Python解释器
Python是一种脚本语言,通过“解释”来执行,也就不需要编译、执行,因此可以写一行代码,看一行结果。
而Python解释器就是用来把代码翻译成可执行代码的工具。我们也可以直接打开Python解释器,并在里边输入代码执行。
在Windows和Mac上安装Python时,会自动安装IDLE(Python GUI),直接点击就可以进入Python解释器;或者在terminal/shell中输入python
来进入,你会看到类似这样的信息。
Python 3.6.1 (v3.6.1:69c0db5050, Mar 21 2017, 01:21:04)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
你可以直接输入2 + 2
来做个简单测试,这时你会看到4
这个结果,类似这样。
>>> 2 + 2
4
做个简单说明,以>>>
开头的行可以输入指令,以...
开头的行是代码块的一部分,直接展示的就是结果
Hello World
这个部分用于体验Python功能。由于我安装了Python3,所以以下均是Python3的语法。
在进行以下测试前,请先打开Python解释器,一下大部分摘自Python官网
小计算器
>>> 2 + 2
4
>>> 50 - 5*6
20
>>> (50 - 5*6) / 4
5.0
>>> 8 / 5
1.6
字符串
>>> print('Hello World')
'Hello World'
>>> 'Hello' + 'World'
'Hello World'
结语
第一篇文章只是做一个简单的介绍,之后会有更深入的学习,敬请期待