\n",
"\n",
"## Good afternoon\n",
"\n",
"Goal today is to give you a whirlwind introduction to the Python programming language. If you know Python already, today will be boring for you. I will not be offended if you chose to open your devices and do other work now. Because, in some ways, this will be a grueling introduction of line by line code, I will have a brief break (I wouldn't be surprised if our numbers dwindle as we go along -- no worries). Goal is to get you set up and comfortable exploring Python. As part of this the first Homework will test that you can install new packages, find functionality that you need, write your own functions, use Jupyther Notebooks, load, analzye, visualize data, and along the way, demonstrate some use of basic Python operations/flow control/data types. \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Installation\n",
"\n",
"#### Mac\n",
"If you are on a Mac, please make sure that XCode is installed!!! This takes awhile, and can be done through the App store. The pre-installed Python is old, not to be used here, but don't remove it!\n",
"\n",
"#### Windows\n",
"If you are using Windows, note that you will need to use Chrome or Firefox (note Edge) for Jupyter Notebooks.\n",
"\n",
"#### How to install\n",
"There are several ways to install Python locally on your computer. \n",
"\n",
"Installation method 1 (old school): You can download a binary of Python directly from the official [Python site](https://www.python.org/) corresponding to your platform. Once you follow the corresponding instructions for the installation process, you can launch the Python interpreter as described below. You should also install Jupyter Lab or Jupyter Notebook from [here](https://jupyter.org/install) using the pip python package manager.\n",
"\n",
"Installation method 2 (recommended): is to use the Anaconda package manager. If you already have Anacona froma Python < 3.8, please [uninstall it](https://docs.anaconda.com/anaconda/install/uninstall/). \n",
"\n",
"Now, to install Anaconda (and up-to-date version of Python, too), just download the installler from [here](https://www.anaconda.com/distribution/) and follow the instructions to install Python 3.8. Go ahead and submit your hopkins email address when you register. As the install proceeds, make sure that you install Anaconda to *your* home directory, not to root. \n",
"\n",
"Option: Install [node.js](https://nodejs.dev/) for additional Jupyter features."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Launch \n",
" \n",
"### To the interpreter!\n",
"\n",
"Each line of python code is translated into instructions for your machine. This is different than compiled languages like C, in which a program is translated into machine format prior to execution. The Python interpreter is what does this translation. If Python is installed, you can launch the interpreter Unix-like systems from the command line ... (walk through process of finding Terminal, etc)\n",
"\n",
"```\n",
"Python\n",
"```\n",
"\n",
"### Jupyter Notebook\n",
"Here in this class, I am never (at least as far as I can predict) going to illustrate Python analysis by accessing the interpreter via the command line. Rather, we are going to use what are called [Jupyter Notebooks](https://jupyter.org/). These notebooks are files that contain executable code, and non-executable content such as text and images. They were designed to enourage both reproducible analysis, and to facilitate productive exploration of data. They are also great for teaching. Jupyter should have been installed using either of the methods listed above. You can launch it from the terminal with either"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```\n",
"jupyter-lab # installed from pip\n",
"\n",
"# or\n",
"\n",
"jupyter lab # installed from anaconda\n",
"\n",
"# or, if you are using Jupyter Notebook\n",
"\n",
"jupyter notebook\n",
"```\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Walk through process and Lab IDE, and components of a Notebook"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Google Colab\n",
" \n",
"[Google Colab](https://colab.research.google.com/) is a cloud-backed compute platform. To save your notebooks you will need to do so either to Google Drive or to Github. If you intend to use the latter, then you must get a Github account (free).\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Homeworks\n",
"\n",
"You will use Jupyter Notebooks to complete your homework. You can either install Python and Jupyter on your own system, or, you can use Google Colab. You will need to turn in .ipynb and .html files. Saving a Notebook to .html is easy with Jupyter (show how). There are several ways to do so from Colab, including this [method](https://python.plainenglish.io/how-to-convert-google-colab-notebook-ipynb-to-html-ccfeda199246)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Python fundamentals\n",
"\n",
"### Built in data types and variables\n",
"\n",
"Let's start with the canonical 1st line of code when you are learning a new language, *Hellow, World!*"
]
},
{
"cell_type": "code",
"execution_count": 86,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello, World\n"
]
}
],
"source": [
"print(\"Hello, World\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What can we learn from this trivial example? First, that Python has built-in functions, like `print()`. And we can see that this function takes as an **argument** a **string**, and it displays this string in the standard output.\n",
"\n",
"You might ask: \"What is an *argument*?\"\n",
"\n",
"The official definition from PC Magazine:\n",
"\n",
"> In programming, a value that is passed between programs, subroutines or functions. Arguments are independent items, or variables, that contain data or codes. When an argument is used to customize a program for a user, it is typically called a \"parameter.\"\n",
"\n",
"OK, so what is a `string`? In this case, a string is a built-in data type that represents sequences of characters. Another built in data type is an `int` that represents positive and negative whole numbers. \n"
]
},
{
"cell_type": "code",
"execution_count": 87,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2037035976334486086268445688409378161051468393665936250636140449354381299763336706183397377"
]
},
"execution_count": 87,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"314\n",
"-1\n",
"0\n",
"2**300 + 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that the size of Python's integers is limited by machine memory -- not by byte size.\n",
"\n",
"There are other several built-in data types, including:\n",
"\n",
"- bool: boolean, True or False\n",
"- float: floting point double precision numbers\n",
"\n",
"We will come back to booleans later today."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's go back to strings for a minute, and talk about *object references* or variables. Variables are ways to store values or data, refer to them later and manipulate or use them. Some important aspects of Python variables:\n",
"- There are some restrictions on variable names, or identifiers (e.g. cannot clash with keywords, need to start with _ or character). \n",
"- dynamic typing (you do not need to tell the interpreter what data type they will point to\n",
"\n",
"An example will help to illustrate how they work:"
]
},
{
"cell_type": "code",
"execution_count": 88,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Winter Spring Winter\n"
]
}
],
"source": [
"a = \"Winter\"\n",
"b = \"Spring\"\n",
"c = a\n",
"print(a, b, c)"
]
},
{
"cell_type": "code",
"execution_count": 89,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Winter Spring Spring\n"
]
}
],
"source": [
"c = b\n",
"print(a, b, c)"
]
},
{
"cell_type": "code",
"execution_count": 90,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Spring Spring Spring\n"
]
}
],
"source": [
"a = c\n",
"print(a, b, c)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Like I said before, strings can be thought of as sequences of characters. Python has nice functionality for dealing with sequences of things. For example, square brackets ([]) instruct the interpreter to fetch the indicated item in the given sequence. Let's assign the first sentence of a [classic stem cell paper](https://www.ncbi.nlm.nih.gov/pmc/articles/PMC300599/) to a variable:"
]
},
{
"cell_type": "code",
"execution_count": 93,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"m\n",
"N\n"
]
}
],
"source": [
"first_sent ='''Normal mammalian hemopoietic tissue produces a continuous supply of differentiated blood cells whose functions are essential for life.'''\n",
"print(first_sent[3])\n",
"\n",
"print(first_sent[0])\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What you can see from this example is that Python starts indexing sequences at 0, not at 1. Also, does not automatically parse this string into words. Finally, note that the triple quotes act to escape line breaks, quotes, etc:"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Normal mammalian hemopoietic tissue produces a continuous\n",
"supply of differentiated blood cells whose functions are essential for life.\n"
]
}
],
"source": [
"first_sent ='''Normal mammalian hemopoietic tissue produces a continuous\n",
"supply of differentiated blood cells whose functions are essential for life.'''\n",
"print(first_sent)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`type()` returns the datatype of a variable"
]
},
{
"cell_type": "code",
"execution_count": 94,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"str"
]
},
"execution_count": 94,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = \"42\"\n",
"type(a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python provides functions to convert between types with mostly the outcomes that you would expect:`type()` returns the datatype of a variable"
]
},
{
"cell_type": "code",
"execution_count": 95,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"42\n"
]
},
{
"data": {
"text/plain": [
"int"
]
},
"execution_count": 95,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b = int(a)\n",
"print(b)\n",
"type(b)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"42"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"int(42)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Collection data types\n",
"\n",
"Lots of times, you want to store more than one value or object; you want something like a collection or list. Python has the following four data types (or *classes*) that can hold more than one object:\n",
"\n",
"- list: holds arbitrary number of objects / values. can be altered (mutable). entries are ordered\n",
"- tuple: same as list, except it **cannot** be altered (immutable)\n",
"- dictionary: holds arbitrary number of objects / values. can be altered (mutable). entries are un-ordered but are indexed by name:value pairs. Mutable\n",
"- set: same as dictionary except entries are unique\n",
"\n",
"NB: These collections are allowed to contain values of different types. For now, let's just look at lists and tuples.\n",
"\n",
"#### Tuples\n",
"Tuples are created with the \",\" operator:"
]
},
{
"cell_type": "code",
"execution_count": 96,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('Bill', 'Ted', 'Kermit', 'Ernie')"
]
},
"execution_count": 96,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"Bill\", \"Ted\", \"Kermit\", \"Ernie\""
]
},
{
"cell_type": "code",
"execution_count": 97,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('OnlyOne',)"
]
},
"execution_count": 97,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"OnlyOne\","
]
},
{
"cell_type": "code",
"execution_count": 98,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'OnlyOne'"
]
},
"execution_count": 98,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"OnlyOne\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Lists\n",
"\n",
"Lists are created by enclosing comma separated sequences with square brackets:"
]
},
{
"cell_type": "code",
"execution_count": 99,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[10, 20, 30, 40]"
]
},
"execution_count": 99,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[10,20,30,40]"
]
},
{
"cell_type": "code",
"execution_count": 100,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['42', 42, 'Spring']"
]
},
"execution_count": 100,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[a, b, c]"
]
},
{
"cell_type": "code",
"execution_count": 101,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['42', 42, 42, 192]"
]
},
"execution_count": 101,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[a,b,b,192]"
]
},
{
"cell_type": "code",
"execution_count": 102,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"list"
]
},
"execution_count": 102,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type([])"
]
},
{
"cell_type": "code",
"execution_count": 103,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tuple"
]
},
"execution_count": 103,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Collections are nestable."
]
},
{
"cell_type": "code",
"execution_count": 105,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[['42', 42, 'Spring'], 'Summer']\n"
]
}
],
"source": [
"newList = [a,b,c]\n",
"biggerList = [newList, \"Summer\"]\n",
"print(biggerList)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The builtin function `len` returns the lenght of a collection:"
]
},
{
"cell_type": "code",
"execution_count": 106,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 106,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(newList)"
]
},
{
"cell_type": "code",
"execution_count": 107,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 107,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(biggerList)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Remember that you can alter lists:"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Winter', 'Spring', 'Spring', 'quasiSpring']\n"
]
}
],
"source": [
"newList = [a,b,c]\n",
"newList.append(\"quasiSpring\")\n",
"print(newList)"
]
},
{
"cell_type": "code",
"execution_count": 108,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Fall', 42, 'Spring']\n"
]
}
],
"source": [
"newList[0] = \"Fall\"\n",
"print(newList)"
]
},
{
"cell_type": "code",
"execution_count": 109,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('42', 42, 'Spring')\n"
]
}
],
"source": [
"newTuple = (a,b,c)\n",
"print(newTuple)"
]
},
{
"cell_type": "code",
"execution_count": 110,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "'tuple' object does not support item assignment",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mnewTuple\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"quasiSpring\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
]
}
],
"source": [
"newTuple[0] = \"quasiSpring\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Logical operations\n",
"\n",
"All programming languages have operators (functions) that return true or false values. There are 4 types of these operators in Python:\n",
"\n",
"- identity operators\n",
"- comparison operators\n",
"- membership operators\n",
"- logical operatiors\n",
"\n",
"Let's start with identity operators first:\n",
"\n",
"#### Identity operators\n",
"\n",
"When you sue these, you want to know if two obects are equivalent in some sense. Here, we want to know whether variables point to the same object or not."
]
},
{
"cell_type": "code",
"execution_count": 111,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 111,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"newA = [\"iPhone\", \"Ipad\", 99]\n",
"newB = [\"iPhone\", \"Ipad\", 99]\n",
"newA is newB"
]
},
{
"cell_type": "code",
"execution_count": 112,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 112,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"newB = newA\n",
"newA is newB"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"These comparisons can be counter-intuitive, but they are fast: just checking whether variables point to the same location in memory"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Comparison operators\n",
"\n",
"Most often, we want to know whether variables have the same *value*. We can use the typical comparison operators for this and related tasks:"
]
},
{
"cell_type": "code",
"execution_count": 113,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 113,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = 10\n",
"b = 70\n",
"a == b"
]
},
{
"cell_type": "code",
"execution_count": 114,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 114,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a < b"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(False, False, False, True)"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a > b, a == b, b <= a, a == a"
]
},
{
"cell_type": "code",
"execution_count": 115,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 115,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = 60\n",
"b = 60\n",
"a == b\n"
]
},
{
"cell_type": "code",
"execution_count": 116,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 116,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a is b # What do you think should happen here???"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Membership operators\n",
"\n",
"Very handy -- we can quickly test whether a value is *in* a collection with membership operators:"
]
},
{
"cell_type": "code",
"execution_count": 117,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 117,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"crazyList = [\"blue\", \"pancake\", \"popcorn\", \"soda\", 192, \"gastrulation\", 24]\n",
"3 in crazyList"
]
},
{
"cell_type": "code",
"execution_count": 118,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 118,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"Albert\" not in crazyList"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Remeber our long sentence from before? You can test for substring occurnces:"
]
},
{
"cell_type": "code",
"execution_count": 119,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Normal mammalian hemopoietic tissue produces a continuous supply of differentiated blood cells whose functions are essential for life.\n"
]
},
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 119,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(first_sent)\n",
"\"are\" in first_sent"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Logical operators\n",
"\n",
"- and\n",
"- or\n",
"- not\n",
"\n",
"and/or return Booleans if they occur in a boolean context (inside a conditional statement) or if their operands are boolean. Otherwise, they will return the operand that determined the result of the operation\n"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"True and True"
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 71,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"True and False"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"True or False"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 73,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"not True or False"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Control Flow \n",
"\n",
"Most programs will alter their behavior based on input or outcomes of prior computations. And often, the same set of computations will be repeated. To implement these behaviors, Python has some flow control statements, including the following:\n",
"\n",
"- if\n",
"- while\n",
"- for\n",
"\n",
"Let's start with the *if* statment:"
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This is always true\n"
]
}
],
"source": [
"if True:\n",
" print(\"This is always true\")\n",
"else:\n",
" print(\"This is never true\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The while executes a set of code (or suite) some number of times depending on the while's boolean statement:"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10\n",
"9\n",
"8\n",
"7\n",
"6\n",
"5\n",
"4\n",
"3\n",
"2\n",
"1\n"
]
}
],
"source": [
"a = 10\n",
"while a > 0:\n",
" print(a)\n",
" a = a - 1\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"the *for* statement uses *in*, and iterates over the values in the supplied collection:"
]
},
{
"cell_type": "code",
"execution_count": 76,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"blue\n",
"pancake\n",
"popcorn\n",
"soda\n",
"192\n",
"gastrulation\n",
"24\n"
]
}
],
"source": [
"crazyList = [\"blue\", \"pancake\", \"popcorn\", \"soda\", 192, \"gastrulation\", 24]\n",
"for item in crazyList:\n",
" print(item)\n"
]
},
{
"cell_type": "code",
"execution_count": 85,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I do not like blue\n",
"I do not like pancake\n",
"I do not like popcorn\n",
"I do not like soda\n",
"I do not like gastrulation\n"
]
}
],
"source": [
"for item in crazyList:\n",
" if isinstance(item, str):\n",
" print(\"I do not like \" + item)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 4
}