Chinaunix首页 | 论坛 | 博客
  • 博客访问: 19729155
  • 博文数量: 679
  • 博客积分: 10495
  • 博客等级: 上将
  • 技术积分: 9308
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-18 10:51
文章分类

全部博文(679)

文章存档

2012年(5)

2011年(38)

2010年(86)

2009年(145)

2008年(170)

2007年(165)

2006年(89)

分类: Python/Ruby

2009-08-16 07:03:09

0    前言和简介

 

       Python 3 不兼容python 2.*,为了兼容性起见,这里依旧学习python 2.6

       Python的定义:“an interpreted, object-oriented, high-level programming language with dynamic semantics.

       本书源码下载:

 

§1    快速入门:基础


2009-8-16

磁针石:xurongzhong#gmail.com

博客:oychw.cublog.cn



下载:

Hacking的参考:see EricRaymonds article How to Become a Hacker at .

Python的图形页面:IDLE, ,是交互式的python shell

除了标准的python之外有ActivePythonStackless PythonJython and IronPython

其他的IDE参见教材33页。

       ActivePython包含了很多扩展。

       Stackless Python修改了python内核代码,对普通用户影响不大。允许更深层次的递归和有更有效的多线程。网址:

       Jython () and IronPython ()pythonc语言之外的实现。Jythonjava实现,针对JVMIronPythonC#实现,针对.NETCLRMONO实现。语法和python有较大差异。

       python 3.0的变化:http://docs.python.org/dev/3.0/whatsnew/3.0.html.第三方模块的更新加入邮件组:python-announce-list。普通讨论:python-list;更多参见用户:comp.lang.python.announce and comp.lang.python。帮助:python-helphelp@python.org

           >>> print "hello,world"

hello,world

>>> 1/2

0

from __future__ import division,强制转换。Future一般用于调用以后版本会实现的功能。

>>> 1 / 2

0.5

>>> 1 // 2

0

>>> 1.0 // 2.0

0.0

>>> 2.75 % 0.5

0.25

注意这里支持浮点数。

乘方:

>>> 2 ** 3

8

>>> -3 ** 2

-9

>>> (-3) ** 2

9

长整数的处理:

>>> 1000000000000000000

1000000000000000000L

    2.2及以前版本处理的范围:2147483647 (or smaller than 2147483648

   

    16进制:

    >>> 0xAF

175

八进制:

>>> 010

8

变量必须赋值才能使用。

 

statement告诉计算机要做什么。

3.0print(42)print成为了函数。

获取输入:

>>> x = input("x: ")

x: 34

>>> y = input("y: ")

y: 42

>>> print x * y

1428

 

函数:

>>> pow(2,3)

8

>>> abs(-10)

10

>>> 1/2

0

>>> round(1.0/2.0)

1.0

截取整数部分的函数floor,在module中。

 

模块:

>>> import math

>>> math.floor(32.9)

32.0

>>> int(math.floor(32.9))

32

 

>>> math.ceil(32.1)

33.0

 

>>> from math import sqrt

>>> sqrt(9)

3.0

这样调用时就可以不用敲math

也可以使用别名:foo = math.sqrt

 

虚数的计算

>>> import cmath

>>> cmath.sqrt(-1)

1j

 

python本身支持虚数的运算,不需要导入cmath,以下也可以成功。

为了不让程序很快退出,可以添加:raw_input("Press ")

 

单双引号没有区别。

字符串的连接可以直接使用加号。

 

 

>>> print repr("Hello, world!")

'Hello, world!'

>>> print repr(10000L)

10000L

>>> print str("Hello, world!")

Hello, world!

>>> print str(10000L)

10000

 

Reprshell中的反引号类似,3.0中已经取消反引号,repr可以继续使用。

 

 

>>> print "The temperature is " + `temp`

The temperature is 42

 

原始输入

>>> raw_input("Enter a number: ")

Enter a number: 3

'3'

 

"""And one more string""",三个双或者单引号做分隔符,里面可以有不转义的单或双引号。

 

>>> print r'C:\nowhere'

C:\nowhere

 

>>> u'Hello, world!'

u'Hello, world!'

阅读(2215) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~