How to Create a Flower With Python
By The Fullstack Academy Team
Spring is a time of new beginnings and growth. With the changing of the season comes a chance for you to take your first steps toward gaining the skills to flourish in tech. But, where to begin sowing the seeds of your tech future? If you are interested in starting a tech career, training bootcamps and programming exercises are great for expanding your knowledge and skills. Not every tech role uses programming, however, many lucrative and fast-growing fields in tech do. For example, employers in data analytics and web development fields are seeking professionals with programming skills. Learning programming can be beneficial for career changers and upskillers alike.
What is Python?
Python is considered a general language that can be used to make a wide variety of programs and isn't specialized for any specific purpose. That’s also one of its biggest strengths–you can make an entire web ecosystem and never leave Python! For example, Python can be used for:
Building AI and machine learning algorithms
Back-end web application development
Constructing other programming languages
Financial quantitative and qualitative analysis
To celebrate spring and new beginnings, you can try out a basic programming exercise on how to build a flower using Python. Throughout the exercise, you will learn some key Python concepts and functions. Keep in mind that Python is case-sensitive, so you’ll want to input information exactly as displayed in the steps. Your final image should look similar to this flower:
Steps to Building a Flower With Python
Before beginning this flower programming exercise, download and install Python on your computer. Python is an open-source programming language that everyone can download for free. Python can be downloaded from the official website. Be sure to select the latest Python download that matches your operating system and follow the download instructions on their website.
After you have downloaded and installed the application on your computer, you’ll open a blank Python interpreter window, also known as a “shell,” by following these steps:
Double-click the IDLE icon in your Python download folder on your computer to open the shell. In Python, the Integrated Development Environment (IDLE) is the standard development environment. Programmers who work with Python may use other development environments and code editors, but for simplicity this exercise uses IDLE.
With IDLE open, save your shell by selecting “File,” then “Save As…” from the top navigation window.
Name your Python shell file “flower” and save the file to your preferred location on your computer.
After your Python application installation and shell file setup is complete, it is time to create a virtual canvas and start building your flower.
Step 1: Import Turtle Python Library
Turtle comes pre-installed in Python and allows users to create images and shapes on a virtual canvas using a command. To access turtle in the Python library, simply type the following into the shell and press “enter” on your keyboard. Pressing “enter” will submit the characters to the machine.
Using “import” is a command or statement to bring a module into your Python code.
Step 2: Setup Turtle Virtual Canvas
Part 2.1: Opening the Turtle Virtual Canvas
Next, you’ll move on to opening a turtle virtual canvas. In your “shell,” type the below directly under the “import turtle” command. These lines contain variables, which give data to the computer for processing. A variable is created the moment you first assign a value to it.
t = turtle.Turtle()
s = turtle.Screen()
After the commands above are entered in the “shell,” a new window will pop up on your computer. This is your turtle virtual canvas. You’ll notice a cursor in the middle of the page. This exercise will not allow you to draw in turtle with your computer cursor or mouse. The cursor on the virtual turtle canvas is only an indicator of where the drawing will start. You’ll continue to program in the shell and your flower image will appear on the turtle virtual canvas when you complete the final steps of this exercise.
Part 2.2: Change Your Canvas Background Color
You can change the background color of your turtle virtual canvas by entering this command in the shell and typing the name of a basic color inside the quotation marks:
To have the color of your flower appear more vibrant, we recommend typing “black” into the text string. In programming, a text string, or text for short, is a cluster of characters, usually inside quotation marks. After typing in the code, press “enter” and your turtle virtual canvas background color will change to match the color you indicated inside of the command.
Part 2.3: Setting Up Your Pen
Use the code below to program the “pen" you'll be using to draft your flower. You will be setting the size of the pen, the speed at which it will draw, and the radius and color of the circle it will produce.
radius = 60
color = ['red','white','red']
The drawing speed, size, and radius of the pen can vary, however, we recommend the programming above for a clear image. The colors of your pen can also vary. You can change the colors of the circle the pen will produce by typing out the name of the color inside of the brackets in the code string.
An unordered collection of unique items like how the colors are listed in commas and brackets in the exercise is called a “set” in programming. Be sure to use quotation marks around the color name and a comma in between each color. For this exercise, you’ll want to use three or more colors. Your colors do not have to repeat, for example, using red twice like in the view above.
Step 3: Creating Your Petals
Now that the turtle virtual canvas, pen, and circle radius and colors are set, the last few steps will be to program the circle pattern in the shell to create the petals of your flower using the text below.
This step utilizes the Python range( ) function and loops. The term “function" refers to a piece of organized, repeatable code that performs a single, specific action. Creating shapes, like a circle, is also considered a function. “Loops” like for x in range(12): are used to run a block of statements repeatedly until a given condition is satisfied for value in a sequence. In this example, once our code has run 12 times, it stops. We wouldn’t want it to keep drawing forever!
for x in range(12):
for i in range(8):
radius = radius + 4
If you are using more than three colors, you’ll want to replace the number 3 in t.color(color[x%3]) to the number of colors you are using. This will incorporate the colors you indicated in the second step into the drawing.
For the radius = radius + 4 placement line you can either backspace or use Ctrl+[ on your keyboard to unindent your string and align it with the color string. From there, you’ll tab “enter” twice on your keyboard and your flower should appear on your virtual turtle canvas.
Your final shell should look like this:
Feel free to experiment with different colors and add your own code. That is what makes programming so fun!