 So thanks for coming to my talk. I'm really happy to be here at EuroPython and just share some Python-y things with you. So my name is Hailey Dunbraver and I'm a web developer. I think I became a developer because deep inside my heart, I'm a nerd. And deep inside that nerd is a developer who deep down is a nerd. And I live in San Diego. So Scotland is an experience. In San Diego, it's nice, about 99% of the year. And I'm a big fan of nature and of hiking. If it's so nice outdoors, you should get outdoors. And just things like that. It's all amazing and beautiful from the little water ripples where the ocean meets the beach to the waves out in the distance. It all has a similar sense of wonder. So at my job, I work primarily with Python using Django. The best part of my job involves training great non-technical employees to learn Python and Django and to join our dev team. And recently, we've had them use the Python Turtle module for some exercises. It allows them to draw with code and it's pretty cool. And here's their finished work. They are signed, placed on the fridge, and these are drawings of Pusheen the cat. And it was super fun. So now you know a bit about me, but you've also been introduced to our topic, which is namely recursion, fractals, and the Python Turtle module. So wait, we've been introduced to those. Well, let's break it down a little bit. And we'll tackle recursion first. So side note, I just added this yesterday. You can go see a visual representation of recursion at the camera obscura attraction near the castle. Using mirrors, they created this ladder that apparently tunnels through the earth all the way to Australia, but you use it at your own risk. So recursion is like when I said that I'm a developer who's deep down a nerd, who's a developer because she's a nerd. So inside each part of this description is a description of itself and down and down and down. You can think of it a little bit like this picture of the Russian nesting dolls. So what is recursion? Well, according to my very favorite learn to code resource, which is called how to think like a computer scientist, recursion is a method of solving problems that involves breaking a problem down into smaller and smaller sub problems until it can be solved trivially. The recursion we'll look at involves a function calling itself. It's a problem within a problem within a problem. So it's probably easiest to get the sense of recursion by looking at an example. And we're going to look at a recursive function to solve factorials. So brief explanation review of math and factorial is going to be equal to the product of all positive integers less than or equal to itself. So here we have five factorial and that's going to equal five times four times three times two times one, which happens to be 120. But inside five factorial is four factorial and inside that is three factorial and then two factorial and then one factorial. And you can see by the color coding on this slide, it helps you see how this is kind of like the Russian nesting dolls, right? So we're going to dig down to the trivial problem, which is going to be one factorial and then we're going to resurface easily solving problems along the way. Here is some sample code to solve the factorial problem. And it's written recursively. Now, a recursive function has three features that we're going to explore. First is the recursive case, which you can see here is where I have if num is greater than one. Now, in the recursive case, the function calls itself. We see here we're returning num times recursion factorial of num minus one. Now, secondly, we have the base case. The base case here is our else statement where we're returning one. Now, this part of the function will not call itself. It's the trivial problem of one factorial. It's the tiniest doll in the nesting set won't call the function. It's there because if it's not, this program will just keep running and then we all die. And so we have to make sure it's in place or else it's just it's not usable. So the third feature that I want to talk about is the third feature is going to move toward the base case. So you can see this here where recursion factorial is called, but it's called for one less than it was previously called. And so it's going to move us closer and closer to being equal to one. So by the way, anything you can program recursively, you can also write a program that won't use recursion. So here's an example of what the factorial problem might look like solved not recursively. It will work. It works fine. It's maybe a little less elegant than the recursive function. All right. So that's recursion. And let's talk about fractals. Fractals are a phenomenon in both math and nature. A fractal is a never-ending pattern with self-similarity across scales. So this is a gif of a caulk snowflake. And no matter how much we zoom, it's going to look the same. It may be at 10x scale. It may be at 1x scale. It may be at .1x scale. We don't know. And they all look the same. And so this is an example of a mathematical fractal. But we see them all around us in nature too. It's kind of like how I mentioned that in San Diego, the waves out in the ocean had a similar beauty to the water ripples where the ocean meets the sand. Other examples could include lightning. See the bolts there where the little tributaries look very much the same as the main branch. These broccoli, which I think are really cool. Each little prick will look generally the same as the full head of broccoli. And of course, real snowflakes, not just caulk snowflakes. And just as an aside, I spent a few days in London before making my way here. And I found this example of a fractal in the Baker Street Underground Station. Sherlock Holmes's silhouette is made up of tiny Sherlock Holmes's silhouettes. One could imagine that the tiny silhouettes are also made out of tinier silhouettes and on and on and on. All right. So we have recursion and we have fractals. But how are these connected? Well, fractals can be described with recursive functions. Yep. And that's cool. But can we experience this in our real lives? Well, I'm going to introduce you to my favorite Python friend, the Python Turtle Module. So what is the Turtle Module? It's a cool, it's a cool bit of code included in the standard library. Before the concept was used in, before it was used in Python, it was used in the logo programming language and this dates back to 1966. So it's been around a long, long time. And what we're going to do is we're going to imagine that a turtle is placed on a big canvas. Its tail has been dipped in paint. And when the turtle moves around the canvas, it draws a line. The turtle is also perfectly trained. So you, the programmer, can tell the turtle, okay, move forward 20 steps, rotate to the left, 90 degrees, et cetera, et cetera. And with just simple instructions, we can create really interesting patterns. So we're going to draw, I like to call this a spirograph. I don't know if European people had those toys growing up. Yeah. So it's very similar to that. And we're going to instantiate a turtle. And it's going to execute some code that's in a loop. And so you'll see it draw a triangle, rotate, and then repeat the process. So let's pull that up. So we're watching it go. Now it's kind of fun about this. I've sped it up so it's kind of a turtle on coffee. But that way we can see it easy. So what's fun about this is you might be teaching someone to code and they don't really understand the concept of a loop. Maybe you're covering that. Well, if you have them program a turtle, you can actually see it execute. And it helps take kind of out their spacey ideas and gives them a concrete shape and form that people can relate to. All right. Going to pull up the slides again. All right. So that's basically the feel of the Python turtle module. But what the really exciting thing we're going to do is, is to draw a fractal with the Python turtle module. So we're going to, let's see. So we're going to draw this triangle. It's called the Sierpinski triangle. And you can see what our turtle drawing is basically going to look like when we're finished. And I'm going to show you the code first and we can talk about it a little. And then we'll watch the turtle run. So I want to draw your attention to a couple of interesting things in this code. And the first of those being that it's necessary to specify how many layers deep we want to go with this triangle. If I had a sharp enough pencil and a lot of patience, I could just keep drawing triangles. You know, just making this finer and finer and finer. But we don't really want to write a program that'll do that. We want to give it a base case basically. We're going to specify how many layers deep to go and then that way we'll hit the base case and the recursion will stop. Now second, if I were drawing this by hand, I would probably follow reasoning similar to the GIF here where I draw one big triangle and then draw little triangles in basically this pattern. However, this is not how the turtle is going to do this. You can see in the code, it calls the Sierpinski function three times. And the way you can kind of think about that is if you had those Russian nesting dolls and you opened one of them, instead of finding just another Russian nesting doll that has nested dolls inside of it, you would find two or three Russian nesting dolls inside this Russian nesting doll. And each of those three would have other dolls inside of them and on and on. So you don't necessarily go layer by layer, but you dig down to the very bottom of one of the nesting dolls, resolve it, and then move back up. And then you move back down through a different nesting doll. And we're going to see that as the code executes. So I'm going to pull that up again. Hold tight. All right, are you guys ready? Let's go. Now this one is super sped up, but you can see it went down to the very bottom, the base level, and executed completely before coming back up and addressing some of the other triangles. And we're going to show you a schematic that'll kind of hopefully make that make a little bit more sense. So here's our code. And here's the final product. And I've color coded some of the levels so you can get an idea of the recursive function calling itself and then calling itself, right? So the little orange triangle is the smallest and is the base case, and the larger ones are a couple levels up. Now, what have we learned? Well, we've learned that recursion is awesome because it's recursive, which is awesome. And we've learned that pythons and turtles are friends. We've also learned that fractals can be mind-blowing even when you're entirely sober. So what can you do with this info? Well, first, if there's someone in your life that wants to learn to code, might be a child, a niece, nephew, son, daughter in your life. It might even be you if you're fairly new to the Python scene. If there's someone in your life who wants to learn to code, this is an awesome resource, and I highly recommend the Python Turtles. I think it's your answer to learning to code. At least a good introduction. Now, if that doesn't describe you, I would encourage you to write some recursive code. Maybe refactor something that you already wrote and make it recursive, or solve a problem on projectoiler.net recursively, or even attempt a fractal. It could be this Serpinski triangle, the Cox snowflake we saw earlier. I also have an example of a fractal tree on my GitHub, and the link will be included in the slides if you want to see that. And maybe you're not up for recursion, but I would still encourage you to give the Python Turtles a try, because it's refreshing to have fun with code, and I 100% certify that using the turtle module is fun. And if you do draw something, please tweet me a screenshot. I'll retweet it, retweet is a hard word. I'll retweet it and rate it out of 10. I use the dog rating method for evaluating Python Turtle creations. This method is where all will score above 10 out of 10, because just like all dogs are awesome, all Python Turtle creations are amazing. You can find me on Twitter here at haleydenby. And I guess that's a wrap. Relevant links will be tweeted, and I will share my slides with the EuroPython people. The slides have links to sources and to relevant information. You'll find them here. Just to give you more info on recursion, fractals, and the Python Turtle module. Thank you so much for listening, and you're all 14 out of 10, and I know I said no questions, but we can do questions. It changed my mind. We can rerun the turtles, too. Oh, we can definitely do pretty pictures. I appreciate a pretty picture. But if we leave that up, there will be a short break before the afternoon keynote that's back in here. So there's enough time for you to kind of head back out and come back in. And that's that. So, sit up. Yeah, there's a link to my GitHub repo on the slides, which I'm sharing with EuroPython, and I'm tweeting out from my Twitter, Haileydenby. Thanks, everyone.