python的历史
Python是一种解释型、面向对象、动态数据类型的高级程序设计语言。
Python由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年。
像Perl语言一样, Python 源代码同样遵循 GPL(GNU General Public License)协议。
python的简介
Python 是一个高层次的结合了解释性、编译性、互动性和面向对象的脚本语言。
Python 的设计具有很强的可读性,相比其他语言经常使用英文关键字,其他语言的一些标点符号,它具有比其他语言更有特色语法结构。
python 是一种解释性语言,交互性语言,面向对象的语言,初学者的语言
python的特点
易于学习 ,易于阅读 ,易于维护
互动模式,便携式 ,可扩展
数据库,GUI编程,可扩展性
一个广泛的标准库
python 下载
Python最新源码,二进制文档,新闻资讯等可以在Python的官网查看到:
Python官网:http://www.python.org/
你可以在一下链接中下载Python的文档,你可以下载 HTML、PDF 和 PostScript 等格式的文档。
Python文档下载地址:www.python.org/doc/
python 安装
linux系统自带python
分别为centos和xubuntu
xubuntu14.10
-
[root@git learn]# python Python2.6.6(r266:84292,Jan222014,09:42:36) [GCC 4.4.720120313(RedHat4.4.7-4)] on linux2 Type"help","copyright","credits"or"license"for more information. >>>
python升级
centos6.5 默认安装的是python2.6.6
首先
关闭防火墙
-
chkconfig iptables off service iptables stop
禁止selinux
-
vim /etc/selinux/config 改为: SELINUX=disabled [root@git ~]# setenforce 0
epel源
-
wget http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm rpm -ivh epel-release-6-8.noarch.rpm
-
crontab -e ntpdate 202.120.2.101 151***/usr/sbin/ntpdate 202.120.2.101;/usr/sbin/hwclock -w >/dev/null 2>&1
1 编译安装
-
yum install -y zlib-devel bzip2-devel openssl-devel xz-libs wget curl gcc openssh openssh-clients python-devel gcc-c++ automake swig libsodium wget --no-check-certificate https://www.python.org/ftp/python/2.7.10/Python-2.7.10.tgz wget http://mirrors.sohu.com/python/2.7.8/Python-2.7.10.tgz (国内源 比较快) tar xf Python-2.7.10.tgz cd Python-2.7.10 ./configure --prefix=/usr/local/yoho/python2.7.10 make && make install
-
#!/bin/bash yum install -y zlib-devel bzip2-devel openssl-devel xz-libs wget curl gcc openssh openssh-clients python-devel gcc-c++ automake swig libsodium openssh openssl #--------------------------------------python------------------------------- wget http://mirrors.sohu.com/python/2.7.8/Python-2.7.10.tgz tar xf Python-2.7.10.tgz-C /opt cd /opt cd Python-2.7.10 ./configure --prefix=/usr/local/yoho/python2.7.10 make && make install [ $?==0 ]&& echo "successful"|| exit 1 #---------------------------------------yum--------------------------------- mv /usr/bin/python /usr/bin/python-old ln -s /usr/local/yoho/python2.7.10/bin/python /usr/ py_version=`python -V` sleep 1 py_yum=`sed -n "/^#/p"/usr/bin/yum | cut -d /-f 4` [ ${py_yum}=="python2.6"]&& echo "yum is ok" && exit 0 sed -i s@#!/usr/bin/python@#!/usr/bin/python2.6@ /usr/bin/yum yum list>/dev/null [ $?==0 ]&& echo "successful"|| exit 4 echo "yum is ok."
首先当然是 python 彩蛋
-
>>>importthis TheZen of Python, by TimPeters 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! >>>
八荣八耻 以动手实践为荣,以只看不练为耻。 以打印日志为荣,以单步跟踪为耻。 以空白分隔为荣,以制表分隔为耻。 以单元测试为荣,以手工测试为耻。 以代码重用为荣,以复制粘贴为耻。 以多态应用为荣,以分支判断为耻。 以Pythonic为荣,以冗余拖沓为耻。 以总结思考为荣,以不求甚解为耻。
先来个复杂的 python终端介绍
-
>>>if2>1: ... print "2 da yu 1 " #python默认缩进4个空格 ...else: #跟shell不一样的地方 每个判断语句关键词后面加 : ... print "2 xiao yu 1" #打印输入用print ... #python的次提示符 2 da yu 1 #输出的结果 >>> #python的提示符 python 第一个程序 >>> print "hello world " hello world >>>
python执行过程
解释器
root@bds:/Data/project/oneday# vim hello.py root@bds:/Data/project/oneday# vim hello.py #!/usr/bin/env python #在文件的开头并顶头写 print "hello world" root@bds:/Data/project/oneday# chmod 755 hello.py #赋予执行的权限 root@bds:/Data/project/oneday# python hello.py hello world
编码风格
注:此处说的的是最少2个字节,可能更多
UTF-8 : 是对Unicode编码的压缩和优化,他不再使用最少使用2个字节,而是将所有的字符和符号进行分类:ascii码中的内容用1个字节保存、欧洲的字符用2个字节保存,东亚的字符用3个字节保存...
gbk 和utf-8的不同
GBK编码:是指中国的中文字符,其它它包含了简体中文与繁体中文字符,另外还有一种字符“gb2312”,这种字符仅能存储简体中文字符。
UTF-8编码:它是一种全国家通过的一种编码,如果你的网站涉及到多个国家的语言,那么建议你选择UTF-8编码。
GBK和UTF8有什么区别?
UTF8编码格式很强大,支持所有国家的语言,正是因为它的强大,才会导致它占用的空间大小要比GBK大,对于网站打开速度而言,也是有一定影响的。
GBK编码格式,它的功能少,仅限于中文字符,当然它所占用的空间大小会随着它的功能而减少,打开网页的速度比较快。
gbk和utf-8的转换关系
操作
-
>>>"步" 'xb2xbd' #现在是gdk编码 >>> str1 ='xb2xbd' #赋值变量 >>> str1.decode("gbk") #解码 u'u6b65' #u 最前面u代表unicode 已经解码为unicode >>> a = str1.decode("gbk").encode("utf-8") # 一步操作 再把unicode编码为utf-8 >>>print a # 打印a出来 如下是一个utf-8编码的字 姝
显示中文
-
#!/usr/bin/env python # -*- coding:uft-8 -*- #加上编码注释 避免出错 固定格式 print "你好 ,世界" 这样就可以打印中文 ,为了避免忘记 ,以后再编程过程中 ,要默认添加
python 注释
# ooxx 单行注释
""" ooxx """ 多行注释
#! /usr/bin/env python #coding=utf-8 """this is input hello world""" print "hello world" #intut something root@bds:/Data/project/learn# python helloworld.py hello world root@bds:/Data/project/learn
python 变量
a = r'
' #输出原型打印
a = u'中文' #定义为unicode编码
global a # 定义a为全局变量
python 数据类型
常用的数据类型
字符串 #str
布尔类型 #bool
整数 #int
浮点数 #float
列表 #list
元组 #tuple
字典 #dict