# C.1.1 9 - 3 -->; 6 # C.1.2 8 * 2.5 -->; 20 # C.1.3 9 / 2 -->; 4 # C.1.4 9 / -2 -->; -5 # C.1.5 9 % 2 -->; 1 # C.1.6 9 % -2 -->; -1 # C.1.7 -9 % 2 -->; 1 # C.1.8 9 / -2.0 -->; -4.5 # C.1.9 4 + 3 * 5 -->; 19 # C.1.10 (4 + 3) * 5 -->; 60 # C.2.1 print x -->; 100 # C.2.2 print x -->; 110 # C.2.3 print x -->; 130 # C.2.4 print x -->; 90 # C.2.5 print x -->; 40 # C.2.6 print x -->; 120 # C.2.7 print x -->; 24 # C.2.8 print x -->; 0 # C.3 x += x - x # x += (x - x) Python sees it has to evalutate the expression that the operator is to add to x # x += (3 - 3) Pyhton fills in the value for x, and evaluates it # x += 0 # x = x + 0 Python evaluates the operator # x = x # x = 3 # print x -->; 3 # C.4.1 1j + 2.4j -->; 3.4j # C.4.2 4j * 4j -->; (-16+0j) # C.4.3 (1+2j) / (3+4j) -->; (0.44+0.08j) # C.4.1 (1+2j) * (1+2j) -->; (-3+4j) # C.4.2 1+2j * 1+2j -->; (1+4j) # In the second version, python evaluates the multiplication first, and the the addition (This is why python always puts results of # complex math operations in parentheses) Python doesn't consider a complex number to be anything different from the sum of two real # numbers. # C.5.1 cmath.sin(-1.0+2.0j) -->; (-3.16577851322+1.95960104142j) # C.5.2 cmath.log(-1.0+3.4j) -->; (0.804718956217+2.0344439358j) # C.5.3 cmath.exp(-cmath.pi * 1.0j) -->; (-1+0j) (Actual output is (-1-1.22464679915e-16j) due to lack of floating point precision in the # cmath.exp function.) # C.5.4 cmath and math have tons of functions in common (sin, cos, exp, log, etc...) which would cause lots of name clashes that could # lead to subtle bugs. # C.6.1 "foo" + 'bar' -->; 'foobar' # C.6.2 "foo" 'bar' -->; 'foobar' # C.6.3 a + b -->; 'foobar' # C.6.4 a b -->; print a b # ^ # SyntaxError: invalid syntax # C.7 'A\nB\nC' # C.8 print 80* '-' # C.9 'first line\nsecond line\nthird line' # C.10 x=3 y=12.5 print 'The rabbit is %d.' % x print 'The rabbit is %d years old.' % x print '%.1f is average.' % y print '%.1f * %d' % (y,x) print '%.1f * %d is %.3g' % (y,x,x*y) # C.11 num = float(raw_input("Enter a number: ")) print num # C.12 def quadratic(a,b,c,x): return a * x ** 2 + b * x + c #If you didn't use '**' as a power operator, you could just do 'a * x * x' # C.13 def GC_content(dnastr): '''Converts a string of DNA information into a percentage representing the GC content of the DNA base pairs, as a floating point number''' return (float(dnastr.count('G')) + float(dnastr.count('C'))) / float(len(list(dnastr)))
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question