python3.0已推出,但据说很多库都不能用了,建议使用2.6版本,我目前使用的是2.5版,与2.6版差距不大。
注意:2.6版本开始,print需要加上括号,否则会提示语法错误。
安装python运行环境:
运行cmd,执行python:
1
D:\>python
2
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on win
3
32
4
Type "help", "copyright", "credits" or "license" for more information.
5
>>>
表示安装成功,>>>为python默认的提示符。
首先来一个经典的hello,world
>>> print 'hello world'
hello world
在此,有必要先认识一些系统内置的非常有用的一些函数
dir()函数用来显示一个类的所有属性和方法,如:
01
>>> dir('this is a string')
02
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__
03
ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__g
04
t__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__
05
', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '
06
__rmul__', '__setattr__', '__str__', 'capitalize', 'center', 'count', 'decode',
07
'encode', 'endswith', 'expandtabs', 'find', 'index', 'isalnum', 'isalpha', 'isdi
08
git', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lst
09
rip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit'
10
, 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', '
11
translate', 'upper', 'zfill']
12
基本上,以__(双划线)开头的方法,你是不能直接通过类去调用(但可以通过其它的途径去调用),它们相当于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)
13
<type 'str'>
14
>>> type(b)
15
<type 'list'>
16
>>> type(type(b))
17
<type 'type'>
id()函数用来显示指定变量的内存地址
>>> a=b=123
>>> m=n='123'
>>> id(a),id(b)
(3178240, 3178240)
>>> id(m),id(n)
6
(5817024, 5817024)
7
>>> m='1'
8
>>> id(m)
9
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之旅吧。