1. 安装python绝大多数的linux系统中已经安装了python,我们可以直接在shell中输入python来进行验证,如下:

[root@ceshi ~]# pythonPython 2.6.6 (r266:84292, Nov 22 2013, 12:11:10) [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>>                  /如果想要退出编辑器直接用ctrl+d [root@ceshi ~]#

如果没有安装编辑器,我们可以采用源码安装大方法。

[root@ceshi ~]# cd /usr/local/src[root@ceshi src]# wget [root@ceshi src]# tar zxvf Python-2.6.6.tgz [root@ceshi src]# cd Python-2.6.6[root@ceshi src]# ./configure  --profile=/usr/local/python[root@ceshi src]# make[root@ceshi src]# make install[root@ceshi src]# export PATH=$PATH:/usr/local/python  /把python命令放入PATH路径中。

2. python交互式解释器,直接输入python,打印出“hello python!”

[root@ceshi ~]# pythonPython 2.6.6 (r266:84292, Nov 22 2013, 12:11:10) [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> print "Hello.world"Hello.world>>>

print就是打印的意思,这句话的意思就是在python交互解释器中打印出“Hello。world”。

3. 变量

    变量基本上就是代表(或引用)某值的名字。我们可以设置如下变量:

>>> x = 3        /由这两个变量,我们可以发现python变量定义与shell基本相同,只是shell中>>> print x      /“=”号两侧不能有空格,而python用不用都可以;python变量在引用时直接3                /直接引用即可,而shell中则需要添加美元符号。>>> y=6>>> print y6>>>

同样的,变量名可以是数字,字母,下划线,但是变量名不能以数字开头。

4. 和用户进行交互,在写程序的时候,有时侯我们需要在程序执行过程中用户输入一些字符。

>>> input(" The meaning of life: ") The meaning of life: 45        /用户输入字符,然后回车,英文语句在这里就是代表提示符45>>>

接下来我们做一个小计算:

>>> x=input("x:")x:34                       /“x:”表示提示符>>> y=input("y: ")y: 43>>> print x*y1462>>>

5. 函数 

    就像小程序一样,可以用来实现特定的功能。python有很多函数,他们能做很巧妙的事情,当然我们也可以自己定义函数,python也拥有很多的内建函数。

    

>>> 2**38>>> pow(2,3)     /这个局势幂函数,8>>> >>> abs(-132)    /绝对值函数132>>> 1.0/20.5>>> round(1.0/2)  /四舍五入函数1.0

整数除法总是会截除结果的小数部分,而round函数则会将结果四舍五入为最接近的整数。但是我们有这样一个需求,如果一个人的年龄为32.9岁,但是我们想把它取整为32岁,因为他还没有到33岁,这时候我们就需要用到floor函数。

6. 模块

    可以把模块想象成导入到python以增强其功能的扩展。需要使用特殊命令import来导入模块。

>>> import math>>> math.floor(32.9)32.0>>> >>> math.ceil(34.5)   /把给定的值转换为大于或等于它的最小整数。35.0

用法:用import导入函数,然后按照“模块.函数”的格式使用这个模块的函数。

>>> int (math.floor(32.9))    /可以使用整型int函数,把浮点型数据转换为整型。32

在确定自己不会导入多个同名函数(从不同的模块导入)的情况下,我们可以这样使用模块。

>>> from math import sqrt  /使用了from模块import函数之后,就可以直接使用函数,而不用模块名做前缀>>> sqrt (9)     /开方函数3.0

另外我们也可以使用变量来引用函数,如下:

>>> foo=sqrt         /接上一步的环境>>> foo(25)5.0
>>> sqrt (-25)      /给一个负数开方,会发现出错,这时候我们要用到复数的概念Traceback (most recent call last):  File "
", line 1, in 
ValueError: math domain error

调用复数所使用的模块cmath。在这个模块下我们可以进行虚数的运算。

>>> import cmath>>> cmath.sqrt(-25)5j>>> >>> (1+3j)*(7-8j)    /进行虚数的运算(31+13j)>>>

6. 字符串

    python打印字符时,使用单引号和双引号的作用是一样的。如下:

>>> "hello.world"'hello.world'>>> 'hello.world''hello.world'

但是当打印的字符里面出现有单引号或者双引号时就会不一样。

>>> "let's go""let's go">>> 'let's go'              /使用单引号就会报错,此时python不会识别第二个单引号后面的字符  File "
", line 1    'let's go'         ^SyntaxError: invalid syntax>>> 'let\'s go'              /但是我们可以使用转义字符,来进行转义。转义字符就是(\)"let's go">>>

    python拼接字符,如下:

>>> x="Hello. world">>> y="let's say">>> y+x                          /“+”号就是拼接字符。"let's sayHello. world">>>

    字符串表示。

str和repr实际上就是把数值转化为字符串。str和long,int一样是一种值的类型,表示字符型。而repr则是一种函数,它会创建一个字符串。repr(x)也可以写作`x`(这里是反引号)。

>>> print repr("Hello.world")'Hello.world'>>> print str("Hello.world")Hello.world>>>

如下一个例子:

>>> tmp=43>>> print "The temperature is " + tmpTraceback (most recent call last):  File "
", line 1, in 
TypeError: cannot concatenate 'str' and 'int' objects>>> print "The temperature is " + `tmp`The temperature is 43>>>

第一个print语句中因为tmp的值是数字类型,因此不能和进行字符串和数字的相加。第二个我们通过repr函数,把数字类型的值转化为了字符型,因此可以相加。

    input函数和raw_iput函数:

input函数会假设用户输入的是合法的python表达式,而raw_input函数会把所有的输入当作是原始的数据。

>>> raw_input("Enter a number: ")Enter a number: 3'3'>>> input("Enter a number: ")Enter a number: 33>>>

    长字符串:假设一个字符需要占用三行,我们可以采取如下的表示方法:

>>> print '''This is a very long string... it continues here... And it's not over yet'''This is a very long stringit continues hereAnd it's not over yet>>>

三个单引号也可以环卫三个双引号。在这种字符内可以同时使用单引号和双引号,而不用转义。

>>> print "Hello \... world"Hello world>>>

普通字符也是可以跨行的,如果一行之中最后一个字符是反斜线,那么,换行符本身就是转义的,也就是被忽略了!

    原始字符串:原始字符串就是不会进行转义,就是把你输入的字符串原始不动的打印出来。

>>> print "hello.\nworld"    /如果不加原始字符串,输出结果是这样的!hello.world>>> print r"hello.\nworld"   /加上原始字符串r,后输出的就是原始字符串。hello.\nworld>>> >>> print r "hello.\nworld"   /注意r的位置,紧接着字符串,否则会出错。  File "
", line 1    print r "hello.\nworld"                          ^SyntaxError: invalid syntax