Last active
June 6, 2017 19:16
-
-
Save mGalarnyk/cba02c54fb984ce5f26253ae7cd9fc8a to your computer and use it in GitHub Desktop.
Python FizzBuzz for the Blog Post: https://medium.com/@GalarnykMichael/python-basics-8-fizzbuzz-441e97c6c767
This file contains hidden or 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
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h1 align=\"center\"> FizzBuzz </h1>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Write a program that prints the numbers from 1 to 20. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1\n", | |
"2\n", | |
"Fizz\n", | |
"4\n", | |
"Buzz\n", | |
"Fizz\n", | |
"7\n", | |
"8\n", | |
"Fizz\n", | |
"Buzz\n", | |
"11\n", | |
"Fizz\n", | |
"13\n", | |
"14\n", | |
"FizzBuzz\n", | |
"16\n", | |
"17\n", | |
"Fizz\n", | |
"19\n", | |
"Buzz\n" | |
] | |
} | |
], | |
"source": [ | |
"# Solution 1\n", | |
"# Concatenating Strings\n", | |
"for num in range(1,21):\n", | |
" string = \"\"\n", | |
" if num % 3 == 0:\n", | |
" string = string + \"Fizz\"\n", | |
" if num % 5 == 0:\n", | |
" string = string + \"Buzz\"\n", | |
" if num % 5 != 0 and num % 3 != 0:\n", | |
" string = string + str(num)\n", | |
" print(string)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1\n", | |
"2\n", | |
"Fizz\n", | |
"4\n", | |
"Buzz\n", | |
"Fizz\n", | |
"7\n", | |
"8\n", | |
"Fizz\n", | |
"Buzz\n", | |
"11\n", | |
"Fizz\n", | |
"13\n", | |
"14\n", | |
"FizzBuzz\n", | |
"16\n", | |
"17\n", | |
"Fizz\n", | |
"19\n", | |
"Buzz\n" | |
] | |
} | |
], | |
"source": [ | |
"# Solution 2\n", | |
"# Utilizing if, elif, and else\n", | |
"\n", | |
"# an elif statement allows you to check multiple expressions\n", | |
"# for True and execute a block of code as soon as one of \n", | |
"# the conditions evaluates to True\n", | |
"\n", | |
"for num in range(1, 21):\n", | |
" if num % 3 == 0 and num % 5 == 0:\n", | |
" print('FizzBuzz')\n", | |
" elif num % 3 == 0:\n", | |
" print('Fizz')\n", | |
" elif num % 5 == 0:\n", | |
" print('Buzz')\n", | |
" else:\n", | |
" print(num)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Python 2 vs Python 3 (range function differences)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# python 2 xrange and Python 3 range are same (resembles a generator)\n", | |
"# python 2 range generates a list\n", | |
"\n", | |
"# Importance: Long lists are slow" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**if this tutorial doesn't cover what you are looking for, please leave a comment on the youtube video and I will try to cover what you are interested in. (Please subscribe if you can!)**" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"https://www.youtube.com/watch?v=XR1QFrbPRnw" | |
] | |
} | |
], | |
"metadata": { | |
"anaconda-cloud": {}, | |
"kernelspec": { | |
"display_name": "Python [conda env:py36]", | |
"language": "python", | |
"name": "conda-env-py36-py" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.1" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment