#Decimal to Binary #the decimal to convert convert= 64 #start with empty string result= '' for i in range(7,-1,-1): if convert >= 2**i: convert -= 2**i result+= '1' else: result += '0' print 'Result is', result #our binary to decimal converion #variable String convert = '0000 0001' #initalize result to zero result = 0 power =0 #loop ('i' is non-specific) for i in range(0,len(convert)): bit= convert [-i-1] #bit is either'1' or '0' if bit == '1': result +=(2**i) #remember bit is i power print 'Result is', result #while True: means an infinate loop #don't forget to define what you are printing g=100 x= 3 while True: if g**2 < x-1 or g**2 > x+1: g=(g+(x/g))/2 else: break print 'The square root of,', x,'is', g #the thing we're trying to sq. root with float x=50 #our guess g=100.0 #not(g**2 >= x-1 and g**2<= x+1); while g**2 < x-1 or g**2 > x+1: g=(g+(x/g))/2 print 'The square root of,', x,'is', g #finding square root # the thing we're trying to sq. root x=25 #our guess g=100 #not(g**2 >= x-1 and g**2<= x+1); while g**2 < x-1 or g**2 > x+1: g=(g+(x/g))/2 print 'The square root of,', x,'is', g
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