设为首页 加入收藏

TOP

Python自动化运维之1、Python入门(一)
2017-09-30 17:17:01 】 浏览:9375
Tags:Python 自动化 入门

Python简介

  python是吉多·范罗苏姆发明的一种面向对象的脚本语言,可能有些人不知道面向对象和脚本具体是什么意思,但是对于一个初学者来说,现在并不需要明白。大家都知道,当下全栈工程师的概念很火,而Python是一种全栈的开发语言,所以你如果能学好Python,那么前端,后端,测试,大数据分析,爬虫等这些工作你都能胜任。

为什么选择Python

  关于语言的选择,有各种各样的讨论,在这里我不多说,就引用Python里面的一个彩蛋来说明为什么要选择Python,在Python解释器里输入import this 就可以看到。

>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

上面的话简单的总结来说就是“优雅”、“明确”、“简单”,或许你还是有些不明白,举个简单的例子,若果同样的功能你用C/C++写可能要写100行代码,而如果用Python写你可能只要20行代码就搞定,同样的如果一个问题有好几种解决方案,但是Python会用一种最简单的方法来实现。所以Python是用最简单最优雅最明确的方法来解决问题。

Python入门 

一、Python 2.x vs 3.x区别

1.print在python2.x是语句,在python3.x是print()函数

Old: print "The answer is", 2*2 
New: print("The answer is", 2*2)
Old: print x, # Trailing comma suppresses newline 
New: print(x, end=" ") # Appends a space instead of a newline
Old: print # Prints a newline
New: print() # You must call the function!
Old: print >>sys.stderr, "fatal error" 
New: print("fatal error", file=sys.stderr)
Old: print (x, y) # prints repr((x, y))
New: print((x, y)) # Not the same as print(x, y)!

2.python3.x全部字符集都是unicode,而在python2.x中是ascii编码,需要设置#-*- coding:utf-8 -*-,中文才不会乱码

3.python2.x一些库名在python3.x的更改

python2.x _winreg ConfigParser copy_reg Queue SocketServer markupbase repr test.test_support
python3.x winreg configparser copyreg queue socketserver _markupbase reprlib test.support

 

 

4.python3.x有一些第三方模块支持不了,当然还有其他的模块,后续慢慢添加....

Twisted  urllib2  scrapy MySQLdb

5.python2.x与python3.x库方法的不同,在后续慢慢体现....

二、安装Python

  在这里我我推荐安装Python3,因为随着时间的推移Python3,必定是未来的趋势,我们要顺应潮流。在Python的官网可以下载相应的版本,网址是https://www.python.org/downloads/,安装上面的提示安装好即可,就不在多说了,此外后面的操作都是基于Ubuntu 15.10下Python3.5.1操作,默认是Python2.7.10版本

      建议使用pyenv(Python版本控制2.7和3.5之间随意切换)、virtualenv(Python虚拟环境)、pycharm(Python的IDE工具)、pip(Python包管理工具)。

windows

1、下载安装包
    https://www.python.org/downloads/
2、安装python2.7.11
    默认安装路径:C:\python27
3、配置环境变量
    【右键计算机】--》【属性】--》【高级系统设置】--》【高级】--》【环境变量】--》【在第二个内容框中找到 变量名为Path 的一行,双击】 --> 【Python安装目录追加到变值值中,用 ; 分割】
    如:原来的值;C:\python27,切记前面有分号
4、安装python3.5.1
    默认安装路径: C:\python35
5、将python3.5中的执行程序python.exe重命名成python3
6、配置环境变量
    【右键计算机】--》【属性】--》【高级系统设置】--》【高级】--》【环境变量】--》【在第二个内容框中找到 变量名为Path 的一行,双击】 --> 【Python安装目录追加到变值值中,用 ; 分割】
     如:原来的值;C:\python35,切记前面有分号
7、这样就实现python2.7和python3.5多版本

三、编写Hello,World

  两种方式输出Hello World,第一种我们用解释器交互式环境,打开shell输入

首页 上一页 1 2 3 4 5 6 7 下一页 尾页 1/7/7
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇python读取excel文件 下一篇Python Base One

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目