#IF AND ELIF # elif statements are use to avoid excessive indentation and are a substitute for the switch or case statements in other languages """ x = int(raw_input("Please enter an integer: ")) if x < 0: x = 0 print 'Negative changed to zero' elif x == 0: print 'Zero' elif x == 1: print 'Single' else: print 'More' """ #FOR STATEMENTS # for statements are used to iterate over items in a sequence, that is a list or string, in the order they appear within the sequence name = 'johndoe' for n in name: print n, #the comma keeps the printed result in one line print('') fruit = ['apple', 'banana', 'cherry'] for f in fruit: print f,',', f[:3] #range() function # this function produces a list of numbers print range(10), 'these are the numbers in range(10)' print range(2, 10), 'these are the numbers in range(2, 10)' print range(2,10)[0] #indexing the range since a List of numbers is produced #break statement # this statement exits the smallest enclosing in a for or while loop for n in range(2, 10): for x in range(2, n): if n % x == 0: print n, 'equals', x, '*', n/x break # else: # loop fell through without finding a factor print n, 'is a prime number'
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