Categories
Python Programming

Learn Python in One Week (May Edition)

It’s Never too Late to Learn a New Skill! Learn to Code

Anyone can learn to code regardless of age, degree or gender.

Always wanted to become a coder? Here is a chance for you to fulfill your dream.

Join Truston Ailende for one week of instensive Python programming for absolute beginners.

If you are ready to learn how to code, click the link below:

https://bit.ly/33ImVXL

Categories
Python Turtle

How to Draw a Bicycle using Python Turtle

This tutorial covers how to draw a bicycle using Python Turtle. To draw with code in the Python programming language we can use the Python Turtle module which comes by default with the Python interpreter.

Drawing a bicycle using the Python Turtle module involves the following steps:

  1. Get an image of the bicycle you want to draw
  2. Get a grid for the image of the bicycle
  3. Combine the grid and the image of the bicycle into a single image
  4. Draw centre lines on the image of the bicycle
  5. Draw the bicycle using Python Turtle

Get an Image

A picture of a bicycle cannot be drawn with code. Rather, an icon should be used.

A suitable source of icon is The Noun Project. The landing page of the site is shown below:

The Noun Project Landing Page

Login or Join. This will enable you to download your icon. Type in the kind of image you want as shown below:

Icon Search

Pressing the search icon will bring out search items as shown below:

Search Results

Scroll down until you find a suitable icon then you click on it. This will take you to the screen shown below:

Selected Image

Click on the Get this icon green button. It will display the screen shown below:

Features

Select the Basic Download checkbox. The button will now turn green as shown below:

Basic Download

Click on the Continue button. This will show the screen shown below:

Final Download

Click on the PNG button. It will download an icon of image type PNG.

Get a Grid

To draw the icon, you need to use a grid. The grid allows you to get the coordinates of the points that you want to draw.

A good online tool to us is the Grid Drawing Tool. The landing page is shown below:

Grid Drawing Tool

Clicking on the start button will take you to the screen shown below:

Upload Screen

Click on the Choose file button. Navigate to the location of the image you downloaded on your machine.

The uploaded screen should look as shown below:

Uploaded Image

Clicking the Next button takes the user to the Rotate screen shown below:

Rotate Screen

Click the Next button to go to the Crop Screen which is shown below:

Crop Screen

Click on the Next button to get to the next screen.

Adjust Screen

Click on the Next button to get the grid. This is shown below:

Grid

Adjust the Grid dimensions to 20 and a line style of 1 pixels. Click the apply grid button as shown below:

Final Grid

Click on the Download button to get the grid image.

Combine the Grid and Image

Once you have your grid and image, you need to combine them into a single image.

To do this you can use any image editing software. However, in my case, I shall use Paint.NET.

Open the image in Paint.NET as shown below:

Paint.NET User Interface

Next import the grid as a layer. To do this, go to the Layers menu and click on Import From File… as shown below:

Import From File

When you import the grid, you will notice that the number of layers becomes two as shown below:

Two Layers

Now click on the Magic Wand. This will enable you to make the black areas transparent. The shortcut for the Magic Wand is S.

Click on a square in and press delete. This will give you the result shown below:

Upper Square

Increase the tolerance slightly until it spreads across the layer as shown below:

Increased Tolerance

Now press the delete button on your computer. It will give you the result shown below:

Deleted Spots

Now reduce the tolerance to 50% and delete all the dark spots. The final image will look as shown below:

Cleaned Image

Now we flatten the image. This will reduce the number of layers to one. The way to do this shown below:

Flatten

Press the Save button to save your image. You can now close Paint.NET. The image is now as shown below:

Modified Image

Draw Centre Lines

Drawing the centre lines is a matter of using the paint software on the computer.

Open the image in Microsoft Paint as shown below:

Microsoft Paint

Reduce the size of the image to 50%. This will enable you see the entire image as shown below:

Image Scaled Down

Under shapes in Microsoft Paint, click on line shape and set its colour to red. Also choose the maximum size.

Now draw the centre lines from one end of the image to the other. This will be as shown below:

Centre Lines

Press Ctrl + S to save the image and close Microsoft Paint. The final image will look as shown below:

Modified Image with Centre Lines

Draw the Bicycle

The bicycle will be drawn in stages. We will start with the wheels and draw the rest of the shape.

To start we shall import the Python Turtle module to our code, make use of the delay function and increase the pen size of our turtle.

Our code will look as shown below:

import turtle
turtle.delay(10)
turtle.pensize(20)

The resulting image is shown below:

The turtle is at the centre of the screen

Next we move to the right of the left bicycle and set the heading o the bicycle to 90 degrees. The coordinate of that position is (-3.5, -2). We shall use a scale factor of 25 so the coordinates are (-87.5, -50).

The code to do this is shown below:

# Move the turtle to the right position of the left tyre
turtle.penup()
turtle.setposition(-87.5, -50)
turtle.setheading(90)

The resulting image is shown below:

Move the turtle to Left Tyre

Next we draw the circle. The radius of the circle is 2.5. Scaled by 25, this is 62.5.

The code to draw the circle is shown below:

# Draw the left tyre
turtle.begin_fill()
turtle.circle(87.5)
turtle.end_fill()

The resulting image is shown below:

Left Tyre Drawn

Next we draw the right tyre. The turtle is moved to the right side of the right tyre and then, the circle is drawn. The position we are trying to get to is (8.5, 2). Scaled up by 25, this is (212.5, -50).

The code to do this is shown below:

# Move the turtle to the right position of the right tyre
turtle.penup()
turtle.setposition(212.5, -50)
turtle.setheading(90)

The resulting figure is shown below:

Set Heading for Right Tyre

Now draw the right tyre like you did the left tyre. The code to do this is shown below:

# Draw the right tyre
turtle.begin_fill()
turtle.circle(62.5)
turtle.end_fill()

The generated image is shown below:

Right Tyre Drawn

Now on to drawing the handles. We draw the line from (5.5, 0) to (3, 6). Scaled up by 25, this will be from (137.5, 0) to (75, 150).

The code to do this is shown below:

# Draw the handle
turtle.penup()
turtle.setposition(137.5, 0)
turtle.pendown()
turtle.setposition(75, 150)

The generated image is shown below:

Drawing the Handle Bar

Next we draw the handle. The handle is made up of two parts: a straight part and a semicircle. The straight part will be drawn first.

To draw the straight part, we draw a line from the position (3, 6) to (5, 6). Scaled by 25, this is (75, 150) to (125, 150).

The code to do this is shown below:

# Draw the straight part of the handle
turtle.setheading(0)
turtle.setposition(125, 150)

The generated image is shown below:

Straight Handle

Now we draw the semicircle. The code to do this is shown below:

# Draw the circular part of the handle
turtle.penup()
turtle.setposition(125, 100)
turtle.pendown()
turtle.circle(25, 180)

The generated image is shown below:

Handle Completed

Next is to draw the seat. The icon shows a seat tube and saddle. We draw the seat tube first.

The code to do this is shown below:

# Draw the seat tube
turtle.penup()
turtle.setposition(12.5, -50)
turtle.pendown()
turtle.setposition(-62.5, 137.5)

The generated image is shown below:

Seat Tube

Next we draw the seat. The key to drawing the seat is to set the heading of the turtle to 180 degrees. Next we draw a line from the current location to the position (-100, 137.5).

The code to do this is shown below:

# Draw the saddle
turtle.setheading(180)
turtle.setposition(-100, 137.5)

The generated image is shown below:

Saddle Drawn

To complete the drawing of the bicycle, we will display the diagram of a bicycle which is shown below:

Labelled Bicycle Diagram

The parts that are remaining to be drawn are the top tube, down tube, seat stay and chain stay.

To draw the top tube we draw a line from the position (100, 75) to (-37.5, 75). The code to do this is shown below:

# Draw the top tube
turtle.penup()
turtle.setposition(100, 75)
turtle.pendown()
turtle.setposition(-37.5, 75)

The generated image is shown below:

Top Tube Drawn

To draw the down tube we draw a line from the position (100, 75) to (12.5, -50) . The code to do this is shown below:

# Draw the down tube
turtle.penup()
turtle.setposition(100, 75)
turtle.pendown()
turtle.setposition(12.5, -50)

The generated image is shown below:

Down Tube Drawn

To draw the seat stay we draw a line from the position (-37.5, 75) to (-150, -50). The code to do this is shown below:

# Draw the seat stay
turtle.penup()
turtle.setposition(-37.5, 75)
turtle.pendown()
turtle.setposition(-150, -50)

The generated image is shown below:

Seat Stay Drawn

To draw the chain stay we draw a from position (12.5, -50) to (-150, -50). The code to do this is shown below:

# Draw the chain stay
turtle.penup()
turtle.setposition(12.5, -50)
turtle.pendown()
turtle.setposition(-150, -50)

The generated image is shown below:

Chain Seat Drawn

At the end of this tutorial, we have successfully drawn a bicycle using the Python Turtle module.

The code for this tutorial is available in its entirety as a GitHub Gist.

Learn Python in One Week

If you want a quick and easy introduction to the Python programming language, you should check out my fifth book: Learn Python in One Week on the Amazon store.

Categories
Python Turtle

How to Draw a Car using Python Turtle

Drawing with code in the Python programming language involves using the Python Turtle module. Python Turtle comes by default with the Python interpreter.

This tutorial covers how to draw a car using the Python Turtle module. Drawing the car involves the following steps:

  1. Get an image of the car you want to draw
  2. Get a grid for the picture of the car
  3. Combine the grid and picture of the car into a single image
  4. Draw center lines on the image of the car
  5. Draw the car using the Python Turtle module

Get an Image of the Car

You cannot replicate a photo using Python Turtle so we need to find a suitable icon.

A great source for icons is The Noun Project. Once you visit the site, you simply search for the kind of image you are looking for.

Get a Grid

Drawing the car with Python Turtle would be impossible without a grid. The grid gives you the coordinates of the points of the image that you want to draw.

I couldn’t find a suitable grid maker for my purposes so I used the grid from the Grid Drawing Tool. You customise the grid you want to your requirements and it gives you the image you want.

Combine the Grid

The grid from the Grid Drawing Tool is then taken and made transparent. The grid and image to be drawn are then combined in such a way that the image is on top of the grid.

A simple software for this is Paint.NET. It is a free software for image editing.

Draw Center Lines

The Python Turtle module has it origins at the points (0, 0). So to draw a image, you need its center lines.

This is easily done using the Paint software available on Windows. The image will now look like this:

Car image with center lines

Draw the Car

The car will be drawn in stages. We shall start from the leftmost position and draw the composite shapes that make up the car until we are done.

To start we shall import the Python Turtle module to our code, make use of the delay function and increase the pen size of our turtle.

Our code will look as shown below:

import turtle
turtle.delay(10)
turtle.pensize(20)

The resulting image is show below:

The turtle is displayed at the center of the screen

Next we move the turtle to the extreme left position. On the image with the center lines, this is (-8, -3). We want to draw with a scale factor of 25 so any coordinate we get, we will multiply by 25. The code to do this is shown below:

# Move the turtle to the extreme left position
turtle.penup()
turtle.setposition(-200,-75)

The resulting image is shown below:

The turtle has moved to the leftmost position

The goal now is to draw a rectangle of length 400 and width 150. The code to do this is show below:

# Draw the rectangle
turtle.pendown()
turtle.forward(400)
turtle.setheading(90)
turtle.forward(150)
turtle.setheading(180)
turtle.forward(400)
turtle.setheading(-90)
turtle.forward(150)

The resulting image is shown below:

The rectangle has been drawn

Next the left tyre should be drawn. To do this, we move the turtle to the leftmost position of the tyre which is at (-7, -3) scaled up to (-175, -75) and we draw the tyre.

The code to do this is shown below:

# Draw the left tire
turtle.penup()
turtle.setposition(-175,-75)
turtle.pendown()
turtle.forward(50)
turtle.setheading(0)
turtle.forward(75)
turtle.setheading(90)
turtle.forward(50)

The resulting image is shown below:

Left tyre drawn

Next the right tyre is drawn. To do so, we move the turtle to the position (100, -75) and we draw the right tyre.

The code to do this is shown below:

# Draw the right tire
turtle.penup()
turtle.setposition(100,-75)
turtle.pendown()
turtle.setheading(270)
turtle.forward(50)
turtle.setheading(0)
turtle.forward(75)
turtle.setheading(90)
turtle.forward(50)

The generated image is shown below:

Right tyre drawn

Now we intend to draw the uppermost line. The left coordinate of the line is at (-5.5, 4). Scaled up, this is (-137.5, 200). The goal would be to draw a line from that point to the point (137.5, 200).

The code to do this is shown below:

# Draw the uppermost line
turtle.penup()
turtle.setposition(-137.5,200)
turtle.pendown()
turtle.setheading(0)
turtle.forward(275)

The generated image is shown below:

Topmost upper line drawn

Next we draw the second uppermost line. To do so, we move the turtle to the position (-150, 150) and draw a line of length 300 pixels.

The code to do this is shown below:

# Draw the second uppermost line
turtle.penup()
turtle.setposition(-150,150)
turtle.pendown()
turtle.setheading(0)
turtle.forward(300)

The resulting image is shown below:

Second uppermost line drawn

To complete the outline of the car, we need to draw the left and right slanting lines.

To draw the left slanting line, we simply draw a line from the tip of the uppermost line to where the left slanting line should touch the rectangle.

The code to do this is shown below:

# Draw the left slanted line
turtle.penup()
turtle.setposition(-137.5,200)
turtle.pendown()
turtle.setposition(-175,75)

The generated image is shown below:

Left slanting line drawn

To draw the right slanting line, we draw a line from the tip of the topmost line to the point where the right slanting line touches the rectangle.

The code to do this is shown below:

# Draw the right slanted line
turtle.penup()
turtle.setposition(137.5,200)
turtle.pendown()
turtle.setposition(175,75)

The generated image is shown below:

Right slanting line drawn

Now we can draw the car proper. The headlamps will be drawn first. The left headlamp is to be drawn first as a circle.

The code to do this is shown below:

# Draw the left circle
turtle.penup()
turtle.setposition(-125,-25)
turtle.pendown()
turtle.circle(25)

The generated image is shown below:

Left headlamp drawn

Next we draw the right headlamp. The code to do this is shown below:

# Draw the right circle
turtle.penup()
turtle.setposition(125,-25)
turtle.pendown()
turtle.circle(25)

The generated image is shown below:

Right headlamp drawn

Now the wipers are to be drawn. The left wiper will be drawn first. THe code to do this is shown below:

# Draw the left wiper
turtle.penup()
turtle.setposition(-87.5, 75)
turtle.pendown()
turtle.setposition(-50, 112.5)

The generated image is shown below:

Left wiper drawn

Next we draw the right wiper. The code to do this is shown below:

# Draw the right wiper
turtle.penup()
turtle.setposition(62.5, 75)
turtle.pendown()
turtle.setposition(100, 112.5)

The generated image is shown below:

Right wiper drawn

Finally the 4 horizontal lines are drawn. The code to do this is shown below:

# Draw the horizontal lines
turtle.penup()
turtle.setposition(-50, -37.5)
turtle.pendown()
turtle.setposition(-50, 0)
turtle.penup()
turtle.setposition(-15, -37.5)
turtle.pendown()
turtle.setposition(-15, 0)
turtle.penup()
turtle.setposition(15, -37.5)
turtle.pendown()
turtle.setposition(15, 0)
turtle.penup()
turtle.setposition(50, -37.5)
turtle.pendown()
turtle.setposition(50, 0)

The generated image is shown below:

Car completed

At the end of this tutorial, we have successfully drawn a car using the Python Turtle module in the Python programming language.

The code in its entirety is available as a GitHub Gist. You can find it here.

Learn Python in One Week

If you want a quick and easy introduction to the Python programming language, you should check out my fifth book: Learn Python in One Week on the Amazon store.

Categories
Flask

Enabling Python Virtualenv in Windows PowerShell

When installing Flask, you might want to create a unique environment for each project. To do this, you need to use a virtual environment.

On Visual Studio Code, type in the following in your Python shell, where env is the name of your virtual environment:

env\Scripts\Activate.ps1

Security Error

To correct the error above, type the code below into your PowerShell window:

Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser

Security Correction

As you can see from the above image, the virtual environment has now been created.

You can now install Flask.

Categories
Python Programming

Relational Operators

Relational operators check the relationship between two operands. They return true if the comparison or relation is true otherwise it will return false.

There are six comparison operators in the Python programming language. They are:

  1. Equal To
  2. Not Equal To
  3. Greater Than
  4. Less Than
  5. Greater Than or Equal To
  6. Less Than or Equal To

Equal To (==)

The equal to operator is represented by the double equal sign (==) in the Python programming language. The single equal sign (=) is used for assignment in the Python programming language.

The equal to symbol checks if two operands are equal.

Not Equal To (!=)

This is the opposite of the equal to symbol. It checks if the two operands are not equal.

Greater Than (>)

This checks if the left operand is greater than the right operand.

Less Than (<)

The less than operator is the opposite of the greater than operator. It checks if the operand on the left is less than the operand on the right.

Greater Than or Equal To (>=)

Checks if the left hand operand is either greater or equal to the right hand operand.

Less Than or Equal To (<=)

Checks if the left hand operand is either less or equal to the right hand operand.

Relational operators allow the programmer to define conditions in their code based on the relationship between two variables.