|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Classes\n", |
| 8 | + "Classes combine code someone else made to create objects. A class, is imported in a package, like ```import time```. Time is a package that has functions. TCLab package has functions created with ```tclab.TCLab()```. Next lesson will show you how to use its functions." |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "markdown", |
| 13 | + "metadata": {}, |
| 14 | + "source": [ |
| 15 | + "# Objects\n", |
| 16 | + "Objects are created to represent a class that has usable functions. You use a variable format to create an object, assigned to a name, such as ```lab = ```. This is called a parent to child relationship. Naming an object is called this because the child inherents all the functions that the parent had. The child object is what you use in your code. You can name objects the name that you will remember and that are easy to type. For tclab, we prefer to use ```lab```, but it's not required. " |
| 17 | + ] |
| 18 | + }, |
| 19 | + { |
| 20 | + "cell_type": "markdown", |
| 21 | + "metadata": {}, |
| 22 | + "source": [ |
| 23 | + "## Explanation for Objects\n", |
| 24 | + "```python\n", |
| 25 | + "lab = tclab.TCLab()\n", |
| 26 | + "```\n", |
| 27 | + "\n", |
| 28 | + "The first part of an object is what you are naming it. For the Temperture Control Kit, we usually choose ```lab``` to name our object. For something like your incubator, you could make an object to connect with it, called something like ```tempcontrol```. \n", |
| 29 | + "\n", |
| 30 | + "The second part after ```=```, goes into the downloaded package called ```tclab```. Then it looks for and uses ```TCLab``` to connect with your tclab. \n", |
| 31 | + "\n", |
| 32 | + "The ```.``` makes the program look for a sub-object, and the ```()``` tells python it is a function. This a function with no input arguments, because no value is put in the parentheses. Functions will be taught next lesson. \n", |
| 33 | + "\n", |
| 34 | + "So ```lab``` or whatever you decide to call it, is assigned to the programming already made found in the package:\n", |
| 35 | + "```python\n", |
| 36 | + "tclab.TCLab()```" |
| 37 | + ] |
| 38 | + }, |
| 39 | + { |
| 40 | + "cell_type": "markdown", |
| 41 | + "metadata": {}, |
| 42 | + "source": [ |
| 43 | + "### Try and Except\n", |
| 44 | + "Try and Except methods are simple. The program will attempt to run the code under ```try```, and if it can't it goes to the ```except``` code. This is useful in a number of situations, such as importing packages you may not have installed." |
| 45 | + ] |
| 46 | + }, |
| 47 | + { |
| 48 | + "cell_type": "code", |
| 49 | + "execution_count": null, |
| 50 | + "metadata": {}, |
| 51 | + "outputs": [], |
| 52 | + "source": [ |
| 53 | + "try:\n", |
| 54 | + " # Attempted code\n", |
| 55 | + "except:\n", |
| 56 | + " # If try code fails to run" |
| 57 | + ] |
| 58 | + }, |
| 59 | + { |
| 60 | + "cell_type": "markdown", |
| 61 | + "metadata": {}, |
| 62 | + "source": [ |
| 63 | + "### Install Temperature Control Lab\n", |
| 64 | + "This code will first ```try``` and import tclab. If it can't do that for some reason it uses ```except``` and installs the program that will let you use your Temperature Control Kit. Run the program to install TCLab. If installing TCLab is not working for you and the connection test below isn't working, try going [here](https://apmonitor.com/pdc/index.php/Main/ArduinoSetup) for additional instruction.\n", |
| 65 | + "\n", |
| 66 | + "(If you want to install this package just for your user, not with admin privileges, keep the code ```--user``` in lines 6 and 8)" |
| 67 | + ] |
| 68 | + }, |
| 69 | + { |
| 70 | + "cell_type": "code", |
| 71 | + "execution_count": null, |
| 72 | + "metadata": {}, |
| 73 | + "outputs": [], |
| 74 | + "source": [ |
| 75 | + "# install tclab\n", |
| 76 | + "try:\n", |
| 77 | + " import tclab\n", |
| 78 | + "except:\n", |
| 79 | + " # Needed to communicate through usb port\n", |
| 80 | + " !pip install --user pyserial\n", |
| 81 | + " # The --user is put in for accounts without admin privileges\n", |
| 82 | + " !pip install --user tclab \n", |
| 83 | + " # restart kernel if this doesn't import\n", |
| 84 | + " import tclab" |
| 85 | + ] |
| 86 | + }, |
| 87 | + { |
| 88 | + "cell_type": "markdown", |
| 89 | + "metadata": {}, |
| 90 | + "source": [ |
| 91 | + "### Connection Test\n", |
| 92 | + "This object is what allows you to connect with the kit, read current temperatures, adjust heaters, or change the LED brightness. In the next lesson we will go over the basics of what this temperture control kit can really do. \n", |
| 93 | + "\n", |
| 94 | + "Plug in your TCLab if it's available. Now try running and the program will create the object from below. Now if tclab package is installed and imported, it should connect, tell you additional information about your TCLab, then disconnect. If it doesn't work, or comes up with an error, go to the file __useful for tclab__ for help.\n", |
| 95 | + "\n", |
| 96 | + "Don't worry if this is confusing, most of it will be explained, next lesson." |
| 97 | + ] |
| 98 | + }, |
| 99 | + { |
| 100 | + "cell_type": "code", |
| 101 | + "execution_count": null, |
| 102 | + "metadata": {}, |
| 103 | + "outputs": [], |
| 104 | + "source": [ |
| 105 | + "import tclab # Imported Package\n", |
| 106 | + "lab = tclab.TCLab() # Object that connects with the kit\n", |
| 107 | + "lab.close() # Disconnects with the Lab" |
| 108 | + ] |
| 109 | + }, |
| 110 | + { |
| 111 | + "cell_type": "markdown", |
| 112 | + "metadata": {}, |
| 113 | + "source": [ |
| 114 | + "## Turtle Package\n", |
| 115 | + "Turtle package will help you learn about objects. Read the comments for explanations of the code. If the window isn't closing, try restarting the kernel by pressing 0 twice." |
| 116 | + ] |
| 117 | + }, |
| 118 | + { |
| 119 | + "cell_type": "code", |
| 120 | + "execution_count": null, |
| 121 | + "metadata": {}, |
| 122 | + "outputs": [], |
| 123 | + "source": [ |
| 124 | + "import turtle # Needed for the turtle package\n", |
| 125 | + "\n", |
| 126 | + "whateverScreen = turtle.Screen() # This creates a screen, the variable can be whatever you want it to be\n", |
| 127 | + "whateverTurtle = turtle.Turtle() # This creates a turtle, the variable can be whatever you want it to be\n", |
| 128 | + "\n", |
| 129 | + "whateverScreen.exitonclick() # Closes turtle window when clicked" |
| 130 | + ] |
| 131 | + }, |
| 132 | + { |
| 133 | + "cell_type": "markdown", |
| 134 | + "metadata": {}, |
| 135 | + "source": [ |
| 136 | + "As you can see, a seperate window opens (made with the screen object) that has a arrow on it (made with the turtle object). Try naming your screen and your turtle something you want. Now that we can make our objects, we need to do something with them. \n", |
| 137 | + "\n", |
| 138 | + "**Here are some simple turtle functions:**\n", |
| 139 | + "\n", |
| 140 | + "Moving\n", |
| 141 | + "```python\n", |
| 142 | + "yourTurtle.forward(number) # Moves turtle forward whatever the number is\n", |
| 143 | + "yourTurtle.backard(number) # Moves turtle backward whatever the number is\n", |
| 144 | + "```\n", |
| 145 | + "\n", |
| 146 | + "Turning\n", |
| 147 | + "```python\n", |
| 148 | + "yourTurtle.right(number) # Turns turtle right whatever the number is\n", |
| 149 | + "yourTurtle.left(number) # Turns turtle left whatever the number is\n", |
| 150 | + "```\n", |
| 151 | + "\n", |
| 152 | + "**And a screen function:**\n", |
| 153 | + "```python\n", |
| 154 | + "yourScreen.exitonclick() # Closes screen if you click it\n", |
| 155 | + "```\n", |
| 156 | + "\n", |
| 157 | + "Go to the file __basic python knowledge__ if you want more turtle functions. Get more practice by running this program and changing it to your liking. You may have to restart the Kernel everytime you run this." |
| 158 | + ] |
| 159 | + }, |
| 160 | + { |
| 161 | + "cell_type": "code", |
| 162 | + "execution_count": null, |
| 163 | + "metadata": {}, |
| 164 | + "outputs": [], |
| 165 | + "source": [ |
| 166 | + "import turtle # Needed for the turtle package\n", |
| 167 | + "\n", |
| 168 | + "nameYourScreen = turtle.Screen() # This creates a screen, the variable can be whatever you want it to be\n", |
| 169 | + "nameYourTurtle = turtle.Turtle() # This creates a turtle, the variable can be whatever you want it to be\n", |
| 170 | + "\n", |
| 171 | + "nameYourTurtle.right(40) # Turns turtle right whatever the number is\n", |
| 172 | + "nameYourTurtle.forward(150) # Moves turtle forward whatever the number is\n", |
| 173 | + "\n", |
| 174 | + "nameYourScreen.exitonclick() # Closes screen if you click it" |
| 175 | + ] |
| 176 | + }, |
| 177 | + { |
| 178 | + "cell_type": "markdown", |
| 179 | + "metadata": {}, |
| 180 | + "source": [ |
| 181 | + "## Activity\n", |
| 182 | + "Make a turtle, and a background, both named whatever you want. Then make a square that's at least 100 pixels long for each side length. Make sure you have ```exitonclick()``` at the end to stop the program when it's done. You may have to restart the Kernel everytime you run this." |
| 183 | + ] |
| 184 | + }, |
| 185 | + { |
| 186 | + "cell_type": "code", |
| 187 | + "execution_count": null, |
| 188 | + "metadata": {}, |
| 189 | + "outputs": [], |
| 190 | + "source": [ |
| 191 | + "import turtle" |
| 192 | + ] |
| 193 | + } |
| 194 | + ], |
| 195 | + "metadata": { |
| 196 | + "kernelspec": { |
| 197 | + "display_name": "Python 3", |
| 198 | + "language": "python", |
| 199 | + "name": "python3" |
| 200 | + }, |
| 201 | + "language_info": { |
| 202 | + "codemirror_mode": { |
| 203 | + "name": "ipython", |
| 204 | + "version": 3 |
| 205 | + }, |
| 206 | + "file_extension": ".py", |
| 207 | + "mimetype": "text/x-python", |
| 208 | + "name": "python", |
| 209 | + "nbconvert_exporter": "python", |
| 210 | + "pygments_lexer": "ipython3", |
| 211 | + "version": "3.7.3" |
| 212 | + } |
| 213 | + }, |
| 214 | + "nbformat": 4, |
| 215 | + "nbformat_minor": 2 |
| 216 | +} |
0 commit comments