Skip to content

Instantly share code, notes, and snippets.

@espeed
Created December 13, 2013 01:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save espeed/7938637 to your computer and use it in GitHub Desktop.
Save espeed/7938637 to your computer and use it in GitHub Desktop.
FizzBuzz Example in Python
# 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
@espeed
Copy link
Author

espeed commented Dec 13, 2013

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:

"Programs must be written for people to read, and only incidentally for machines to execute."
-- Abelson & Sussman

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment