
Introduction
Hello everyone, welcome to copyassignment.com. In this tutorial, we are going to learn how to draw snowflakes using python turtle module. This article is super interesting and easy to understand. In this snowflakes article, we have drawn various small and large size snowflakes. For that, we have used the random module of Python turtle. We can create branches of various sizes at various random locations on the screen.
For the complete code, you can go to the bottom of the page you will get the entire code with output.
So let’s start
Import Turtle
import turtle
from random import randint
We have imported the turtle module so that we can access its inbuilt methods and functions. We used the random module an in-built module of Python to generate random numbers. we have used the randint() function, to print random values for a list or string.
Set the turtle Object and the Screen of Snowflakes using Python Turtle
p=turtle.Turtle()
p.shape("turtle")
#speed of turtle
p.speed(1500)
p.pensize(4)
scr=turtle.Screen()
#setting the background color & title
scr.bgcolor("SkyBlue1")
scr.title("Merry Christmas")
In this piece of code, we set the turtle object as p. Turtle shape is of “turtle”.The speed of the turtle is 1500. The screen background color is SkyBlue. Set the title of the screen.
Create the Snowflake function by setting the coordinates, color, and size of snowflakes
#function for setting the colour x and y coordinates and size of snowflake
def snowflake(color,x,y,size):
p.penup()
p.goto(x,y)
p.pendown()
p.color(color)
p.left(90)
#Set the for loop for create the snowflake branches
for i in range(0,branches):
p.forward(100*size/100)
p.backward(40*size/100)
p.left(40)
p.forward(30*size/100)
p.backward(30*size/100)
p.right(80)
p.forward(30*size/100)
p.backward(30*size/100)
p.left(40)
p.backward(40*size/100)
p.left(40)
p.forward(30*size/100)
p.backward(30*size/100)
p.right(80)
p.forward(30 * size / 100)
p.backward(30 * size / 100)
p.left(40)
p.backward(20*size/100)
p.right(360/branches)
In this block of code, we have created a function for snowflakes where we have set the parameters as x and y coordinates, color, and size of the snowflake. So here we have not individually defined the x and y coordinates the size and the color. The actual parameters are responsible for the desired output.
In the for loop block, we create the branches for our snowflakes randomly. It is done by calling the randint(5,10) function. Any number between 5 to 10 is passed randomly to the snowflake(). According to that our snowflake branches are created. The size variable in this block allows for an increase or decrease in the snowflake size. The last function i.e right(360/branches) takes care of the actual number of branches on every iteration.
Setting text color, style, and size in the snowflake python turtle
#writing the text and providing the color
p.color("green")
style=('Courier',20,'italic')
p.write('Beautiful Snow', font=style,align='center')
In this piece of code, we have set the color of the text to green,font=courier,size=20, and italic. p.write function to write the text on the graphics screen.
Passing the actual parameters to the snowflake function
#for loop for to develop 20 snowflakes
for i in range(0,20):
randomX = randint(-200,200)
randomY = randint(-200,200)
#size will be passed randomly between 5 and 40 by using randint
randomSize = randint(5,40)
branches=randint(5,10)
snowflake("white",randomX,randomY,randomSize)
turtle.done()
In this block of code, we have passed actual parameters for snowflakes which are the color,x,y, and size to the snowflake function. The snowflakes are displayed anywhere between -200 and 200 as a result of the randint(). Sizes are any number between 5 and 40. The Colour we have passed for snowflakes is white.
Complete Code Draw Snowflakes using Python Turtle
#import Turtle import turtle from random import randint p=turtle.Turtle() p.shape("turtle") #speed of turtle p.speed(1500) p.pensize(4) scr=turtle.Screen() #setting the background color & title scr.bgcolor("SkyBlue1") scr.title("Merry Christmas") #function for setting the color a and y coordinates and size of snowflake def snowflake(color,x,y,size): p.penup() p.goto(x,y) p.pendown() p.color(color) p.left(90) #Set the for loop for create the snowflake branches for i in range(0,branches): #creates the shape of the snowflake p.forward(100*size/100) p.backward(40*size/100) p.left(40) p.forward(30*size/100) p.backward(30*size/100) p.right(80) p.forward(30*size/100) p.backward(30*size/100) p.left(40) p.backward(40*size/100) p.left(40) p.forward(30*size/100) p.backward(30*size/100) p.right(80) p.forward(30 * size / 100) p.backward(30 * size / 100) p.left(40) p.backward(20*size/100) p.right(360/branches) #writing the text and providing the color p.color("green") style=('Courier',20,'italic') p.write('Beautiful Snow', font=style,align='center') #for loop for to develop 20 snowflakes for i in range(0,20): randomX = randint(-200,200) randomY = randint(-200,200) randomSize = randint(5,40) branches=randint(5,10) snowflake("white",randomX,randomY,randomSize) turtle.done()
Output

So here you can see we have created beautiful snowflakes. Like this shape of snowflakes, you can create various different shapes of snowflakes you like.
Also Read:
- Top 25 Pattern Programs in C++
- Currency Converter in C++
- SQLite | CRUD Operations in Python
- Number Guessing Game in C++
- Image background remover in Python
- C++ Project Structure
- Python | Check if a string is a palindrome or not without Recursion
- Python | Check if a number is an Armstrong Number or not using Recursion
- Python | Check if a number is an Armstrong Number or not without using Recursion
- Python | Shuffle a list using recursion
- Python | Shuffle a list without recursion
- Python | Implementing switch case using functions
- Python function to check whether a number is perfect or not
- Python | Find LCM using function
- Python | Find HCF using function
- Python | Convert the binary number to decimal without using library function
- Python | Create a basic operations calculator(+, -, /, and x), create a separate function for each operation
- Python | Detecting the number of local variables declared in a function
- Python | Making a chain of function decorators (bold italic underline etc)
- Python | Access function inside a function
- Event Management System Project in Python
- ATM machine program in C++
- Python | Create a function with a pass statement
- Python | Function to calculate the square root of a number
- Python | A function that calculates the power of a number
- Python | A function that accepts 2 integers and adds them and returns their sum
- Python | Function that takes a list of integers and returns the last integer
- Python | Return multiple values from a function
- Python function that takes a list and returns a new list with unique elements of the first list
- Python | Generate a random number in the range of 1 to 10 using the function