Python简介
Python是一种计算机程序设计语言,它结合了解释性、编译性、互动性和面向对象的脚本语言,非常简单易用。Python 的设计具有很强的可读性,相比其他语言经常使用英文关键字,其他语言的一些标点符号,它具有比其他语言更有特色语法结构。很多著名的网站都是用它编写的,如豆瓣、YouTube等。随着现在运维自动化,云计算,虚拟化,机器智能等技术的快速发展,python在我们的视野中也越来越受重视,许多大型网站就是用Python开发的,例如YouTube、Instagram,Google、Yahoo等,甚至NASA(美国航空航天局)都大量地使用Python,国内的网站如:豆瓣,知乎,拉勾网等•国内大公司,BAT,京东,网易,运维开发,或者高级DBA都需要会Python。Python核心团队计划在2020年停止支持 Python2,所以接下来我们的学习以Python3为主。
Python的安装使用
一、Windows下安装Python3。
1、安装
为了不去设置环境变量,我选择Customize installation安装,并且勾选Add Python 3.6 to PATH。


选择安装路径,我安装到C:\python36下面。


安装完成。

到cmd下面去测试一下能否使用,直接输入python -V可以查看版本号。

直接输入python,即可进入。

2、Linux下面安装Python3
选择要下载的版本下载到Linux机器上面。
tar zxvf Python-3.6.4.tgz
cd Python-3.6.4
./configure --ssprefix=/usr/local/python3.6
make && make install
这样安装以后,没有覆盖老的版本,我们将原来的/usr/bin/python链接改为别的名字,再建立新版本python的链接
mv /usr/bin/python /usr/bin/python_old2
ln -s /usr/local/python3.6/bin/python /usr/bin/python
查看Python版本
python –V
注意:这样安装好python3后,yum不能使用了,解决办法,修改/usr/bin/yum的 第一行为:
#!/usr/bin/python_old2
3、python编辑器的选择
常见的编辑器有Sublime Text、Vim、Pycharm、文本编辑器等,我们选择一个最好用的pycharm用作python编辑器。
二、安装Pycharm
1、我们选择专业版,按照默认选项一直下一步安装即可。





右边的滚动条拉到最下面,Accept按钮就可以选中了。


运行后提示需要激活,按下图选择,并输入http://xidea.online进行激活。

注意:如果上述方法不能激活,可以自行在网上搜索激活方法。
三、Pycharm的使用技巧
1、设置python文件的抬头,即新建一个python文件的时候,默认会添加如下的内容。
点击:File->setting->Editor->Code Style->File and Code Templates->Python Script输入如下内容:
其中: time 创建时间 author 作者 file 文件名
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#@Time : ${DATE} ${TIME}
#@Author: yangjian
#@File : ${NAME}.py

2、在pycharm中编写python代码
新建一个工程:File->New Project

新建一个python文件:在工程上面右键->New->Python File


3、下面开始编写简单的代码以及运行:
(1)、不需要传入参数
name = input("Please input your name:")
# 打印输出有下面三种方法,最常用的是第一种
print("hello {0}".format(name))
print("hello" + name)
print("hello %s" %name)

(2)、需要传入参数


运行代码,有如下两种方式:

4、pycharm使用技巧
快捷键
Ctrl+c 复制当前行或选定的代码块到剪贴板
Ctrl+x 剪切当前行或选定的代码块到剪贴板
Ctrl+v 粘贴
Ctrl+d 复制当前行、或者选择的块
Ctrl+n 跳转到类
Ctrl+shift+n 快速查找文件名
Ctrl+shift+f 全局查找,快速查找关键字的文件
Ctrl+shift+r 全局替换
Ctrl+鼠标左键 查看源代码
Ctrl+alt+方向左右键 看源码的时候前进返回
Ctrl+a 全选
ctrl+alt+l 格式化代码
Ctrl+/ 注释选中的代码
Alt+enter 自动导入包
Tab 多行同时向后移动
shift+tab 与Tab相反
alt+选中像notepad++多行操作
调试代码
断点:就是程序执行到这个地方就停下来。
F7:Step Into 进入到代码。
F8:Step Over 跳到下一步。
F9:resume program 恢复程序或者执行到下一个断点。

(未完待续)