Created
December 13, 2013 01:35
-
-
Save espeed/7938637 to your computer and use it in GitHub Desktop.
FizzBuzz Example in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# FizzBuzz Example in Python | |
# by James Thornton, http://jamesthornton.com | |
for num in range (1, 101): | |
fizz = "" if num % 3 else "FIZZ" | |
buzz = "" if num % 5 else "BUZZ" | |
print fizz + buzz if fizz or buzz else num |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This version of FizzBuzz makes use of string concatenation and the fact that Python treats empty strings as False.
There's a famous quote in the preface to the first edition of SICP that says:
See http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-7.html#%_chap_Temp_4
Python has some nice syntactic sugar that makes code more readable, which you discover as you get more into it.
It's often said that Python is executable pseudocode, meaning that Python code looks similar to what you would write on the blackboard if you were describing the procedure in English.