{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Python Basics"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Whitespace Is Important"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"is odd\n",
"2\n",
"is even\n",
"3\n",
"is odd\n",
"4\n",
"is even\n",
"5\n",
"is odd\n",
"6\n",
"is even\n",
"All done.\n"
]
}
],
"source": [
"listOfNumbers = [1, 2, 3, 4, 5, 6]\n",
"\n",
"for number in listOfNumbers:\n",
" print(number)\n",
" if (number % 2 == 0):\n",
" print(\"is even\")\n",
" else:\n",
" print(\"is odd\")\n",
" \n",
"print (\"All done.\")\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Importing Modules"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[26.26896031 20.46757767 24.7393334 18.5804176 28.10939893 24.44392283\n",
" 22.49569857 33.07079592 26.10290179 23.43825608]\n"
]
}
],
"source": [
"import numpy as np\n",
"\n",
"A = np.random.normal(25.0, 5.0, 10)\n",
"print (A)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Lists"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6\n"
]
}
],
"source": [
"x = [1, 2, 3, 4, 5, 6]\n",
"print(len(x))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x[:3]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[4, 5, 6]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x[3:]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[5, 6]"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x[-2:]"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3, 4, 5, 6, 7, 8]"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x.extend([7,8])\n",
"x"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3, 4, 5, 6, 7, 8, 9]"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x.append(9)\n",
"x"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[1, 2, 3, 4, 5, 6, 7, 8, 9], [10, 11, 12]]"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y = [10, 11, 12]\n",
"listOfLists = [x, y]\n",
"listOfLists"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"11"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y[1]"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3]"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"z = [3, 2, 1]\n",
"z.sort()\n",
"z"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[3, 2, 1]"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"z.sort(reverse=True)\n",
"z"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Tuples"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Tuples are just immutable lists. Use () instead of []\n",
"x = (1, 2, 3)\n",
"len(x)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y = (4, 5, 6)\n",
"y[2]"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(1, 2, 3), (4, 5, 6)]"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"listOfTuples = [x, y]\n",
"listOfTuples"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"32\n",
"120000\n"
]
}
],
"source": [
"(age, income) = \"32,120000\".split(',')\n",
"print(age)\n",
"print(income)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Dictionaries"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Janeway\n"
]
}
],
"source": [
"# Like a map or hash table in other languages\n",
"captains = {}\n",
"captains[\"Enterprise\"] = \"Kirk\"\n",
"captains[\"Enterprise D\"] = \"Picard\"\n",
"captains[\"Deep Space Nine\"] = \"Sisko\"\n",
"captains[\"Voyager\"] = \"Janeway\"\n",
"\n",
"print(captains[\"Voyager\"])"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Kirk\n"
]
}
],
"source": [
"print(captains.get(\"Enterprise\"))"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"None\n"
]
}
],
"source": [
"print(captains.get(\"NX-01\"))"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enterprise: Kirk\n",
"Enterprise D: Picard\n",
"Deep Space Nine: Sisko\n",
"Voyager: Janeway\n"
]
}
],
"source": [
"for ship in captains:\n",
" print(ship + \": \" + captains[ship])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Functions"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4\n"
]
}
],
"source": [
"def SquareIt(x):\n",
" return x * x\n",
"\n",
"print(SquareIt(2))\n"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"9\n"
]
}
],
"source": [
"#You can pass functions around as parameters\n",
"def DoSomething(f, x):\n",
" return f(x)\n",
"\n",
"print(DoSomething(SquareIt, 3))"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"27\n"
]
}
],
"source": [
"#Lambda functions let you inline simple functions\n",
"print(DoSomething(lambda x: x * x * x, 3))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Boolean Expressions"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n"
]
}
],
"source": [
"print(1 == 3)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
]
}
],
"source": [
"print(True or False)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n"
]
}
],
"source": [
"print(1 is 3)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"All is well with the world\n"
]
}
],
"source": [
"if 1 is 3:\n",
" print(\"How did that happen?\")\n",
"elif 1 > 3:\n",
" print(\"Yikes\")\n",
"else:\n",
" print(\"All is well with the world\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Looping"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n",
"3\n",
"4\n",
"5\n",
"6\n",
"7\n",
"8\n",
"9\n"
]
}
],
"source": [
"for x in range(10):\n",
" print(x)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"2\n",
"3\n",
"4\n",
"5\n"
]
}
],
"source": [
"for x in range(10):\n",
" if (x is 1):\n",
" continue\n",
" if (x > 5):\n",
" break\n",
" print(x)"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n",
"3\n",
"4\n",
"5\n",
"6\n",
"7\n",
"8\n",
"9\n"
]
}
],
"source": [
"x = 0\n",
"while (x < 10):\n",
" print(x)\n",
" x += 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Activity"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Write some code that creates a list of integers, loops through each element of the list, and only prints out even numbers!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 1
}