Python is a very powerful programming language with huge support from the community around it. The language is used everywhere, in application development, Artificial Intelligence, developing middleware, etc.

The huge number of AI libraries in combination with its powerful backend frameworks like Django and Flask makes Python number one choice for the upcoming Data Scientist and Machine Learning Engineers promising them the chance to create powerful web apps that will employ Artificial Intelligence having written the backend and the algorithms in one codebase.

Now, if you are a parent who looks into getting their kids into Python, you might want to choose the perfect course for them. Juni Learning is a perfect platform that will transform your children into great coders.

Also, here are tips for parents who look into preparing your kids for Harvard University and the best programming for kids tools.

In today’s article, we are going to show you, how you can draw with Python.

 

Also, we have a new private Facebook group where we are going to share some materials that are not going to be published online and will be available for our members only. The members will have early access to every new post we make and share your thoughts, tips, articles and questions. Become part of our private Facebook group now.
Join Laconicml Group

 

Drawing with Python

Beginner Coding Project: Python & Harry Potter

Interested in getting your child into computer programming but unsure of where to start? You’re not alone. Finding simple programming project ideas that interest pre-teens and teenagers can be tricky, especially when learning a programming language for the first time.

If your child has any interest in Harry Potter, this short tutorial will introduce them to programming with a simple, fun project. We will use Python, a popular programming language, to draw a symbol any Harry Potter fan will recognize: the Deathly Hallows. We’ll walk you through all of the steps below!

Getting set up

Programs can be written using many different software tools. Fortunately, one of the simplest to set up is Repl.it, which allows us to write, run, and save our code in the browser. Start by signing up for a repl.it account.

Now that you’re logged in, create your project by clicking on the +new repl button at the top of the page:

 

Repl.it

Repl.it

 

Choose the programming language Python (with Turtle) and name your project Deathly Hallows. Now, we’re ready to start writing some code!

 

Python (with Turtle)

Python (with Turtle)

 

Starting with Python Turtle

For this project, we are going to use the Python module turtle. In Python, modules are like add-on tools we use to give our programs extra functionality. In this project, we will use a turtle to help us draw graphics on the screen.

When we use turtle in Python, we create a Turtle object on our screen. Then, we issue commands in our code to the Turtle, by telling it to move, turn, and change color.

Thus, at the beginning of our code, we need to import the turtle module, and we need to create the Turtle object we will use. The Turtle can have any name, like a bob.

The name of the Turtle is called a variable, and variables in programming are always one word (no spaces) and usually start with a lowercase letter. Here is the code we start with to accomplish these two things:

 

import turtle
bob = turtle.Turtle()

Now, we can try giving our Turtle a command. Let’s try telling bob to move forward 200 steps. This is how we do that:

bob.forward(200)

 

Try running your code. There’s a line on your screen! We just gave bob the command to move forward by 200 pixels.

Wherever the Turtle moves, it drags a pen behind it, leaving a line. In programming, when we issue commands like this, we are calling a function.

Let’s say we want to have bob draw a square. The next thing we need to do is to turn left by 90 degrees. Following the same pattern as before, our code should now look like this:

 

bob.forward(200)
bob.left(90)

 

Notice that the Turtle’s head is now pointing down, as it is ready to move forward in a new direction! How do you think we would complete the code to draw a square?

 

bob.forward(200)
bob.left(90)
bob.forward(200)
bob.left(90)
bob.forward(200)
bob.left(90)
bob.forward(200)
bob.left(90)

 

Notice that our code is getting repetitive. We like to avoid making our programs repetitive because it makes them more prone to small mistakes (what if one of the numbers had been written incorrectly?). Luckily, it is very easy to repeat code in programming by using loops.

What specific code do we need to repeat here? The Turtle is going forward and turning right four times total. Thus, we need to repeat those two lines of code four times.

In Python, we use a for loop to repeat certain lines of code a certain number of times. To tell the computer we’re about to repeat some code, we start with the line for i in range(4):. The number 4 represents the number of times the code inside the loop is going to repeat, and it’s important to remember the colon at the end of the line. The code that goes inside the loop has to be tabbed or indented, so that the computer knows what to repeat, like this:

 

for i in range(4):
bob.forward(200)
bob.left(90)

 

Now we have an efficient program to draw a square! To make sure we really understand our code, let’s try to modify it to draw a triangle with three equal sides. Experiment with your code to get it right!

Hint: How many times do we need to repeat our code? How many sides does a triangle have? If you were walking in a triangular shape, how much would you need to turn at every corner?

Keep experimenting until you land on something like this:

 

for i in range(3):
bob.forward(200)
bob.left(120)

 

You’ve successfully created the first shape in our Deathly Hallows symbol!

Next, we will draw the circle. Let’s move bob to the middle of the bottom side of the triangle, where we will start drawing the circle. Write a line of code that tells the Turtle to move forward just slightly less than half of the length of your triangle side. For example, if each side of our triangle is 200 pixels, then we would write below our existing code:

 

bob.forward(95)

 

To draw the circle, we need to think of a circle as a shape with many very short sides. The steps are then similar to how we drew our square and triangle: draw a line, turn, draw a line, turn, etc. However, this time we need to cover 360 degrees with many shorter sides. To make the math simple, let’s repeat our code 36 times, turning 10 degrees each time. You can experiment with how far the Turtle moves forward each repetition to make the perfectly sized circle for your graphic:

 

for i in range(36):
bob.forward(10)
bob.left(10)

 

We’re almost there! The last piece we’re missing is the vertical line to the top. You should be able to experiment with our commands to move forward and turn to put this last line in place.

Hint: We need to move forward a tiny bit to put our Turtle right in the center of the bottom side of our triangle, turn the head to face up, and move forward to draw the vertical line. After some experimenting, our code to do so looks like this:

 

bob.forward(6)
bob.left(90)
bob.forward(172)

 

And here’s our result!

 

Drawing With Python Programming Language

 

The entire program is in full at the bottom of this post.

 

 

Learn More about Coding in Python

If you or your student enjoyed this project and would like to start building on what we’ve learned here to create more advanced designs and video games, what we offer at Juni may be a great fit for you. Juni is an online academy specializing in computer science.

We offer private coding classes for students ages 5-18 for all levels, starting from zero experience to the USA Computing Olympiad. The Deathly Hallows project we worked on here is the starting point of our Python Level 1course, aimed for students ages 11-18 who have taken our Scratch Level 2: Game Master course or equivalent. Our other courses cover programming languages like Java, HTML, and Javascript.

During our classes, our students work with one of our Juni instructors, from a top Computer Science university, to develop their coding skills. They work one-on-one or in groups through our project-based course curriculum, building on fundamental programming concepts, and developing logical thinking and problem-solving strategies.

Get started with our Admissions Team today to see what class is best for your child.

Final Program

Here is the code to draw the Deathly Hallows symbol in Python in full:

 

import turtle

bob = turtle.Turtle()
bob.speed(1000)

for i in range(3):
bob.forward(200)
bob.left(120)

bob.forward(95)

for i in range(36):
bob.forward(10)
bob.left(10)

bob.forward(6)
bob.left(90)
bob.forward(172)

 

Conclusion

We don’t have to continue telling you what Python is capable of, you know it yourself, and if you haven’t started learning it, this is a great opportunity.

Like with every post we do, we encourage you to continue learning, trying, and creating.

This article originally appeared on junilearning.com

Facebook Comments