Skip to content

Instantly share code, notes, and snippets.

@danydodson
Last active February 21, 2025 08:39
Show Gist options
  • Select an option

  • Save danydodson/933164ff6a6f3d4828a93b22cd508618 to your computer and use it in GitHub Desktop.

Select an option

Save danydodson/933164ff6a6f3d4828a93b22cd508618 to your computer and use it in GitHub Desktop.
simple python
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Simple Programs\n",
"\n",
"The examples below will increase in number of lines of code and difficulty.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__[ 1 line ]__ Output\n",
"\n",
"---"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print('Hello, world!')\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__[ 2 lines ]__ Input, assignment\n",
"\n",
"---"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"name = 'Dany Dodson' \n",
"# name = input('What is your name?\\n')\n",
"\n",
"print('Hi, %s.' % name)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__[ 3 lines ]__ For loop, built-in enumerate function, new style formatting\n",
"\n",
"---"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"friends = ['john', 'pat', 'gary', 'michael']\n",
"\n",
"for i, name in enumerate(friends):\n",
" print(\"iteration {iteration} is {name}\".format(iteration=i, name=name))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__[ 4 lines ]__ Fibonacci, tuple assignment\n",
"\n",
"---"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"parents, babies = (1, 1)\n",
"\n",
"while babies < 100:\n",
" print('This generation has {0} babies'.format(babies))\n",
" parents, babies = (babies, parents + babies)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__[ 5 lines ]__ Functions\n",
"\n",
"---"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def greet(name):\n",
" print('Hello', name)\n",
"\n",
"\n",
"greet('Jack')\n",
"greet('Jill')\n",
"greet('Bob')\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__[ 6 lines ]__ Import, regular expressions\n",
"\n",
"---"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import re\n",
"\n",
"for test_string in ['555-1212', 'ILL-EGAL']:\n",
" if re.match(r'^\\d{3}-\\d{4}$', test_string):\n",
" print(test_string, 'is a valid US local phone number')\n",
" else:\n",
" print(test_string, 'rejected')\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__[ 7 lines ]__ Dictionaries, generator expressions\n",
"\n",
"---"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"prices = {'apple': 0.40, 'banana': 0.50}\n",
"\n",
"my_purchase = {'apple': 1, 'banana': 6}\n",
"\n",
"grocery_bill = sum(prices[fruit] * my_purchase[fruit]\n",
" for fruit in my_purchase)\n",
"\n",
"print('I owe the grocer $%.2f' % grocery_bill)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__[ 8 lines ]__ Command line arguments, exception handling\n",
"\n",
"---"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# This program adds up integers that have been passed as arguments in the command line\n",
"\n",
"import sys\n",
"\n",
"try:\n",
" total = sum(int(arg) for arg in sys.argv[1:])\n",
" print('sum =', total)\n",
"except ValueError:\n",
" print('Please supply integer arguments')\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__[ 9 lines ]__ Opening files\n",
"\n",
"---"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# indent your Python code to put into an email\n",
"import glob\n",
"\n",
"# glob supports Unix style pathname extensions\n",
"python_files = glob.glob('*.py')\n",
"\n",
"for file_name in sorted(python_files):\n",
" print(' ------' + file_name)\n",
"\n",
" with open(file_name) as f:\n",
" for line in f:\n",
" print(' ' + line.rstrip())\n",
"\n",
" print()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__[ 10 lines ]__ Time, conditionals, from..import, for..else\n",
"\n",
"---"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from time import localtime\n",
"\n",
"activities = {8: 'Sleeping',\n",
" 9: 'Commuting',\n",
" 17: 'Working',\n",
" 18: 'Commuting',\n",
" 20: 'Eating',\n",
" 22: 'Resting'}\n",
"\n",
"time_now = localtime()\n",
"hour = time_now.tm_hour\n",
"\n",
"for activity_time in sorted(activities.keys()):\n",
" if hour < activity_time:\n",
" print(activities[activity_time])\n",
" break\n",
"else:\n",
" print('Unknown, AFK or sleeping!')\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__[ 11 lines ]__ Triple-quoted strings, while loop\n",
"\n",
"---"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"REFRAIN = '''\n",
"%d bottles of beer on the wall,\n",
"%d bottles of beer,\n",
"take one down, pass it around,\n",
"%d bottles of beer on the wall!\n",
"'''\n",
"\n",
"bottles_of_beer = 9\n",
"\n",
"while bottles_of_beer > 1:\n",
" print(REFRAIN % (bottles_of_beer, bottles_of_beer,\n",
" bottles_of_beer - 1))\n",
" bottles_of_beer -= 1\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.10.4"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "dc523c05e5ae33db0947b27beaa52bdf1ff221fb2732535c6593644e0b701dae"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment