一,默认情况下,一个整数被另外一个整数除,计算结果小数部分被截除,只有整数部分
>>> 1/2 0 >>> 5/2 2
解决方法:用实数(包含小数点的十进制数)而不是整数进行运算;
>>> 5.0/2 2.5 >>> 5/2.0 2.5 >>> 5.0/2.0 2.5
二.在python中,双斜线执行整除,即使是浮点数也是保留整数部分
>>> 5//2 2 >>> 5.0//2 2.0 >>> 5//2.0 2.0 >>> 5.0//2.0 2.0
三,取余运算x%y的结果是x除以y的余数.
>>> 5%2 1 >>> 5.0%2 1.0 >>> 5%2.0 1.0 >>> 5.0%2.0 1.0
四,幂运算.
>>> 2**4 16 >>> -2**4 -16 >>> (-2)**4 16
五,长整形数:python可以处理非常大的整数,普通的整数不能大于2147483627,如果需要套数,可以使用长整型数,只是结尾有一L.长整数与普通整数可以混合使用,不用担心其区别.
>>> 8946549684654658749846*98745665465468546L+8394934 883433002231102047034574438007003738850L
六,十六进制与八进制,首数字是0就是八进制.
>>> 0xAF 175 >>> 0xaa 170 >>> 010 8 >>> 034 28
七,变量(variable):变量基本上就是代表某值的名字.在python中,变量不需要像Java一样申明是什么类型.
>>> x = 5 >>> x*4 20
八,获取用户输入(input函数)
>>> input("The meaning of life:") The meaning of life:90 90 >>> x = input("input x value:") input x value:100 >>> print x 100 >>> x * 200 20000
九,函数(pow):幂运算可以是**,同时也可以用函数来实现.也可以自己定义函数,把python提供的标准函数叫内建函数.如abs得到绝对值,round获取四舍五入的值.
>>> 2 ** 4 16 >>> pow(2,4) 16 >>> abs(-10) 10 >>> round(5/2) 2.0
十,模块,想要用到python其它功能,而需要用import来导入模块.(2种导入方式,一种导入此模块的全部功能,另一种导入此模块中的特定功能)
>>> import math >>> math.floor(32.9) 32.0 >>> math.sqrt(9) 3.0 >>> from math import sqrt >>> sqrt(16) 4.0
十一,python提供了对复数的支持,需要导入特定有模块cmath.
>>> sqrt(-1) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: math domain error >>> import cmath >>> cmath.sqrt(-1) 1j >>> (1+3j)*(9+4j) (-3+31j)
二,字符串
1.拼接字符串
>>> "hello."+"world!" 'hello.world!' >>> x="hello," >>> y="world!" >>> x+y 'hello,world!'
2.字符串表示,str与repr
>>> "Hello,world!" 'Hello,world!' >>> print "Hello,world!" Hello,world! >>> 10000L 10000L >>> print 10000L 10000
以上看到,用Print的时候没有引号,同时数值L也去掉了.这是2种机制,一是通过str函数,会把值转换为合理的形式的字符串,方便用户理解.而repr会创建一个字符串,以合法的Python表达式的形式来表示值.
>>> print repr("Hello,world!") 'Hello,world!' >>> print repr(10000L) 10000L >>> print str("Hello,world!") Hello,world! >>> print str(10000L) 10000
总之:str与repr是将Python值转换成字符串的2种方式,函数str让字符串更易于阅读,而repr则把结果字符串转换成合法的Python表达式.
3.input与raw_input的比较
在文本中输入程序
name = input("What is your name?")name print "Hello."+name+"!"
这是一个合法的程序,但是运行会不行,
bluceshang@bluceshang:~/python_demo$ python input_or_rawinput.py What is your name?sss Traceback (most recent call last): File "input_or_rawinput.py", line 4, in <module> name = input("What is your name?") File "<string>", line 1, in <module> NameError: name 'sss' is not defined bluceshang@bluceshang:~/python_demo$ python input_or_rawinput.py What is your name?"sss" Hello.sss! bluceshang@bluceshang:~/python_demo$
这样要求用户带着引号输入他们的名字是过分的.因此这时需要使用raw_input函数,它会把所有的输入的当作原始数据(raw data),然后将其放入字符串中.
bluceshang@bluceshang:~/python_demo$ python input_or_rawinput.py What is your name?sss Hello.sss! >>> input("Enter a number:") Enter a number:3 3 >>> raw_input("Enter a number:") Enter a number:3 '3'
4.长字符串
如果需要一个非常长的字符串,需要跨钓竿,可以使用三个引号代替普通的引号,当然也可以使用三个""",eg:"""Like This""".因为这种与众不同的方式,可以在字符串中同时使用单引号与双引号,而不需要使用反斜线转义.
>>> print '''This ia a very long string. ... It continues here. ... And it's not over yet. ... "Hello,world!" ... Still here.''' This ia a very long string. It continues here. And it's not over yet. "Hello,world!" Still here.