Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 148 additions & 0 deletions assignments/A01_Python_Functions.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Assignment 1: Python Functions\n",
"## Computational Methods in Psychology (and Neuroscience)\n",
"### Psychology 4500/7559 --- Fall 2020\n",
"By: Per B. Sederberg, PhD\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Objectives\n",
"\n",
"Upon completion of this assignment, the student will be able to:\n",
"\n",
"1. Create and run a Python script\n",
"\n",
"2. Demonstrate the use of basic math and comparison operators\n",
"\n",
"3. Use conditional statements in Python\n",
"\n",
"4. Use the \"for\" statement to create loops\n",
"\n",
"5. Create custom functions, using parameters to make them generalizable\n",
"\n",
"6. Document code with in-code comments and docstrings\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Assignment\n",
"\n",
"- Read Think Python chapters 3, 5, and 6 (Functions, Conditionals and Recursion, and Fruitful Functions). Be sure to open a new Jupyter Notebook and type in \n",
" the examples as you read through the text. You can also try the Exercises\n",
" at the end of each chapter.\n",
"\n",
"- After you have read the chapters, write code in *this notebook* (***after making a copy and renaming it to have your userid in the title --- e.g., A01_Python_Functions_mst3k***) that performs the \n",
" following tasks:\n",
"\n",
" 1. Get two numbers from the user \n",
" 2. Compare the numbers\n",
" 3. If the first number is smaller than the second, calculate the mean\n",
" 4. If the first number is larger than the second, calculate the difference\n",
" 5. If the two numbers are the same, calculate the factorial using a custom function\n",
" 6. Display the results (indicating what operation you did)\n",
"\n",
"- Test your code and debug as necessary.\n",
"\n",
"- ***When you are done, save this notebook as HTML (`File -> Download as -> HTML`) and upload it to the matching assignment on UVACollab.***\n",
"\n",
"## HINTS\n",
"\n",
"- Be sure to comment your code\n",
"- Make use of the `input` function to get numbers (see examples from class)\n",
"- Use the template below to calculate the factorial using a non-recursive\n",
" function\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Name: (your name here)\n",
"# User ID: (your userid here)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# ***finish this function replacing the `pass` with your code***\n",
"def factorial(n):\n",
" \"\"\"\n",
" Add your docstring here\n",
" \"\"\"\n",
" # Check the input\n",
" pass\n",
"\n",
" # make sure the input is an integer greater than or equal to 1\n",
" if type(n) is not int:\n",
" # Let the user know you need an integer\n",
" pass\n",
" \n",
" # Initialize a variable to 1\n",
" pass\n",
"\n",
" # Successively multiply the variable by the integers 2 up to n\n",
" for blah in range(blahblah):\n",
" # do something here\n",
" pass\n",
" \n",
" # return the result\n",
" return result\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# ***your code here for reading in the numbers and operating correctly***\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Challenge problem (not required, but fun!)\n",
"\n",
"- Re-write the factorial function using a recursive algorithm.\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.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
3 changes: 3 additions & 0 deletions quotes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"The hardest thing to do is to be true to yourself, especially when everybody is watching." -Dave Chappelle