C:\Users\Administrator>python
Python 2.6.1 (r261:67517, Dec 4 2008, 16:51:00) [MSC v.1500 32 bit (Intel)] on win32
Type “help”, “copyright”, “credits” or “license” for more information.
>>> 1+3
4
>>> 1+3.
4.0
>>> 9-7.0
2.0
>>> 2-4
-2
>>> 2-4.0
-2.0
>>> 6*5
30
>>> 6*5.0
30.0
>>> 4/6
0
>>> 4/6.
0.66666666666666663
整除,即保留整数部分
>>> 6//2
3
>>> 6//4
1
求余
>>> 8%3
2
>>> 8%4
0
平方
>>> 3**2
9
>>> 3**3
27
>>> -3**3
-27
>>> (-3)**3
-27
内建函数
平方
>>> pow(2,3)
8
绝对值
>>> abs(-12)
12
将浮点数四舍五入成最接近的整数值
>>> round(1.0/2.0)
1.0
模块
>>>import math
>>>math.floor(32.9)
32.0
>>>int(math.floor(32.9))
32
>>>from math import sqrt
>>>sqrt(9)
3.0
>>>import cmath
>>>cmath.sqrt(-1)
1j
>>>cmath.sqrt(1)
(1+0j)
>>>(1+3j)*(9+4j)
(-3+31j)
字符串表示
值被转换成字符串的两种机制,一种是通过str函数,将值转换成合理形式的字符串,另一种是通过repr函数,它会创建一个字符串,以合法的python表达式的形式来表示值。
str与int,long一样是一种数据类型,而repr仅仅是一种函数
>>>print repr("hello world")
'hello world'
>>>print repr(1000L)
1000L
>>>print str("hello world")
hello world
>>>print str("1000L")
1000
>>>temp=35
>>>print "the temperature is" + str(temp)
the temperature is 35
input和raw_input的区别
>>> input("enter a number:")
enter a number:3
3
>>> raw_input("enter a number:")
enter a number:3
'3'
>>> input("enter a number:")
enter a number:xshow
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'xshow' is not defined
>>> input("enter a number:")
enter a number:"xshow"
'xshow'
长字符串
>>> print '''this is a very long string
... it continus here
... and it's not over yet
... "hello,world!"
... still here.'''
this is a very long string
it continus here
and it's not over yet
"hello,world!"
still here.