可爱的python之切片操作符(Slice)

2009年11月18日 4:33 下午  |  分类:python

什么是切片?

字符串、列表、元组在python中都符合“序列”这一特征,只要符合这一特征的变量我们都可以用切片(slice)去存取它们的任意部分。我们可以把序列想像成一个队列,我可能需要前面三位、后面三位、或从第三位后的四位、或隔一个取一个等,我们用切片操作符来实现上述要求。

切片操作符在python中的原型是
[start:stop:step]

即:[开始索引:结束索引:步长值]

开始索引:同其它语言一样,从0开始。序列从左向右方向中,第一个值的索引为0,最后一个为-1

结束索引:切片操作符将取到该索引为止,不包含该索引的值。

步长值:默认是一个接着一个切取,如果为2,则表示进行隔一取一操作。步长值为正时表示从左向右取,如果为负,则表示从右向左取。步长值不能为0

继续阅读 »

可爱的python之语句与语法

2009年11月15日 5:25 下午  |  分类:python

注释(#)

python中的注释是以#开头的,不像c++中有/**/之类的多行注释。

继续(\):表示继续上一行,通常情况下,一行表示一个语句,一行过长的语句可以使用\进行分解

语句块(:):在c#/php/javascript/c++中,对于复合语句是用{}来标识,而在python中,是用:配合缩进来实现语句块,如:

if a>0:
    #todo
else:
    #todo

继续阅读 »

可爱的python之快速入门

2009年11月14日 6:49 下午  |  分类:python

python所支持的数据类型:
整型、长整型、布尔型、浮点型、复数、字符串、列表(list)、元组(Tuple)、字典(Dict)、object

python能支持理论意义上的长整型,如C#中的长整型是Int64,即最大值为2的64次方,而python中的长整型只与机器的虚拟内存大小有关,你完全不用考虑溢出这样的异常。

python是一种动态语言,虽然在定义变量时无需定义变量的类型,但并不意味着在运行的过程中,系统会自动更改变量的类型,所以,python是一种强类型的动态语言,它与asp/javascript/php完全不一样,比如:

>>> a=123
>>> print 'input '+ a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
>>>

继续阅读 »

可爱的python之hello,world!

2009年11月13日 9:57 下午  |  分类:python

python3.0已推出,但据说很多库都不能用了,建议使用2.6版本,我目前使用的是2.5版,与2.6版差距不大。

注意:2.6版本开始,print需要加上括号,否则会提示语法错误。

安装python运行环境:

  1. 下载for windows的安装包,http://www.python.org/,不过,正式对外的下载地址被和谐了,请移步到这里下载:http://www.python.org/ftp/python/
  2. 运行下载的.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之旅吧。

Pages: « 1 2 3 4 5 6 7 ...111 112 113 »