Python基础篇(一)

     首先需要从Python的官网下载python的安装程序,下载地址为:www.python.org/downloads。最新的版本为3.4.1,下载和操作系统匹配的安装程序并安装即可。

     

     安装好了后,在开始里面应该可以找到Python的相关启动项,如上图所示。

     从上图可以看到,图形界面(GUI)方式和命令行(commad line)方式都可以运行Python命令,可以自行选择。

    

     下面是在命令行(commad line)方式中运行Python的第一个小程序:hello world

      Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.1600 32 bit (In
      tel)] on win32
      Type "help", "copyright", "credits" or "license" for more information.
      >>> print("hello world")
      hello world

      print是Python的关键字,作用是进行打印操作。

 

      可以在Python中进行简单的加减乘除等操作,这在MySQL等数据库中也是可以的。

      >>> 1/2
      0.5

      类似于Java,Python也有对数字的ceil和floor操作。需要先使用import math引入math模块。

      >>> import math
      >>> math.floor(1/2)
      0
      >>> math.ceil(1/2)
      1

      求平方根:

      >>> math.sqrt(9)
      3.0

 

      Python中的幂运算需要注意,幂运算的优先级要比一元运算符的优先级要高

      >>> 3**2
      9
      >>> -3**2
      -9
      >>> (-3)**2
      9

     

      Python中也支持8进制和16机制:

      >>> 0xaf
      175
      >>> 0o66
      54

 

     和Java,Python的变量名可以包括数字,字母和下划线,但是数字不能作为变量名的第一个字符。

     >>> x=3
     >>> x*2
     6

 

    获取用户的输入:   

    >>> input("the meaning of life:")
    the meaning of life:happy
    'happy'

    默认的是String型,必要时要做类型转换    

    >>> x = (int)(input("x="))
    x=3
    >>> y = (int)(input("y="))
    y=6
    >>> x*y
    18

 

    编写Python脚本

    在window环境下在记事本中写如下脚本,并另存为hello.py    

    name = input("what is your name ?")
    print("hello " + name);
    input("Press<enter>")

    双击hello.py,发现会将输入的名字回显。

    除了双击外,还有2种方式运行Python脚本

   1.未将Python.exe所在路径加入到path时,命令行运行:

      E:\Python>python F:\工作\hello.py

      what is your name ?app

      hello app
      Press<enter>

      E:\Python是我Python的安装目录,在其下面可以找到Python.exe

   2.已将Python.exe所在路径加入到path时      

     C:\Documents and Settings\Administrator>python F:\工作\hello.py
     what is your name ?app
     hello app
     Press<enter>

   上面这些和Java的path道理上是一样的。

 

     Python中的注释:

     Python中的注释符为井号(#),在其后的整行的内容都会被当做注释,而不被执行。

 

     Python中的转义:

     Python在处理字符串时,双引号和单引号都是可以的。

     >>> print("let it go")
     let it go
     >>> print('let it go')
     let it go

     但是一行中有2个以上的单引号或2个以上的双引号,就要考虑进行转义,Python解释器可能无法很好地识别语义。

     >>> print('let\'s go')
     let's go

     >>> print("\"hello\" she said")
     "hello" she said

      如果有一个很长的字符串,跨越多行,而且其中有单引号,双引号,可以使用三个引号来代替普通的引号,三个点是换行时系统自动添加的。

      >>> print('''"let's go
      ... " she said''')
      "let's go
      " she said

     转移符\还有一个作用,可以忽略换行,将多行信息在一行中打印:

     >>> print("hello \
      ... world")
     hello world

     一行中有多个反斜线需要转义时,可以使用r来解决,但是不适用于单引号和双引号的情况:

     >>> print(r"c:\windows\dociment\docs")
     c:\windows\dociment\docs

     使用r来解决上述问题时,最后一个字符不能是反斜线,否则解释器不知道是该换行还是该正常显示:

     >>> print(r"c:\windows\dociment\docs\")
    File "<stdin>", line 1
    print(r"c:\windows\dociment\docs\")
                                      ^
    SyntaxError: EOL while scanning string literal

    可以用以下的语句代替:

    >>> print(r"c:\windows\dociment\docs"+ "\\")
    c:\windows\dociment\docs\

 

     字符串相关处理:

     >>> temp = 37
     >>> print("common tempature is: " + temp)
    Traceback (most recent call last):
     File "<stdin>", line 1, in <module>  

     TypeError: Can't convert 'int' object to str implicitly

     这时需要将整数类型转换为字符型,可以使用repr或者str,这里想通过强制转换的方式是不行的。

     >>> temp = str(37)
     >>> print("common tempature is: " + temp)
     common tempature is: 37

 

     Python 3.0以上版本使用的字符集都是Unicode字符集。

posted on 2014-07-13 22:35  lnlvinso  阅读(289)  评论(0编辑  收藏  举报