python3.0已推出,但据说很多库都不能用了,建议使用2.6版本,我目前使用的是2.5版,与2.6版差距不大。
注意:2.6版本开始,print需要加上括号,否则会提示语法错误。
安装python运行环境:
- 下载for windows的安装包,http://www.python.org/,不过,正式对外的下载地址被和谐了,请移步到这里下载:http://www.python.org/ftp/python/
- 运行下载的.msi文件执行安装程序,默认会安装在系统盘符:/python25目录下,当然你可以更改该目录,但建议使用默认值,安装完成后会自动注册环境变量
运行cmd,执行python:
D:\>python
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>>
表示安装成功,>>>为python默认的提示符。
首先来一个经典的hello,world
>>> print 'hello world'
hello world
在此,有必要先认识一些系统内置的非常有用的一些函数
dir()函数用来显示一个类的所有属性和方法,如:
>>> dir('this is a string')
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__
ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__g
t__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__
', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '
__rmul__', '__setattr__', '__str__', 'capitalize', 'center', 'count', 'decode',
'encode', 'endswith', 'expandtabs', 'find', 'index', 'isalnum', 'isalpha', 'isdi
git', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lst
rip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit'
, 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', '
translate', 'upper', 'zfill']
>>>
基本上,以__(双划线)开头的方法,你是不能直接通过类去调用(但可以通过其它的途径去调用),它们相当于php类中的”魔术方法”。
type()函数用来显示当前变量的类型,如:
>>> m=1L
>>> n=1
>>> i=1.3
>>> a='123'
>>> b=range(10)
>>> type(m)
<type 'long'>
>>> type(n)
<type 'int'>
>>> type(i)
<type 'float'>
>>> type(a)
<type 'str'>
>>> type(b)
<type 'list'>
>>> type(type(b))
<type 'type'>
id()函数用来显示指定变量的内存地址
>>> a=b=123
>>> m=n='123'
>>> id(a),id(b)
(3178240, 3178240)
>>> id(m),id(n)
(5817024, 5817024)
>>> m='1'
>>> id(m)
5792000
help()顾名思义是用来查看帮助的
>>> help(str.find)
Help on method_descriptor:
find(...)
S.find(sub [,start [,end]]) -> int
Return the lowest index in S where substring sub is found,
such that sub is contained within s[start,end]. Optional
arguments start and end are interpreted as in slice notation.
Return -1 on failure.
另外,python的语句块是用缩进来标识的,不像c/c++/java/c#那样用{}来匹分,初次接触可能会不习惯,但时间长了,你会喜欢上这样的风格,因为它会让你感觉看所有的python代码都是一样的,不会出现参差不齐的”括号”风格。
开始我们的python之旅吧。