Drawing Flowers in Python
Daniel Bourke
·
Beginner
·9y ago
Skills:
ML Maths Basics60%
Key Takeaways
The video demonstrates how to draw flowers in Python using the turtle library, defining functions to draw petals and flowers with specified parameters, and utilizing turtle movement and control to create various shapes and designs.
Full Transcript
okay so my task was to draw one of these shapes using Python and now I didn't exactly draw one of these shapes I just decided to draw some pretty flowers on my own which may be a bit easier than drawing something like that or that but nonetheless I'll show you what I drew anyway so we'll just move this off the screen here's my code I'll show you what it does and then I'll walk walk you through each of it step by step so we'll start off we'll turn the speed down a little bit let's make it eight save it and go up here and go run oh there's an error this is all live there we go maybe that'll work now so you've got a little turtle there using the Python draw function see that one drew a flower to start with didn't intentionally make it look like that then we got flower two and then it finishes up with a blue flower there we go you get the point so how did we get from this to this and now I think the graphics is going to be not responsive that's all right we can run it again so what do we start with import turtle now why do we import turtle because turtle is a library of let's just call it drawing a whole bunch of drawing codes that is already inbuilt to Python so a lot of people have done the work beforehand and if you download the program say Idol start a new project new file import turtle you'll then have access to all the functions related to turtle and what do they include well you can go to something like this summary of turtle methods interact interactive Python org and it shows you a whole bunch of total commands that you can use so how did we get ours to draw flowers so step one define a function to draw the petal now that's what DF stands for define I'm pretty sure it defines a function so we're defining a function called petal that takes in three parameters T are an angle and we'll get to those in a second so within our petal function we start a four loop so for I in range - and now range essentially what that means is it starts at zero and it counts two to right so zero one two it's got three three numbers in there and then for that range so each time for each of those zero one and two they want to use the turtle so T in this case stands for total this T up here is is total radius angle so circle we wanted to draw it at a radius of our and we'll get to where R comes into play in a moment at an angle of angle and then after after it's done that drawing a circle we want it to turn turtle to turn left so t dot left so just think of all the t's here and now i've typed this short as total so T equals total I could even comment that at the top T equals total not my codes not the cleaner stuff but I'm slowly learning and getting better getting better be every day I hope so T dot left at an angle of 180 degrees minus angle so that's our angle here because if we turned left at 180 degrees it would draw some weird shapes whereas we want it to turn left at a particular angle so we get the shape of a flower so we define the petal function next we define the actual flower function so this takes in a parameter T turtle n which is a specific amount of x we define that later on for the radius and the angle again and now it's going to be the same as these two values our angle and T the only thing different in here is the N it takes him and now the end we'll get to in a bit is the number of petals that we want our flower to have so for I in range n why are we using M because as I said that's the number of petals we want to use so that's how many times we want the petal to be drawn so in range n run the petal function so we're calling in petal here in within the flower function and we wanted to after it's after it's drawn an individual petal we wanted to turn left 368 of it see how the 60 degree is divided by N and now remember M was the number of petals that we wanted to draw so why is it 360 degrees divided by n well because we want our flower to eventually look like a circle so we divide the degrees in a local by the number of flower petals we want okay n and then eventually it will turn into a flower with the shape of a circle with the number of petals and making up that that circle and now we define movement here and so it takes in a T so the turtle and length which is how much we want it to move so a number of pixels on the screen and now I put this in here we'll get to that in a second to give the background color to give the screen a background color so window Eagles turtle dot screen again this could be just T if we wanted it to but we'll leave it as turtle for the time being turtle dot screen window dot background color BG color takes in a color here argument now for ambulatory now this could be blue but we won't do blue because our flower one of our flowers is blue and so when we run the move function we want the turtle pen PU so what does PU stand for so if we come back to here PU is short for pen up so picks the turtles pin up so it won't when the pens up it won't draw similar to pen down puts the turtles pen down because turtle essentially remember is just a drawing function and it's it's a pen so PD pen down drawing PU and up not drawing so we want to pick the pen up to move the turtle forward a specified length and then to put the pen down and start drawing so here's where we define our turtle now I've called mine Sam this could be whatever you want it could be Bob could be Charlotte the name doesn't really matter we're just giving the variable a name and now Sam eagles turtle turtle and what does this mean well we're calling we're calling in the turtle function up here from the turtle Marie to say that the variable Sam will be a total aka a drawing drawing function and now we can adjust some of this is where we get the the turtle or the pen to start drawing and now we can find the the speed of the drawing and the speed I think I'm not sure I tried it at different speeds I tried a hundred and it goes very fast so we'll try that in a second but essentially whyme is the fastest slowest zero won't move at all but for this sake to make it fast we'll go a hundred and now to draw our first flower we've got Sam color green now you can probably guess that this means that the this flower is going to be green because we're calling the turtle we're giving it a value of green and now this Sam dot shape turtle this could if we delete this let me just show you what that comes out is so see how there was an arrow just roaring that went really fast because I hadn't on 100 speed if we if we go back to having turtle there it'll use a turtle to draw the flowers not an arrow now I'm not sure why this this keeps crashing but let's get out of there alright so move Sam negative 150 now that means that from the start I'm calling the move function here and I'm putting in Sam which is the turtle and then the amount I want it to be off center so it's going to move along the x-axis negative 150 pixels from the center so what does that look like well when we draw out green flower see how the green one's not in the center the red ones in the center because we're shifting it across from the start so we're moving it from the start because the turtle when it when it starts by default will start in this Center and then why are these two we have a red one that's drawn with outline a blue one that's shown with outline and then a green one that's drawn with fill well because on the green one we have these two commands here begin fill and fill but we'll get to those in a second let's talk about this flower command here flower function more more would be more correct term so we go up here def flower define flower function takes in a parameter turtle and an angle so here's what we're doing here we're we're entering these values with God flower takes in T value Sam with an N value of 7 with an R value of 60 and an angle value of 60 so see there T Sam and seven sixty point zero angle sixty point zero and so let's have a look how these relates to our drawings so we go over here seven will be the N so essentially it's going to draw it's going to run this petal function here n amount of times so what do you think if we were to run the petal drawing function seven times we should result with we should end up with seven petals right so one two three four five six seven perfect that's what we want now what what happens here what's the R so that's the radius so that's 60 60 pixels so the radius here of of a flower petal is 60 pixels and then the angle it turns is also 60 and so that's that's how we get this curvature up here rather than it being a circle because remember up here we're taking in an angle and why why does it complete why all of all the petals and even even spacing around each other well because remember we've add that when we finish drawing each individual pedal we run T dot left at an angle of 360 degrees divided by n so what is that so if we go here 360 divided by 751 so each one of these pedals is at an angle of 51 degrees and then we repeat the process here for flap the red flower and the blue flower except the red flower has ten petals and the blue flower has 14 petals and they have the red flower is as a radius of 40 pixels and the blue flower has a radius of 70 pixels so what happens if we change this number to like a hundred and let's say this one to like 25 on the red flower let's have a look what happens there we go so now the flower has 25 petals as you can see and each of them is a has a radius of a hundred pixels and but the angle is still at 80 so what if we change this to maybe 30 and let's see what happens there we go it turns a lot tighter let's change this to 30 maybe I got the angle and radius mixed up ah it's the width of the pedal there we go that's cool so across so rather than from here to here it's from here to here now have we covered everything oh yes the filth so why is this filled in and this is not well with with the turtle function there's a begin fill and an end fill command as you'll find over here on this summary of total methods so begin fill remember the starting point for a fill of polygons okay where did you start drawing and fill close the polygon and fill with the current fill color so the end fill and why do we run it why do we run these functions above and below the flower function well because begin fill is going to remember where the turtle initially started so let's turn this right down to let's see one and in the end fill is going to remember where let's go a bit higher that's Mary - we don't want to be waiting - all and then the end fill is going to be it's going to remember the final point of where the turtle was and then it's going to backfill the entire drawing with the color you had been using so in our case green but let's what's for demonstrations sake just change this to blue just so you can see that the color can change so nice and slow the turtle started off in the middle point it's going to draw seven petals here because we've got seven here it's going to draw each of them with a radius of 60 ah there we go the end fill so right on the last point it remembers where it finished and where it started and fills the rest of this shape with with the color you were drawing it so let's let's do the same for the other two sam dot begin the end and see how it's going very slow it's because we've changed our speed to 2 for this next demonstration I'll make sure I put it up to like a hundred or something like that again all right now if this all works correctly we should have all of our flowers filled up with their specific color let's change this one back to green and let's run for one last time nice and fast I'll look at that pretty pattern there we go so that is a quick and easy way of drawing some shapes in Python using the turtle feature defining a whole bunch of functions so we've got one two three functions here and then we're calling them flower we're calling and adding in the parameters here and within the flower function we're calling the petal function which we've defined up here and then before the flower function starts to to go we're calling the move function and so everything works from top to bottom so it's going to start off by defining Sam because if you imagine there's a line here we define all about rules first and then we start to write what we want what we want to happen so we define Sam as the turtle okay the drawing parameter and give it a speed and then we give it a color give it a shape now this shape can be a number of different things turtle is a fun one we call the move function so rather than the green flower starting in the center we move it across a little bit and then we begin the fill in the position where the turtle ends up so negative 150 pixels from the center and then it starts to draw the flower with these parameters that we've defined up here and then it ends the fill and colors the flower in green then we repeat the process for the red in the blue flower and we're done we get some some pretty shapes so that's a that's my brief explanation or not not fairly brief I'm finding this video it's kind of last 20 minutes now but that's that's essentially how how I did this project on Udacity and how did I do it well I looked into like a whole bunch of googling you can see I've got some websites here with Python code example of how to use turtle and then I just chopped and changed the code to suit me but the project asked me to draw one of these shapes and I ended up with wherever they've gone it's lagging out but yeah hope you gained some value out of this if you haven't well then I'll keep making more videos and hope you get your stuff out of that but I'm rambling now so I'm gonna get back to the next project and oh I'll record that when I'm doing it and then we'll we'll see where we end up so and catch you later thanks for watching
Original Description
Disclaimer: I'm only a beginner programmer so what I say in the video should not be taken as gospel. I'm just uploading what I learn. Everything is my interpretation. I'm open to any advice.
I've been working on the Programming Foundations with Python course on Udacity.
One of the example tasks was to create a python program that would draw an object using turtle (a built-in drawing feature of python).
In this video, I run through the code I created during the lessons.
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Daniel Bourke · Daniel Bourke · 3 of 60
1
2
▶
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Xbox One S Unboxing and Xbox One Comparison
Daniel Bourke
Text/Profanity Checker in Python
Daniel Bourke
Drawing Flowers in Python
Daniel Bourke
Finding The Right Medium - TDBS 18 April 2017
Daniel Bourke
What Is Neuralink??! - TDBS 22 April 2017
Daniel Bourke
Disagree and Commit, Words of Wisdom from Jeff Bezos - TDBS 19 April 2017
Daniel Bourke
A Lesson In Movement | Raw Training Australia
Daniel Bourke
FALLING IS FUN | Functional Friday 4
Daniel Bourke
My first HACKATHON! | 100 Days of Code 1
Daniel Bourke
MORE MACHINE LEARNING | 100 Days of Code 2
Daniel Bourke
TensorBoard and learning from Einstein | 100 Days of Code 3
Daniel Bourke
Job Interview Tips and Open Ocean Swim | 100 Days of Code 4
Daniel Bourke
I Want To Help 100,000 People Workout | AI Powered Personal Trainer
Daniel Bourke
MACHINE LEARNING IN 5 MINUTES
Daniel Bourke
COFFEE, YOGA and AWS | 100 Days of Code 5
Daniel Bourke
MY FIRST STARTUP WEEKEND | 100 Days of Code 6
Daniel Bourke
GENERATING TV SCRIPTS WITH DEEP LEARNING | 100 Days of Code 7
Daniel Bourke
Attention, please
Daniel Bourke
TEACHING BOTS TO PLAY GAMES | 100 Days of Code 9
Daniel Bourke
Udacity Deep Learning Nanodegree Language Translation Project Submission | 100 Days of Code 10
Daniel Bourke
Learning about Generative Adversarial Networks on Udacity | 100 Days of Code 11
Daniel Bourke
Completing Andrew Ng's Machine Learning Course on Coursera | 100 Days of Code 12
Daniel Bourke
Finishing the Treehouse Python Track | 100 Days of Code 13
Daniel Bourke
GENERATING FACES WITH GANs | 100 Days of Code 14
Daniel Bourke
Graduating From the Udacity Deep Learning Nanodegree | 100 Days of Code 15
Daniel Bourke
WHAT I'VE LEARNED FROM TALKING TO PEOPLE
Daniel Bourke
3 Life Principles I Learned From Ray Dalio
Daniel Bourke
PYTHON && POETRY | 100 Days of Code 16
Daniel Bourke
Physique Update and 6 Things I Wish I Knew Before Starting Gym
Daniel Bourke
The 100 Days is Over! | 100 Days of Code 17
Daniel Bourke
How to Burn Over 100 Calories in 4 Minutes
Daniel Bourke
Solving Sudoku with AI | Learning Intelligence 1
Daniel Bourke
Upper Body Calisthenics Workout in the Park
Daniel Bourke
What is an Adversarial Search Agent? | Learning Intelligence 2
Daniel Bourke
My Self-Created Artificial Intelligence Master's Degree | Learning Intelligence 0
Daniel Bourke
Try Going Over It Again | Learning Intelligence 3
Daniel Bourke
Python and Pullups | Learning Intelligence 4
Daniel Bourke
AI Meets Blockchain! | Learning Intelligence 5
Daniel Bourke
How to Pass the Turing Test + I FAILED | Learning Intelligence 6
Daniel Bourke
Biology and Physics meet Computer Science | Learning Intelligence 7
Daniel Bourke
Udacity Artificial Intelligence Nanodegree Project 3 Progress | Learning Intelligence 8
Daniel Bourke
Passing Project 3 of Udacity's Artificial Intelligence Nanodegree | Learning Intelligence 9
Daniel Bourke
Bayes Networks, Hidden Markov Models and How I Wake Up | Learning Intelligence 10
Daniel Bourke
Udacity AI Nanodegree Progress and Bayes' Rule Explained | Learning Intelligence 11
Daniel Bourke
Udacity AI Nanodegree Project 4 Planning and Progress | Learning Intelligence 12
Daniel Bourke
Finishing Term 1 of Udacity's Artificial Intelligence Nanodegree | Learning Intelligence 13
Daniel Bourke
deeplearning.ai Progress! | Learning Intelligence 14
Daniel Bourke
Coursera Deep Learning Specialization Progress | Learning Intelligence 15
Daniel Bourke
Computer Vision Basics + More deeplearning.ai Progress! | Learning Intelligence 16
Daniel Bourke
My Experience at CodeCamp, Intro to Keras and Failing Hard | Learning Intelligence 17
Daniel Bourke
In-Depth Udacity Deep Learning Nanodegree Review
Daniel Bourke
Completing the Deeplearning.ai Specialization on Coursera | Learning Intelligence 18
Daniel Bourke
You're Never Too Young to Start Learning AI - Learning Intelligence Talks with Shaik Asad
Daniel Bourke
Starting Term 2 of the Udacity Artificial Intelligence Nanodegree | Learning Intelligence 19
Daniel Bourke
Submitting the Computer Vision Capstone Project | Udacity AI Nanodegree | Learning Intelligence 20
Daniel Bourke
Leg Day at World Gym Northlakes ft. Ben Jones Fitness
Daniel Bourke
deeplearning.ai Sequence Models Course Progress | Learning Intelligence 21
Daniel Bourke
Graduating from the deeplearning.ai Coursera Specialization | Learning Intelligence 22
Daniel Bourke
Udacity Artificial Intelligence Nanodegree NLP Concentration Progress | Learning Intelligence 23
Daniel Bourke
Learning How to Build What's Next at Google Cloud On Board Brisbane
Daniel Bourke
More on: ML Maths Basics
View skill →
🎓
Tutor Explanation
DeepCamp AI