├── doyouseeme.txt ├── _config.yml ├── Day3_Iteration └── data │ ├── brian.png │ └── image00022.tif ├── Day1_Code_Blocks ├── .ipynb_checkpoints │ ├── Python Syntax 6-checkpoint.ipynb │ └── Python Syntax 4-checkpoint.ipynb └── Python Syntax 4.ipynb ├── Day2_Encapulation ├── protein_sequence_lengths.py └── Python Syntax 6.ipynb ├── Day4_Abstraction ├── dataframe_to_binary.py ├── Data Analysis1.ipynb └── .ipynb_checkpoints │ └── Data Analysis1-checkpoint.ipynb ├── README.md ├── index.md └── Day0_Python_Syntax ├── Python Syntax1.ipynb ├── Python Syntax 4.ipynb ├── .ipynb_checkpoints └── Python Syntax 4-checkpoint.ipynb ├── Python Syntax2.ipynb └── Python Syntax 3.ipynb /doyouseeme.txt: -------------------------------------------------------------------------------- 1 | Hi! 2 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-tactile 2 | markdown: GFM 3 | show_downloads: true 4 | -------------------------------------------------------------------------------- /Day3_Iteration/data/brian.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickdelgrosso/Intro-to-Python-Course/HEAD/Day3_Iteration/data/brian.png -------------------------------------------------------------------------------- /Day3_Iteration/data/image00022.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickdelgrosso/Intro-to-Python-Course/HEAD/Day3_Iteration/data/image00022.tif -------------------------------------------------------------------------------- /Day1_Code_Blocks/.ipynb_checkpoints/Python Syntax 6-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [] 9 | } 10 | ], 11 | "metadata": { 12 | "kernelspec": { 13 | "display_name": "Python 3", 14 | "language": "python", 15 | "name": "python3" 16 | }, 17 | "language_info": { 18 | "codemirror_mode": { 19 | "name": "ipython", 20 | "version": 3 21 | }, 22 | "file_extension": ".py", 23 | "mimetype": "text/x-python", 24 | "name": "python", 25 | "nbconvert_exporter": "python", 26 | "pygments_lexer": "ipython3", 27 | "version": "3.7.4" 28 | } 29 | }, 30 | "nbformat": 4, 31 | "nbformat_minor": 4 32 | } 33 | -------------------------------------------------------------------------------- /Day2_Encapulation/protein_sequence_lengths.py: -------------------------------------------------------------------------------- 1 | dimport os 2 | from os import path 3 | from pathlib import Path 4 | import numpy as np 5 | import pandas as pd 6 | import matplotlib.pyplot as plt 7 | from Bio.Seq import Seq 8 | from Bio import SeqIO 9 | 10 | ## ftp://ftp.uniprot.org/pub/databases/uniprot/current_release/knowledgebase/complete/uniprot_sprot.fasta.gz 11 | 12 | current_directory = os.getcwd() 13 | session_path = path.join(current_directory, "MyExperiment", "data", "sessions") 14 | all_files = os.listdir(session_path) 15 | idx = 0 16 | idx2 = 0 17 | 18 | sequence_files = [] 19 | while idx < len(all_files): 20 | file = all_files[idx] 21 | filename, extension = path.splitext(file) 22 | if 'fasta' in extension.lower(): 23 | sequence_files.append(file) 24 | idx = idx + 1 25 | sequence_ids = [] 26 | 27 | sequences = [] 28 | for sequence_file in sequence_files: 29 | for seq_record in SeqIO.parse(sequence_file, "fasta"): 30 | id = seq_record.id 31 | 32 | sequence = seq_record.seq 33 | sequences.append(sequence) 34 | sequence_ids.append(id) 35 | while idx2 < len(sequences): 36 | sequence = sequences[idx2] 37 | sequence_id = sequence_ids[idx2] 38 | print("Sequence ID: {}; Length: {}".format(sequence_id, len(sequence)) 39 | 40 | -------------------------------------------------------------------------------- /Day4_Abstraction/dataframe_to_binary.py: -------------------------------------------------------------------------------- 1 | """ 2 | dataframe_to_binary.py 3 | Author: Nicholas A. Del Grosso 4 | 5 | some functions that convert dataframes to a basic binary file format. 6 | An example of What Not To Do (TM) and part of an explanation of "binary" 7 | as a format in general. 8 | """ 9 | 10 | from typing import * 11 | import struct 12 | import pandas as pd 13 | 14 | class BinaryFormatter(NamedTuple): 15 | header: str 16 | body: str 17 | 18 | def rec_dtype_to_struct_fmt(rec) -> str: 19 | return "".join(f[0].char for f in rec.dtype.fields.values()) 20 | 21 | def rec_fields_to_struct_fmt(rec): 22 | return "".join(str(len(s))+"s" for s in rec.dtype.fields) 23 | 24 | 25 | def to_binary_file(df: pd.DataFrame, filename: str) -> BinaryFormatter: 26 | """ 27 | Saves the dataframe to file, and also returns a BinaryFormatter containnig the Struct 28 | format string of the header and body of the file for later reading. 29 | """ 30 | recs = df.to_records(index=False) 31 | body_fmt = rec_dtype_to_struct_fmt(recs) 32 | body = recs.tobytes() 33 | header_fmt = rec_fields_to_struct_fmt(recs) 34 | header = "".join(recs.dtype.fields).encode() 35 | with open(filename, 'wb') as f: 36 | f.write(header) 37 | f.write(body) 38 | return BinaryFormatter(header=header_fmt, body=body_fmt) 39 | 40 | 41 | def from_binary_file(filename: str, formatter: BinaryFormatter) -> pd.DataFrame: 42 | with open(filename, 'rb') as f: 43 | header = f.read(struct.calcsize(formatter.header)) 44 | body = f.read() 45 | return pd.DataFrame( 46 | data=list(struct.iter_unpack(formatter.body, body)), 47 | columns=[s.decode() for s in struct.unpack(formatter.header, header)] 48 | ) 49 | 50 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Introduction to Python Course 2 | 3 | 4 | ## Course Description: 5 | 6 | In this hands-on workshop, we cover the basics of the Python programming language and some of its scientific library in order to enable students to conduct data analysis using the core principles of good scientific practice, focusing on techniques for performing essential data analysis steps using real-life problems that the practicing scientist regularly encounters in his/her work. Libraries covered will be NumPy, matplotlib, Pandas, Seaborn, Scipy-Stats, and Scikit-Learn. Students will learn to write their own functions, scripts, and Jupyter Notebooks in order to perform reproducible data analyses on a wide variety of data types, with a focus on tabular and image data. No prior programming, math, or statistics knowledge is required. Participants will receive a certificate of completion at the end of the course, and will leave with increased confidence in their ability to use computational tools in their research. 7 | 8 | Major Focuses of the Course: 9 | 10 | - Fundamentals of Programming (Scripts, Functions, Loops, Conditionals, and Modules) 11 | - Python Data Structures (Lists, Tuples, Dictionaries, Arrays, and DataFrames) 12 | - Reading and Writing to Various File Formats (CSV, HDF5, SQL, and Image Formats) 13 | - Writing Code to Building Publication-Ready Figures 14 | - Fitting Statistical Models to Data with Statsmodels, Scikit-Learn, and PyMC3 15 | - Good Coding Practices (Version Control, Code Organization, Styling Practices, Pair Programming, and Literate Programming) 16 | - Data Analysis Workflows (NbConvert and SnakeMake) 17 | - Reproducibility Practices in Open Science (Dependency Management, Executable Analysis, DOIs) 18 | - Understanding the Terminal and the Core Scientific Software Stack (Bash, Compilers, and Interpreters) 19 | 20 | ## Software to Download for the Course 21 | 22 | - [**Anaconda Python 3.7 Software Distribution**](https://www.anaconda.com/distribution/#download-section) 23 | - [**Git Version Control**](https://git-scm.com/downloads) 24 | 25 | ## Exercise Materials 26 | 27 | ### Day 0: Introduction to the Python Language 28 | - **Notebook Environments and Python Operators**: [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/nickdelgrosso/Intro-to-Python-Course/blob/master/Day0_Python_Syntax/Python%20Syntax1.ipynb) 29 | - **The Built-In Python Collection Types**: [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/nickdelgrosso/Intro-to-Python-Course/blob/master/Day0_Python_Syntax/Python%20Syntax2.ipynb) 30 | - **Activity**: Running Jupyter Locally 31 | - **Name Assignment, Module Importing, and Comprehensions**: [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/nickdelgrosso/Intro-to-Python-Course/blob/master/Day0_Python_Syntax/Python%20Syntax%203.ipynb) 32 | - **Activity**: "Using OOP: Drawing with Turtle" 33 | 34 | #### References 35 | 36 | - [Table of Python Built-In Functions](https://docs.python.org/3/library/functions.html#built-in-functions) 37 | - [Documentation for Python Built-In Libraries](https://docs.python.org/3/library/index.html) 38 | 39 | ### Day 1: Methods, Conditionals, Loops, and Arrays 40 | - **Types, Objects, and Methods**: [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/nickdelgrosso/Intro-to-Python-Course/blob/master/Day1_Code_Blocks/Python%20Syntax%204.ipynb) 41 | - **Conditionals: "If" statements**: [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/nickdelgrosso/Intro-to-Python-Course/blob/master/Day1_Code_Blocks/Python%20Syntax%205.ipynb) 42 | - **Loops and Iteration**: [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/nickdelgrosso/Intro-to-Python-Course/blob/master/Day1_Code_Blocks/Python%20Syntax%206.ipynb) 43 | - **Activity**: Getting "Zipfy" with Literature 44 | 45 | 46 | ### Day 2: Broadcasting and Encapusulation 47 | - **Installing and Managing Python from the Command Line** 48 | - **Simplifying For-Loops and Filtering with Numpy Arrays**: [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/nickdelgrosso/Intro-to-Python-Course/blob/master/Day2_Encapsulation/Broadcasting.ipynb) 49 | - **Activity**: Exploring Central Tendancy by Writing a Confidence Interval Script 50 | - **Encapsulating Code with Functions**: [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/nickdelgrosso/Intro-to-Python-Course/blob/masterDay2_Encapsulation/Python%20Syntax%206.ipynb) 51 | - **Activity**: Refactoring a Script 52 | 53 | 54 | ### Day 3: Numpy Arrays 55 | 56 | ### Day 4: DataFrames 57 | - Jupyter Exercises: File Formats and Associated Data Structures: 58 | - Goal: To show how to load various file types into Python (e.g. json, text, csv, hdf5, mat, pickle, binary packets, etc) 59 | - Goal: to review and discuss the various pros/cons with these types (performance, human readability, compression, schema, structure, metadata, etc) 60 | - Jupyter Exercises: Intro to Pandas DataFrames 61 | - Goal: To get familiar with Pandas and working with tabular data. 62 | 63 | 64 | - Afternoon **Abstracting Short Data Analysis Pipelines**: 65 | - Demo: the Split-Apply-Combine Workflow in Pandas 66 | - Goal: To discuss analysis abstraction and learn powerful techniques for manipulating data with pandas 67 | - Special Topics (On Request) 68 | -------------------------------------------------------------------------------- /index.md: -------------------------------------------------------------------------------- 1 | # Introduction to Python Course 2 | 3 | **Please Fill Out**: [Course Availability Doodle for Last Two Days](https://doodle.com/poll/66gynq6tqcawxqx7) 4 | 5 | ## Course Description: 6 | 7 | In this hands-on workshop, we cover the basics of the Python programming language and some of its scientific library in order to enable students to conduct data analysis using the core principles of good scientific practice, focusing on techniques for performing essential data analysis steps using real-life problems that the practicing scientist regularly encounters in his/her work. Libraries covered will be NumPy, matplotlib, Pandas, Seaborn, Scipy-Stats, and Scikit-Learn. Students will learn to write their own functions, scripts, and Jupyter Notebooks in order to perform reproducible data analyses on a wide variety of data types, with a focus on tabular and image data. No prior programming, math, or statistics knowledge is required. Participants will receive a certificate of completion at the end of the course, and will leave with increased confidence in their ability to use computational tools in their research. 8 | 9 | Major Focuses of the Course: 10 | 11 | - Fundamentals of Programming (Scripts, Functions, Loops, Conditionals, and Modules) 12 | - Python Data Structures (Lists, Tuples, Dictionaries, Arrays, and DataFrames) 13 | - Reading and Writing to Various File Formats (CSV, HDF5, SQL, and Image Formats) 14 | - Writing Code to Building Publication-Ready Figures 15 | - Fitting Statistical Models to Data with Statsmodels, Scikit-Learn, and PyMC3 16 | - Good Coding Practices (Version Control, Code Organization, Styling Practices, Pair Programming, and Literate Programming) 17 | - Data Analysis Workflows (NbConvert and SnakeMake) 18 | - Reproducibility Practices in Open Science (Dependency Management, Executable Analysis, DOIs) 19 | - Understanding the Terminal and the Core Scientific Software Stack (Bash, Compilers, and Interpreters) 20 | 21 | ## Software to Download for the Course 22 | 23 | - [**Anaconda Python 3.7 Software Distribution**](https://www.anaconda.com/distribution/#download-section) 24 | - [**Git Version Control**](https://git-scm.com/downloads) 25 | 26 | ## Exercise Materials 27 | 28 | ### Day 0: Introduction to the Python Language 29 | - **Notebook Environments and Python Operators**: [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/nickdelgrosso/Intro-to-Python-Course/blob/master/Day0_Python_Syntax/Python%20Syntax1.ipynb) 30 | - **The Built-In Python Collection Types**: [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/nickdelgrosso/Intro-to-Python-Course/blob/master/Day0_Python_Syntax/Python%20Syntax2.ipynb) 31 | - **Activity**: Running Jupyter Locally 32 | - **Name Assignment, Module Importing, and Comprehensions**: [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/nickdelgrosso/Intro-to-Python-Course/blob/master/Day0_Python_Syntax/Python%20Syntax%203.ipynb) 33 | - **Activity**: "Using OOP: Drawing with Turtle" 34 | 35 | #### References 36 | 37 | - [Table of Python Built-In Functions](https://docs.python.org/3/library/functions.html#built-in-functions) 38 | - [Documentation for Python Built-In Libraries](https://docs.python.org/3/library/index.html) 39 | 40 | ### Day 1: Methods, Conditionals, Loops, and Arrays 41 | - **Types, Objects, and Methods**: [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/nickdelgrosso/Intro-to-Python-Course/blob/master/Day1_Code_Blocks/Python%20Syntax%204.ipynb) 42 | - **Conditionals: "If" statements**: [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/nickdelgrosso/Intro-to-Python-Course/blob/master/Day1_Code_Blocks/Python%20Syntax%205.ipynb) 43 | - **Loops and Iteration**: [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/nickdelgrosso/Intro-to-Python-Course/blob/master/Day1_Code_Blocks/Python%20Syntax%206.ipynb) 44 | - **Activity**: Getting "Zipfy" with Literature 45 | 46 | 47 | ### Day 2: Broadcasting and Encapusulation 48 | - **Installing and Managing Python from the Command Line** 49 | - **Simplifying For-Loops and Filtering with Numpy Arrays**: [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/nickdelgrosso/Intro-to-Python-Course/blob/master/Day2_Encapsulation/Broadcasting.ipynb) 50 | - **Activity**: Exploring Central Tendancy by Writing a Confidence Interval Script 51 | - **Encapsulating Code with Functions**: [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/nickdelgrosso/Intro-to-Python-Course/blob/masterDay2_Encapsulation/Python%20Syntax%206.ipynb) 52 | - **Activity**: Refactoring a Script 53 | 54 | 55 | ### Day 3: Numpy Arrays 56 | 57 | ### Day 4: DataFrames 58 | - Jupyter Exercises: File Formats and Associated Data Structures: 59 | - Goal: To show how to load various file types into Python (e.g. json, text, csv, hdf5, mat, pickle, binary packets, etc) 60 | - Goal: to review and discuss the various pros/cons with these types (performance, human readability, compression, schema, structure, metadata, etc) 61 | - Jupyter Exercises: Intro to Pandas DataFrames 62 | - Goal: To get familiar with Pandas and working with tabular data. 63 | 64 | 65 | - Afternoon **Abstracting Short Data Analysis Pipelines**: 66 | - Demo: the Split-Apply-Combine Workflow in Pandas 67 | - Goal: To discuss analysis abstraction and learn powerful techniques for manipulating data with pandas 68 | - Special Topics (On Request) -------------------------------------------------------------------------------- /Day4_Abstraction/Data Analysis1.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "%matplotlib inline" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Pandas and DataFrames\n", 17 | "\n", 18 | "Often, we have tables of data--collections of named columns arranged in rows. The **Pandas** package gives us a **DataFrame()** class that lets us index these columns the same way as with dicts, while still getting the benefit of Numpy arrays, meaning we can still write vectorized code. \n", 19 | "\n", 20 | "Let's start playing with the analysis now. We'll examine Pandas in more depth in the coming days." 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 2, 26 | "metadata": {}, 27 | "outputs": [], 28 | "source": [ 29 | "import pandas as pd" 30 | ] 31 | }, 32 | { 33 | "cell_type": "markdown", 34 | "metadata": {}, 35 | "source": [ 36 | "## Making DataFrames from a Data File\n", 37 | "\n", 38 | "Pandas has functions that can make DataFrames from a wide variety of file types. To do this, use one of the functions in Pandas that start with \"read_\". Here is a non-exclusive list of examples:\n", 39 | "\n", 40 | "| File Type | Function Name |\n", 41 | "| :----: | :---: |\n", 42 | "| Excel | pd.read_excel() |\n", 43 | "| CSV | pd.read_csv() |\n", 44 | "| TSV | pd.read_table() |\n", 45 | "| H5, HDF, HDF5 | pd.read_hdf() |\n", 46 | "| JSON | pd.read_json() |\n", 47 | "| SQL | pd.read_sql_table() |\n", 48 | "\n", 49 | "\n", 50 | "If your file type isn't listed here, chances are that there is a Python package with a Dataframe-creating function for your data out there--it's a very popular data structure!" 51 | ] 52 | }, 53 | { 54 | "cell_type": "markdown", 55 | "metadata": {}, 56 | "source": [ 57 | "## Loading the Data\n", 58 | "\n", 59 | "Please open the file “MentalRotation.csv” (pd.read_csv()) and use it to answer the following questions about the results of the Mental Rotation psychology experiment. If you reach the end of the exercises, explore the dataset and DataFrames more and see what you can find about this experiment!" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": null, 65 | "metadata": {}, 66 | "outputs": [], 67 | "source": [] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": null, 72 | "metadata": {}, 73 | "outputs": [], 74 | "source": [] 75 | }, 76 | { 77 | "cell_type": "markdown", 78 | "metadata": {}, 79 | "source": [ 80 | "## Examining the Dataset\n", 81 | "\n", 82 | "**head()**, **tail()**, **sample()**" 83 | ] 84 | }, 85 | { 86 | "cell_type": "markdown", 87 | "metadata": {}, 88 | "source": [ 89 | "Look at the first 5 lines of the dataset" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": null, 95 | "metadata": {}, 96 | "outputs": [], 97 | "source": [] 98 | }, 99 | { 100 | "cell_type": "markdown", 101 | "metadata": {}, 102 | "source": [ 103 | "Look at the last 5 lines of the dataset" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": null, 109 | "metadata": {}, 110 | "outputs": [], 111 | "source": [] 112 | }, 113 | { 114 | "cell_type": "markdown", 115 | "metadata": {}, 116 | "source": [ 117 | "Check 3 random lines in the dataset." 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": null, 123 | "metadata": {}, 124 | "outputs": [], 125 | "source": [] 126 | }, 127 | { 128 | "cell_type": "markdown", 129 | "metadata": {}, 130 | "source": [ 131 | "How Many Total Trials (rows) are in the study?" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": null, 137 | "metadata": {}, 138 | "outputs": [], 139 | "source": [] 140 | }, 141 | { 142 | "cell_type": "markdown", 143 | "metadata": {}, 144 | "source": [ 145 | "What is the maximum number of trials that one subject performed?" 146 | ] 147 | }, 148 | { 149 | "cell_type": "markdown", 150 | "metadata": {}, 151 | "source": [] 152 | }, 153 | { 154 | "cell_type": "markdown", 155 | "metadata": {}, 156 | "source": [ 157 | "### Making New Columns" 158 | ] 159 | }, 160 | { 161 | "cell_type": "markdown", 162 | "metadata": {}, 163 | "source": [ 164 | "Convert the Time column to seconds by dividing it by 1000." 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": null, 170 | "metadata": {}, 171 | "outputs": [], 172 | "source": [] 173 | }, 174 | { 175 | "cell_type": "markdown", 176 | "metadata": {}, 177 | "source": [ 178 | "Change the \"Correct\" column to *bool* (True/False) using the **astype()** method" 179 | ] 180 | }, 181 | { 182 | "cell_type": "code", 183 | "execution_count": null, 184 | "metadata": {}, 185 | "outputs": [], 186 | "source": [] 187 | }, 188 | { 189 | "cell_type": "markdown", 190 | "metadata": {}, 191 | "source": [ 192 | "### The mean() method" 193 | ] 194 | }, 195 | { 196 | "cell_type": "markdown", 197 | "metadata": {}, 198 | "source": [ 199 | "What is the mean response time, across all trials?" 200 | ] 201 | }, 202 | { 203 | "cell_type": "code", 204 | "execution_count": null, 205 | "metadata": {}, 206 | "outputs": [], 207 | "source": [] 208 | }, 209 | { 210 | "cell_type": "markdown", 211 | "metadata": {}, 212 | "source": [ 213 | "What percent of trials were answered correctly?" 214 | ] 215 | }, 216 | { 217 | "cell_type": "code", 218 | "execution_count": null, 219 | "metadata": {}, 220 | "outputs": [], 221 | "source": [] 222 | }, 223 | { 224 | "cell_type": "markdown", 225 | "metadata": {}, 226 | "source": [ 227 | "What percent of trials were “Matching” trials?" 228 | ] 229 | }, 230 | { 231 | "cell_type": "code", 232 | "execution_count": null, 233 | "metadata": {}, 234 | "outputs": [], 235 | "source": [] 236 | }, 237 | { 238 | "cell_type": "markdown", 239 | "metadata": {}, 240 | "source": [ 241 | "### Slicing" 242 | ] 243 | }, 244 | { 245 | "cell_type": "markdown", 246 | "metadata": {}, 247 | "source": [ 248 | "Is there a difference in accuracy between matching and non-matching trials?" 249 | ] 250 | }, 251 | { 252 | "cell_type": "code", 253 | "execution_count": null, 254 | "metadata": {}, 255 | "outputs": [], 256 | "source": [] 257 | }, 258 | { 259 | "cell_type": "markdown", 260 | "metadata": {}, 261 | "source": [ 262 | "Is there a response time difference between matching and nonmatching\n", 263 | "trials?" 264 | ] 265 | }, 266 | { 267 | "cell_type": "code", 268 | "execution_count": null, 269 | "metadata": {}, 270 | "outputs": [], 271 | "source": [] 272 | }, 273 | { 274 | "cell_type": "markdown", 275 | "metadata": {}, 276 | "source": [ 277 | "Is there a response time difference between matching and nonmatching trials, for different rotation Angles?" 278 | ] 279 | }, 280 | { 281 | "cell_type": "code", 282 | "execution_count": null, 283 | "metadata": {}, 284 | "outputs": [], 285 | "source": [] 286 | }, 287 | { 288 | "cell_type": "markdown", 289 | "metadata": {}, 290 | "source": [ 291 | "### Plotting" 292 | ] 293 | }, 294 | { 295 | "cell_type": "markdown", 296 | "metadata": {}, 297 | "source": [ 298 | "Plot the response time distribution as a histogram." 299 | ] 300 | }, 301 | { 302 | "cell_type": "code", 303 | "execution_count": null, 304 | "metadata": {}, 305 | "outputs": [], 306 | "source": [] 307 | }, 308 | { 309 | "cell_type": "markdown", 310 | "metadata": {}, 311 | "source": [ 312 | "Plot the average response time for each stimulus category (matching and non-matching)" 313 | ] 314 | }, 315 | { 316 | "cell_type": "code", 317 | "execution_count": null, 318 | "metadata": {}, 319 | "outputs": [], 320 | "source": [] 321 | }, 322 | { 323 | "cell_type": "markdown", 324 | "metadata": {}, 325 | "source": [ 326 | "Is there a correlation between Angle of mental rotation and response time? Visualize the relationship" 327 | ] 328 | }, 329 | { 330 | "cell_type": "code", 331 | "execution_count": null, 332 | "metadata": {}, 333 | "outputs": [], 334 | "source": [] 335 | }, 336 | { 337 | "cell_type": "markdown", 338 | "metadata": {}, 339 | "source": [ 340 | "Is there a difference in the relationship between Angle of mental rotation and response time, between stimulus categories?" 341 | ] 342 | }, 343 | { 344 | "cell_type": "code", 345 | "execution_count": null, 346 | "metadata": {}, 347 | "outputs": [], 348 | "source": [] 349 | } 350 | ], 351 | "metadata": { 352 | "kernelspec": { 353 | "display_name": "Python 3", 354 | "language": "python", 355 | "name": "python3" 356 | }, 357 | "language_info": { 358 | "codemirror_mode": { 359 | "name": "ipython", 360 | "version": 3 361 | }, 362 | "file_extension": ".py", 363 | "mimetype": "text/x-python", 364 | "name": "python", 365 | "nbconvert_exporter": "python", 366 | "pygments_lexer": "ipython3", 367 | "version": "3.7.4" 368 | } 369 | }, 370 | "nbformat": 4, 371 | "nbformat_minor": 4 372 | } 373 | -------------------------------------------------------------------------------- /Day4_Abstraction/.ipynb_checkpoints/Data Analysis1-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "%matplotlib inline" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Pandas and DataFrames\n", 17 | "\n", 18 | "Often, we have tables of data--collections of named columns arranged in rows. The **Pandas** package gives us a **DataFrame()** class that lets us index these columns the same way as with dicts, while still getting the benefit of Numpy arrays, meaning we can still write vectorized code. \n", 19 | "\n", 20 | "Let's start playing with the analysis now. We'll examine Pandas in more depth in the coming days." 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 2, 26 | "metadata": {}, 27 | "outputs": [], 28 | "source": [ 29 | "import pandas as pd" 30 | ] 31 | }, 32 | { 33 | "cell_type": "markdown", 34 | "metadata": {}, 35 | "source": [ 36 | "## Making DataFrames from a Data File\n", 37 | "\n", 38 | "Pandas has functions that can make DataFrames from a wide variety of file types. To do this, use one of the functions in Pandas that start with \"read_\". Here is a non-exclusive list of examples:\n", 39 | "\n", 40 | "| File Type | Function Name |\n", 41 | "| :----: | :---: |\n", 42 | "| Excel | pd.read_excel() |\n", 43 | "| CSV | pd.read_csv() |\n", 44 | "| TSV | pd.read_table() |\n", 45 | "| H5, HDF, HDF5 | pd.read_hdf() |\n", 46 | "| JSON | pd.read_json() |\n", 47 | "| SQL | pd.read_sql_table() |\n", 48 | "\n", 49 | "\n", 50 | "If your file type isn't listed here, chances are that there is a Python package with a Dataframe-creating function for your data out there--it's a very popular data structure!" 51 | ] 52 | }, 53 | { 54 | "cell_type": "markdown", 55 | "metadata": {}, 56 | "source": [ 57 | "## Loading the Data\n", 58 | "\n", 59 | "Please open the file “MentalRotation.csv” (pd.read_csv()) and use it to answer the following questions about the results of the Mental Rotation psychology experiment. If you reach the end of the exercises, explore the dataset and DataFrames more and see what you can find about this experiment!" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": null, 65 | "metadata": {}, 66 | "outputs": [], 67 | "source": [] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": null, 72 | "metadata": {}, 73 | "outputs": [], 74 | "source": [] 75 | }, 76 | { 77 | "cell_type": "markdown", 78 | "metadata": {}, 79 | "source": [ 80 | "## Examining the Dataset\n", 81 | "\n", 82 | "**head()**, **tail()**, **sample()**" 83 | ] 84 | }, 85 | { 86 | "cell_type": "markdown", 87 | "metadata": {}, 88 | "source": [ 89 | "Look at the first 5 lines of the dataset" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": null, 95 | "metadata": {}, 96 | "outputs": [], 97 | "source": [] 98 | }, 99 | { 100 | "cell_type": "markdown", 101 | "metadata": {}, 102 | "source": [ 103 | "Look at the last 5 lines of the dataset" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": null, 109 | "metadata": {}, 110 | "outputs": [], 111 | "source": [] 112 | }, 113 | { 114 | "cell_type": "markdown", 115 | "metadata": {}, 116 | "source": [ 117 | "Check 3 random lines in the dataset." 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": null, 123 | "metadata": {}, 124 | "outputs": [], 125 | "source": [] 126 | }, 127 | { 128 | "cell_type": "markdown", 129 | "metadata": {}, 130 | "source": [ 131 | "How Many Total Trials (rows) are in the study?" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": null, 137 | "metadata": {}, 138 | "outputs": [], 139 | "source": [] 140 | }, 141 | { 142 | "cell_type": "markdown", 143 | "metadata": {}, 144 | "source": [ 145 | "What is the maximum number of trials that one subject performed?" 146 | ] 147 | }, 148 | { 149 | "cell_type": "markdown", 150 | "metadata": {}, 151 | "source": [] 152 | }, 153 | { 154 | "cell_type": "markdown", 155 | "metadata": {}, 156 | "source": [ 157 | "### Making New Columns" 158 | ] 159 | }, 160 | { 161 | "cell_type": "markdown", 162 | "metadata": {}, 163 | "source": [ 164 | "Convert the Time column to seconds by dividing it by 1000." 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": null, 170 | "metadata": {}, 171 | "outputs": [], 172 | "source": [] 173 | }, 174 | { 175 | "cell_type": "markdown", 176 | "metadata": {}, 177 | "source": [ 178 | "Change the \"Correct\" column to *bool* (True/False) using the **astype()** method" 179 | ] 180 | }, 181 | { 182 | "cell_type": "code", 183 | "execution_count": null, 184 | "metadata": {}, 185 | "outputs": [], 186 | "source": [] 187 | }, 188 | { 189 | "cell_type": "markdown", 190 | "metadata": {}, 191 | "source": [ 192 | "### The mean() method" 193 | ] 194 | }, 195 | { 196 | "cell_type": "markdown", 197 | "metadata": {}, 198 | "source": [ 199 | "What is the mean response time, across all trials?" 200 | ] 201 | }, 202 | { 203 | "cell_type": "code", 204 | "execution_count": null, 205 | "metadata": {}, 206 | "outputs": [], 207 | "source": [] 208 | }, 209 | { 210 | "cell_type": "markdown", 211 | "metadata": {}, 212 | "source": [ 213 | "What percent of trials were answered correctly?" 214 | ] 215 | }, 216 | { 217 | "cell_type": "code", 218 | "execution_count": null, 219 | "metadata": {}, 220 | "outputs": [], 221 | "source": [] 222 | }, 223 | { 224 | "cell_type": "markdown", 225 | "metadata": {}, 226 | "source": [ 227 | "What percent of trials were “Matching” trials?" 228 | ] 229 | }, 230 | { 231 | "cell_type": "code", 232 | "execution_count": null, 233 | "metadata": {}, 234 | "outputs": [], 235 | "source": [] 236 | }, 237 | { 238 | "cell_type": "markdown", 239 | "metadata": {}, 240 | "source": [ 241 | "### Slicing" 242 | ] 243 | }, 244 | { 245 | "cell_type": "markdown", 246 | "metadata": {}, 247 | "source": [ 248 | "Is there a difference in accuracy between matching and non-matching trials?" 249 | ] 250 | }, 251 | { 252 | "cell_type": "code", 253 | "execution_count": null, 254 | "metadata": {}, 255 | "outputs": [], 256 | "source": [] 257 | }, 258 | { 259 | "cell_type": "markdown", 260 | "metadata": {}, 261 | "source": [ 262 | "Is there a response time difference between matching and nonmatching\n", 263 | "trials?" 264 | ] 265 | }, 266 | { 267 | "cell_type": "code", 268 | "execution_count": null, 269 | "metadata": {}, 270 | "outputs": [], 271 | "source": [] 272 | }, 273 | { 274 | "cell_type": "markdown", 275 | "metadata": {}, 276 | "source": [ 277 | "Is there a response time difference between matching and nonmatching trials, for different rotation Angles?" 278 | ] 279 | }, 280 | { 281 | "cell_type": "code", 282 | "execution_count": null, 283 | "metadata": {}, 284 | "outputs": [], 285 | "source": [] 286 | }, 287 | { 288 | "cell_type": "markdown", 289 | "metadata": {}, 290 | "source": [ 291 | "### Plotting" 292 | ] 293 | }, 294 | { 295 | "cell_type": "markdown", 296 | "metadata": {}, 297 | "source": [ 298 | "Plot the response time distribution as a histogram." 299 | ] 300 | }, 301 | { 302 | "cell_type": "code", 303 | "execution_count": null, 304 | "metadata": {}, 305 | "outputs": [], 306 | "source": [] 307 | }, 308 | { 309 | "cell_type": "markdown", 310 | "metadata": {}, 311 | "source": [ 312 | "Plot the average response time for each stimulus category (matching and non-matching)" 313 | ] 314 | }, 315 | { 316 | "cell_type": "code", 317 | "execution_count": null, 318 | "metadata": {}, 319 | "outputs": [], 320 | "source": [] 321 | }, 322 | { 323 | "cell_type": "markdown", 324 | "metadata": {}, 325 | "source": [ 326 | "Is there a correlation between Angle of mental rotation and response time? Visualize the relationship" 327 | ] 328 | }, 329 | { 330 | "cell_type": "code", 331 | "execution_count": null, 332 | "metadata": {}, 333 | "outputs": [], 334 | "source": [] 335 | }, 336 | { 337 | "cell_type": "markdown", 338 | "metadata": {}, 339 | "source": [ 340 | "Is there a difference in the relationship between Angle of mental rotation and response time, between stimulus categories?" 341 | ] 342 | }, 343 | { 344 | "cell_type": "code", 345 | "execution_count": null, 346 | "metadata": {}, 347 | "outputs": [], 348 | "source": [] 349 | } 350 | ], 351 | "metadata": { 352 | "kernelspec": { 353 | "display_name": "Python 3", 354 | "language": "python", 355 | "name": "python3" 356 | }, 357 | "language_info": { 358 | "codemirror_mode": { 359 | "name": "ipython", 360 | "version": 3 361 | }, 362 | "file_extension": ".py", 363 | "mimetype": "text/x-python", 364 | "name": "python", 365 | "nbconvert_exporter": "python", 366 | "pygments_lexer": "ipython3", 367 | "version": "3.7.4" 368 | } 369 | }, 370 | "nbformat": 4, 371 | "nbformat_minor": 4 372 | } 373 | -------------------------------------------------------------------------------- /Day0_Python_Syntax/Python Syntax1.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "slideshow": { 7 | "slide_type": "slide" 8 | } 9 | }, 10 | "source": [ 11 | "\n", 12 | "\n", 13 | "# Intro. to Python Syntax, Part 1\n", 14 | "## Operators" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": { 20 | "slideshow": { 21 | "slide_type": "slide" 22 | } 23 | }, 24 | "source": [ 25 | "## Exercises: Using Python as a calculator\n", 26 | "\n", 27 | "Using the operators in the table below, ask Python to solve these difficult math equations:\n", 28 | "\n", 29 | "|Assign to a Variable | Add | Subract | Multiply | Divide | Power | Integer Divide | Remainder after Division | \n", 30 | "| :---------------: | :---:| :-----: | :------: | :----: | :---: | :------------: | :----------------------: |\n", 31 | "| = | + | - | * | / | ** | // | % |" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": { 37 | "slideshow": { 38 | "slide_type": "subslide" 39 | } 40 | }, 41 | "source": [ 42 | "1. What is two plus three?" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": null, 48 | "metadata": {}, 49 | "outputs": [], 50 | "source": [] 51 | }, 52 | { 53 | "cell_type": "markdown", 54 | "metadata": { 55 | "slideshow": { 56 | "slide_type": "fragment" 57 | } 58 | }, 59 | "source": [ 60 | "2. What is two times three?" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": null, 66 | "metadata": {}, 67 | "outputs": [], 68 | "source": [] 69 | }, 70 | { 71 | "cell_type": "markdown", 72 | "metadata": { 73 | "slideshow": { 74 | "slide_type": "fragment" 75 | } 76 | }, 77 | "source": [ 78 | "3. What is two to the third power?" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": null, 84 | "metadata": {}, 85 | "outputs": [], 86 | "source": [] 87 | }, 88 | { 89 | "cell_type": "markdown", 90 | "metadata": { 91 | "slideshow": { 92 | "slide_type": "subslide" 93 | } 94 | }, 95 | "source": [ 96 | "3.5 How many (whole) times does 7 go into 100?" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": null, 102 | "metadata": {}, 103 | "outputs": [], 104 | "source": [] 105 | }, 106 | { 107 | "cell_type": "markdown", 108 | "metadata": {}, 109 | "source": [ 110 | "4. What is the remainder after dividing 3 from 2?" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": null, 116 | "metadata": {}, 117 | "outputs": [], 118 | "source": [] 119 | }, 120 | { 121 | "cell_type": "markdown", 122 | "metadata": { 123 | "slideshow": { 124 | "slide_type": "subslide" 125 | } 126 | }, 127 | "source": [ 128 | "#### Let's See: Does Python do operations left-to-right, or follow the mathematical order of operations (addition and subtraction only after multiplication and division)?\n", 129 | "\n", 130 | "Read each of the following math expressions to yourself and calculate (on paper, or in your head) what the answer shoud be. Then check the answer by calculating it in Python. Were they the same answer?" 131 | ] 132 | }, 133 | { 134 | "cell_type": "markdown", 135 | "metadata": { 136 | "slideshow": { 137 | "slide_type": "subslide" 138 | } 139 | }, 140 | "source": [ 141 | "3 + 5 + 2" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": null, 147 | "metadata": {}, 148 | "outputs": [], 149 | "source": [] 150 | }, 151 | { 152 | "cell_type": "markdown", 153 | "metadata": { 154 | "slideshow": { 155 | "slide_type": "fragment" 156 | } 157 | }, 158 | "source": [ 159 | "3 * 5 + 2" 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": null, 165 | "metadata": {}, 166 | "outputs": [], 167 | "source": [] 168 | }, 169 | { 170 | "cell_type": "markdown", 171 | "metadata": { 172 | "slideshow": { 173 | "slide_type": "fragment" 174 | } 175 | }, 176 | "source": [ 177 | "3 + 5 * 2" 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": null, 183 | "metadata": {}, 184 | "outputs": [], 185 | "source": [] 186 | }, 187 | { 188 | "cell_type": "markdown", 189 | "metadata": { 190 | "slideshow": { 191 | "slide_type": "fragment" 192 | } 193 | }, 194 | "source": [ 195 | "(3 * 5) + 2" 196 | ] 197 | }, 198 | { 199 | "cell_type": "code", 200 | "execution_count": null, 201 | "metadata": {}, 202 | "outputs": [], 203 | "source": [] 204 | }, 205 | { 206 | "cell_type": "markdown", 207 | "metadata": { 208 | "slideshow": { 209 | "slide_type": "fragment" 210 | } 211 | }, 212 | "source": [ 213 | "3 * (5 + 2)" 214 | ] 215 | }, 216 | { 217 | "cell_type": "code", 218 | "execution_count": null, 219 | "metadata": {}, 220 | "outputs": [], 221 | "source": [] 222 | }, 223 | { 224 | "cell_type": "markdown", 225 | "metadata": { 226 | "slideshow": { 227 | "slide_type": "fragment" 228 | } 229 | }, 230 | "source": [ 231 | "What order is Python running the code?" 232 | ] 233 | }, 234 | { 235 | "cell_type": "markdown", 236 | "metadata": { 237 | "slideshow": { 238 | "slide_type": "slide" 239 | } 240 | }, 241 | "source": [ 242 | "# Testing Your Logic\n", 243 | "## Bools\n", 244 | "\n", 245 | "You can also ask Python logical questions (questions that have a \"True\" or \"False\" answer). \n", 246 | "\n", 247 | "| \"Is Equal to\" | \"Is Greater than\" | \"Is Greater than or Equal to\"| \"Is not Equal to\" |\n", 248 | "| :----------: | :----------: | :----------: | :----------: |\n", 249 | "| == | > | >= | != |\n", 250 | "\n", 251 | "Python will return \"True\" if the statement is true, \"False\" if False\n", 252 | "\n", 253 | "```python\n", 254 | ">>> 3 > 2\n", 255 | "True\n", 256 | "\n", 257 | ">>> 3 < 2\n", 258 | "False\n", 259 | "\n", 260 | ">>> 3 == 2\n", 261 | "False\n", 262 | "```\n", 263 | "\n", 264 | "Using logical operators, decide if the following assertions are True or False:" 265 | ] 266 | }, 267 | { 268 | "cell_type": "markdown", 269 | "metadata": { 270 | "slideshow": { 271 | "slide_type": "subslide" 272 | } 273 | }, 274 | "source": [ 275 | "1. One hundred is bigger than ten." 276 | ] 277 | }, 278 | { 279 | "cell_type": "code", 280 | "execution_count": null, 281 | "metadata": {}, 282 | "outputs": [], 283 | "source": [] 284 | }, 285 | { 286 | "cell_type": "markdown", 287 | "metadata": { 288 | "slideshow": { 289 | "slide_type": "fragment" 290 | } 291 | }, 292 | "source": [ 293 | "2. Thirty squared is not equal to nine-hundred." 294 | ] 295 | }, 296 | { 297 | "cell_type": "code", 298 | "execution_count": null, 299 | "metadata": {}, 300 | "outputs": [], 301 | "source": [] 302 | }, 303 | { 304 | "cell_type": "markdown", 305 | "metadata": { 306 | "slideshow": { 307 | "slide_type": "fragment" 308 | } 309 | }, 310 | "source": [ 311 | "3. These two equations are equal: \n", 312 | " - 30 - 5 * 2\n", 313 | " - 100 / 5" 314 | ] 315 | }, 316 | { 317 | "cell_type": "code", 318 | "execution_count": null, 319 | "metadata": {}, 320 | "outputs": [], 321 | "source": [] 322 | }, 323 | { 324 | "cell_type": "markdown", 325 | "metadata": { 326 | "slideshow": { 327 | "slide_type": "subslide" 328 | } 329 | }, 330 | "source": [ 331 | "4. The sum of the numbers from 1 to 5 are at least 15." 332 | ] 333 | }, 334 | { 335 | "cell_type": "code", 336 | "execution_count": null, 337 | "metadata": {}, 338 | "outputs": [], 339 | "source": [] 340 | }, 341 | { 342 | "cell_type": "markdown", 343 | "metadata": { 344 | "slideshow": { 345 | "slide_type": "fragment" 346 | } 347 | }, 348 | "source": [ 349 | "(optional). The length of all the DNA in a single adult human, unravelled end to end, is greater than the distance from the earth to the the moon." 350 | ] 351 | }, 352 | { 353 | "cell_type": "code", 354 | "execution_count": null, 355 | "metadata": {}, 356 | "outputs": [], 357 | "source": [] 358 | }, 359 | { 360 | "cell_type": "code", 361 | "execution_count": null, 362 | "metadata": {}, 363 | "outputs": [], 364 | "source": [] 365 | }, 366 | { 367 | "cell_type": "markdown", 368 | "metadata": {}, 369 | "source": [ 370 | "### Combining Logical Expressions: \"And\" and \"Or\"\n", 371 | "\n", 372 | "Finally, you can combine logical expression with \"and\" and \"or\" operators. Python has 2 ways of saying \"and\" and or:\n", 373 | "\n", 374 | "| \"And\" | \"Or\" |\n", 375 | "| :---: | :---:|\n", 376 | "| and | or |\n", 377 | "| & | \\| |\n", 378 | "\n", 379 | "\n", 380 | "```python\n", 381 | ">>> 3 < 5 & 4 > 2\n", 382 | "True\n", 383 | "\n", 384 | ">>> 3 < 5 and 4 > 2\n", 385 | "True\n", 386 | "```\n" 387 | ] 388 | } 389 | ], 390 | "metadata": { 391 | "celltoolbar": "Slideshow", 392 | "kernelspec": { 393 | "display_name": "Python 3", 394 | "language": "python", 395 | "name": "python3" 396 | }, 397 | "language_info": { 398 | "codemirror_mode": { 399 | "name": "ipython", 400 | "version": 3 401 | }, 402 | "file_extension": ".py", 403 | "mimetype": "text/x-python", 404 | "name": "python", 405 | "nbconvert_exporter": "python", 406 | "pygments_lexer": "ipython3", 407 | "version": "3.7.4" 408 | } 409 | }, 410 | "nbformat": 4, 411 | "nbformat_minor": 4 412 | } 413 | -------------------------------------------------------------------------------- /Day0_Python_Syntax/Python Syntax 4.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "slideshow": { 7 | "slide_type": "slide" 8 | } 9 | }, 10 | "source": [ 11 | "# Built-In Type Methods\n", 12 | "\n", 13 | "As we discussed in the lecture, all Python objects are **Objects**, which means they have **Properties/Attributes** (data) and **Methods** associated with them. \n", 14 | "\n", 15 | "| Object | type(Object) | Example Method | Example Attribute | \n", 16 | "| :----: | :----------: | :------------: | :---------------: |\n", 17 | "| \"Hello\" | str | lower() | |\n", 18 | "| 42 | int | bit_length() | numerator |\n", 19 | "| 3.14 | float | is_integer() | imag |\n", 20 | "| ['a', 'b'] | list | append() | |\n", 21 | "\n", 22 | "\n", 23 | "These are accessible via the **. (dot)**, and an object's methods can be browsed in the notebook by typing the **Tab** key after the dot.\n", 24 | "\n", 25 | "For example, here we use a **string** **Class**'s *title()* method to capitalize the first letter of each word of the film title:" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 49, 31 | "metadata": { 32 | "slideshow": { 33 | "slide_type": "subslide" 34 | } 35 | }, 36 | "outputs": [ 37 | { 38 | "data": { 39 | "text/plain": [ 40 | "'the lion king'" 41 | ] 42 | }, 43 | "execution_count": 49, 44 | "metadata": {}, 45 | "output_type": "execute_result" 46 | } 47 | ], 48 | "source": [ 49 | "film = 'the lion king'\n", 50 | "film" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 51, 56 | "metadata": {}, 57 | "outputs": [ 58 | { 59 | "data": { 60 | "text/plain": [ 61 | "str" 62 | ] 63 | }, 64 | "execution_count": 51, 65 | "metadata": {}, 66 | "output_type": "execute_result" 67 | } 68 | ], 69 | "source": [ 70 | "type(film)" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 50, 76 | "metadata": {}, 77 | "outputs": [ 78 | { 79 | "data": { 80 | "text/plain": [ 81 | "'The Lion King'" 82 | ] 83 | }, 84 | "execution_count": 50, 85 | "metadata": {}, 86 | "output_type": "execute_result" 87 | } 88 | ], 89 | "source": [ 90 | "bigfilm = film.title()\n", 91 | "bigfilm" 92 | ] 93 | }, 94 | { 95 | "cell_type": "markdown", 96 | "metadata": { 97 | "slideshow": { 98 | "slide_type": "subslide" 99 | } 100 | }, 101 | "source": [ 102 | "## Exercises: Strings" 103 | ] 104 | }, 105 | { 106 | "cell_type": "markdown", 107 | "metadata": {}, 108 | "source": [ 109 | "Use the string methods to modify the following strings to the versions described below." 110 | ] 111 | }, 112 | { 113 | "cell_type": "markdown", 114 | "metadata": { 115 | "slideshow": { 116 | "slide_type": "subslide" 117 | } 118 | }, 119 | "source": [ 120 | "### Changing Strings\n", 121 | "\n", 122 | "Use the **strip**, **swapcase**, **title**, **lower**, **upper**, and **zfill** methods, or the **\\+** or **\\*** operators, to perform the following modifications:" 123 | ] 124 | }, 125 | { 126 | "cell_type": "markdown", 127 | "metadata": { 128 | "slideshow": { 129 | "slide_type": "fragment" 130 | } 131 | }, 132 | "source": [ 133 | "**\"I am angry\"** -> **\"I AM ANGRY\"**" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": null, 139 | "metadata": {}, 140 | "outputs": [], 141 | "source": [] 142 | }, 143 | { 144 | "cell_type": "markdown", 145 | "metadata": { 146 | "slideshow": { 147 | "slide_type": "fragment" 148 | } 149 | }, 150 | "source": [ 151 | "**\"the lion king\"** -> **\"The Lion King\"**" 152 | ] 153 | }, 154 | { 155 | "cell_type": "code", 156 | "execution_count": null, 157 | "metadata": {}, 158 | "outputs": [], 159 | "source": [] 160 | }, 161 | { 162 | "cell_type": "markdown", 163 | "metadata": { 164 | "slideshow": { 165 | "slide_type": "fragment" 166 | } 167 | }, 168 | "source": [ 169 | "**He said, \"Are you okay?\"** -> **hE SAID, \"aRE YOU OKAY?\"**" 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": null, 175 | "metadata": {}, 176 | "outputs": [], 177 | "source": [] 178 | }, 179 | { 180 | "cell_type": "markdown", 181 | "metadata": { 182 | "slideshow": { 183 | "slide_type": "fragment" 184 | } 185 | }, 186 | "source": [ 187 | "**three** -> **threethreethree**" 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": null, 193 | "metadata": {}, 194 | "outputs": [], 195 | "source": [] 196 | }, 197 | { 198 | "cell_type": "markdown", 199 | "metadata": { 200 | "slideshow": { 201 | "slide_type": "subslide" 202 | } 203 | }, 204 | "source": [ 205 | "### Searching Substrings\n", 206 | "\n", 207 | "Use the **replace**, **count**, and **index** methods to search for a substring and answer the following questions using the example sentence below:\n", 208 | "\n", 209 | "```python\n", 210 | "sentence = \"The dog ate my watermelon when we were digging at the beach!\"\n", 211 | "```" 212 | ] 213 | }, 214 | { 215 | "cell_type": "code", 216 | "execution_count": 27, 217 | "metadata": { 218 | "slideshow": { 219 | "slide_type": "subslide" 220 | } 221 | }, 222 | "outputs": [], 223 | "source": [ 224 | "sentence = \"The dog ate my watermelon when we were digging at the beach!\"" 225 | ] 226 | }, 227 | { 228 | "cell_type": "markdown", 229 | "metadata": { 230 | "slideshow": { 231 | "slide_type": "fragment" 232 | } 233 | }, 234 | "source": [ 235 | "How many \"w\" characters are in this sentence?" 236 | ] 237 | }, 238 | { 239 | "cell_type": "code", 240 | "execution_count": null, 241 | "metadata": {}, 242 | "outputs": [], 243 | "source": [] 244 | }, 245 | { 246 | "cell_type": "markdown", 247 | "metadata": { 248 | "slideshow": { 249 | "slide_type": "fragment" 250 | } 251 | }, 252 | "source": [ 253 | "What character index does the word \"watermelon\" start on? (the first? The tenth?)" 254 | ] 255 | }, 256 | { 257 | "cell_type": "code", 258 | "execution_count": null, 259 | "metadata": {}, 260 | "outputs": [], 261 | "source": [] 262 | }, 263 | { 264 | "cell_type": "markdown", 265 | "metadata": { 266 | "slideshow": { 267 | "slide_type": "fragment" 268 | } 269 | }, 270 | "source": [ 271 | "Replace the word \"dog\" in the sentence with the word \"cat\"" 272 | ] 273 | }, 274 | { 275 | "cell_type": "code", 276 | "execution_count": null, 277 | "metadata": {}, 278 | "outputs": [], 279 | "source": [] 280 | }, 281 | { 282 | "cell_type": "markdown", 283 | "metadata": { 284 | "slideshow": { 285 | "slide_type": "slide" 286 | } 287 | }, 288 | "source": [ 289 | "### Formatting Strings\n", 290 | "\n", 291 | "The most common method used for strings is the **format()** methods, so it deserves special mention. It is used to insert data into a string. It is similar to the **replace()** method but only replaces curly braces **{}**, but it is *way* more powerful. In fact, there is a whole web page dedicated to showing all the ways you can use it: https://pyformat.info/\n", 292 | "\n", 293 | "```python\n", 294 | "label_fmt = \"Sample {}: Vial {}\"\n", 295 | "label_fmt.format(3, 4)\n", 296 | ">> \"Sample 3: Vial 4\"\n", 297 | "```" 298 | ] 299 | }, 300 | { 301 | "cell_type": "markdown", 302 | "metadata": { 303 | "slideshow": { 304 | "slide_type": "subslide" 305 | } 306 | }, 307 | "source": [ 308 | "Using the **format()** method, make the following three sentences out of the original sentence below:" 309 | ] 310 | }, 311 | { 312 | "cell_type": "code", 313 | "execution_count": 52, 314 | "metadata": { 315 | "slideshow": { 316 | "slide_type": "-" 317 | } 318 | }, 319 | "outputs": [], 320 | "source": [ 321 | "sentence = \"I bought {} dogs.\"" 322 | ] 323 | }, 324 | { 325 | "cell_type": "markdown", 326 | "metadata": { 327 | "slideshow": { 328 | "slide_type": "fragment" 329 | } 330 | }, 331 | "source": [ 332 | "1. \"I bought 3 dogs.\"" 333 | ] 334 | }, 335 | { 336 | "cell_type": "code", 337 | "execution_count": null, 338 | "metadata": { 339 | "slideshow": { 340 | "slide_type": "-" 341 | } 342 | }, 343 | "outputs": [], 344 | "source": [] 345 | }, 346 | { 347 | "cell_type": "markdown", 348 | "metadata": { 349 | "slideshow": { 350 | "slide_type": "fragment" 351 | } 352 | }, 353 | "source": [ 354 | "2. \"Wendy bought 5 dogs.\"" 355 | ] 356 | }, 357 | { 358 | "cell_type": "code", 359 | "execution_count": null, 360 | "metadata": {}, 361 | "outputs": [], 362 | "source": [] 363 | }, 364 | { 365 | "cell_type": "markdown", 366 | "metadata": { 367 | "slideshow": { 368 | "slide_type": "fragment" 369 | } 370 | }, 371 | "source": [ 372 | "3. \"George bought 700 dogs.\"" 373 | ] 374 | }, 375 | { 376 | "cell_type": "code", 377 | "execution_count": null, 378 | "metadata": {}, 379 | "outputs": [], 380 | "source": [] 381 | }, 382 | { 383 | "cell_type": "markdown", 384 | "metadata": { 385 | "slideshow": { 386 | "slide_type": "slide" 387 | } 388 | }, 389 | "source": [ 390 | "## Lists\n", 391 | "\n", 392 | "Lists represent sequences of data. They are made with either the square brackets **[]** or the **list()** constructor function. They can be appended to, just like strings, but they can contain any kind of data--numbers, strings, even other lists. Their methods mostly revolve around inserting and removing things:\n", 393 | "\n", 394 | "| Method | What it Does | \n", 395 | "| :-----: | :----------: |\n", 396 | "| append() | Appends an item to the end of the list. | \n", 397 | "| extend() | Appends several items to the end of the list. | \n", 398 | "| remove() | Removes an item from the list. |\n", 399 | "| clear() | Removes everything from the list. | \n", 400 | "| copy() | Returns a copy of the list. |\n", 401 | "| count() | Removes a specified item from the list | \n", 402 | "| sort() | Sorts the list. |\n", 403 | "| reverse()| Reverses the order of items in the list. |" 404 | ] 405 | }, 406 | { 407 | "cell_type": "markdown", 408 | "metadata": { 409 | "slideshow": { 410 | "slide_type": "subslide" 411 | } 412 | }, 413 | "source": [ 414 | "**Example**\n", 415 | "\n", 416 | "Here is a list of colors:" 417 | ] 418 | }, 419 | { 420 | "cell_type": "code", 421 | "execution_count": 56, 422 | "metadata": {}, 423 | "outputs": [ 424 | { 425 | "data": { 426 | "text/plain": [ 427 | "['Red', 'Blue', 'Yello']" 428 | ] 429 | }, 430 | "execution_count": 56, 431 | "metadata": {}, 432 | "output_type": "execute_result" 433 | } 434 | ], 435 | "source": [ 436 | "colors = ['Red', 'Blue', 'Yello']\n", 437 | "colors" 438 | ] 439 | }, 440 | { 441 | "cell_type": "markdown", 442 | "metadata": { 443 | "slideshow": { 444 | "slide_type": "-" 445 | } 446 | }, 447 | "source": [ 448 | "Let's append the color 'Yellow' to the list:" 449 | ] 450 | }, 451 | { 452 | "cell_type": "code", 453 | "execution_count": 57, 454 | "metadata": {}, 455 | "outputs": [ 456 | { 457 | "data": { 458 | "text/plain": [ 459 | "['Red', 'Blue', 'Yello', 'Yellow']" 460 | ] 461 | }, 462 | "execution_count": 57, 463 | "metadata": {}, 464 | "output_type": "execute_result" 465 | } 466 | ], 467 | "source": [ 468 | "colors.append('Yellow')\n", 469 | "colors" 470 | ] 471 | }, 472 | { 473 | "cell_type": "markdown", 474 | "metadata": { 475 | "slideshow": { 476 | "slide_type": "-" 477 | } 478 | }, 479 | "source": [ 480 | "*Note*: If you run the above code multiple times, 'Yellow' will be appended to the list multiple times!" 481 | ] 482 | }, 483 | { 484 | "cell_type": "markdown", 485 | "metadata": { 486 | "slideshow": { 487 | "slide_type": "subslide" 488 | } 489 | }, 490 | "source": [ 491 | "### List Method Exercises" 492 | ] 493 | }, 494 | { 495 | "cell_type": "markdown", 496 | "metadata": { 497 | "slideshow": { 498 | "slide_type": "fragment" 499 | } 500 | }, 501 | "source": [ 502 | "1. Make a list of 3 movie titles." 503 | ] 504 | }, 505 | { 506 | "cell_type": "code", 507 | "execution_count": null, 508 | "metadata": {}, 509 | "outputs": [], 510 | "source": [] 511 | }, 512 | { 513 | "cell_type": "markdown", 514 | "metadata": { 515 | "slideshow": { 516 | "slide_type": "fragment" 517 | } 518 | }, 519 | "source": [ 520 | "2. You have a list of subjects for your experiment, and you want to add another one to that list! How could you do it?" 521 | ] 522 | }, 523 | { 524 | "cell_type": "code", 525 | "execution_count": 40, 526 | "metadata": {}, 527 | "outputs": [], 528 | "source": [ 529 | "subjects = ['NOP234', 'GHS673', 'JGL212']\n", 530 | "new_subject = 'ASF193'" 531 | ] 532 | }, 533 | { 534 | "cell_type": "code", 535 | "execution_count": null, 536 | "metadata": {}, 537 | "outputs": [], 538 | "source": [] 539 | }, 540 | { 541 | "cell_type": "markdown", 542 | "metadata": { 543 | "slideshow": { 544 | "slide_type": "subslide" 545 | } 546 | }, 547 | "source": [ 548 | "3. Now, a bunch of new subjects appeared! How do you extend/append them to the main list?" 549 | ] 550 | }, 551 | { 552 | "cell_type": "code", 553 | "execution_count": 58, 554 | "metadata": { 555 | "slideshow": { 556 | "slide_type": "-" 557 | } 558 | }, 559 | "outputs": [], 560 | "source": [ 561 | "subjects = ['NOP234', 'GHS673', 'JGL212']\n", 562 | "new_subjects = ['ASF193', 'THW994', 'JJZ231']" 563 | ] 564 | }, 565 | { 566 | "cell_type": "code", 567 | "execution_count": null, 568 | "metadata": {}, 569 | "outputs": [], 570 | "source": [] 571 | }, 572 | { 573 | "cell_type": "markdown", 574 | "metadata": { 575 | "slideshow": { 576 | "slide_type": "fragment" 577 | } 578 | }, 579 | "source": [ 580 | "4. Please sort those subjects alphabetically. It looks better that way, doesn't it?" 581 | ] 582 | }, 583 | { 584 | "cell_type": "code", 585 | "execution_count": 2, 586 | "metadata": {}, 587 | "outputs": [], 588 | "source": [ 589 | "subjects = ['NOP234', 'GHS673', 'jGL212', 'ASF193', 'THW994', 'JJZ231']" 590 | ] 591 | }, 592 | { 593 | "cell_type": "code", 594 | "execution_count": null, 595 | "metadata": {}, 596 | "outputs": [], 597 | "source": [] 598 | }, 599 | { 600 | "cell_type": "markdown", 601 | "metadata": { 602 | "slideshow": { 603 | "slide_type": "subslide" 604 | } 605 | }, 606 | "source": [ 607 | "5. Oh, no, 'JGL202' was a terrible subject, he intentionally ruined your study. Well, there's no way you're keeping him. How do you remove him from the list?" 608 | ] 609 | }, 610 | { 611 | "cell_type": "code", 612 | "execution_count": 1, 613 | "metadata": {}, 614 | "outputs": [], 615 | "source": [ 616 | "subjects = ['NOP234', 'GHS673', 'JGL212', 'ASF193', 'THW994', 'JJZ231']" 617 | ] 618 | }, 619 | { 620 | "cell_type": "markdown", 621 | "metadata": { 622 | "slideshow": { 623 | "slide_type": "slide" 624 | } 625 | }, 626 | "source": [ 627 | "## Further Learning Resources" 628 | ] 629 | }, 630 | { 631 | "cell_type": "markdown", 632 | "metadata": {}, 633 | "source": [ 634 | " - Official Documentation on String Methods: https://docs.python.org/3/library/stdtypes.html#string-methods\n", 635 | " \n", 636 | " - Online Tutorial on Python Strings: https://realpython.com/python-strings/\n", 637 | " \n", 638 | " - Official Documentation on List Methods: https://docs.python.org/3/library/stdtypes.html#lists\n", 639 | " \n", 640 | " - Online Tutorial on Python Lists: https://realpython.com/python-lists-tuples/\n", 641 | " \n", 642 | " " 643 | ] 644 | }, 645 | { 646 | "cell_type": "markdown", 647 | "metadata": { 648 | "slideshow": { 649 | "slide_type": "slide" 650 | } 651 | }, 652 | "source": [ 653 | "## Extra Exercises\n", 654 | "\n", 655 | "If you'd like to do some more, here they are!\n", 656 | "\n", 657 | "\n", 658 | "1. You want to label your figure title with the subject code, and the subject code changes depending on which subject is being shown! How could you stick a subject's name in your title string?" 659 | ] 660 | }, 661 | { 662 | "cell_type": "code", 663 | "execution_count": 4, 664 | "metadata": {}, 665 | "outputs": [], 666 | "source": [ 667 | "subject = 'NOP234'\n", 668 | "title = \"Mean Values of SUBJECT NAME's data over Time\"" 669 | ] 670 | }, 671 | { 672 | "cell_type": "code", 673 | "execution_count": null, 674 | "metadata": {}, 675 | "outputs": [], 676 | "source": [] 677 | }, 678 | { 679 | "cell_type": "markdown", 680 | "metadata": {}, 681 | "source": [ 682 | "2. If you had formatted your title string in the following way, though, another string method would be more useful. Which method would you use for this title:" 683 | ] 684 | }, 685 | { 686 | "cell_type": "code", 687 | "execution_count": 3, 688 | "metadata": {}, 689 | "outputs": [], 690 | "source": [ 691 | "subject1 = 'NOP234'\n", 692 | "subject2 = 'GHS673'\n", 693 | "title = \"Performance Comparison between Subjects {} and {}\"" 694 | ] 695 | }, 696 | { 697 | "cell_type": "code", 698 | "execution_count": null, 699 | "metadata": {}, 700 | "outputs": [], 701 | "source": [] 702 | }, 703 | { 704 | "cell_type": "markdown", 705 | "metadata": {}, 706 | "source": [ 707 | "3. The conference says it only takes abstracts that have a maximum word count of 100 words. Did our abstract make the cut? \n", 708 | "\n", 709 | "*Hint: the len() function is useful here*" 710 | ] 711 | }, 712 | { 713 | "cell_type": "code", 714 | "execution_count": 6, 715 | "metadata": {}, 716 | "outputs": [], 717 | "source": [ 718 | "abstract = \"\"\"We analyze the locomotor behavior of the rat during exploration, and show that digitally collected data (time series of positions) provide a sufftcient basis for establishing that the rat uses several distinct modes of motion (first, second, third, and sometimes fourth gear). The distinction between these modes is obtained by first segmenting the time series into sequences of data points occurring between arrests (as ascertained within the resolution of the data acquisition system). The statistical distribution of the maximal amount of motion occurring within each of these episodes is then analyzed and shown to be multi modal. This enables us to decompose motion into distinct modes.\"\"\"\n" 719 | ] 720 | }, 721 | { 722 | "cell_type": "markdown", 723 | "metadata": {}, 724 | "source": [ 725 | "Oh, wait, now that I look closer, it actually says that it's a maximum of 100 **unique** words--duplicated words don't count. How many unique words do we have in our abstract? (What a strange conference...)\n", 726 | "\n", 727 | "\n", 728 | "*Hint: Try this out with the **set()** class.*" 729 | ] 730 | }, 731 | { 732 | "cell_type": "code", 733 | "execution_count": null, 734 | "metadata": {}, 735 | "outputs": [], 736 | "source": [] 737 | } 738 | ], 739 | "metadata": { 740 | "kernelspec": { 741 | "display_name": "Python 3", 742 | "language": "python", 743 | "name": "python3" 744 | }, 745 | "language_info": { 746 | "codemirror_mode": { 747 | "name": "ipython", 748 | "version": 3 749 | }, 750 | "file_extension": ".py", 751 | "mimetype": "text/x-python", 752 | "name": "python", 753 | "nbconvert_exporter": "python", 754 | "pygments_lexer": "ipython3", 755 | "version": "3.7.4" 756 | } 757 | }, 758 | "nbformat": 4, 759 | "nbformat_minor": 4 760 | } 761 | -------------------------------------------------------------------------------- /Day1_Code_Blocks/Python Syntax 4.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "slideshow": { 7 | "slide_type": "slide" 8 | } 9 | }, 10 | "source": [ 11 | "# Built-In Type Methods\n", 12 | "\n", 13 | "As we discussed in the lecture, all Python objects are **Objects**, which means they have **Properties/Attributes** (data) and **Methods** associated with them. \n", 14 | "\n", 15 | "| Object | type(Object) | Example Method | Example Attribute | \n", 16 | "| :----: | :----------: | :------------: | :---------------: |\n", 17 | "| \"Hello\" | str | lower() | |\n", 18 | "| 42 | int | bit_length() | numerator |\n", 19 | "| 3.14 | float | is_integer() | imag |\n", 20 | "| ['a', 'b'] | list | append() | |\n", 21 | "\n", 22 | "\n", 23 | "These are accessible via the **. (dot)**, and an object's methods can be browsed in the notebook by typing the **Tab** key after the dot.\n", 24 | "\n", 25 | "For example, here we use a **string** **Class**'s *title()* method to capitalize the first letter of each word of the film title:" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 49, 31 | "metadata": { 32 | "slideshow": { 33 | "slide_type": "subslide" 34 | } 35 | }, 36 | "outputs": [ 37 | { 38 | "data": { 39 | "text/plain": [ 40 | "'the lion king'" 41 | ] 42 | }, 43 | "execution_count": 49, 44 | "metadata": {}, 45 | "output_type": "execute_result" 46 | } 47 | ], 48 | "source": [ 49 | "film = 'the lion king'\n", 50 | "film" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 51, 56 | "metadata": {}, 57 | "outputs": [ 58 | { 59 | "data": { 60 | "text/plain": [ 61 | "str" 62 | ] 63 | }, 64 | "execution_count": 51, 65 | "metadata": {}, 66 | "output_type": "execute_result" 67 | } 68 | ], 69 | "source": [ 70 | "type(film)" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 50, 76 | "metadata": {}, 77 | "outputs": [ 78 | { 79 | "data": { 80 | "text/plain": [ 81 | "'The Lion King'" 82 | ] 83 | }, 84 | "execution_count": 50, 85 | "metadata": {}, 86 | "output_type": "execute_result" 87 | } 88 | ], 89 | "source": [ 90 | "bigfilm = film.title()\n", 91 | "bigfilm" 92 | ] 93 | }, 94 | { 95 | "cell_type": "markdown", 96 | "metadata": { 97 | "slideshow": { 98 | "slide_type": "subslide" 99 | } 100 | }, 101 | "source": [ 102 | "## Exercises: Strings" 103 | ] 104 | }, 105 | { 106 | "cell_type": "markdown", 107 | "metadata": {}, 108 | "source": [ 109 | "Use the string methods to modify the following strings to the versions described below." 110 | ] 111 | }, 112 | { 113 | "cell_type": "markdown", 114 | "metadata": { 115 | "slideshow": { 116 | "slide_type": "subslide" 117 | } 118 | }, 119 | "source": [ 120 | "### Changing Strings\n", 121 | "\n", 122 | "Use the **strip**, **swapcase**, **title**, **lower**, **upper**, and **zfill** methods, or the **\\+** or **\\*** operators, to perform the following modifications:" 123 | ] 124 | }, 125 | { 126 | "cell_type": "markdown", 127 | "metadata": { 128 | "slideshow": { 129 | "slide_type": "fragment" 130 | } 131 | }, 132 | "source": [ 133 | "**\"I am angry\"** -> **\"I AM ANGRY\"**" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": null, 139 | "metadata": {}, 140 | "outputs": [], 141 | "source": [] 142 | }, 143 | { 144 | "cell_type": "markdown", 145 | "metadata": { 146 | "slideshow": { 147 | "slide_type": "fragment" 148 | } 149 | }, 150 | "source": [ 151 | "**\"the lion king\"** -> **\"The Lion King\"**" 152 | ] 153 | }, 154 | { 155 | "cell_type": "code", 156 | "execution_count": null, 157 | "metadata": {}, 158 | "outputs": [], 159 | "source": [] 160 | }, 161 | { 162 | "cell_type": "markdown", 163 | "metadata": { 164 | "slideshow": { 165 | "slide_type": "fragment" 166 | } 167 | }, 168 | "source": [ 169 | "**He said, \"Are you okay?\"** -> **hE SAID, \"aRE YOU OKAY?\"**" 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": null, 175 | "metadata": {}, 176 | "outputs": [], 177 | "source": [] 178 | }, 179 | { 180 | "cell_type": "markdown", 181 | "metadata": { 182 | "slideshow": { 183 | "slide_type": "fragment" 184 | } 185 | }, 186 | "source": [ 187 | "**three** -> **threethreethree**" 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": null, 193 | "metadata": {}, 194 | "outputs": [], 195 | "source": [] 196 | }, 197 | { 198 | "cell_type": "markdown", 199 | "metadata": { 200 | "slideshow": { 201 | "slide_type": "subslide" 202 | } 203 | }, 204 | "source": [ 205 | "### Searching Substrings\n", 206 | "\n", 207 | "Use the **replace**, **count**, and **index** methods to search for a substring and answer the following questions using the example sentence below:\n", 208 | "\n", 209 | "```python\n", 210 | "sentence = \"The dog ate my watermelon when we were digging at the beach!\"\n", 211 | "```" 212 | ] 213 | }, 214 | { 215 | "cell_type": "code", 216 | "execution_count": 27, 217 | "metadata": { 218 | "slideshow": { 219 | "slide_type": "subslide" 220 | } 221 | }, 222 | "outputs": [], 223 | "source": [ 224 | "sentence = \"The dog ate my watermelon when we were digging at the beach!\"" 225 | ] 226 | }, 227 | { 228 | "cell_type": "markdown", 229 | "metadata": { 230 | "slideshow": { 231 | "slide_type": "fragment" 232 | } 233 | }, 234 | "source": [ 235 | "How many \"w\" characters are in this sentence?" 236 | ] 237 | }, 238 | { 239 | "cell_type": "code", 240 | "execution_count": null, 241 | "metadata": {}, 242 | "outputs": [], 243 | "source": [] 244 | }, 245 | { 246 | "cell_type": "markdown", 247 | "metadata": { 248 | "slideshow": { 249 | "slide_type": "fragment" 250 | } 251 | }, 252 | "source": [ 253 | "What character index does the word \"watermelon\" start on? (the first? The tenth?)" 254 | ] 255 | }, 256 | { 257 | "cell_type": "code", 258 | "execution_count": null, 259 | "metadata": {}, 260 | "outputs": [], 261 | "source": [] 262 | }, 263 | { 264 | "cell_type": "markdown", 265 | "metadata": { 266 | "slideshow": { 267 | "slide_type": "fragment" 268 | } 269 | }, 270 | "source": [ 271 | "Replace the word \"dog\" in the sentence with the word \"cat\"" 272 | ] 273 | }, 274 | { 275 | "cell_type": "code", 276 | "execution_count": null, 277 | "metadata": {}, 278 | "outputs": [], 279 | "source": [] 280 | }, 281 | { 282 | "cell_type": "markdown", 283 | "metadata": { 284 | "slideshow": { 285 | "slide_type": "slide" 286 | } 287 | }, 288 | "source": [ 289 | "### Formatting Strings\n", 290 | "\n", 291 | "The most common method used for strings is the **format()** methods, so it deserves special mention. It is used to insert data into a string. It is similar to the **replace()** method but only replaces curly braces **{}**, but it is *way* more powerful. In fact, there is a whole web page dedicated to showing all the ways you can use it: https://pyformat.info/\n", 292 | "\n", 293 | "```python\n", 294 | "label_fmt = \"Sample {}: Vial {}\"\n", 295 | "label_fmt.format(3, 4)\n", 296 | ">> \"Sample 3: Vial 4\"\n", 297 | "```" 298 | ] 299 | }, 300 | { 301 | "cell_type": "markdown", 302 | "metadata": { 303 | "slideshow": { 304 | "slide_type": "subslide" 305 | } 306 | }, 307 | "source": [ 308 | "Using the **format()** method, make the following three sentences out of the original sentence below:" 309 | ] 310 | }, 311 | { 312 | "cell_type": "code", 313 | "execution_count": 52, 314 | "metadata": { 315 | "slideshow": { 316 | "slide_type": "-" 317 | } 318 | }, 319 | "outputs": [], 320 | "source": [ 321 | "sentence = \"I bought {} dogs.\"" 322 | ] 323 | }, 324 | { 325 | "cell_type": "markdown", 326 | "metadata": { 327 | "slideshow": { 328 | "slide_type": "fragment" 329 | } 330 | }, 331 | "source": [ 332 | "1. \"I bought 3 dogs.\"" 333 | ] 334 | }, 335 | { 336 | "cell_type": "code", 337 | "execution_count": null, 338 | "metadata": { 339 | "slideshow": { 340 | "slide_type": "-" 341 | } 342 | }, 343 | "outputs": [], 344 | "source": [] 345 | }, 346 | { 347 | "cell_type": "markdown", 348 | "metadata": { 349 | "slideshow": { 350 | "slide_type": "fragment" 351 | } 352 | }, 353 | "source": [ 354 | "2. \"Wendy bought 5 dogs.\"" 355 | ] 356 | }, 357 | { 358 | "cell_type": "code", 359 | "execution_count": null, 360 | "metadata": {}, 361 | "outputs": [], 362 | "source": [] 363 | }, 364 | { 365 | "cell_type": "markdown", 366 | "metadata": { 367 | "slideshow": { 368 | "slide_type": "fragment" 369 | } 370 | }, 371 | "source": [ 372 | "3. \"George bought 700 dogs.\"" 373 | ] 374 | }, 375 | { 376 | "cell_type": "code", 377 | "execution_count": null, 378 | "metadata": {}, 379 | "outputs": [], 380 | "source": [] 381 | }, 382 | { 383 | "cell_type": "markdown", 384 | "metadata": { 385 | "slideshow": { 386 | "slide_type": "slide" 387 | } 388 | }, 389 | "source": [ 390 | "## Lists\n", 391 | "\n", 392 | "Lists represent sequences of data. They are made with either the square brackets **[]** or the **list()** constructor function. They can be appended to, just like strings, but they can contain any kind of data--numbers, strings, even other lists. Their methods mostly revolve around inserting and removing things:\n", 393 | "\n", 394 | "| Method | What it Does | \n", 395 | "| :-----: | :----------: |\n", 396 | "| append() | Appends an item to the end of the list. | \n", 397 | "| extend() | Appends several items to the end of the list. | \n", 398 | "| remove() | Removes an item from the list. |\n", 399 | "| clear() | Removes everything from the list. | \n", 400 | "| copy() | Returns a copy of the list. |\n", 401 | "| count() | Removes a specified item from the list | \n", 402 | "| sort() | Sorts the list. |\n", 403 | "| reverse()| Reverses the order of items in the list. |" 404 | ] 405 | }, 406 | { 407 | "cell_type": "markdown", 408 | "metadata": { 409 | "slideshow": { 410 | "slide_type": "subslide" 411 | } 412 | }, 413 | "source": [ 414 | "**Example**\n", 415 | "\n", 416 | "Here is a list of colors:" 417 | ] 418 | }, 419 | { 420 | "cell_type": "code", 421 | "execution_count": 56, 422 | "metadata": {}, 423 | "outputs": [ 424 | { 425 | "data": { 426 | "text/plain": [ 427 | "['Red', 'Blue', 'Yello']" 428 | ] 429 | }, 430 | "execution_count": 56, 431 | "metadata": {}, 432 | "output_type": "execute_result" 433 | } 434 | ], 435 | "source": [ 436 | "colors = ['Red', 'Blue', 'Yello']\n", 437 | "colors" 438 | ] 439 | }, 440 | { 441 | "cell_type": "markdown", 442 | "metadata": { 443 | "slideshow": { 444 | "slide_type": "-" 445 | } 446 | }, 447 | "source": [ 448 | "Let's append the color 'Yellow' to the list:" 449 | ] 450 | }, 451 | { 452 | "cell_type": "code", 453 | "execution_count": 57, 454 | "metadata": {}, 455 | "outputs": [ 456 | { 457 | "data": { 458 | "text/plain": [ 459 | "['Red', 'Blue', 'Yello', 'Yellow']" 460 | ] 461 | }, 462 | "execution_count": 57, 463 | "metadata": {}, 464 | "output_type": "execute_result" 465 | } 466 | ], 467 | "source": [ 468 | "colors.append('Yellow')\n", 469 | "colors" 470 | ] 471 | }, 472 | { 473 | "cell_type": "markdown", 474 | "metadata": { 475 | "slideshow": { 476 | "slide_type": "-" 477 | } 478 | }, 479 | "source": [ 480 | "*Note*: If you run the above code multiple times, 'Yellow' will be appended to the list multiple times!" 481 | ] 482 | }, 483 | { 484 | "cell_type": "markdown", 485 | "metadata": { 486 | "slideshow": { 487 | "slide_type": "subslide" 488 | } 489 | }, 490 | "source": [ 491 | "### List Method Exercises" 492 | ] 493 | }, 494 | { 495 | "cell_type": "markdown", 496 | "metadata": { 497 | "slideshow": { 498 | "slide_type": "fragment" 499 | } 500 | }, 501 | "source": [ 502 | "1. Make a list of 3 movie titles." 503 | ] 504 | }, 505 | { 506 | "cell_type": "code", 507 | "execution_count": null, 508 | "metadata": {}, 509 | "outputs": [], 510 | "source": [] 511 | }, 512 | { 513 | "cell_type": "markdown", 514 | "metadata": { 515 | "slideshow": { 516 | "slide_type": "fragment" 517 | } 518 | }, 519 | "source": [ 520 | "2. You have a list of subjects for your experiment, and you want to add another one to that list! How could you do it?" 521 | ] 522 | }, 523 | { 524 | "cell_type": "code", 525 | "execution_count": 40, 526 | "metadata": {}, 527 | "outputs": [], 528 | "source": [ 529 | "subjects = ['NOP234', 'GHS673', 'JGL212']\n", 530 | "new_subject = 'ASF193'" 531 | ] 532 | }, 533 | { 534 | "cell_type": "code", 535 | "execution_count": null, 536 | "metadata": {}, 537 | "outputs": [], 538 | "source": [] 539 | }, 540 | { 541 | "cell_type": "markdown", 542 | "metadata": { 543 | "slideshow": { 544 | "slide_type": "subslide" 545 | } 546 | }, 547 | "source": [ 548 | "3. Now, a bunch of new subjects appeared! How do you extend/append them to the main list?" 549 | ] 550 | }, 551 | { 552 | "cell_type": "code", 553 | "execution_count": 58, 554 | "metadata": { 555 | "slideshow": { 556 | "slide_type": "-" 557 | } 558 | }, 559 | "outputs": [], 560 | "source": [ 561 | "subjects = ['NOP234', 'GHS673', 'JGL212']\n", 562 | "new_subjects = ['ASF193', 'THW994', 'JJZ231']" 563 | ] 564 | }, 565 | { 566 | "cell_type": "code", 567 | "execution_count": null, 568 | "metadata": {}, 569 | "outputs": [], 570 | "source": [] 571 | }, 572 | { 573 | "cell_type": "markdown", 574 | "metadata": { 575 | "slideshow": { 576 | "slide_type": "fragment" 577 | } 578 | }, 579 | "source": [ 580 | "4. Please sort those subjects alphabetically. It looks better that way, doesn't it?" 581 | ] 582 | }, 583 | { 584 | "cell_type": "code", 585 | "execution_count": 2, 586 | "metadata": {}, 587 | "outputs": [], 588 | "source": [ 589 | "subjects = ['NOP234', 'GHS673', 'jGL212', 'ASF193', 'THW994', 'JJZ231']" 590 | ] 591 | }, 592 | { 593 | "cell_type": "code", 594 | "execution_count": null, 595 | "metadata": {}, 596 | "outputs": [], 597 | "source": [] 598 | }, 599 | { 600 | "cell_type": "markdown", 601 | "metadata": { 602 | "slideshow": { 603 | "slide_type": "subslide" 604 | } 605 | }, 606 | "source": [ 607 | "5. Oh, no, 'JGL202' was a terrible subject, he intentionally ruined your study. Well, there's no way you're keeping him. How do you remove him from the list?" 608 | ] 609 | }, 610 | { 611 | "cell_type": "code", 612 | "execution_count": 1, 613 | "metadata": {}, 614 | "outputs": [], 615 | "source": [ 616 | "subjects = ['NOP234', 'GHS673', 'JGL212', 'ASF193', 'THW994', 'JJZ231']" 617 | ] 618 | }, 619 | { 620 | "cell_type": "markdown", 621 | "metadata": { 622 | "slideshow": { 623 | "slide_type": "slide" 624 | } 625 | }, 626 | "source": [ 627 | "## Further Learning Resources" 628 | ] 629 | }, 630 | { 631 | "cell_type": "markdown", 632 | "metadata": {}, 633 | "source": [ 634 | " - Official Documentation on String Methods: https://docs.python.org/3/library/stdtypes.html#string-methods\n", 635 | " \n", 636 | " - Online Tutorial on Python Strings: https://realpython.com/python-strings/\n", 637 | " \n", 638 | " - Official Documentation on List Methods: https://docs.python.org/3/library/stdtypes.html#lists\n", 639 | " \n", 640 | " - Online Tutorial on Python Lists: https://realpython.com/python-lists-tuples/\n", 641 | " \n", 642 | " " 643 | ] 644 | }, 645 | { 646 | "cell_type": "markdown", 647 | "metadata": { 648 | "slideshow": { 649 | "slide_type": "slide" 650 | } 651 | }, 652 | "source": [ 653 | "## Extra Exercises\n", 654 | "\n", 655 | "If you'd like to do some more, here they are!\n", 656 | "\n", 657 | "\n", 658 | "1. You want to label your figure title with the subject code, and the subject code changes depending on which subject is being shown! How could you stick a subject's name in your title string?" 659 | ] 660 | }, 661 | { 662 | "cell_type": "code", 663 | "execution_count": 4, 664 | "metadata": {}, 665 | "outputs": [], 666 | "source": [ 667 | "subject = 'NOP234'\n", 668 | "title = \"Mean Values of SUBJECT NAME's data over Time\"" 669 | ] 670 | }, 671 | { 672 | "cell_type": "code", 673 | "execution_count": null, 674 | "metadata": {}, 675 | "outputs": [], 676 | "source": [] 677 | }, 678 | { 679 | "cell_type": "markdown", 680 | "metadata": {}, 681 | "source": [ 682 | "2. If you had formatted your title string in the following way, though, another string method would be more useful. Which method would you use for this title:" 683 | ] 684 | }, 685 | { 686 | "cell_type": "code", 687 | "execution_count": 3, 688 | "metadata": {}, 689 | "outputs": [], 690 | "source": [ 691 | "subject1 = 'NOP234'\n", 692 | "subject2 = 'GHS673'\n", 693 | "title = \"Performance Comparison between Subjects {} and {}\"" 694 | ] 695 | }, 696 | { 697 | "cell_type": "code", 698 | "execution_count": null, 699 | "metadata": {}, 700 | "outputs": [], 701 | "source": [] 702 | }, 703 | { 704 | "cell_type": "markdown", 705 | "metadata": {}, 706 | "source": [ 707 | "3. The conference says it only takes abstracts that have a maximum word count of 100 words. Did our abstract make the cut? \n", 708 | "\n", 709 | "*Hint: the len() function is useful here*" 710 | ] 711 | }, 712 | { 713 | "cell_type": "code", 714 | "execution_count": 6, 715 | "metadata": {}, 716 | "outputs": [], 717 | "source": [ 718 | "abstract = \"\"\"We analyze the locomotor behavior of the rat during exploration, and show that digitally collected data (time series of positions) provide a sufftcient basis for establishing that the rat uses several distinct modes of motion (first, second, third, and sometimes fourth gear). The distinction between these modes is obtained by first segmenting the time series into sequences of data points occurring between arrests (as ascertained within the resolution of the data acquisition system). The statistical distribution of the maximal amount of motion occurring within each of these episodes is then analyzed and shown to be multi modal. This enables us to decompose motion into distinct modes.\"\"\"\n" 719 | ] 720 | }, 721 | { 722 | "cell_type": "markdown", 723 | "metadata": {}, 724 | "source": [ 725 | "Oh, wait, now that I look closer, it actually says that it's a maximum of 100 **unique** words--duplicated words don't count. How many unique words do we have in our abstract? (What a strange conference...)\n", 726 | "\n", 727 | "\n", 728 | "*Hint: Try this out with the **set()** class.*" 729 | ] 730 | }, 731 | { 732 | "cell_type": "code", 733 | "execution_count": null, 734 | "metadata": {}, 735 | "outputs": [], 736 | "source": [] 737 | } 738 | ], 739 | "metadata": { 740 | "kernelspec": { 741 | "display_name": "Python 3", 742 | "language": "python", 743 | "name": "python3" 744 | }, 745 | "language_info": { 746 | "codemirror_mode": { 747 | "name": "ipython", 748 | "version": 3 749 | }, 750 | "file_extension": ".py", 751 | "mimetype": "text/x-python", 752 | "name": "python", 753 | "nbconvert_exporter": "python", 754 | "pygments_lexer": "ipython3", 755 | "version": "3.7.4" 756 | } 757 | }, 758 | "nbformat": 4, 759 | "nbformat_minor": 4 760 | } 761 | -------------------------------------------------------------------------------- /Day0_Python_Syntax/.ipynb_checkpoints/Python Syntax 4-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "slideshow": { 7 | "slide_type": "slide" 8 | } 9 | }, 10 | "source": [ 11 | "# Built-In Type Methods\n", 12 | "\n", 13 | "As we discussed in the lecture, all Python objects are **Objects**, which means they have **Properties/Attributes** (data) and **Methods** associated with them. \n", 14 | "\n", 15 | "| Object | type(Object) | Example Method | Example Attribute | \n", 16 | "| :----: | :----------: | :------------: | :---------------: |\n", 17 | "| \"Hello\" | str | lower() | |\n", 18 | "| 42 | int | bit_length() | numerator |\n", 19 | "| 3.14 | float | is_integer() | imag |\n", 20 | "| ['a', 'b'] | list | append() | |\n", 21 | "\n", 22 | "\n", 23 | "These are accessible via the **. (dot)**, and an object's methods can be browsed in the notebook by typing the **Tab** key after the dot.\n", 24 | "\n", 25 | "For example, here we use a **string** **Class**'s *title()* method to capitalize the first letter of each word of the film title:" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 49, 31 | "metadata": { 32 | "slideshow": { 33 | "slide_type": "subslide" 34 | } 35 | }, 36 | "outputs": [ 37 | { 38 | "data": { 39 | "text/plain": [ 40 | "'the lion king'" 41 | ] 42 | }, 43 | "execution_count": 49, 44 | "metadata": {}, 45 | "output_type": "execute_result" 46 | } 47 | ], 48 | "source": [ 49 | "film = 'the lion king'\n", 50 | "film" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 51, 56 | "metadata": {}, 57 | "outputs": [ 58 | { 59 | "data": { 60 | "text/plain": [ 61 | "str" 62 | ] 63 | }, 64 | "execution_count": 51, 65 | "metadata": {}, 66 | "output_type": "execute_result" 67 | } 68 | ], 69 | "source": [ 70 | "type(film)" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 50, 76 | "metadata": {}, 77 | "outputs": [ 78 | { 79 | "data": { 80 | "text/plain": [ 81 | "'The Lion King'" 82 | ] 83 | }, 84 | "execution_count": 50, 85 | "metadata": {}, 86 | "output_type": "execute_result" 87 | } 88 | ], 89 | "source": [ 90 | "bigfilm = film.title()\n", 91 | "bigfilm" 92 | ] 93 | }, 94 | { 95 | "cell_type": "markdown", 96 | "metadata": { 97 | "slideshow": { 98 | "slide_type": "subslide" 99 | } 100 | }, 101 | "source": [ 102 | "## Exercises: Strings" 103 | ] 104 | }, 105 | { 106 | "cell_type": "markdown", 107 | "metadata": {}, 108 | "source": [ 109 | "Use the string methods to modify the following strings to the versions described below." 110 | ] 111 | }, 112 | { 113 | "cell_type": "markdown", 114 | "metadata": { 115 | "slideshow": { 116 | "slide_type": "subslide" 117 | } 118 | }, 119 | "source": [ 120 | "### Changing Strings\n", 121 | "\n", 122 | "Use the **strip**, **swapcase**, **title**, **lower**, **upper**, and **zfill** methods, or the **\\+** or **\\*** operators, to perform the following modifications:" 123 | ] 124 | }, 125 | { 126 | "cell_type": "markdown", 127 | "metadata": { 128 | "slideshow": { 129 | "slide_type": "fragment" 130 | } 131 | }, 132 | "source": [ 133 | "**\"I am angry\"** -> **\"I AM ANGRY\"**" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": null, 139 | "metadata": {}, 140 | "outputs": [], 141 | "source": [] 142 | }, 143 | { 144 | "cell_type": "markdown", 145 | "metadata": { 146 | "slideshow": { 147 | "slide_type": "fragment" 148 | } 149 | }, 150 | "source": [ 151 | "**\"the lion king\"** -> **\"The Lion King\"**" 152 | ] 153 | }, 154 | { 155 | "cell_type": "code", 156 | "execution_count": null, 157 | "metadata": {}, 158 | "outputs": [], 159 | "source": [] 160 | }, 161 | { 162 | "cell_type": "markdown", 163 | "metadata": { 164 | "slideshow": { 165 | "slide_type": "fragment" 166 | } 167 | }, 168 | "source": [ 169 | "**He said, \"Are you okay?\"** -> **hE SAID, \"aRE YOU OKAY?\"**" 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": null, 175 | "metadata": {}, 176 | "outputs": [], 177 | "source": [] 178 | }, 179 | { 180 | "cell_type": "markdown", 181 | "metadata": { 182 | "slideshow": { 183 | "slide_type": "fragment" 184 | } 185 | }, 186 | "source": [ 187 | "**three** -> **threethreethree**" 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": null, 193 | "metadata": {}, 194 | "outputs": [], 195 | "source": [] 196 | }, 197 | { 198 | "cell_type": "markdown", 199 | "metadata": { 200 | "slideshow": { 201 | "slide_type": "subslide" 202 | } 203 | }, 204 | "source": [ 205 | "### Searching Substrings\n", 206 | "\n", 207 | "Use the **replace**, **count**, and **index** methods to search for a substring and answer the following questions using the example sentence below:\n", 208 | "\n", 209 | "```python\n", 210 | "sentence = \"The dog ate my watermelon when we were digging at the beach!\"\n", 211 | "```" 212 | ] 213 | }, 214 | { 215 | "cell_type": "code", 216 | "execution_count": 27, 217 | "metadata": { 218 | "slideshow": { 219 | "slide_type": "subslide" 220 | } 221 | }, 222 | "outputs": [], 223 | "source": [ 224 | "sentence = \"The dog ate my watermelon when we were digging at the beach!\"" 225 | ] 226 | }, 227 | { 228 | "cell_type": "markdown", 229 | "metadata": { 230 | "slideshow": { 231 | "slide_type": "fragment" 232 | } 233 | }, 234 | "source": [ 235 | "How many \"w\" characters are in this sentence?" 236 | ] 237 | }, 238 | { 239 | "cell_type": "code", 240 | "execution_count": null, 241 | "metadata": {}, 242 | "outputs": [], 243 | "source": [] 244 | }, 245 | { 246 | "cell_type": "markdown", 247 | "metadata": { 248 | "slideshow": { 249 | "slide_type": "fragment" 250 | } 251 | }, 252 | "source": [ 253 | "What character index does the word \"watermelon\" start on? (the first? The tenth?)" 254 | ] 255 | }, 256 | { 257 | "cell_type": "code", 258 | "execution_count": null, 259 | "metadata": {}, 260 | "outputs": [], 261 | "source": [] 262 | }, 263 | { 264 | "cell_type": "markdown", 265 | "metadata": { 266 | "slideshow": { 267 | "slide_type": "fragment" 268 | } 269 | }, 270 | "source": [ 271 | "Replace the word \"dog\" in the sentence with the word \"cat\"" 272 | ] 273 | }, 274 | { 275 | "cell_type": "code", 276 | "execution_count": null, 277 | "metadata": {}, 278 | "outputs": [], 279 | "source": [] 280 | }, 281 | { 282 | "cell_type": "markdown", 283 | "metadata": { 284 | "slideshow": { 285 | "slide_type": "slide" 286 | } 287 | }, 288 | "source": [ 289 | "### Formatting Strings\n", 290 | "\n", 291 | "The most common method used for strings is the **format()** methods, so it deserves special mention. It is used to insert data into a string. It is similar to the **replace()** method but only replaces curly braces **{}**, but it is *way* more powerful. In fact, there is a whole web page dedicated to showing all the ways you can use it: https://pyformat.info/\n", 292 | "\n", 293 | "```python\n", 294 | "label_fmt = \"Sample {}: Vial {}\"\n", 295 | "label_fmt.format(3, 4)\n", 296 | ">> \"Sample 3: Vial 4\"\n", 297 | "```" 298 | ] 299 | }, 300 | { 301 | "cell_type": "markdown", 302 | "metadata": { 303 | "slideshow": { 304 | "slide_type": "subslide" 305 | } 306 | }, 307 | "source": [ 308 | "Using the **format()** method, make the following three sentences out of the original sentence below:" 309 | ] 310 | }, 311 | { 312 | "cell_type": "code", 313 | "execution_count": 52, 314 | "metadata": { 315 | "slideshow": { 316 | "slide_type": "-" 317 | } 318 | }, 319 | "outputs": [], 320 | "source": [ 321 | "sentence = \"I bought {} dogs.\"" 322 | ] 323 | }, 324 | { 325 | "cell_type": "markdown", 326 | "metadata": { 327 | "slideshow": { 328 | "slide_type": "fragment" 329 | } 330 | }, 331 | "source": [ 332 | "1. \"I bought 3 dogs.\"" 333 | ] 334 | }, 335 | { 336 | "cell_type": "code", 337 | "execution_count": null, 338 | "metadata": { 339 | "slideshow": { 340 | "slide_type": "-" 341 | } 342 | }, 343 | "outputs": [], 344 | "source": [] 345 | }, 346 | { 347 | "cell_type": "markdown", 348 | "metadata": { 349 | "slideshow": { 350 | "slide_type": "fragment" 351 | } 352 | }, 353 | "source": [ 354 | "2. \"Wendy bought 5 dogs.\"" 355 | ] 356 | }, 357 | { 358 | "cell_type": "code", 359 | "execution_count": null, 360 | "metadata": {}, 361 | "outputs": [], 362 | "source": [] 363 | }, 364 | { 365 | "cell_type": "markdown", 366 | "metadata": { 367 | "slideshow": { 368 | "slide_type": "fragment" 369 | } 370 | }, 371 | "source": [ 372 | "3. \"George bought 700 dogs.\"" 373 | ] 374 | }, 375 | { 376 | "cell_type": "code", 377 | "execution_count": null, 378 | "metadata": {}, 379 | "outputs": [], 380 | "source": [] 381 | }, 382 | { 383 | "cell_type": "markdown", 384 | "metadata": { 385 | "slideshow": { 386 | "slide_type": "slide" 387 | } 388 | }, 389 | "source": [ 390 | "## Lists\n", 391 | "\n", 392 | "Lists represent sequences of data. They are made with either the square brackets **[]** or the **list()** constructor function. They can be appended to, just like strings, but they can contain any kind of data--numbers, strings, even other lists. Their methods mostly revolve around inserting and removing things:\n", 393 | "\n", 394 | "| Method | What it Does | \n", 395 | "| :-----: | :----------: |\n", 396 | "| append() | Appends an item to the end of the list. | \n", 397 | "| extend() | Appends several items to the end of the list. | \n", 398 | "| remove() | Removes an item from the list. |\n", 399 | "| clear() | Removes everything from the list. | \n", 400 | "| copy() | Returns a copy of the list. |\n", 401 | "| count() | Removes a specified item from the list | \n", 402 | "| sort() | Sorts the list. |\n", 403 | "| reverse()| Reverses the order of items in the list. |" 404 | ] 405 | }, 406 | { 407 | "cell_type": "markdown", 408 | "metadata": { 409 | "slideshow": { 410 | "slide_type": "subslide" 411 | } 412 | }, 413 | "source": [ 414 | "**Example**\n", 415 | "\n", 416 | "Here is a list of colors:" 417 | ] 418 | }, 419 | { 420 | "cell_type": "code", 421 | "execution_count": 56, 422 | "metadata": {}, 423 | "outputs": [ 424 | { 425 | "data": { 426 | "text/plain": [ 427 | "['Red', 'Blue', 'Yello']" 428 | ] 429 | }, 430 | "execution_count": 56, 431 | "metadata": {}, 432 | "output_type": "execute_result" 433 | } 434 | ], 435 | "source": [ 436 | "colors = ['Red', 'Blue', 'Yello']\n", 437 | "colors" 438 | ] 439 | }, 440 | { 441 | "cell_type": "markdown", 442 | "metadata": { 443 | "slideshow": { 444 | "slide_type": "-" 445 | } 446 | }, 447 | "source": [ 448 | "Let's append the color 'Yellow' to the list:" 449 | ] 450 | }, 451 | { 452 | "cell_type": "code", 453 | "execution_count": 57, 454 | "metadata": {}, 455 | "outputs": [ 456 | { 457 | "data": { 458 | "text/plain": [ 459 | "['Red', 'Blue', 'Yello', 'Yellow']" 460 | ] 461 | }, 462 | "execution_count": 57, 463 | "metadata": {}, 464 | "output_type": "execute_result" 465 | } 466 | ], 467 | "source": [ 468 | "colors.append('Yellow')\n", 469 | "colors" 470 | ] 471 | }, 472 | { 473 | "cell_type": "markdown", 474 | "metadata": { 475 | "slideshow": { 476 | "slide_type": "-" 477 | } 478 | }, 479 | "source": [ 480 | "*Note*: If you run the above code multiple times, 'Yellow' will be appended to the list multiple times!" 481 | ] 482 | }, 483 | { 484 | "cell_type": "markdown", 485 | "metadata": { 486 | "slideshow": { 487 | "slide_type": "subslide" 488 | } 489 | }, 490 | "source": [ 491 | "### List Method Exercises" 492 | ] 493 | }, 494 | { 495 | "cell_type": "markdown", 496 | "metadata": { 497 | "slideshow": { 498 | "slide_type": "fragment" 499 | } 500 | }, 501 | "source": [ 502 | "1. Make a list of 3 movie titles." 503 | ] 504 | }, 505 | { 506 | "cell_type": "code", 507 | "execution_count": null, 508 | "metadata": {}, 509 | "outputs": [], 510 | "source": [] 511 | }, 512 | { 513 | "cell_type": "markdown", 514 | "metadata": { 515 | "slideshow": { 516 | "slide_type": "fragment" 517 | } 518 | }, 519 | "source": [ 520 | "2. You have a list of subjects for your experiment, and you want to add another one to that list! How could you do it?" 521 | ] 522 | }, 523 | { 524 | "cell_type": "code", 525 | "execution_count": 40, 526 | "metadata": {}, 527 | "outputs": [], 528 | "source": [ 529 | "subjects = ['NOP234', 'GHS673', 'JGL212']\n", 530 | "new_subject = 'ASF193'" 531 | ] 532 | }, 533 | { 534 | "cell_type": "code", 535 | "execution_count": null, 536 | "metadata": {}, 537 | "outputs": [], 538 | "source": [] 539 | }, 540 | { 541 | "cell_type": "markdown", 542 | "metadata": { 543 | "slideshow": { 544 | "slide_type": "subslide" 545 | } 546 | }, 547 | "source": [ 548 | "3. Now, a bunch of new subjects appeared! How do you extend/append them to the main list?" 549 | ] 550 | }, 551 | { 552 | "cell_type": "code", 553 | "execution_count": 58, 554 | "metadata": { 555 | "slideshow": { 556 | "slide_type": "-" 557 | } 558 | }, 559 | "outputs": [], 560 | "source": [ 561 | "subjects = ['NOP234', 'GHS673', 'JGL212']\n", 562 | "new_subjects = ['ASF193', 'THW994', 'JJZ231']" 563 | ] 564 | }, 565 | { 566 | "cell_type": "code", 567 | "execution_count": null, 568 | "metadata": {}, 569 | "outputs": [], 570 | "source": [] 571 | }, 572 | { 573 | "cell_type": "markdown", 574 | "metadata": { 575 | "slideshow": { 576 | "slide_type": "fragment" 577 | } 578 | }, 579 | "source": [ 580 | "4. Please sort those subjects alphabetically. It looks better that way, doesn't it?" 581 | ] 582 | }, 583 | { 584 | "cell_type": "code", 585 | "execution_count": 2, 586 | "metadata": {}, 587 | "outputs": [], 588 | "source": [ 589 | "subjects = ['NOP234', 'GHS673', 'jGL212', 'ASF193', 'THW994', 'JJZ231']" 590 | ] 591 | }, 592 | { 593 | "cell_type": "code", 594 | "execution_count": null, 595 | "metadata": {}, 596 | "outputs": [], 597 | "source": [] 598 | }, 599 | { 600 | "cell_type": "markdown", 601 | "metadata": { 602 | "slideshow": { 603 | "slide_type": "subslide" 604 | } 605 | }, 606 | "source": [ 607 | "5. Oh, no, 'JGL202' was a terrible subject, he intentionally ruined your study. Well, there's no way you're keeping him. How do you remove him from the list?" 608 | ] 609 | }, 610 | { 611 | "cell_type": "code", 612 | "execution_count": 1, 613 | "metadata": {}, 614 | "outputs": [], 615 | "source": [ 616 | "subjects = ['NOP234', 'GHS673', 'JGL212', 'ASF193', 'THW994', 'JJZ231']" 617 | ] 618 | }, 619 | { 620 | "cell_type": "markdown", 621 | "metadata": { 622 | "slideshow": { 623 | "slide_type": "slide" 624 | } 625 | }, 626 | "source": [ 627 | "## Further Learning Resources" 628 | ] 629 | }, 630 | { 631 | "cell_type": "markdown", 632 | "metadata": {}, 633 | "source": [ 634 | " - Official Documentation on String Methods: https://docs.python.org/3/library/stdtypes.html#string-methods\n", 635 | " \n", 636 | " - Online Tutorial on Python Strings: https://realpython.com/python-strings/\n", 637 | " \n", 638 | " - Official Documentation on List Methods: https://docs.python.org/3/library/stdtypes.html#lists\n", 639 | " \n", 640 | " - Online Tutorial on Python Lists: https://realpython.com/python-lists-tuples/\n", 641 | " \n", 642 | " " 643 | ] 644 | }, 645 | { 646 | "cell_type": "markdown", 647 | "metadata": { 648 | "slideshow": { 649 | "slide_type": "slide" 650 | } 651 | }, 652 | "source": [ 653 | "## Extra Exercises\n", 654 | "\n", 655 | "If you'd like to do some more, here they are!\n", 656 | "\n", 657 | "\n", 658 | "1. You want to label your figure title with the subject code, and the subject code changes depending on which subject is being shown! How could you stick a subject's name in your title string?" 659 | ] 660 | }, 661 | { 662 | "cell_type": "code", 663 | "execution_count": 4, 664 | "metadata": {}, 665 | "outputs": [], 666 | "source": [ 667 | "subject = 'NOP234'\n", 668 | "title = \"Mean Values of SUBJECT NAME's data over Time\"" 669 | ] 670 | }, 671 | { 672 | "cell_type": "code", 673 | "execution_count": null, 674 | "metadata": {}, 675 | "outputs": [], 676 | "source": [] 677 | }, 678 | { 679 | "cell_type": "markdown", 680 | "metadata": {}, 681 | "source": [ 682 | "2. If you had formatted your title string in the following way, though, another string method would be more useful. Which method would you use for this title:" 683 | ] 684 | }, 685 | { 686 | "cell_type": "code", 687 | "execution_count": 3, 688 | "metadata": {}, 689 | "outputs": [], 690 | "source": [ 691 | "subject1 = 'NOP234'\n", 692 | "subject2 = 'GHS673'\n", 693 | "title = \"Performance Comparison between Subjects {} and {}\"" 694 | ] 695 | }, 696 | { 697 | "cell_type": "code", 698 | "execution_count": null, 699 | "metadata": {}, 700 | "outputs": [], 701 | "source": [] 702 | }, 703 | { 704 | "cell_type": "markdown", 705 | "metadata": {}, 706 | "source": [ 707 | "3. The conference says it only takes abstracts that have a maximum word count of 100 words. Did our abstract make the cut? \n", 708 | "\n", 709 | "*Hint: the len() function is useful here*" 710 | ] 711 | }, 712 | { 713 | "cell_type": "code", 714 | "execution_count": 6, 715 | "metadata": {}, 716 | "outputs": [], 717 | "source": [ 718 | "abstract = \"\"\"We analyze the locomotor behavior of the rat during exploration, and show that digitally collected data (time series of positions) provide a sufftcient basis for establishing that the rat uses several distinct modes of motion (first, second, third, and sometimes fourth gear). The distinction between these modes is obtained by first segmenting the time series into sequences of data points occurring between arrests (as ascertained within the resolution of the data acquisition system). The statistical distribution of the maximal amount of motion occurring within each of these episodes is then analyzed and shown to be multi modal. This enables us to decompose motion into distinct modes.\"\"\"\n" 719 | ] 720 | }, 721 | { 722 | "cell_type": "markdown", 723 | "metadata": {}, 724 | "source": [ 725 | "Oh, wait, now that I look closer, it actually says that it's a maximum of 100 **unique** words--duplicated words don't count. How many unique words do we have in our abstract? (What a strange conference...)\n", 726 | "\n", 727 | "\n", 728 | "*Hint: Try this out with the **set()** class.*" 729 | ] 730 | }, 731 | { 732 | "cell_type": "code", 733 | "execution_count": null, 734 | "metadata": {}, 735 | "outputs": [], 736 | "source": [] 737 | } 738 | ], 739 | "metadata": { 740 | "kernelspec": { 741 | "display_name": "Python 3", 742 | "language": "python", 743 | "name": "python3" 744 | }, 745 | "language_info": { 746 | "codemirror_mode": { 747 | "name": "ipython", 748 | "version": 3 749 | }, 750 | "file_extension": ".py", 751 | "mimetype": "text/x-python", 752 | "name": "python", 753 | "nbconvert_exporter": "python", 754 | "pygments_lexer": "ipython3", 755 | "version": "3.7.4" 756 | } 757 | }, 758 | "nbformat": 4, 759 | "nbformat_minor": 4 760 | } 761 | -------------------------------------------------------------------------------- /Day1_Code_Blocks/.ipynb_checkpoints/Python Syntax 4-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "slideshow": { 7 | "slide_type": "slide" 8 | } 9 | }, 10 | "source": [ 11 | "# Built-In Type Methods\n", 12 | "\n", 13 | "As we discussed in the lecture, all Python objects are **Objects**, which means they have **Properties/Attributes** (data) and **Methods** associated with them. \n", 14 | "\n", 15 | "| Object | type(Object) | Example Method | Example Attribute | \n", 16 | "| :----: | :----------: | :------------: | :---------------: |\n", 17 | "| \"Hello\" | str | lower() | |\n", 18 | "| 42 | int | bit_length() | numerator |\n", 19 | "| 3.14 | float | is_integer() | imag |\n", 20 | "| ['a', 'b'] | list | append() | |\n", 21 | "\n", 22 | "\n", 23 | "These are accessible via the **. (dot)**, and an object's methods can be browsed in the notebook by typing the **Tab** key after the dot.\n", 24 | "\n", 25 | "For example, here we use a **string** **Class**'s *title()* method to capitalize the first letter of each word of the film title:" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 49, 31 | "metadata": { 32 | "slideshow": { 33 | "slide_type": "subslide" 34 | } 35 | }, 36 | "outputs": [ 37 | { 38 | "data": { 39 | "text/plain": [ 40 | "'the lion king'" 41 | ] 42 | }, 43 | "execution_count": 49, 44 | "metadata": {}, 45 | "output_type": "execute_result" 46 | } 47 | ], 48 | "source": [ 49 | "film = 'the lion king'\n", 50 | "film" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 51, 56 | "metadata": {}, 57 | "outputs": [ 58 | { 59 | "data": { 60 | "text/plain": [ 61 | "str" 62 | ] 63 | }, 64 | "execution_count": 51, 65 | "metadata": {}, 66 | "output_type": "execute_result" 67 | } 68 | ], 69 | "source": [ 70 | "type(film)" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 50, 76 | "metadata": {}, 77 | "outputs": [ 78 | { 79 | "data": { 80 | "text/plain": [ 81 | "'The Lion King'" 82 | ] 83 | }, 84 | "execution_count": 50, 85 | "metadata": {}, 86 | "output_type": "execute_result" 87 | } 88 | ], 89 | "source": [ 90 | "bigfilm = film.title()\n", 91 | "bigfilm" 92 | ] 93 | }, 94 | { 95 | "cell_type": "markdown", 96 | "metadata": { 97 | "slideshow": { 98 | "slide_type": "subslide" 99 | } 100 | }, 101 | "source": [ 102 | "## Exercises: Strings" 103 | ] 104 | }, 105 | { 106 | "cell_type": "markdown", 107 | "metadata": {}, 108 | "source": [ 109 | "Use the string methods to modify the following strings to the versions described below." 110 | ] 111 | }, 112 | { 113 | "cell_type": "markdown", 114 | "metadata": { 115 | "slideshow": { 116 | "slide_type": "subslide" 117 | } 118 | }, 119 | "source": [ 120 | "### Changing Strings\n", 121 | "\n", 122 | "Use the **strip**, **swapcase**, **title**, **lower**, **upper**, and **zfill** methods, or the **\\+** or **\\*** operators, to perform the following modifications:" 123 | ] 124 | }, 125 | { 126 | "cell_type": "markdown", 127 | "metadata": { 128 | "slideshow": { 129 | "slide_type": "fragment" 130 | } 131 | }, 132 | "source": [ 133 | "**\"I am angry\"** -> **\"I AM ANGRY\"**" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": null, 139 | "metadata": {}, 140 | "outputs": [], 141 | "source": [] 142 | }, 143 | { 144 | "cell_type": "markdown", 145 | "metadata": { 146 | "slideshow": { 147 | "slide_type": "fragment" 148 | } 149 | }, 150 | "source": [ 151 | "**\"the lion king\"** -> **\"The Lion King\"**" 152 | ] 153 | }, 154 | { 155 | "cell_type": "code", 156 | "execution_count": null, 157 | "metadata": {}, 158 | "outputs": [], 159 | "source": [] 160 | }, 161 | { 162 | "cell_type": "markdown", 163 | "metadata": { 164 | "slideshow": { 165 | "slide_type": "fragment" 166 | } 167 | }, 168 | "source": [ 169 | "**He said, \"Are you okay?\"** -> **hE SAID, \"aRE YOU OKAY?\"**" 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": null, 175 | "metadata": {}, 176 | "outputs": [], 177 | "source": [] 178 | }, 179 | { 180 | "cell_type": "markdown", 181 | "metadata": { 182 | "slideshow": { 183 | "slide_type": "fragment" 184 | } 185 | }, 186 | "source": [ 187 | "**three** -> **threethreethree**" 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": null, 193 | "metadata": {}, 194 | "outputs": [], 195 | "source": [] 196 | }, 197 | { 198 | "cell_type": "markdown", 199 | "metadata": { 200 | "slideshow": { 201 | "slide_type": "subslide" 202 | } 203 | }, 204 | "source": [ 205 | "### Searching Substrings\n", 206 | "\n", 207 | "Use the **replace**, **count**, and **index** methods to search for a substring and answer the following questions using the example sentence below:\n", 208 | "\n", 209 | "```python\n", 210 | "sentence = \"The dog ate my watermelon when we were digging at the beach!\"\n", 211 | "```" 212 | ] 213 | }, 214 | { 215 | "cell_type": "code", 216 | "execution_count": 27, 217 | "metadata": { 218 | "slideshow": { 219 | "slide_type": "subslide" 220 | } 221 | }, 222 | "outputs": [], 223 | "source": [ 224 | "sentence = \"The dog ate my watermelon when we were digging at the beach!\"" 225 | ] 226 | }, 227 | { 228 | "cell_type": "markdown", 229 | "metadata": { 230 | "slideshow": { 231 | "slide_type": "fragment" 232 | } 233 | }, 234 | "source": [ 235 | "How many \"w\" characters are in this sentence?" 236 | ] 237 | }, 238 | { 239 | "cell_type": "code", 240 | "execution_count": null, 241 | "metadata": {}, 242 | "outputs": [], 243 | "source": [] 244 | }, 245 | { 246 | "cell_type": "markdown", 247 | "metadata": { 248 | "slideshow": { 249 | "slide_type": "fragment" 250 | } 251 | }, 252 | "source": [ 253 | "What character index does the word \"watermelon\" start on? (the first? The tenth?)" 254 | ] 255 | }, 256 | { 257 | "cell_type": "code", 258 | "execution_count": null, 259 | "metadata": {}, 260 | "outputs": [], 261 | "source": [] 262 | }, 263 | { 264 | "cell_type": "markdown", 265 | "metadata": { 266 | "slideshow": { 267 | "slide_type": "fragment" 268 | } 269 | }, 270 | "source": [ 271 | "Replace the word \"dog\" in the sentence with the word \"cat\"" 272 | ] 273 | }, 274 | { 275 | "cell_type": "code", 276 | "execution_count": null, 277 | "metadata": {}, 278 | "outputs": [], 279 | "source": [] 280 | }, 281 | { 282 | "cell_type": "markdown", 283 | "metadata": { 284 | "slideshow": { 285 | "slide_type": "slide" 286 | } 287 | }, 288 | "source": [ 289 | "### Formatting Strings\n", 290 | "\n", 291 | "The most common method used for strings is the **format()** methods, so it deserves special mention. It is used to insert data into a string. It is similar to the **replace()** method but only replaces curly braces **{}**, but it is *way* more powerful. In fact, there is a whole web page dedicated to showing all the ways you can use it: https://pyformat.info/\n", 292 | "\n", 293 | "```python\n", 294 | "label_fmt = \"Sample {}: Vial {}\"\n", 295 | "label_fmt.format(3, 4)\n", 296 | ">> \"Sample 3: Vial 4\"\n", 297 | "```" 298 | ] 299 | }, 300 | { 301 | "cell_type": "markdown", 302 | "metadata": { 303 | "slideshow": { 304 | "slide_type": "subslide" 305 | } 306 | }, 307 | "source": [ 308 | "Using the **format()** method, make the following three sentences out of the original sentence below:" 309 | ] 310 | }, 311 | { 312 | "cell_type": "code", 313 | "execution_count": 52, 314 | "metadata": { 315 | "slideshow": { 316 | "slide_type": "-" 317 | } 318 | }, 319 | "outputs": [], 320 | "source": [ 321 | "sentence = \"I bought {} dogs.\"" 322 | ] 323 | }, 324 | { 325 | "cell_type": "markdown", 326 | "metadata": { 327 | "slideshow": { 328 | "slide_type": "fragment" 329 | } 330 | }, 331 | "source": [ 332 | "1. \"I bought 3 dogs.\"" 333 | ] 334 | }, 335 | { 336 | "cell_type": "code", 337 | "execution_count": null, 338 | "metadata": { 339 | "slideshow": { 340 | "slide_type": "-" 341 | } 342 | }, 343 | "outputs": [], 344 | "source": [] 345 | }, 346 | { 347 | "cell_type": "markdown", 348 | "metadata": { 349 | "slideshow": { 350 | "slide_type": "fragment" 351 | } 352 | }, 353 | "source": [ 354 | "2. \"Wendy bought 5 dogs.\"" 355 | ] 356 | }, 357 | { 358 | "cell_type": "code", 359 | "execution_count": null, 360 | "metadata": {}, 361 | "outputs": [], 362 | "source": [] 363 | }, 364 | { 365 | "cell_type": "markdown", 366 | "metadata": { 367 | "slideshow": { 368 | "slide_type": "fragment" 369 | } 370 | }, 371 | "source": [ 372 | "3. \"George bought 700 dogs.\"" 373 | ] 374 | }, 375 | { 376 | "cell_type": "code", 377 | "execution_count": null, 378 | "metadata": {}, 379 | "outputs": [], 380 | "source": [] 381 | }, 382 | { 383 | "cell_type": "markdown", 384 | "metadata": { 385 | "slideshow": { 386 | "slide_type": "slide" 387 | } 388 | }, 389 | "source": [ 390 | "## Lists\n", 391 | "\n", 392 | "Lists represent sequences of data. They are made with either the square brackets **[]** or the **list()** constructor function. They can be appended to, just like strings, but they can contain any kind of data--numbers, strings, even other lists. Their methods mostly revolve around inserting and removing things:\n", 393 | "\n", 394 | "| Method | What it Does | \n", 395 | "| :-----: | :----------: |\n", 396 | "| append() | Appends an item to the end of the list. | \n", 397 | "| extend() | Appends several items to the end of the list. | \n", 398 | "| remove() | Removes an item from the list. |\n", 399 | "| clear() | Removes everything from the list. | \n", 400 | "| copy() | Returns a copy of the list. |\n", 401 | "| count() | Removes a specified item from the list | \n", 402 | "| sort() | Sorts the list. |\n", 403 | "| reverse()| Reverses the order of items in the list. |" 404 | ] 405 | }, 406 | { 407 | "cell_type": "markdown", 408 | "metadata": { 409 | "slideshow": { 410 | "slide_type": "subslide" 411 | } 412 | }, 413 | "source": [ 414 | "**Example**\n", 415 | "\n", 416 | "Here is a list of colors:" 417 | ] 418 | }, 419 | { 420 | "cell_type": "code", 421 | "execution_count": 56, 422 | "metadata": {}, 423 | "outputs": [ 424 | { 425 | "data": { 426 | "text/plain": [ 427 | "['Red', 'Blue', 'Yello']" 428 | ] 429 | }, 430 | "execution_count": 56, 431 | "metadata": {}, 432 | "output_type": "execute_result" 433 | } 434 | ], 435 | "source": [ 436 | "colors = ['Red', 'Blue', 'Yello']\n", 437 | "colors" 438 | ] 439 | }, 440 | { 441 | "cell_type": "markdown", 442 | "metadata": { 443 | "slideshow": { 444 | "slide_type": "-" 445 | } 446 | }, 447 | "source": [ 448 | "Let's append the color 'Yellow' to the list:" 449 | ] 450 | }, 451 | { 452 | "cell_type": "code", 453 | "execution_count": 57, 454 | "metadata": {}, 455 | "outputs": [ 456 | { 457 | "data": { 458 | "text/plain": [ 459 | "['Red', 'Blue', 'Yello', 'Yellow']" 460 | ] 461 | }, 462 | "execution_count": 57, 463 | "metadata": {}, 464 | "output_type": "execute_result" 465 | } 466 | ], 467 | "source": [ 468 | "colors.append('Yellow')\n", 469 | "colors" 470 | ] 471 | }, 472 | { 473 | "cell_type": "markdown", 474 | "metadata": { 475 | "slideshow": { 476 | "slide_type": "-" 477 | } 478 | }, 479 | "source": [ 480 | "*Note*: If you run the above code multiple times, 'Yellow' will be appended to the list multiple times!" 481 | ] 482 | }, 483 | { 484 | "cell_type": "markdown", 485 | "metadata": { 486 | "slideshow": { 487 | "slide_type": "subslide" 488 | } 489 | }, 490 | "source": [ 491 | "### List Method Exercises" 492 | ] 493 | }, 494 | { 495 | "cell_type": "markdown", 496 | "metadata": { 497 | "slideshow": { 498 | "slide_type": "fragment" 499 | } 500 | }, 501 | "source": [ 502 | "1. Make a list of 3 movie titles." 503 | ] 504 | }, 505 | { 506 | "cell_type": "code", 507 | "execution_count": null, 508 | "metadata": {}, 509 | "outputs": [], 510 | "source": [] 511 | }, 512 | { 513 | "cell_type": "markdown", 514 | "metadata": { 515 | "slideshow": { 516 | "slide_type": "fragment" 517 | } 518 | }, 519 | "source": [ 520 | "2. You have a list of subjects for your experiment, and you want to add another one to that list! How could you do it?" 521 | ] 522 | }, 523 | { 524 | "cell_type": "code", 525 | "execution_count": 40, 526 | "metadata": {}, 527 | "outputs": [], 528 | "source": [ 529 | "subjects = ['NOP234', 'GHS673', 'JGL212']\n", 530 | "new_subject = 'ASF193'" 531 | ] 532 | }, 533 | { 534 | "cell_type": "code", 535 | "execution_count": null, 536 | "metadata": {}, 537 | "outputs": [], 538 | "source": [] 539 | }, 540 | { 541 | "cell_type": "markdown", 542 | "metadata": { 543 | "slideshow": { 544 | "slide_type": "subslide" 545 | } 546 | }, 547 | "source": [ 548 | "3. Now, a bunch of new subjects appeared! How do you extend/append them to the main list?" 549 | ] 550 | }, 551 | { 552 | "cell_type": "code", 553 | "execution_count": 58, 554 | "metadata": { 555 | "slideshow": { 556 | "slide_type": "-" 557 | } 558 | }, 559 | "outputs": [], 560 | "source": [ 561 | "subjects = ['NOP234', 'GHS673', 'JGL212']\n", 562 | "new_subjects = ['ASF193', 'THW994', 'JJZ231']" 563 | ] 564 | }, 565 | { 566 | "cell_type": "code", 567 | "execution_count": null, 568 | "metadata": {}, 569 | "outputs": [], 570 | "source": [] 571 | }, 572 | { 573 | "cell_type": "markdown", 574 | "metadata": { 575 | "slideshow": { 576 | "slide_type": "fragment" 577 | } 578 | }, 579 | "source": [ 580 | "4. Please sort those subjects alphabetically. It looks better that way, doesn't it?" 581 | ] 582 | }, 583 | { 584 | "cell_type": "code", 585 | "execution_count": 2, 586 | "metadata": {}, 587 | "outputs": [], 588 | "source": [ 589 | "subjects = ['NOP234', 'GHS673', 'jGL212', 'ASF193', 'THW994', 'JJZ231']" 590 | ] 591 | }, 592 | { 593 | "cell_type": "code", 594 | "execution_count": null, 595 | "metadata": {}, 596 | "outputs": [], 597 | "source": [] 598 | }, 599 | { 600 | "cell_type": "markdown", 601 | "metadata": { 602 | "slideshow": { 603 | "slide_type": "subslide" 604 | } 605 | }, 606 | "source": [ 607 | "5. Oh, no, 'JGL202' was a terrible subject, he intentionally ruined your study. Well, there's no way you're keeping him. How do you remove him from the list?" 608 | ] 609 | }, 610 | { 611 | "cell_type": "code", 612 | "execution_count": 1, 613 | "metadata": {}, 614 | "outputs": [], 615 | "source": [ 616 | "subjects = ['NOP234', 'GHS673', 'JGL212', 'ASF193', 'THW994', 'JJZ231']" 617 | ] 618 | }, 619 | { 620 | "cell_type": "markdown", 621 | "metadata": { 622 | "slideshow": { 623 | "slide_type": "slide" 624 | } 625 | }, 626 | "source": [ 627 | "## Further Learning Resources" 628 | ] 629 | }, 630 | { 631 | "cell_type": "markdown", 632 | "metadata": {}, 633 | "source": [ 634 | " - Official Documentation on String Methods: https://docs.python.org/3/library/stdtypes.html#string-methods\n", 635 | " \n", 636 | " - Online Tutorial on Python Strings: https://realpython.com/python-strings/\n", 637 | " \n", 638 | " - Official Documentation on List Methods: https://docs.python.org/3/library/stdtypes.html#lists\n", 639 | " \n", 640 | " - Online Tutorial on Python Lists: https://realpython.com/python-lists-tuples/\n", 641 | " \n", 642 | " " 643 | ] 644 | }, 645 | { 646 | "cell_type": "markdown", 647 | "metadata": { 648 | "slideshow": { 649 | "slide_type": "slide" 650 | } 651 | }, 652 | "source": [ 653 | "## Extra Exercises\n", 654 | "\n", 655 | "If you'd like to do some more, here they are!\n", 656 | "\n", 657 | "\n", 658 | "1. You want to label your figure title with the subject code, and the subject code changes depending on which subject is being shown! How could you stick a subject's name in your title string?" 659 | ] 660 | }, 661 | { 662 | "cell_type": "code", 663 | "execution_count": 4, 664 | "metadata": {}, 665 | "outputs": [], 666 | "source": [ 667 | "subject = 'NOP234'\n", 668 | "title = \"Mean Values of SUBJECT NAME's data over Time\"" 669 | ] 670 | }, 671 | { 672 | "cell_type": "code", 673 | "execution_count": null, 674 | "metadata": {}, 675 | "outputs": [], 676 | "source": [] 677 | }, 678 | { 679 | "cell_type": "markdown", 680 | "metadata": {}, 681 | "source": [ 682 | "2. If you had formatted your title string in the following way, though, another string method would be more useful. Which method would you use for this title:" 683 | ] 684 | }, 685 | { 686 | "cell_type": "code", 687 | "execution_count": 3, 688 | "metadata": {}, 689 | "outputs": [], 690 | "source": [ 691 | "subject1 = 'NOP234'\n", 692 | "subject2 = 'GHS673'\n", 693 | "title = \"Performance Comparison between Subjects {} and {}\"" 694 | ] 695 | }, 696 | { 697 | "cell_type": "code", 698 | "execution_count": null, 699 | "metadata": {}, 700 | "outputs": [], 701 | "source": [] 702 | }, 703 | { 704 | "cell_type": "markdown", 705 | "metadata": {}, 706 | "source": [ 707 | "3. The conference says it only takes abstracts that have a maximum word count of 100 words. Did our abstract make the cut? \n", 708 | "\n", 709 | "*Hint: the len() function is useful here*" 710 | ] 711 | }, 712 | { 713 | "cell_type": "code", 714 | "execution_count": 6, 715 | "metadata": {}, 716 | "outputs": [], 717 | "source": [ 718 | "abstract = \"\"\"We analyze the locomotor behavior of the rat during exploration, and show that digitally collected data (time series of positions) provide a sufftcient basis for establishing that the rat uses several distinct modes of motion (first, second, third, and sometimes fourth gear). The distinction between these modes is obtained by first segmenting the time series into sequences of data points occurring between arrests (as ascertained within the resolution of the data acquisition system). The statistical distribution of the maximal amount of motion occurring within each of these episodes is then analyzed and shown to be multi modal. This enables us to decompose motion into distinct modes.\"\"\"\n" 719 | ] 720 | }, 721 | { 722 | "cell_type": "markdown", 723 | "metadata": {}, 724 | "source": [ 725 | "Oh, wait, now that I look closer, it actually says that it's a maximum of 100 **unique** words--duplicated words don't count. How many unique words do we have in our abstract? (What a strange conference...)\n", 726 | "\n", 727 | "\n", 728 | "*Hint: Try this out with the **set()** class.*" 729 | ] 730 | }, 731 | { 732 | "cell_type": "code", 733 | "execution_count": null, 734 | "metadata": {}, 735 | "outputs": [], 736 | "source": [] 737 | } 738 | ], 739 | "metadata": { 740 | "kernelspec": { 741 | "display_name": "Python 3", 742 | "language": "python", 743 | "name": "python3" 744 | }, 745 | "language_info": { 746 | "codemirror_mode": { 747 | "name": "ipython", 748 | "version": 3 749 | }, 750 | "file_extension": ".py", 751 | "mimetype": "text/x-python", 752 | "name": "python", 753 | "nbconvert_exporter": "python", 754 | "pygments_lexer": "ipython3", 755 | "version": "3.7.4" 756 | } 757 | }, 758 | "nbformat": 4, 759 | "nbformat_minor": 4 760 | } 761 | -------------------------------------------------------------------------------- /Day0_Python_Syntax/Python Syntax2.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Intro to Python Syntax, Part 2\n", 8 | "## Collections" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "metadata": { 14 | "slideshow": { 15 | "slide_type": "slide" 16 | } 17 | }, 18 | "source": [ 19 | "## Grouping Together Your Data into a Collection\n", 20 | "Python also has operators for collecting related data together. Most of this course will revolve around the pros and cons of different ways of collecting data, but let's take a look at them:\n", 21 | "\n", 22 | "| \"tuple\" (fixed sequence) | \"list\" (changeable sequence) | \"str\" (sequence of text characters) | \"bytes\" (sequence of bytes) | \"set\" {mathematical set) | \"dict\" (key-value mapping) | \n", 23 | "| :---------:| :----: | :--------: | :--------: | :--------: | :--------: |\n", 24 | "| (1, 2, 3) | [1, 2, 3] | \"123\" '123' | b\"123\" b'123' | {1, 2, 3} | {1: 2, 3: 4} |" 25 | ] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "metadata": { 30 | "slideshow": { 31 | "slide_type": "subslide" 32 | } 33 | }, 34 | "source": [ 35 | "#### Exercises: Making Collections" 36 | ] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "metadata": {}, 41 | "source": [ 42 | "Make four difference types of sequences, each containing only the numbers from 1 to 5." 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": null, 48 | "metadata": {}, 49 | "outputs": [], 50 | "source": [] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": null, 55 | "metadata": {}, 56 | "outputs": [], 57 | "source": [] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": null, 62 | "metadata": {}, 63 | "outputs": [], 64 | "source": [] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": null, 69 | "metadata": {}, 70 | "outputs": [], 71 | "source": [] 72 | }, 73 | { 74 | "cell_type": "markdown", 75 | "metadata": { 76 | "slideshow": { 77 | "slide_type": "subslide" 78 | } 79 | }, 80 | "source": [ 81 | "Make a sequence containing 3 names of people in this class." 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": null, 87 | "metadata": {}, 88 | "outputs": [], 89 | "source": [] 90 | }, 91 | { 92 | "cell_type": "markdown", 93 | "metadata": { 94 | "slideshow": { 95 | "slide_type": "fragment" 96 | } 97 | }, 98 | "source": [ 99 | "Make a list of four animals, ordering them by size." 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": null, 105 | "metadata": {}, 106 | "outputs": [], 107 | "source": [] 108 | }, 109 | { 110 | "cell_type": "markdown", 111 | "metadata": { 112 | "slideshow": { 113 | "slide_type": "subslide" 114 | } 115 | }, 116 | "source": [ 117 | "Make a mapping of classmates and colors, relating each classmate to their favorite color." 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": null, 123 | "metadata": {}, 124 | "outputs": [], 125 | "source": [] 126 | }, 127 | { 128 | "cell_type": "markdown", 129 | "metadata": { 130 | "slideshow": { 131 | "slide_type": "fragment" 132 | } 133 | }, 134 | "source": [ 135 | "Make a mapping of colors to names, relating each color to the people that like it" 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": null, 141 | "metadata": {}, 142 | "outputs": [], 143 | "source": [] 144 | }, 145 | { 146 | "cell_type": "markdown", 147 | "metadata": { 148 | "slideshow": { 149 | "slide_type": "subslide" 150 | } 151 | }, 152 | "source": [ 153 | "Collect the set of all letters in your first name." 154 | ] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "execution_count": null, 159 | "metadata": {}, 160 | "outputs": [], 161 | "source": [] 162 | }, 163 | { 164 | "cell_type": "markdown", 165 | "metadata": { 166 | "slideshow": { 167 | "slide_type": "fragment" 168 | } 169 | }, 170 | "source": [ 171 | "List examples of each type of collection in Python." 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": null, 177 | "metadata": {}, 178 | "outputs": [], 179 | "source": [] 180 | }, 181 | { 182 | "cell_type": "markdown", 183 | "metadata": { 184 | "slideshow": { 185 | "slide_type": "subslide" 186 | } 187 | }, 188 | "source": [ 189 | "Sequences, in particular, have some interesting properties can be concatenated together with the '+' operator:\n", 190 | "\n", 191 | "```python\n", 192 | ">>> \"Hello\" + \"World\"\n", 193 | "\"HelloWorld\"\n", 194 | "```" 195 | ] 196 | }, 197 | { 198 | "cell_type": "markdown", 199 | "metadata": {}, 200 | "source": [ 201 | "Try concatenating three different types of sequences (containing whatever you wish)" 202 | ] 203 | }, 204 | { 205 | "cell_type": "code", 206 | "execution_count": null, 207 | "metadata": {}, 208 | "outputs": [], 209 | "source": [] 210 | }, 211 | { 212 | "cell_type": "code", 213 | "execution_count": null, 214 | "metadata": {}, 215 | "outputs": [], 216 | "source": [] 217 | }, 218 | { 219 | "cell_type": "code", 220 | "execution_count": null, 221 | "metadata": {}, 222 | "outputs": [], 223 | "source": [] 224 | }, 225 | { 226 | "cell_type": "markdown", 227 | "metadata": {}, 228 | "source": [ 229 | "### Transforming Collections\n", 230 | "\n", 231 | "Although we've mostly used operators to make collections, there are also functions that can change one type of collection to another type:\n", 232 | "\n", 233 | "| | tuple | list | dict | set | string |\n", 234 | "| :--: | :---: | :---: | :---: | :---: | :---: |\n", 235 | "| **function:** | tuple() | list() | dict() | set() | str() |\n", 236 | "| **operator:** | () | [] | {} | {} | \"\" |\n" 237 | ] 238 | }, 239 | { 240 | "cell_type": "markdown", 241 | "metadata": {}, 242 | "source": [ 243 | "**Exercises**: Given a collection, change it into the requested type:" 244 | ] 245 | }, 246 | { 247 | "cell_type": "markdown", 248 | "metadata": {}, 249 | "source": [ 250 | "1. list" 251 | ] 252 | }, 253 | { 254 | "cell_type": "code", 255 | "execution_count": 55, 256 | "metadata": {}, 257 | "outputs": [ 258 | { 259 | "data": { 260 | "text/plain": [ 261 | "(1, 2, 3)" 262 | ] 263 | }, 264 | "execution_count": 55, 265 | "metadata": {}, 266 | "output_type": "execute_result" 267 | } 268 | ], 269 | "source": [ 270 | "(1, 2, 3)" 271 | ] 272 | }, 273 | { 274 | "cell_type": "markdown", 275 | "metadata": {}, 276 | "source": [ 277 | "2. set" 278 | ] 279 | }, 280 | { 281 | "cell_type": "code", 282 | "execution_count": 56, 283 | "metadata": {}, 284 | "outputs": [ 285 | { 286 | "data": { 287 | "text/plain": [ 288 | "('a', 'b', 'a', 'c', 'b')" 289 | ] 290 | }, 291 | "execution_count": 56, 292 | "metadata": {}, 293 | "output_type": "execute_result" 294 | } 295 | ], 296 | "source": [ 297 | "(\"a\", \"b\", \"a\", \"c\", \"b\")" 298 | ] 299 | }, 300 | { 301 | "cell_type": "markdown", 302 | "metadata": {}, 303 | "source": [ 304 | "3. string" 305 | ] 306 | }, 307 | { 308 | "cell_type": "code", 309 | "execution_count": 57, 310 | "metadata": {}, 311 | "outputs": [ 312 | { 313 | "data": { 314 | "text/plain": [ 315 | "[1, 2, 3, 4]" 316 | ] 317 | }, 318 | "execution_count": 57, 319 | "metadata": {}, 320 | "output_type": "execute_result" 321 | } 322 | ], 323 | "source": [ 324 | "[1, 2, 3, 4]" 325 | ] 326 | }, 327 | { 328 | "cell_type": "markdown", 329 | "metadata": {}, 330 | "source": [ 331 | "4. list" 332 | ] 333 | }, 334 | { 335 | "cell_type": "code", 336 | "execution_count": 58, 337 | "metadata": {}, 338 | "outputs": [ 339 | { 340 | "data": { 341 | "text/plain": [ 342 | "'1234'" 343 | ] 344 | }, 345 | "execution_count": 58, 346 | "metadata": {}, 347 | "output_type": "execute_result" 348 | } 349 | ], 350 | "source": [ 351 | "\"1234\"" 352 | ] 353 | }, 354 | { 355 | "cell_type": "markdown", 356 | "metadata": {}, 357 | "source": [ 358 | "5. Tuple" 359 | ] 360 | }, 361 | { 362 | "cell_type": "code", 363 | "execution_count": 59, 364 | "metadata": {}, 365 | "outputs": [ 366 | { 367 | "data": { 368 | "text/plain": [ 369 | "'Hello, World!'" 370 | ] 371 | }, 372 | "execution_count": 59, 373 | "metadata": {}, 374 | "output_type": "execute_result" 375 | } 376 | ], 377 | "source": [ 378 | "\"Hello, World!\"" 379 | ] 380 | }, 381 | { 382 | "cell_type": "markdown", 383 | "metadata": {}, 384 | "source": [ 385 | "6. Dict" 386 | ] 387 | }, 388 | { 389 | "cell_type": "code", 390 | "execution_count": 60, 391 | "metadata": {}, 392 | "outputs": [ 393 | { 394 | "data": { 395 | "text/plain": [ 396 | "[(1, 2), (2, 3), (3, 4)]" 397 | ] 398 | }, 399 | "execution_count": 60, 400 | "metadata": {}, 401 | "output_type": "execute_result" 402 | } 403 | ], 404 | "source": [ 405 | "[(1, 2), (2, 3), (3, 4)]" 406 | ] 407 | }, 408 | { 409 | "cell_type": "markdown", 410 | "metadata": {}, 411 | "source": [ 412 | "6. Set" 413 | ] 414 | }, 415 | { 416 | "cell_type": "code", 417 | "execution_count": 61, 418 | "metadata": {}, 419 | "outputs": [ 420 | { 421 | "data": { 422 | "text/plain": [ 423 | "{'a': 1, 'b': 2, 'c': 3}" 424 | ] 425 | }, 426 | "execution_count": 61, 427 | "metadata": {}, 428 | "output_type": "execute_result" 429 | } 430 | ], 431 | "source": [ 432 | "{'a': 1, 'b': 2, 'c': 3}" 433 | ] 434 | }, 435 | { 436 | "cell_type": "markdown", 437 | "metadata": {}, 438 | "source": [ 439 | "7. Discussion: Why does Python give an error when trying to change this list into a dict?" 440 | ] 441 | }, 442 | { 443 | "cell_type": "code", 444 | "execution_count": 62, 445 | "metadata": {}, 446 | "outputs": [ 447 | { 448 | "ename": "TypeError", 449 | "evalue": "cannot convert dictionary update sequence element #0 to a sequence", 450 | "output_type": "error", 451 | "traceback": [ 452 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 453 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 454 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mdict\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m4\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m5\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 455 | "\u001b[1;31mTypeError\u001b[0m: cannot convert dictionary update sequence element #0 to a sequence" 456 | ] 457 | } 458 | ], 459 | "source": [ 460 | "dict([1, 2, 3, 4, 5])" 461 | ] 462 | }, 463 | { 464 | "cell_type": "markdown", 465 | "metadata": { 466 | "slideshow": { 467 | "slide_type": "slide" 468 | } 469 | }, 470 | "source": [ 471 | "## Extracting Data From a Collection\n", 472 | "\n", 473 | "Data can be indexed/queried/extracted from collectoins using the square brackets: [ ]\n", 474 | "\n", 475 | "In sequences, putting a number inside the the brackets extracts the nth (counting from zero) value\n", 476 | "```python\n", 477 | ">>> (1, 2, 3)[1]\n", 478 | "2\n", 479 | "\n", 480 | ">>> (1, 2, 3)[0]\n", 481 | "1\n", 482 | "\n", 483 | ">>> (1, 2, 3)[-1]\n", 484 | "3\n", 485 | "```\n", 486 | "\n", 487 | "You can \"slice\" a sequence (get all from one index to another index) using the colon [:]\n", 488 | "```python\n", 489 | ">>> (10, 20, 30, 40, 50, 60)[1:3]\n", 490 | "(20, 30)\n", 491 | "\n", 492 | ">>> (10, 20, 30, 40, 50, 60)[:3]\n", 493 | "(10, 20, 30)\n", 494 | "\n", 495 | ">>> (10, 20, 30, 40, 50, 60)[3:]\n", 496 | "(40, 50, 60)\n", 497 | "```\n", 498 | "\n", 499 | "In dicts, putting the key inside the brackets returns the associated value.\n", 500 | "```python\n", 501 | ">>> {'a': 3, 'b': 4}['a']\n", 502 | "3\n", 503 | "\n", 504 | ">>> {'a': 3, 'b': 4}['b']\n", 505 | "4\n", 506 | "```\n" 507 | ] 508 | }, 509 | { 510 | "cell_type": "markdown", 511 | "metadata": { 512 | "slideshow": { 513 | "slide_type": "subslide" 514 | } 515 | }, 516 | "source": [ 517 | "## Indexing Exercises\n", 518 | "\n", 519 | "Using the example dataset *scores*, select only the described elements from each list:" 520 | ] 521 | }, 522 | { 523 | "cell_type": "markdown", 524 | "metadata": { 525 | "slideshow": { 526 | "slide_type": "fragment" 527 | } 528 | }, 529 | "source": [ 530 | "0. The first score" 531 | ] 532 | }, 533 | { 534 | "cell_type": "code", 535 | "execution_count": null, 536 | "metadata": {}, 537 | "outputs": [], 538 | "source": [ 539 | "(0.2, 0.3, 0.9, 1.1, 2.2, 2.9, 0.0, 0.7, 1.3, 0.3, 0.5, 0.1, 0.0)" 540 | ] 541 | }, 542 | { 543 | "cell_type": "markdown", 544 | "metadata": { 545 | "slideshow": { 546 | "slide_type": "fragment" 547 | } 548 | }, 549 | "source": [ 550 | "1. The third score" 551 | ] 552 | }, 553 | { 554 | "cell_type": "code", 555 | "execution_count": null, 556 | "metadata": {}, 557 | "outputs": [], 558 | "source": [ 559 | "(0.2, 0.3, 0.9, 1.1, 2.2, 2.9, 0.0, 0.7, 1.3, 0.3, 0.5, 0.1, 0.0)" 560 | ] 561 | }, 562 | { 563 | "cell_type": "markdown", 564 | "metadata": { 565 | "slideshow": { 566 | "slide_type": "subslide" 567 | } 568 | }, 569 | "source": [ 570 | "2. The last score" 571 | ] 572 | }, 573 | { 574 | "cell_type": "code", 575 | "execution_count": null, 576 | "metadata": {}, 577 | "outputs": [], 578 | "source": [ 579 | "(0.2, 0.3, 0.9, 1.1, 2.2, 2.9, 0.0, 0.7, 1.3, 0.3, 0.5, 0.1, 0.0)" 580 | ] 581 | }, 582 | { 583 | "cell_type": "markdown", 584 | "metadata": {}, 585 | "source": [ 586 | "2a. The 3rd from the last score" 587 | ] 588 | }, 589 | { 590 | "cell_type": "code", 591 | "execution_count": 65, 592 | "metadata": {}, 593 | "outputs": [], 594 | "source": [ 595 | "(0.2, 0.3, 0.9, 1.1, 2.2, 2.9, 0.0, 0.7, 1.3, 0.3, 0.5, 0.1, 0.0)" 596 | ] 597 | }, 598 | { 599 | "cell_type": "markdown", 600 | "metadata": { 601 | "slideshow": { 602 | "slide_type": "fragment" 603 | } 604 | }, 605 | "source": [ 606 | "3. The 2nd through 5th score" 607 | ] 608 | }, 609 | { 610 | "cell_type": "code", 611 | "execution_count": null, 612 | "metadata": {}, 613 | "outputs": [], 614 | "source": [ 615 | "(0.2, 0.3, 0.9, 1.1, 2.2, 2.9, 0.0, 0.7, 1.3, 0.3, 0.5, 0.1, 0.0)" 616 | ] 617 | }, 618 | { 619 | "cell_type": "markdown", 620 | "metadata": { 621 | "slideshow": { 622 | "slide_type": "fragment" 623 | } 624 | }, 625 | "source": [ 626 | "4. Every second score (the first, third, fifth, etc)" 627 | ] 628 | }, 629 | { 630 | "cell_type": "code", 631 | "execution_count": null, 632 | "metadata": {}, 633 | "outputs": [], 634 | "source": [ 635 | "(0.2, 0.3, 0.9, 1.1, 2.2, 2.9, 0.0, 0.7, 1.3, 0.3, 0.5, 0.1, 0.0)" 636 | ] 637 | }, 638 | { 639 | "cell_type": "markdown", 640 | "metadata": { 641 | "slideshow": { 642 | "slide_type": "subslide" 643 | } 644 | }, 645 | "source": [ 646 | "5. Every score after the 4th score" 647 | ] 648 | }, 649 | { 650 | "cell_type": "code", 651 | "execution_count": 11, 652 | "metadata": {}, 653 | "outputs": [ 654 | { 655 | "data": { 656 | "text/plain": [ 657 | "(0.2, 0.3, 0.9, 1.1, 2.2, 2.9, 0.0, 0.7, 1.3, 0.3, 0.5, 0.1, 0.0)" 658 | ] 659 | }, 660 | "execution_count": 11, 661 | "metadata": {}, 662 | "output_type": "execute_result" 663 | } 664 | ], 665 | "source": [ 666 | "(0.2, 0.3, 0.9, 1.1, 2.2, 2.9, 0.0, 0.7, 1.3, 0.3, 0.5, 0.1, 0.0)" 667 | ] 668 | }, 669 | { 670 | "cell_type": "markdown", 671 | "metadata": { 672 | "slideshow": { 673 | "slide_type": "fragment" 674 | } 675 | }, 676 | "source": [ 677 | "6. Every second score from the 2nd to the 8th." 678 | ] 679 | }, 680 | { 681 | "cell_type": "code", 682 | "execution_count": 66, 683 | "metadata": {}, 684 | "outputs": [], 685 | "source": [ 686 | "(0.2, 0.3, 0.9, 1.1, 2.2, 2.9, 0.0, 0.7, 1.3, 0.3, 0.5, 0.1, 0.0)" 687 | ] 688 | }, 689 | { 690 | "cell_type": "markdown", 691 | "metadata": {}, 692 | "source": [ 693 | "7. Every score except the first and last." 694 | ] 695 | }, 696 | { 697 | "cell_type": "code", 698 | "execution_count": null, 699 | "metadata": {}, 700 | "outputs": [], 701 | "source": [ 702 | "(0.2, 0.3, 0.9, 1.1, 2.2, 2.9, 0.0, 0.7, 1.3, 0.3, 0.5, 0.1, 0.0)" 703 | ] 704 | }, 705 | { 706 | "cell_type": "markdown", 707 | "metadata": { 708 | "slideshow": { 709 | "slide_type": "slide" 710 | } 711 | }, 712 | "source": [ 713 | "## Aggregating Your Collections into Single Values (Descriptive Statistics)\n", 714 | "\n", 715 | "Python can use named **functions** that turn data into something else. By doing this repeatedly, in sequence, we can build data processing pipelines!\n", 716 | "\n", 717 | "Functions in Python have the following syntax:\n", 718 | "\n", 719 | "```\n", 720 | "output = function(input)\n", 721 | "```\n", 722 | "\n", 723 | "First, let's look at some **built-in** functions to get a sense of how they work: **min()**, **max()**, **sum()**, and **len()**:\n", 724 | "\n", 725 | "```python\n", 726 | ">>> min([1, 2, 3, 2])\n", 727 | "1\n", 728 | "\n", 729 | ">>> max([1, 2, 3, 2])\n", 730 | "3\n", 731 | "\n", 732 | ">>> sum([1, 2, 3, 2])\n", 733 | "8\n", 734 | "\n", 735 | ">>> len([1, 2, 3, 2])\n", 736 | "4\n", 737 | "```" 738 | ] 739 | }, 740 | { 741 | "cell_type": "markdown", 742 | "metadata": { 743 | "slideshow": { 744 | "slide_type": "subslide" 745 | } 746 | }, 747 | "source": [ 748 | "### Exercises: \n", 749 | "Read the following lines of Python and predict their output. Then, run the code to see if you were correct." 750 | ] 751 | }, 752 | { 753 | "cell_type": "markdown", 754 | "metadata": { 755 | "slideshow": { 756 | "slide_type": "fragment" 757 | } 758 | }, 759 | "source": [ 760 | "```python\n", 761 | "min([3, 6, 5, 2])\n", 762 | "```" 763 | ] 764 | }, 765 | { 766 | "cell_type": "code", 767 | "execution_count": null, 768 | "metadata": {}, 769 | "outputs": [], 770 | "source": [] 771 | }, 772 | { 773 | "cell_type": "markdown", 774 | "metadata": { 775 | "slideshow": { 776 | "slide_type": "fragment" 777 | } 778 | }, 779 | "source": [ 780 | "```python\n", 781 | "max([3, 6, 5, 2][2:])\n", 782 | "```" 783 | ] 784 | }, 785 | { 786 | "cell_type": "code", 787 | "execution_count": null, 788 | "metadata": {}, 789 | "outputs": [], 790 | "source": [] 791 | }, 792 | { 793 | "cell_type": "markdown", 794 | "metadata": { 795 | "slideshow": { 796 | "slide_type": "subslide" 797 | } 798 | }, 799 | "source": [ 800 | "```python\n", 801 | "min([max((2, 4, 1)), len((2, 4, 1)), min(2, 4, 1)])\n", 802 | "```" 803 | ] 804 | }, 805 | { 806 | "cell_type": "code", 807 | "execution_count": null, 808 | "metadata": {}, 809 | "outputs": [], 810 | "source": [] 811 | }, 812 | { 813 | "cell_type": "markdown", 814 | "metadata": { 815 | "slideshow": { 816 | "slide_type": "fragment" 817 | } 818 | }, 819 | "source": [ 820 | "```python\n", 821 | "(min([1, 2]), max([3, 4, 5]))\n", 822 | "```" 823 | ] 824 | }, 825 | { 826 | "cell_type": "code", 827 | "execution_count": null, 828 | "metadata": {}, 829 | "outputs": [], 830 | "source": [] 831 | }, 832 | { 833 | "cell_type": "markdown", 834 | "metadata": { 835 | "slideshow": { 836 | "slide_type": "fragment" 837 | } 838 | }, 839 | "source": [ 840 | "```python\n", 841 | "max([1, 2, 3, 4, 5, 6][1:3])\n", 842 | "```" 843 | ] 844 | }, 845 | { 846 | "cell_type": "code", 847 | "execution_count": null, 848 | "metadata": {}, 849 | "outputs": [], 850 | "source": [] 851 | }, 852 | { 853 | "cell_type": "markdown", 854 | "metadata": { 855 | "slideshow": { 856 | "slide_type": "subslide" 857 | } 858 | }, 859 | "source": [ 860 | "```python\n", 861 | "len({3, 4, 5, 5, 4})\n", 862 | "```" 863 | ] 864 | }, 865 | { 866 | "cell_type": "code", 867 | "execution_count": null, 868 | "metadata": {}, 869 | "outputs": [], 870 | "source": [] 871 | }, 872 | { 873 | "cell_type": "markdown", 874 | "metadata": { 875 | "slideshow": { 876 | "slide_type": "fragment" 877 | } 878 | }, 879 | "source": [ 880 | "```python\n", 881 | "len({1: 2, 3: 4, 5: 6, 7: 8})\n", 882 | "```" 883 | ] 884 | }, 885 | { 886 | "cell_type": "code", 887 | "execution_count": null, 888 | "metadata": {}, 889 | "outputs": [], 890 | "source": [] 891 | }, 892 | { 893 | "cell_type": "markdown", 894 | "metadata": {}, 895 | "source": [ 896 | "### Discussion: sum() of strings\n", 897 | "In Python, why does this work:" 898 | ] 899 | }, 900 | { 901 | "cell_type": "code", 902 | "execution_count": 2, 903 | "metadata": {}, 904 | "outputs": [ 905 | { 906 | "data": { 907 | "text/plain": [ 908 | "'HiBye'" 909 | ] 910 | }, 911 | "execution_count": 2, 912 | "metadata": {}, 913 | "output_type": "execute_result" 914 | } 915 | ], 916 | "source": [ 917 | "\"Hi\" + \"Bye\"" 918 | ] 919 | }, 920 | { 921 | "cell_type": "markdown", 922 | "metadata": {}, 923 | "source": [ 924 | "... but not this:" 925 | ] 926 | }, 927 | { 928 | "cell_type": "code", 929 | "execution_count": 1, 930 | "metadata": {}, 931 | "outputs": [ 932 | { 933 | "ename": "TypeError", 934 | "evalue": "unsupported operand type(s) for +: 'int' and 'str'", 935 | "output_type": "error", 936 | "traceback": [ 937 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 938 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 939 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0msum\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"Hi\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"Bye\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 940 | "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'" 941 | ] 942 | } 943 | ], 944 | "source": [ 945 | "sum([\"Hi\", \"Bye\"])" 946 | ] 947 | }, 948 | { 949 | "cell_type": "markdown", 950 | "metadata": {}, 951 | "source": [ 952 | "What are some mental models we can use to reason about this difference in behavior? And what does the error mean?" 953 | ] 954 | } 955 | ], 956 | "metadata": { 957 | "celltoolbar": "Slideshow", 958 | "kernelspec": { 959 | "display_name": "Python 3", 960 | "language": "python", 961 | "name": "python3" 962 | }, 963 | "language_info": { 964 | "codemirror_mode": { 965 | "name": "ipython", 966 | "version": 3 967 | }, 968 | "file_extension": ".py", 969 | "mimetype": "text/x-python", 970 | "name": "python", 971 | "nbconvert_exporter": "python", 972 | "pygments_lexer": "ipython3", 973 | "version": "3.7.4" 974 | } 975 | }, 976 | "nbformat": 4, 977 | "nbformat_minor": 4 978 | } 979 | -------------------------------------------------------------------------------- /Day0_Python_Syntax/Python Syntax 3.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Intro to Python Syntax, Part 3\n", 8 | "## Code and Data Assignment" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "metadata": { 14 | "slideshow": { 15 | "slide_type": "slide" 16 | } 17 | }, 18 | "source": [ 19 | "## Python as an Ecosystem of Functions\n", 20 | "\n", 21 | "There are millions of functions that can be used in Python. To access them, you first have to **import** their **packages/modules**. Then you can specify the functions inside the packages using the following syntax:\n", 22 | "\n", 23 | "```python\n", 24 | "import package\n", 25 | "output = package.function(input)\n", 26 | "```\n", 27 | "\n", 28 | "Sometimes, packages have **subpackages** that contain functions, too:\n", 29 | "\n", 30 | "```python\n", 31 | "output = package.subpackage.function(input)\n", 32 | "```\n", 33 | "\n", 34 | "If you only need one subpackage in a function, you can make your lines shorter by importing the subpackage directly:\n", 35 | "\n", 36 | "```python\n", 37 | "from package import subpackage\n", 38 | "output = subpackage.function(input)\n", 39 | "```\n", 40 | "\n", 41 | "To get a list of functions in a package, try using the **dir()** function. To learn what the function does, try the **help()** function.\n", 42 | "\n", 43 | "```python\n", 44 | "import math\n", 45 | "dir(math)\n", 46 | "help(math.sqrt)\n", 47 | "```\n" 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "metadata": { 53 | "slideshow": { 54 | "slide_type": "subslide" 55 | } 56 | }, 57 | "source": [ 58 | "Let's try out the built-in **math** and **statistics** packages to do some calculating!" 59 | ] 60 | }, 61 | { 62 | "cell_type": "markdown", 63 | "metadata": { 64 | "slideshow": { 65 | "slide_type": "fragment" 66 | } 67 | }, 68 | "source": [ 69 | "What is the mean of all the integers from 1 to 6?" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": null, 75 | "metadata": {}, 76 | "outputs": [], 77 | "source": [] 78 | }, 79 | { 80 | "cell_type": "markdown", 81 | "metadata": { 82 | "slideshow": { 83 | "slide_type": "fragment" 84 | } 85 | }, 86 | "source": [ 87 | "What is the standard deviation of the integers from 1 to 6?" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": null, 93 | "metadata": {}, 94 | "outputs": [], 95 | "source": [] 96 | }, 97 | { 98 | "cell_type": "markdown", 99 | "metadata": { 100 | "slideshow": { 101 | "slide_type": "subslide" 102 | } 103 | }, 104 | "source": [ 105 | "What is the square root of 72?" 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": null, 111 | "metadata": {}, 112 | "outputs": [], 113 | "source": [] 114 | }, 115 | { 116 | "cell_type": "markdown", 117 | "metadata": { 118 | "slideshow": { 119 | "slide_type": "fragment" 120 | } 121 | }, 122 | "source": [ 123 | "What is pi?" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": null, 129 | "metadata": {}, 130 | "outputs": [], 131 | "source": [] 132 | }, 133 | { 134 | "cell_type": "markdown", 135 | "metadata": { 136 | "slideshow": { 137 | "slide_type": "fragment" 138 | } 139 | }, 140 | "source": [ 141 | "What how many radians are in 360 degrees?" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": null, 147 | "metadata": {}, 148 | "outputs": [], 149 | "source": [] 150 | }, 151 | { 152 | "cell_type": "markdown", 153 | "metadata": { 154 | "slideshow": { 155 | "slide_type": "subslide" 156 | } 157 | }, 158 | "source": [ 159 | "Can you find a package that might have functions for random numbers? Use it to generate a couple random numbers!" 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": 15, 165 | "metadata": {}, 166 | "outputs": [], 167 | "source": [] 168 | }, 169 | { 170 | "cell_type": "markdown", 171 | "metadata": { 172 | "slideshow": { 173 | "slide_type": "fragment" 174 | } 175 | }, 176 | "source": [ 177 | "Look for names of some more built-in packages online! Try importing them here." 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": null, 183 | "metadata": {}, 184 | "outputs": [], 185 | "source": [] 186 | }, 187 | { 188 | "cell_type": "markdown", 189 | "metadata": { 190 | "slideshow": { 191 | "slide_type": "slide" 192 | } 193 | }, 194 | "source": [ 195 | "## Python as State Machine\n", 196 | "\n", 197 | "Python can save all of its **objects** to your computer's internal memory by **assigning** a a **name** to it. This is like giving the object a tag that can be referenced later, and it has many subtleties that we'll be exploring throughout the course. Here's the syntax:\n", 198 | "\n", 199 | "```\n", 200 | "name = data\n", 201 | "```\n", 202 | "\n", 203 | "For example:\n", 204 | "\n", 205 | "```python\n", 206 | "x = 3\n", 207 | "nick = 100\n", 208 | "j_people = [\"John\", \"Jeff\", \"Judy\", \"Jenny\"]\n", 209 | "x2 = 4\n", 210 | "```\n", 211 | "\n", 212 | "While it looks like a normal math expression, this is, in reality, a **statement**; it changes the state of the program. Let's explore it here:\n", 213 | "\n" 214 | ] 215 | }, 216 | { 217 | "cell_type": "markdown", 218 | "metadata": { 219 | "slideshow": { 220 | "slide_type": "subslide" 221 | } 222 | }, 223 | "source": [ 224 | "#### Exercises" 225 | ] 226 | }, 227 | { 228 | "cell_type": "markdown", 229 | "metadata": { 230 | "slideshow": { 231 | "slide_type": "fragment" 232 | } 233 | }, 234 | "source": [ 235 | "What should **x** equal?\n", 236 | "```python\n", 237 | "x = 3\n", 238 | "x = 4\n", 239 | "```" 240 | ] 241 | }, 242 | { 243 | "cell_type": "code", 244 | "execution_count": null, 245 | "metadata": {}, 246 | "outputs": [], 247 | "source": [] 248 | }, 249 | { 250 | "cell_type": "markdown", 251 | "metadata": { 252 | "slideshow": { 253 | "slide_type": "subslide" 254 | } 255 | }, 256 | "source": [ 257 | "```python\n", 258 | "x = 3\n", 259 | "x = x + 1\n", 260 | "x = x + 1\n", 261 | "x = x + 1\n", 262 | "```" 263 | ] 264 | }, 265 | { 266 | "cell_type": "code", 267 | "execution_count": null, 268 | "metadata": {}, 269 | "outputs": [], 270 | "source": [] 271 | }, 272 | { 273 | "cell_type": "markdown", 274 | "metadata": { 275 | "slideshow": { 276 | "slide_type": "fragment" 277 | } 278 | }, 279 | "source": [ 280 | "```python\n", 281 | "x = 3\n", 282 | "x += 1\n", 283 | "x += 1\n", 284 | "x += 1\n", 285 | "```" 286 | ] 287 | }, 288 | { 289 | "cell_type": "code", 290 | "execution_count": null, 291 | "metadata": {}, 292 | "outputs": [], 293 | "source": [] 294 | }, 295 | { 296 | "cell_type": "markdown", 297 | "metadata": { 298 | "slideshow": { 299 | "slide_type": "subslide" 300 | } 301 | }, 302 | "source": [ 303 | "```python\n", 304 | "x = \"Nick\"\n", 305 | "x = 0\n", 306 | "```" 307 | ] 308 | }, 309 | { 310 | "cell_type": "code", 311 | "execution_count": null, 312 | "metadata": {}, 313 | "outputs": [], 314 | "source": [] 315 | }, 316 | { 317 | "cell_type": "markdown", 318 | "metadata": { 319 | "slideshow": { 320 | "slide_type": "subslide" 321 | } 322 | }, 323 | "source": [ 324 | "```python\n", 325 | "max = sum\n", 326 | "x = max([1, 2, 3])\n", 327 | "```" 328 | ] 329 | }, 330 | { 331 | "cell_type": "code", 332 | "execution_count": null, 333 | "metadata": {}, 334 | "outputs": [], 335 | "source": [] 336 | }, 337 | { 338 | "cell_type": "markdown", 339 | "metadata": { 340 | "slideshow": { 341 | "slide_type": "fragment" 342 | } 343 | }, 344 | "source": [ 345 | "```python\n", 346 | "max = sum\n", 347 | "x = sum([1, 2, 3])\n", 348 | "```" 349 | ] 350 | }, 351 | { 352 | "cell_type": "code", 353 | "execution_count": null, 354 | "metadata": {}, 355 | "outputs": [], 356 | "source": [] 357 | }, 358 | { 359 | "cell_type": "markdown", 360 | "metadata": { 361 | "slideshow": { 362 | "slide_type": "subslide" 363 | } 364 | }, 365 | "source": [ 366 | "```python\n", 367 | "x = [1, 2, 3]\n", 368 | "x[1] = 4\n", 369 | "```" 370 | ] 371 | }, 372 | { 373 | "cell_type": "code", 374 | "execution_count": null, 375 | "metadata": {}, 376 | "outputs": [], 377 | "source": [] 378 | }, 379 | { 380 | "cell_type": "markdown", 381 | "metadata": { 382 | "slideshow": { 383 | "slide_type": "fragment" 384 | } 385 | }, 386 | "source": [ 387 | "```python\n", 388 | "x = [1, 2, 3]\n", 389 | "x[-1] = 4\n", 390 | "```" 391 | ] 392 | }, 393 | { 394 | "cell_type": "code", 395 | "execution_count": null, 396 | "metadata": {}, 397 | "outputs": [], 398 | "source": [] 399 | }, 400 | { 401 | "cell_type": "markdown", 402 | "metadata": { 403 | "slideshow": { 404 | "slide_type": "fragment" 405 | } 406 | }, 407 | "source": [ 408 | "```python\n", 409 | "x = [1, 2, 3]\n", 410 | "x[0] = 4\n", 411 | "```" 412 | ] 413 | }, 414 | { 415 | "cell_type": "code", 416 | "execution_count": null, 417 | "metadata": {}, 418 | "outputs": [], 419 | "source": [] 420 | }, 421 | { 422 | "cell_type": "markdown", 423 | "metadata": {}, 424 | "source": [ 425 | "```python\n", 426 | "x = [1, 2, 3]\n", 427 | "x = x[1]\n", 428 | "x *= 2\n", 429 | "```" 430 | ] 431 | }, 432 | { 433 | "cell_type": "code", 434 | "execution_count": null, 435 | "metadata": {}, 436 | "outputs": [], 437 | "source": [] 438 | }, 439 | { 440 | "cell_type": "markdown", 441 | "metadata": {}, 442 | "source": [ 443 | "### Mapping Collections: Transforming Each Element of a Collection\n", 444 | "\n", 445 | "Often, you want to do something to the data inside your collection--double it, increase it, compute some metric, etc. \n", 446 | "At the end, you still have the same *number* of elements, but the values themselves have changed. \n", 447 | "We will be looking at lots of ways to accomplish this in Python, but first we're going to use a **for-loop** in a format called a **comprehension**.\n", 448 | "\n", 449 | "Comprehensions produce a new collection containing new values *\"for each\"* value *in* the original collection. They look like this:\n", 450 | "\n", 451 | "```python\n", 452 | ">>> data = [1, 2, 3]\n", 453 | ">>> list(x * 2 for x in data)\n", 454 | "[1, 4, 9]\n", 455 | "```\n", 456 | "\n", 457 | "The code above does the same as the following:\n", 458 | "```python\n", 459 | ">>> data = [1, 2, 3]\n", 460 | ">>> [data[0] * 2, data[1] * 2, data[2] * 2]\n", 461 | "[1, 4, 9]\n", 462 | "```\n", 463 | "\n", 464 | "Another example:\n", 465 | "\n", 466 | "```python\n", 467 | ">>> data = [1, 2, 3]\n", 468 | ">>> tuple(math.sqrt(value) + 2 for value in data)\n", 469 | "(3.0, 3.414, 3.732)\n", 470 | "```\n", 471 | "\n", 472 | "```python\n", 473 | ">>> data = [1, 2, 3]\n", 474 | ">>> (math.sqrt(data[0]) + 2, math.sqrt(data[1]) + 2, math.sqrt(data[2]) + 2)\n", 475 | "(3.0, 3.414, 3.732)\n", 476 | "```\n", 477 | "\n", 478 | "Notice that the variable **x** and **value** are created without using the assignment operator! You can name this anything you want, and it is created anew for each individual element of the collection.\n", 479 | "\n", 480 | "For lists, Python has a shortcut (called a **list comprehension**):\n", 481 | "\n", 482 | "```python\n", 483 | ">>> data = [1, 2, 3]\n", 484 | ">>> [x * 2 for x in data]\n", 485 | "[1, 4, 9]\n", 486 | "```\n", 487 | "\n", 488 | "There is also one for dict (**\"dict comprehensions\"**), but not for any other type. See below:\n", 489 | "\n", 490 | "```python\n", 491 | ">>> data = [1, 2, 3]\n", 492 | ">>> {x: x * 2 for x in data}\n", 493 | "{1: 1, 2: 4, 3: 9}\n", 494 | "```" 495 | ] 496 | }, 497 | { 498 | "cell_type": "markdown", 499 | "metadata": {}, 500 | "source": [ 501 | "**Exercises**: Transform each value in the given collection into the requested collection type using the requested transformation\n", 502 | "\n", 503 | "Example: A tuple of each value in \"data\", with each value increased by 3\n", 504 | "```python\n", 505 | ">>> data = [10, 20, 30]\n", 506 | ">>> tuple(x + 3 for x in data)\n", 507 | "(13, 23, 33)\n", 508 | "```" 509 | ] 510 | }, 511 | { 512 | "cell_type": "markdown", 513 | "metadata": {}, 514 | "source": [ 515 | "1. Make a list that adds 1 to each value in \"data\":" 516 | ] 517 | }, 518 | { 519 | "cell_type": "code", 520 | "execution_count": 5, 521 | "metadata": {}, 522 | "outputs": [], 523 | "source": [ 524 | "data = [1, 2, 3]" 525 | ] 526 | }, 527 | { 528 | "cell_type": "markdown", 529 | "metadata": {}, 530 | "source": [ 531 | "2. Make a tuple that calculates the absolute value of each element in \"data\", using the built-in abs() function:" 532 | ] 533 | }, 534 | { 535 | "cell_type": "code", 536 | "execution_count": 6, 537 | "metadata": {}, 538 | "outputs": [], 539 | "source": [ 540 | "data = (-2, -1, 0, 1, 2)" 541 | ] 542 | }, 543 | { 544 | "cell_type": "markdown", 545 | "metadata": {}, 546 | "source": [ 547 | "2b. Make a list of the cosines of each value in \"data\":" 548 | ] 549 | }, 550 | { 551 | "cell_type": "code", 552 | "execution_count": 31, 553 | "metadata": {}, 554 | "outputs": [], 555 | "source": [ 556 | "data = (-2, -1, 0, 1, 2, 3)" 557 | ] 558 | }, 559 | { 560 | "cell_type": "markdown", 561 | "metadata": {}, 562 | "source": [ 563 | "2c. Round all these numbers to the nearest integer (use the \"round()\" function):" 564 | ] 565 | }, 566 | { 567 | "cell_type": "code", 568 | "execution_count": 33, 569 | "metadata": {}, 570 | "outputs": [], 571 | "source": [ 572 | "data = [1.2, 1.5, 0.7, -2.1]" 573 | ] 574 | }, 575 | { 576 | "cell_type": "markdown", 577 | "metadata": {}, 578 | "source": [ 579 | "3. Make a list of all the first letters of each name in the list:" 580 | ] 581 | }, 582 | { 583 | "cell_type": "code", 584 | "execution_count": 8, 585 | "metadata": {}, 586 | "outputs": [], 587 | "source": [ 588 | "data = [\"John\", \"Harry\", \"Moe\", \"Luke\"]" 589 | ] 590 | }, 591 | { 592 | "cell_type": "markdown", 593 | "metadata": {}, 594 | "source": [ 595 | "4. Make a tuple of the lengths of each name:" 596 | ] 597 | }, 598 | { 599 | "cell_type": "code", 600 | "execution_count": 9, 601 | "metadata": {}, 602 | "outputs": [], 603 | "source": [ 604 | "data = [\"John\", \"Harry\", \"Moe\", \"Luke\"]" 605 | ] 606 | }, 607 | { 608 | "cell_type": "markdown", 609 | "metadata": {}, 610 | "source": [ 611 | "5. Make a dictionary that maps all the words in the list to their length:" 612 | ] 613 | }, 614 | { 615 | "cell_type": "code", 616 | "execution_count": 13, 617 | "metadata": {}, 618 | "outputs": [], 619 | "source": [ 620 | "data = [\"John\", \"Harry\", \"Moe\", \"Luke\"]" 621 | ] 622 | }, 623 | { 624 | "cell_type": "markdown", 625 | "metadata": {}, 626 | "source": [ 627 | "6. Make a set of all the word lengths in the tuple:" 628 | ] 629 | }, 630 | { 631 | "cell_type": "code", 632 | "execution_count": 14, 633 | "metadata": {}, 634 | "outputs": [], 635 | "source": [ 636 | "data = (\"John\", \"Harry\", \"Moe\", \"Luke\")" 637 | ] 638 | }, 639 | { 640 | "cell_type": "markdown", 641 | "metadata": {}, 642 | "source": [ 643 | "### Filtering Collections\n", 644 | "\n", 645 | "What if you only want to include *some* values in a collection? With the **if** statement and a **logical expression**, you can do it in a comprehension! For example:\n", 646 | "\n", 647 | "```python\n", 648 | ">>> data = [1, 2, 3, 4]\n", 649 | ">>> [x for x in data if x > 2]\n", 650 | "[3, 4]\n", 651 | "```\n", 652 | "\n", 653 | "This can be combined with various transformations as well!\n", 654 | "\n", 655 | "```python\n", 656 | ">>> data = [\"John\", \"Harry\", \"Moe\", \"Luke\"]\n", 657 | ">>> [x[0] for x in data if len(x) < 5]\n", 658 | "[\"J\", \"M\", \"L\"]\n", 659 | "```" 660 | ] 661 | }, 662 | { 663 | "cell_type": "markdown", 664 | "metadata": {}, 665 | "source": [ 666 | "**Exercises**:" 667 | ] 668 | }, 669 | { 670 | "cell_type": "markdown", 671 | "metadata": {}, 672 | "source": [ 673 | "Get All positive values in the following list:" 674 | ] 675 | }, 676 | { 677 | "cell_type": "code", 678 | "execution_count": 25, 679 | "metadata": {}, 680 | "outputs": [], 681 | "source": [ 682 | "data = [-6, 3, -1, 10, -5, 0]" 683 | ] 684 | }, 685 | { 686 | "cell_type": "markdown", 687 | "metadata": {}, 688 | "source": [ 689 | "Make a tuple of all names that start with the letter \"L\":" 690 | ] 691 | }, 692 | { 693 | "cell_type": "code", 694 | "execution_count": null, 695 | "metadata": {}, 696 | "outputs": [], 697 | "source": [ 698 | "data = (\"John\", \"Harry\", \"Moe\", \"Luke\")" 699 | ] 700 | }, 701 | { 702 | "cell_type": "markdown", 703 | "metadata": {}, 704 | "source": [ 705 | "Make a list of all names that have more than 3 letters in the name:" 706 | ] 707 | }, 708 | { 709 | "cell_type": "code", 710 | "execution_count": 26, 711 | "metadata": {}, 712 | "outputs": [], 713 | "source": [ 714 | "data = (\"John\", \"Harry\", \"Moe\", \"Luke\")" 715 | ] 716 | }, 717 | { 718 | "cell_type": "markdown", 719 | "metadata": {}, 720 | "source": [ 721 | "Make a list of the last letter of all names that have more than 3 letters in the name:" 722 | ] 723 | }, 724 | { 725 | "cell_type": "code", 726 | "execution_count": 27, 727 | "metadata": {}, 728 | "outputs": [], 729 | "source": [ 730 | "data = (\"John\", \"Harry\", \"Moe\", \"Luke\")" 731 | ] 732 | }, 733 | { 734 | "cell_type": "markdown", 735 | "metadata": {}, 736 | "source": [ 737 | "Make a list of all values who have positive cosines:" 738 | ] 739 | }, 740 | { 741 | "cell_type": "code", 742 | "execution_count": 28, 743 | "metadata": {}, 744 | "outputs": [], 745 | "source": [ 746 | "data = [1, 2, 3, 4, 5, 6, 7]" 747 | ] 748 | }, 749 | { 750 | "cell_type": "markdown", 751 | "metadata": {}, 752 | "source": [ 753 | "Make a dictionary, mapping each name to its length, but only if the sine of its length is positive:" 754 | ] 755 | }, 756 | { 757 | "cell_type": "code", 758 | "execution_count": 34, 759 | "metadata": {}, 760 | "outputs": [], 761 | "source": [ 762 | "data = [\"John\", \"Harry\", \"Moe\", \"Luke\"]" 763 | ] 764 | }, 765 | { 766 | "cell_type": "markdown", 767 | "metadata": {}, 768 | "source": [ 769 | "### Combining Them: Filtering, Transforming, and Aggregating\n", 770 | "\n", 771 | "Okay, let's combine everything together into a single step! To get the minimum value of the squares of all values less than 0 in the following list:\n", 772 | "\n", 773 | "```python\n", 774 | ">>> data = [-5, -3, 1, 2, 3]\n", 775 | ">>> min(x ** 2 for x in data if x < 0)\n", 776 | "9\n", 777 | "```\n", 778 | "\n", 779 | "Let's try it out!" 780 | ] 781 | }, 782 | { 783 | "cell_type": "markdown", 784 | "metadata": {}, 785 | "source": [ 786 | "**Exercises**:" 787 | ] 788 | }, 789 | { 790 | "cell_type": "markdown", 791 | "metadata": {}, 792 | "source": [ 793 | "The sum of all squares for all values in the dataset [1, 7, 3, 4, 9] greater than 4." 794 | ] 795 | }, 796 | { 797 | "cell_type": "code", 798 | "execution_count": null, 799 | "metadata": {}, 800 | "outputs": [], 801 | "source": [] 802 | }, 803 | { 804 | "cell_type": "markdown", 805 | "metadata": {}, 806 | "source": [ 807 | "The minimum length of all names in the list who has at least 4 unique letters in their name: [\"Bobby\", \"Cindy\", \"Anna\", \"Joshua\", \"Alan\", \"Hannah\", \"Jeffrey\"]" 808 | ] 809 | }, 810 | { 811 | "cell_type": "code", 812 | "execution_count": null, 813 | "metadata": {}, 814 | "outputs": [], 815 | "source": [] 816 | }, 817 | { 818 | "cell_type": "markdown", 819 | "metadata": {}, 820 | "source": [ 821 | "The total length of all the names in the list that starts with an \"R\": [\"Joey\", \"Monica\", \"Chandler\", \"Rachel\", \"Ross\", \"Phoebe\"]" 822 | ] 823 | }, 824 | { 825 | "cell_type": "code", 826 | "execution_count": null, 827 | "metadata": {}, 828 | "outputs": [], 829 | "source": [] 830 | }, 831 | { 832 | "cell_type": "code", 833 | "execution_count": null, 834 | "metadata": {}, 835 | "outputs": [], 836 | "source": [] 837 | }, 838 | { 839 | "cell_type": "markdown", 840 | "metadata": {}, 841 | "source": [ 842 | "### Chaining Them: Repeatedly Filtering and Transforming before Aggregating\n", 843 | "Often, data analysis pipelines require multiple repeated transforms and filters before the analysis is complete. This can get quite unwieldy, to say the least. For example:" 844 | ] 845 | }, 846 | { 847 | "cell_type": "code", 848 | "execution_count": 21, 849 | "metadata": {}, 850 | "outputs": [ 851 | { 852 | "data": { 853 | "text/plain": [ 854 | "(16,)" 855 | ] 856 | }, 857 | "execution_count": 21, 858 | "metadata": {}, 859 | "output_type": "execute_result" 860 | } 861 | ], 862 | "source": [ 863 | "tuple(x for x in (x ** 2 for x in (len(x) for x in (x for x in [\"Joey\", \"Monica\", \"Chandler\", \"Rachel\", \"Ross\", \"Phoebe\"]) if x[0] == 'R') if x < 10) if x < 36)" 864 | ] 865 | }, 866 | { 867 | "cell_type": "markdown", 868 | "metadata": {}, 869 | "source": [ 870 | "You could split this into multiple lines, but it doesn't always help keep it readable. For example:" 871 | ] 872 | }, 873 | { 874 | "cell_type": "code", 875 | "execution_count": 22, 876 | "metadata": {}, 877 | "outputs": [ 878 | { 879 | "data": { 880 | "text/plain": [ 881 | "(16,)" 882 | ] 883 | }, 884 | "execution_count": 22, 885 | "metadata": {}, 886 | "output_type": "execute_result" 887 | } 888 | ], 889 | "source": [ 890 | "tuple \\\n", 891 | "(x for x in \n", 892 | "(x ** 2 for x in \n", 893 | "(len(x) for x in\n", 894 | "(x for x in \n", 895 | "[\"Joey\", \"Monica\", \"Chandler\", \"Rachel\", \"Ross\", \"Phoebe\"])\n", 896 | "if x[0] == 'R')\n", 897 | "if x < 10) \n", 898 | "if x < 36)" 899 | ] 900 | }, 901 | { 902 | "cell_type": "markdown", 903 | "metadata": {}, 904 | "source": [ 905 | "The simplest way is to just split it into multiple steps:" 906 | ] 907 | }, 908 | { 909 | "cell_type": "code", 910 | "execution_count": 24, 911 | "metadata": {}, 912 | "outputs": [ 913 | { 914 | "data": { 915 | "text/plain": [ 916 | "(16,)" 917 | ] 918 | }, 919 | "execution_count": 24, 920 | "metadata": {}, 921 | "output_type": "execute_result" 922 | } 923 | ], 924 | "source": [ 925 | "friends = [\"Joey\", \"Monica\", \"Chandler\", \"Rachel\", \"Ross\", \"Phoebe\"]\n", 926 | "friends = [len(x) for x in friends if x[0] == 'R']\n", 927 | "friends = [x ** 2 for x in friends if x < 10]\n", 928 | "tuple(x for x in friends if x < 36)" 929 | ] 930 | }, 931 | { 932 | "cell_type": "markdown", 933 | "metadata": {}, 934 | "source": [ 935 | "This is a little better, but it can be a lot simpler. We will be looking at many ways to make it easier for us to transform and filter data in later units." 936 | ] 937 | }, 938 | { 939 | "cell_type": "markdown", 940 | "metadata": { 941 | "slideshow": { 942 | "slide_type": "slide" 943 | } 944 | }, 945 | "source": [ 946 | "## Review: Class Discussion\n", 947 | "\n", 948 | " 1. Python has a lot of symbols in its syntax. What are they, and what are they used for?\n", 949 | " 2. What are functions? How do you use them? Where can you get them?\n", 950 | " 3. What kinds of data collections are built-in to Python? What concepts do they represent?\n", 951 | " 4. What is a \"logical expression\"? What is a \"state change\"?\n", 952 | " 5. What is the \"for...in\" statement about? How do you use it?\n", 953 | " 6. What is the \"if\" statement about? How do you use it?\n", 954 | " 7. Would you like a coffee break?" 955 | ] 956 | } 957 | ], 958 | "metadata": { 959 | "kernelspec": { 960 | "display_name": "Python 3", 961 | "language": "python", 962 | "name": "python3" 963 | }, 964 | "language_info": { 965 | "codemirror_mode": { 966 | "name": "ipython", 967 | "version": 3 968 | }, 969 | "file_extension": ".py", 970 | "mimetype": "text/x-python", 971 | "name": "python", 972 | "nbconvert_exporter": "python", 973 | "pygments_lexer": "ipython3", 974 | "version": "3.7.4" 975 | } 976 | }, 977 | "nbformat": 4, 978 | "nbformat_minor": 4 979 | } 980 | -------------------------------------------------------------------------------- /Day2_Encapulation/Python Syntax 6.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Encapsulating Code with Functions\n", 8 | "Just as we've learned that Python does a lot of work \"under the hood\"--translating its \"syntactic sugar\" into many lines of Python, interpreting that code into the C language, then compiling it into machine language, and so on--we too can wrap our code into simple functions, thereby making those lines easier to understand, debug, and reuse. The basic unit of encapsulation in programming is called a **function**, and they can mostly be thought of as a pipeline:\n", 9 | "\n", 10 | "**Input(s) -> [Some Code] -> Output(s)**\n", 11 | "\n", 12 | "In Python, a function is any object that is **callable** (i.e. can use the parentheses after the name). When **calling** functions, Python's syntax is like so:\n", 13 | "\n", 14 | "```python\n", 15 | "output = some_code(input1, input2)\n", 16 | "```\n", 17 | "\n", 18 | "Creating a function is done using the same code block style we've seen in for-loops and if-loops:\n", 19 | "\n", 20 | "```python\n", 21 | "def some_code(input1, input2):\n", 22 | " return output\n", 23 | "```\n", 24 | "\n", 25 | "Functions can take as many inputs as you want them to have. They can even have **optional** inputs, which have **default** values. In Python, all optional inputs must appear at the end of the list of functions, like so:\n", 26 | "\n", 27 | "```python\n", 28 | "def some_code(input1, input2, input3=42, input4=\"hello):\n", 29 | " return output\n", 30 | "```\n", 31 | "\n", 32 | "Functions are essential to programming because they **simplify** them! Good functions are simple to reason about, help provide an **abstraction** of that code's purpose, and help **teach** about the nature of the tasks that code is carrying out, and are **testable** (i.e. they can be checked to see if they are working properly).\n", 33 | "\n", 34 | "Let's build some and get a feel for them!" 35 | ] 36 | }, 37 | { 38 | "cell_type": "markdown", 39 | "metadata": {}, 40 | "source": [ 41 | "**Exercises**: Build functions that carry out that requested task, then use them on the given data." 42 | ] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "metadata": {}, 47 | "source": [ 48 | "**Example**: Write a function that returns the sum of two numbers. Use it to add x and y below:" 49 | ] 50 | }, 51 | { 52 | "cell_type": "markdown", 53 | "metadata": {}, 54 | "source": [ 55 | "Function:" 56 | ] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "execution_count": 2, 61 | "metadata": {}, 62 | "outputs": [], 63 | "source": [ 64 | "def add(a, b):\n", 65 | " return a + b" 66 | ] 67 | }, 68 | { 69 | "cell_type": "markdown", 70 | "metadata": {}, 71 | "source": [ 72 | "Data:" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 3, 78 | "metadata": {}, 79 | "outputs": [], 80 | "source": [ 81 | "x = 3\n", 82 | "y = 5" 83 | ] 84 | }, 85 | { 86 | "cell_type": "markdown", 87 | "metadata": {}, 88 | "source": [ 89 | "Calling function on data:" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 4, 95 | "metadata": {}, 96 | "outputs": [ 97 | { 98 | "data": { 99 | "text/plain": [ 100 | "8" 101 | ] 102 | }, 103 | "execution_count": 4, 104 | "metadata": {}, 105 | "output_type": "execute_result" 106 | } 107 | ], 108 | "source": [ 109 | "add(x, y)" 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": 7, 115 | "metadata": {}, 116 | "outputs": [ 117 | { 118 | "data": { 119 | "text/plain": [ 120 | "[]" 121 | ] 122 | }, 123 | "execution_count": 7, 124 | "metadata": {}, 125 | "output_type": "execute_result" 126 | } 127 | ], 128 | "source": [ 129 | "a, b, *c = (1, 2)\n", 130 | "c" 131 | ] 132 | }, 133 | { 134 | "cell_type": "markdown", 135 | "metadata": {}, 136 | "source": [ 137 | "**Exercise 1:** Write a function that returns the absolute (positive) difference between two numbers." 138 | ] 139 | }, 140 | { 141 | "cell_type": "markdown", 142 | "metadata": {}, 143 | "source": [ 144 | "Function:" 145 | ] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "execution_count": 47, 150 | "metadata": {}, 151 | "outputs": [], 152 | "source": [ 153 | "def abs_difference(x, y):\n", 154 | " ...\n" 155 | ] 156 | }, 157 | { 158 | "cell_type": "markdown", 159 | "metadata": {}, 160 | "source": [ 161 | "Data: " 162 | ] 163 | }, 164 | { 165 | "cell_type": "code", 166 | "execution_count": 49, 167 | "metadata": {}, 168 | "outputs": [], 169 | "source": [ 170 | "a = 25\n", 171 | "b = 65" 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": null, 177 | "metadata": {}, 178 | "outputs": [], 179 | "source": [] 180 | }, 181 | { 182 | "cell_type": "markdown", 183 | "metadata": {}, 184 | "source": [ 185 | "**Exercise 2:** Write a function that returns the square root of the sum of squares of two numbers" 186 | ] 187 | }, 188 | { 189 | "cell_type": "markdown", 190 | "metadata": {}, 191 | "source": [ 192 | "Function:" 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": null, 198 | "metadata": {}, 199 | "outputs": [], 200 | "source": [] 201 | }, 202 | { 203 | "cell_type": "markdown", 204 | "metadata": {}, 205 | "source": [ 206 | "Data:" 207 | ] 208 | }, 209 | { 210 | "cell_type": "code", 211 | "execution_count": null, 212 | "metadata": {}, 213 | "outputs": [], 214 | "source": [ 215 | "x = 5\n", 216 | "y = 4" 217 | ] 218 | }, 219 | { 220 | "cell_type": "code", 221 | "execution_count": null, 222 | "metadata": {}, 223 | "outputs": [], 224 | "source": [] 225 | }, 226 | { 227 | "cell_type": "markdown", 228 | "metadata": {}, 229 | "source": [ 230 | "**Exercise 3:** Write a function that returns \"Positive\" if their difference is positive, and \"Negative\" if their difference is negative" 231 | ] 232 | }, 233 | { 234 | "cell_type": "markdown", 235 | "metadata": {}, 236 | "source": [ 237 | "Function:" 238 | ] 239 | }, 240 | { 241 | "cell_type": "code", 242 | "execution_count": null, 243 | "metadata": {}, 244 | "outputs": [], 245 | "source": [] 246 | }, 247 | { 248 | "cell_type": "markdown", 249 | "metadata": {}, 250 | "source": [ 251 | "Data:" 252 | ] 253 | }, 254 | { 255 | "cell_type": "code", 256 | "execution_count": 50, 257 | "metadata": {}, 258 | "outputs": [], 259 | "source": [ 260 | "x = 10\n", 261 | "y = 5" 262 | ] 263 | }, 264 | { 265 | "cell_type": "code", 266 | "execution_count": null, 267 | "metadata": {}, 268 | "outputs": [], 269 | "source": [] 270 | }, 271 | { 272 | "cell_type": "markdown", 273 | "metadata": {}, 274 | "source": [ 275 | "**Exercise 4:** Write a function that returns **both** the sum of the first two inputs and the difference between the second and third input: " 276 | ] 277 | }, 278 | { 279 | "cell_type": "markdown", 280 | "metadata": {}, 281 | "source": [ 282 | "Function:" 283 | ] 284 | }, 285 | { 286 | "cell_type": "code", 287 | "execution_count": null, 288 | "metadata": {}, 289 | "outputs": [], 290 | "source": [] 291 | }, 292 | { 293 | "cell_type": "markdown", 294 | "metadata": {}, 295 | "source": [ 296 | "Data:" 297 | ] 298 | }, 299 | { 300 | "cell_type": "code", 301 | "execution_count": 52, 302 | "metadata": {}, 303 | "outputs": [], 304 | "source": [ 305 | "a = 10\n", 306 | "b = 15\n", 307 | "c = 20" 308 | ] 309 | }, 310 | { 311 | "cell_type": "code", 312 | "execution_count": null, 313 | "metadata": {}, 314 | "outputs": [], 315 | "source": [] 316 | }, 317 | { 318 | "cell_type": "markdown", 319 | "metadata": {}, 320 | "source": [ 321 | "**Exercise 5**: Write a function that adds two numbers together if the inputs are both numbers, and concatenates the inputs if they are both strings." 322 | ] 323 | }, 324 | { 325 | "cell_type": "markdown", 326 | "metadata": {}, 327 | "source": [ 328 | "Function:" 329 | ] 330 | }, 331 | { 332 | "cell_type": "code", 333 | "execution_count": null, 334 | "metadata": {}, 335 | "outputs": [], 336 | "source": [] 337 | }, 338 | { 339 | "cell_type": "markdown", 340 | "metadata": {}, 341 | "source": [ 342 | "Data:" 343 | ] 344 | }, 345 | { 346 | "cell_type": "code", 347 | "execution_count": 51, 348 | "metadata": {}, 349 | "outputs": [], 350 | "source": [ 351 | "x = \"Hello\"\n", 352 | "y = \"World\"" 353 | ] 354 | }, 355 | { 356 | "cell_type": "code", 357 | "execution_count": null, 358 | "metadata": {}, 359 | "outputs": [], 360 | "source": [] 361 | }, 362 | { 363 | "cell_type": "markdown", 364 | "metadata": {}, 365 | "source": [ 366 | "## Using Functions to Wrap Code\n", 367 | "\n", 368 | "In general, we want to make code as abstract as possible, but at the same time we should be specific about what we are trying to accomplish. These two goals--abstraction and specificity--are often at odds with each other. Encapsulation solves this problem by allowing us to put specific code inside function definitions, and abstract code in the code that calls it.\n", 369 | "\n", 370 | "Usually, the process of producing this code format follows three steps:\n", 371 | " 1. Write code that works. (Focus on the specifics)\n", 372 | " 2. Wrap it in a function. (Encapsulate it)\n", 373 | " 3. Call the function in your script (Abstract it)\n", 374 | " \n", 375 | " \n", 376 | "For example:\n", 377 | "```python\n", 378 | "data = [2, 6, 3, 7, 8, 9, 1]\n", 379 | "squares = []\n", 380 | "for el in data:\n", 381 | " square = el ** 2\n", 382 | " squares.append(square)\n", 383 | "sum_of_squares\n", 384 | "```\n", 385 | "\n", 386 | "will become:\n", 387 | "\n", 388 | "```python\n", 389 | "def sum_of_squares(data):\n", 390 | " squares = []\n", 391 | " for el in data:\n", 392 | " square = el ** 2\n", 393 | " squares.append(square)\n", 394 | " return sum(squares)\n", 395 | "\n", 396 | "\n", 397 | "data = [2, 6, 3, 7, 8, 9, 1]\n", 398 | "sum_of_squares(data)\n", 399 | "```\n", 400 | "\n", 401 | "Let's practice doing this with various types of loops:" 402 | ] 403 | }, 404 | { 405 | "cell_type": "markdown", 406 | "metadata": {}, 407 | "source": [ 408 | "**Exercises** Take the following working Python code and rewrite it so that it uses functions:" 409 | ] 410 | }, 411 | { 412 | "cell_type": "markdown", 413 | "metadata": {}, 414 | "source": [ 415 | "**Exercise 1**: The code below squares all of the numbers and removes all of the strings from the list. Make it into a function, **square_numbers**:" 416 | ] 417 | }, 418 | { 419 | "cell_type": "code", 420 | "execution_count": 71, 421 | "metadata": {}, 422 | "outputs": [ 423 | { 424 | "data": { 425 | "text/plain": [ 426 | "[25, 2916, 9, 36]" 427 | ] 428 | }, 429 | "execution_count": 71, 430 | "metadata": {}, 431 | "output_type": "execute_result" 432 | } 433 | ], 434 | "source": [ 435 | "data = [5, \"missing\", 54, \"bad\", 3, 6]\n", 436 | "good_data = []\n", 437 | "good_data_squared = []\n", 438 | "idx = 0\n", 439 | "while idx < len(data):\n", 440 | " el = data[idx]\n", 441 | " if isinstance(el, int):\n", 442 | " good_data_squared.append(el ** 2)\n", 443 | " idx += 1\n", 444 | "good_data_squared" 445 | ] 446 | }, 447 | { 448 | "cell_type": "markdown", 449 | "metadata": {}, 450 | "source": [ 451 | "Put the modified code below:" 452 | ] 453 | }, 454 | { 455 | "cell_type": "code", 456 | "execution_count": null, 457 | "metadata": {}, 458 | "outputs": [], 459 | "source": [] 460 | }, 461 | { 462 | "cell_type": "markdown", 463 | "metadata": {}, 464 | "source": [ 465 | "**Exercise 2**: The code below calculates the **standard deviation** of the data. Put it in a function called **standard_deviation** and use the function on the data:" 466 | ] 467 | }, 468 | { 469 | "cell_type": "code", 470 | "execution_count": 62, 471 | "metadata": {}, 472 | "outputs": [ 473 | { 474 | "data": { 475 | "text/plain": [ 476 | "2.7726341266023544" 477 | ] 478 | }, 479 | "execution_count": 62, 480 | "metadata": {}, 481 | "output_type": "execute_result" 482 | } 483 | ], 484 | "source": [ 485 | "import math\n", 486 | "\n", 487 | "data = [2, 6, 8, 2, 5, 8, 9, 2]\n", 488 | "\n", 489 | "mean = 0\n", 490 | "std = 0\n", 491 | "for el in data:\n", 492 | " mean += el / len(data)\n", 493 | "dev_squareds = []\n", 494 | "for el in data:\n", 495 | " dev = (el - mean) ** 2\n", 496 | " dev_squareds.append(dev)\n", 497 | "sum_dev_squareds = sum(dev_squareds)\n", 498 | "standard_dev = sum_dev_squareds / len(data) * 1.\n", 499 | "standard_dev = math.sqrt(standard_dev)\n", 500 | "standard_dev" 501 | ] 502 | }, 503 | { 504 | "cell_type": "markdown", 505 | "metadata": {}, 506 | "source": [ 507 | "Put the modified code below:" 508 | ] 509 | }, 510 | { 511 | "cell_type": "code", 512 | "execution_count": null, 513 | "metadata": {}, 514 | "outputs": [], 515 | "source": [] 516 | }, 517 | { 518 | "cell_type": "markdown", 519 | "metadata": {}, 520 | "source": [ 521 | "**Exercise 3:** The code below generates a **bootstrap** sample of the data, getting a random selection of the data **boot_n** times and calculating the mean of that sample, so that many estimates of the mean can be made from a single dataset. Put it in a function called **bootstrap_means**." 522 | ] 523 | }, 524 | { 525 | "cell_type": "code", 526 | "execution_count": 68, 527 | "metadata": {}, 528 | "outputs": [ 529 | { 530 | "data": { 531 | "text/plain": [ 532 | "[5.909090909090909,\n", 533 | " 5.636363636363637,\n", 534 | " 4.090909090909091,\n", 535 | " 5.7272727272727275,\n", 536 | " 3.909090909090909]" 537 | ] 538 | }, 539 | "execution_count": 68, 540 | "metadata": {}, 541 | "output_type": "execute_result" 542 | } 543 | ], 544 | "source": [ 545 | "import random\n", 546 | "\n", 547 | "data = [2, 6, 8, 2, 5, 8, 9, 2, 6, 2, 10]\n", 548 | "n_boot = 5\n", 549 | "means = []\n", 550 | "for rep in range(n_boot):\n", 551 | " sample = random.choices(data, k=len(data))\n", 552 | " mean = sum(sample) / len(sample)\n", 553 | " rep = rep * 2\n", 554 | " means.append(mean)\n", 555 | "means\n" 556 | ] 557 | }, 558 | { 559 | "cell_type": "markdown", 560 | "metadata": {}, 561 | "source": [ 562 | "Put the modified code below:" 563 | ] 564 | }, 565 | { 566 | "cell_type": "code", 567 | "execution_count": null, 568 | "metadata": {}, 569 | "outputs": [], 570 | "source": [] 571 | }, 572 | { 573 | "cell_type": "markdown", 574 | "metadata": {}, 575 | "source": [ 576 | "**Exercise 4**: The code below generates a labeled figure showing the difference in mean survival rate between two datasets, the *experimental* treatment and the *control* treatment. Put it in a function called **plot_treatment_effects**" 577 | ] 578 | }, 579 | { 580 | "cell_type": "code", 581 | "execution_count": 81, 582 | "metadata": {}, 583 | "outputs": [ 584 | { 585 | "data": { 586 | "image/png": "iVBORw0KGgoAAAANSUhEUgAAA3QAAAEWCAYAAAAuM9XDAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAIABJREFUeJzt3XfYLHV5//H3hyJdUTk2EI+KYrABHrBgFEWNYokaI7YYK0ZN1J8miMTYosYWg12xYUEUC0ZFESxIUIqASBErglQ5iEiVev/+mO8je5an7J5z9uzuc96v63quZ3fqvTsz9849852ZVBWSJEmSpOmzzrgDkCRJkiStHAs6SZIkSZpSFnSSJEmSNKUs6CRJkiRpSlnQSZIkSdKUsqCTJEmSpCllQSdJmmhJ9k/y5nHHIWnxSvLMJIcNOOxzkhw1wli2TfKTJJcledmo5rOqkmyd5PIk6447loUkOSLJC8Ydx6hY0K1GbaWe+bshyVU97585gvl9NskbVvd0B5z3NknmfYhhkjcnqSQv6ev+r637a0cb5eQa57KTZpPkGUmOb/nq/CTfSvLg1TDdkRZjbceqkry7r/sTW/f9RzXvaZZkaft+1ht3LBpckjOTXJNki77uJ7XluXQNxzMx69FsO+xJdk1yziDjV9UBVfWoUcUypL2A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j3/j159oWtSeZRwLZbuDkpXV9XZSQ6iayL0K+AnKzG5o4G30e3EHAkcPOT4H6NrhnRikgDL6e4A1+uDwKeSnNxiPBn40wLT/Tzw0SQvA55SVb8ZMi5JY2B+krQYVVUleSLwP0n2ossnVwCvBjbqG/xguiaTP6U707ZXVV0AsEBe3CDJsXQnbp4+qs+iyZEbW4FobZPkvsBHq2rnVZzOrsC/VtVIL7pNsi6wflX9ud0R7rvA3avqmlHOV9KaZ36SpOElORNYVlUXjTsWrTmeoVtLtVt6vwx4xbhjGcLGwPdbs6cAL3ZnSVp8zE+SJA3OM3SSJEmSNKW8KYokSZIkTSkLOkmSJEmaUhZ0kiRJkjSlLOgkSZIkaUpZ0EmSJEnSlPr/oullS9oL4HEAAAAASUVORK5CYII=\n", 587 | "text/plain": [ 588 | "
" 589 | ] 590 | }, 591 | "metadata": { 592 | "needs_background": "light" 593 | }, 594 | "output_type": "display_data" 595 | } 596 | ], 597 | "source": [ 598 | "import statistics\n", 599 | "import matplotlib.pyplot as plt\n", 600 | "\n", 601 | "treatment = [7, 8, 6, 5, 8, 4, 3, 5, 8, 7, 5, 6, 9, 4, 8]\n", 602 | "control = [5, 7, 3, 6, 5, 7, 3, 5, 2, 5, 7, 4, 6, 5, 3, 4, 2]\n", 603 | "fig, (ax1, ax2, ax3) = plt.subplots(ncols=3, figsize=(15, 4))\n", 604 | "ax1.hist(treatment, bins=5);\n", 605 | "ax1.set(title='Treatment Measuremnts', xlabel='Jump Height', ylabel='Frequency')\n", 606 | "ax2.hist(control, bins=5)\n", 607 | "ax2.set_title('Control Measurements')\n", 608 | "ax2.set_xlabel('Jump Height')\n", 609 | "ax2.set_ylabel('Frequency')\n", 610 | "treatment2 = [el ** 2 for el in treatment]\n", 611 | "control2 = [el ** 2 for el in treatment]\n", 612 | "mean_treatment = sum(treatment) / len(treatment)\n", 613 | "mean_control = sum(control) / len(control)\n", 614 | "x = [\"Treatment\", \"Control\"]\n", 615 | "heights = [mean_treatment, mean_control]\n", 616 | "ax3.bar(x, heights)\n", 617 | "ax3.set(title=\"Mean Jump Height of Experimental Groups\", xlabel=\"Group\", ylabel=\"Jump Height\")\n", 618 | "fig;\n" 619 | ] 620 | }, 621 | { 622 | "cell_type": "markdown", 623 | "metadata": {}, 624 | "source": [ 625 | "Put the modified code below:" 626 | ] 627 | }, 628 | { 629 | "cell_type": "code", 630 | "execution_count": null, 631 | "metadata": {}, 632 | "outputs": [], 633 | "source": [] 634 | }, 635 | { 636 | "cell_type": "markdown", 637 | "metadata": {}, 638 | "source": [ 639 | "## Refactoring Code: Improving it without breaking it\n", 640 | "\n", 641 | "By modularizing code, we give ourselves the ability to modify small parts of the code without having to worry about the rest of it--so long as the inputs and outputs don't change, anything that happens in the middle doesn't matter to the code that calls the function!\n", 642 | "\n", 643 | "The exercises above all contain things that can be improved, whether that is to make them simpler, more readable, or more reliable, any improvements are helpful. Let's work through them again and make the following improvements:\n", 644 | "\n", 645 | " 1. **Remove Orphan Code**: Often, code that isn't actually used by the function is left sitting there, lost and forgotten. Deleting those lines will make it easier to see how everything works and improve readability.\n", 646 | " 2. **Change variable names to something clearer**: variables like x and y are not helpful. Make them something that represents that result of the line!\n", 647 | " 3. **Reduce the number of steps**: If there are several lines doing something you think is simple, either compress them to a single line or make a new function that represents that action. If you know that the function you want already exists in another package, then import that package and use it!\n", 648 | " 4. **Convert While loops to For loops**: If you see iteration happening, use a for-loop! If it's a single action, why not make it a comprehension?\n", 649 | " \n", 650 | "**Exercises** Refactor the functions created in the previous section. Make sure to re-run the code with each change you make to verify that it still works!" 651 | ] 652 | }, 653 | { 654 | "cell_type": "markdown", 655 | "metadata": {}, 656 | "source": [] 657 | } 658 | ], 659 | "metadata": { 660 | "kernelspec": { 661 | "display_name": "Python 3", 662 | "language": "python", 663 | "name": "python3" 664 | }, 665 | "language_info": { 666 | "codemirror_mode": { 667 | "name": "ipython", 668 | "version": 3 669 | }, 670 | "file_extension": ".py", 671 | "mimetype": "text/x-python", 672 | "name": "python", 673 | "nbconvert_exporter": "python", 674 | "pygments_lexer": "ipython3", 675 | "version": "3.7.4" 676 | } 677 | }, 678 | "nbformat": 4, 679 | "nbformat_minor": 4 680 | } 681 | --------------------------------------------------------------------------------