python教程:安装python运行环境以及简单程序
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:
1D:\>python
2Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) on win
332
4Type "help", "copyright", "credits" or "license" for more information.
5>>>
表示安装成功,>>>为python默认的提示符。
首先来一个经典的hello,world
查看源代码
打印帮助
1>>> print 'hello world'
2hello world
在此,有必要先认识一些系统内置的非常有用的一些函数
dir()函数用来显示一个类的所有属性和方法,如:
01>>> dir('this is a string')
02['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__
03ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__g
04t__', '__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
08git', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lst
09rip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit'
10, 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', '
11translate', 'upper', 'zfill']
12>>>
基本上,以__(双划线)开头的方法,你是不能直接通过类去调用(但可以通过其它的途径去调用),它们相当于php类中的”魔术方法”。
type()函数用来显示当前变量的类型,如:
01>>> m=1L
02>>> n=1
03>>> i=1.3
04>>> a='123'
05>>> b=range(10)
06>>> type(m)
07<type 'long'>
08>>> type(n)
09<type 'int'>
10>>> type(i)
11<type 'float'>
12>>> type(a)
13<type 'str'>
14>>> type(b)
15<type 'list'>
16>>> type(type(b))
17<type 'type'>
id()函数用来显示指定变量的内存地址
1>>> a=b=123
2>>> m=n='123'
3>>> id(a),id(b)
4(3178240, 3178240)
5>>> id(m),id(n)
6(5817024, 5817024)
7>>> m='1'
8>>> id(m)
95792000
help()顾名思义是用来查看帮助的
这里的代码请查看这里:http://www.webjx.com/program/zonghe-17850.html
另外,python的语句块是用缩进来标识的,不像c/c++/java/c#那样用{}来匹分,初次接触可能会不习惯,但时间长了,你会喜欢上这样的风格,因为它会让你感觉看所有的python代码都是一样的,不会出现参差不齐的”括号”风格。
页:
[1]