标签:场景 error oat 模块 rom 2.0 文件 amt 包含
# -*- coding:utf-8 -*- #1.4 #除法 print 1 / 2 print 1.0 / 2 print 10 / 3 print 10.0 / 3.0 print int(1.0/2) print float(1/2) #如果使用“//”,那么就算是浮点数,双斜线也会执行整除 print 1 // 2 print 1.0 // 2.0 #取余 print 10 % 3 print 2.75 % 0.5 print int(2.75 % 0.5) #乘方运算 print 2 ** 3 print -3 ** 2 print (-3) ** 2 #幂运算符比取反的优先级要高 #长整数 print 1000000000000000000 print 11234567890123456L * 123456789345673456L + 100 #十六进制与八进制数 print 0xAF print 010
输出如下:
0 0.5 3 3.33333333333 0 0.0 0 0.0 1 0.25 0 8 -9 9 1000000000000000000 1386983681400638600588387302184036 175 8
除法运算:单斜线与双斜线
# x = input("x= ") y = input("y= ") print x * y # 输出如下: x= 12 y= 23 276
x1 = raw_input("x1= ") y1 = raw_input("y1= ") print x1 * y1
File "ex1-7.py", line 8 y2 = int(raw_input("y2= ") ^ SyntaxError: invalid syntax
x2 = int(raw_input("x2= ") y2 = int(raw_input("y2= ") print x2 * y2 x3 = int(raw_input("x3= ") y3 = raw_input("y3= ") print x3 * y3
输出如下:
x2= 100 y2= 23 2300 x3= 10 y3= 123 123123123123123123123123123123
raw_input()函数对于所有的输入都当作字符串来处理
print pow(2,10) print abs(-10) print 1/2 print round(1.0/2.0) # 结果如下: 1024 10 0 1.0 >>> divmod(5,2) (2, 1)
import math print math.floor(32.9) print int(math.floor(32.9)) x = math.ceil(12.1001) print x print int(x)
# 输出如下: 32.0 32 13.0 13
from math import floor from math import ceil from math import sqrt x = floor(23.89) y = ceil(12.001) z = sqrt(100.0) print "x is %s, y is %s, z is %s." % (x,y,z) # 输出如下: x is 23.0, y is 13.0, z is 10.0.
>>> from math import sqrt >>> 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)
#!/usr/bin/python
chomod a+x hello.py
#!/usr/bin/python2.6 print "Hello,World!" print "This‘s a test." print "let\‘s go!" print ‘let\‘s go!‘ print "\"Hello,World!\",she said."
Hello,World! This‘s a test. let‘s go! let‘s go! "Hello,World!",she said.
字符串拼接:
>>> "let‘s say " ‘Hello,world‘ "let‘s say Hello,world" >>> x = "Hello, " >>> y = "World!" >>> x y File "<stdin>", line 1 x y ^ SyntaxError: invalid syntax >>> x + y ‘Hello, World!‘ >>> x,y (‘Hello, ‘, ‘World!‘)
>>> print repr("Hello,world!") ‘Hello,world!‘ >>> print str("Hello,world!") Hello,world! >>> print str(100000L) 100000 >>> temp = 42 >>> print "The temperature is :",temp The temperature is : 42 >>> print "The temperature is :" + temp Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: cannot concatenate ‘str‘ and ‘int‘ objects >>> print "The temperature is :" + `temp` The temperature is :42
str函数可以把值转换为合理性似的字符串,以便用户理解
>>> print "C:\\Users\\鹏飞\\Desktop\\新建文件夹" C:\Users\鹏飞\Desktop\新建文件夹 >>> print "C:\Users\鹏飞\Desktop\新建文件夹" C:\Users\鹏飞\Desktop\新建文件夹 >>> print "C:\nUsers\鹏飞\Desktop\新建文件夹" C: Users\鹏飞\Desktop\新建文件夹 >>> print "C:\\nUsers\鹏飞\Desktop\新建文件夹" C:\nUsers\鹏飞\Desktop\新建文件夹
对于一些特殊的符号,我们在输出时需要用到反斜线进行转义,但是当特殊符号比较多的时候就非常麻烦了,这时候原始字符串就派上用场了。在原始字符串中输入的每个字符串都会与书写的方式保持一致:
>>> print r‘C:\nowwhere‘ C:\nowwhere >>> print "C:\nowwhere\zbc\xyz\001" ValueError: invalid \x escape >>> print r"C:\nowwhere\zbc\xyz\001" C:\nowwhere\zbc\xyz\001
>>> print r‘let‘s go‘ File "<stdin>", line 1 print r‘let‘s go‘ ^ SyntaxError: invalid syntax >>> print ‘let\‘s go‘ let‘s go >>> print r‘let\‘s go‘ let\‘s go
但是,不能再原始字符串结尾输入反斜杠,除非你对反斜杠进行了转义:
>>> print r"This is illegal\" File "<stdin>", line 1 print r"This is illegal\" ^ SyntaxError: EOL while scanning string literal >>> print r"This is illegal \\" This is illegal \>>> print "This is illegal \\" This is illegal \
Unicode字符串:
>>> u"hello,world" u‘hello,world‘ >>> u"hello,world 测试" u‘hello,world \u6d4b\u8bd5‘ >>> r"hello,world 测试" ‘hello,world \xe6\xb5\x8b\xe8\xaf\x95‘
1.12 小结
#!/usr/bin/python # -*- coding:utf-8 -*- # import math import cmath x1 = int(raw_input("Please input the num of x1 :")) y1 = input("Please input the num of y1 :") z1 = raw_input("Please input some words: ") print "x1 * y1 =",x1 * y1 print "x1 * z1 =",x1 * z1 print "ceil(12.001) = ",math.ceil(12.001) print "floor(32.998) = ",math.floor(32.998) print "sqrt(16) = ",math.sqrt(16) print "cmath.sqrt(-15) =",cmath.sqrt(-15) print "round(10.0/3.0) =",round(10.0/3.0) print "abs(-101) =",abs(-101) # 输出如下: Please input the num of x1 :6 Please input the num of y1 :10 Please input some words: xyz x1 * y1 = 60 x1 * z1 = xyz xyz xyz xyz xyz xyz ceil(12.001) = 13.0 floor(32.998) = 32.0 sqrt(16) = 4.0 cmath.sqrt(-15) = 3.87298334621j round(10.0/3.0) = 3.0 abs(-101) = 101
标签:场景 error oat 模块 rom 2.0 文件 amt 包含
原文地址:http://www.cnblogs.com/zhangpf/p/7593586.html