|
| 1 | +--- |
| 2 | +layout: intro |
| 3 | +theme: neversink |
| 4 | +color: bowdoin |
| 5 | +routerMode: hash |
| 6 | +favicon: https://avatars.githubusercontent.com/u/9260792 |
| 7 | +--- |
| 8 | + |
| 9 | +# Importing and Turtle |
| 10 | + |
| 11 | +<br> |
| 12 | + |
| 13 | +<hr><br> |
| 14 | + |
| 15 | +Christopher Martin - _Bowdoin College_ <a href="https://bowdoin.edu/" class="ns-c-iconlink"><mdi-open-in-new /></a> |
| 16 | + |
| 17 | +<twemoji-framed-picture v-drag="[602,83,144,144]" /> |
| 18 | +<twemoji-turtle v-drag="[722,70,215,215]" /> |
| 19 | +<twemoji-artist-palette v-drag="[658,212,77,77]" /> |
| 20 | +<twemoji-paintbrush v-drag="[747,255,54,54]" /> |
| 21 | + |
| 22 | +--- |
| 23 | +layout: section |
| 24 | +color: bowdoin |
| 25 | +--- |
| 26 | + |
| 27 | +### In today's class, we are going to be taking a short break from the fundamentals of programming to explore something more fun and interactive! |
| 28 | + |
| 29 | +<twemoji-star-struck v-drag="[830,397,90,90]" /> |
| 30 | + |
| 31 | +--- |
| 32 | +layout: section |
| 33 | +color: bowdoin |
| 34 | +--- |
| 35 | + |
| 36 | +### So far, our programs have been written in one file |
| 37 | + |
| 38 | +<br> |
| 39 | + |
| 40 | +### This is not super realistic as programs often consist of hundreds, thousands, and even <u>millions</u> of lines of code! |
| 41 | + |
| 42 | +<twemoji-face-screaming-in-fear v-drag="[848,388,90,90,10]" /> |
| 43 | + |
| 44 | +--- |
| 45 | +layout: top-title |
| 46 | +color: bowdoin-title |
| 47 | +--- |
| 48 | + |
| 49 | +:: title :: |
| 50 | + |
| 51 | +# Too much code! |
| 52 | + |
| 53 | +:: content :: |
| 54 | + |
| 55 | +<br> |
| 56 | + |
| 57 | +## It would be almost impossible to maintain a codebase that big in one huge file, _especially_ if multiple people were working on it together! |
| 58 | + |
| 59 | +<br> |
| 60 | +<br> |
| 61 | + |
| 62 | +## It makes it very difficult to share / reuse code since we would need to constantly copy-paste it into every new project we have! |
| 63 | + |
| 64 | +<br> |
| 65 | +<br> |
| 66 | +<br> |
| 67 | +<br> |
| 68 | + |
| 69 | +## To fix these issues, we need a way to split our programs up across multiple files and somehow reference them in one another! |
| 70 | + |
| 71 | +<twemoji-angry-face v-drag="[861,288,90,90]" /> |
| 72 | + |
| 73 | +--- |
| 74 | +layout: top-title |
| 75 | +color: bowdoin-title |
| 76 | +--- |
| 77 | + |
| 78 | +:: title :: |
| 79 | + |
| 80 | +# `import` Statements |
| 81 | + |
| 82 | +:: content :: |
| 83 | + |
| 84 | +<br> |
| 85 | + |
| 86 | +# `import` statements allow us to tell Python to include code from other files in our code when we execute our programs! |
| 87 | + |
| 88 | +<br> |
| 89 | + |
| 90 | +### With them, you can split your own programs into multiple, more focused, files and it allows you to reuse those files by referencing them in future programs as a ==library==. |
| 91 | + |
| 92 | +<br> |
| 93 | +<br> |
| 94 | + |
| 95 | +## Additionally, you can also use ==libraries== written by other people! |
| 96 | + |
| 97 | +<br> |
| 98 | + |
| 99 | +### Python is actually _very_ good at this and it is why it is so commonly used in web development, data science, AI, and so many other areas! |
| 100 | + |
| 101 | +--- |
| 102 | +layout: top-title |
| 103 | +color: bowdoin-title |
| 104 | +--- |
| 105 | + |
| 106 | +:: title :: |
| 107 | + |
| 108 | +# `import` Statements in Action |
| 109 | + |
| 110 | +:: content :: |
| 111 | + |
| 112 | +#### We can reference things from these external files by ==qualifying== them with the name of the file (without `.py`) followed by a period and the thing we want to access. |
| 113 | + |
| 114 | +```python {monaco-run} {autorun:true, editorOptions: { lineNumbers:'on', fontSize:14}} |
| 115 | +# Here we ask Python to 'include' the built-in math library. |
| 116 | +import math # We don't add a ".py" here! |
| 117 | + |
| 118 | +print(f"e is equal to {math.e}") |
| 119 | + |
| 120 | +degrees: float = 270.0 |
| 121 | +print(f"{degrees}° in radians is {math.radians(degrees) / math.pi}π") |
| 122 | + |
| 123 | +a: int = 30 |
| 124 | +b: int = 20 |
| 125 | +print(f"The gcd of {a} and {b} is {math.gcd(a, b)}") |
| 126 | +print(f"The lcm of {a} and {b} is {math.lcm(a, b)}") |
| 127 | +``` |
| 128 | + |
| 129 | +--- |
| 130 | +layout: section |
| 131 | +color: bowdoin |
| 132 | +--- |
| 133 | + |
| 134 | +# Now, let's look at the `turtle` library! |
| 135 | + |
| 136 | +<br> |
| 137 | + |
| 138 | +### Turtle is a built-in graphics library that allows us to draw things to the screen! |
| 139 | + |
| 140 | +<twemoji-framed-picture v-drag="[611,314,144,144]" /> |
| 141 | +<twemoji-turtle v-drag="[738,287,215,215]" /> |
| 142 | +<twemoji-artist-palette v-drag="[686,450,77,77]" /> |
| 143 | +<twemoji-paintbrush v-drag="[768,475,54,54]" /> |
0 commit comments