#example 1 """decrement down to 'blastoff' n = 5 while n > 0: print n n = n - 1 print "Blastoff!" #example 2 infinite loops and breaks while True: line = raw_input('> ') if line == 'done': break print line print 'Done!' #example 3 infinite loops and continue while True: line = raw_input('> ') if line[0] == "#": continue if line == 'done': break print line print 'Done!' #example 4 for loops friends = ['Joseph', 'Glenn', 'Sally'] for friend in friends: print 'Happy New Year: ', friend print 'Done!' """ #example 5 #another for loop count = 0 for itervar in [3, 41, 12, 9, 74, 15]: count = count + 1 print 'Count: ', count #example 6 #an iteration loop that computes total set of numbers total = 0 for itervar in [3, 41, 12, 9, 74, 15]: total = total + itervar print 'Total: ', total #example 7 #find the largest value in a list or sequence largest = None print 'Before: ', largest for itervar in [3, 41, 12, 9, 74, 15]: if largest is None or itervar > largest: largest = itervar print 'Loop: ', itervar, largest print 'largest: ', largest #example 8 #find smallest value in a list or sequence smallest = None print 'Before: ', smallest for itervar in [3, 41, 12, 9, 74, 15]: if smallest is None or itervar < smallest: smallest = itervar print 'Loop: ', itervar, smallest print 'Smallest: ', smallest #example 9 #using min() and max() functions def min(values): value = float(values) smallest = None for value in values: if smalles is None or value < smallest: smallest = value return smallest print smallest values = raw_input('Enter values: ') min(values)
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