How to draw smiley using Python Graphics
Tags: beginners, tutorial, python, programming
Published: 2023-10-29 23:56:49

## How to draw smiley using Python Graphics

Using the **_Turtle graphics library_** in Python we can draw a smiley face. It creates a yellow circle for the face, two smaller circles for the eyes, and a curved line for the smile. The smiley face will be displayed in a Turtle graphics window, and the program will finish when you close the window.

This code uses a Turtle graphics object to draw a smiley face step by step, including the face, eyes, and smile. The `penup()` and `pendown()` commands are used to control when the turtle's pen is lifted from the canvas to move without drawing and when it's lowered to start drawing again. The `goto()` command is used to move the turtle to specific positions. Finally, the `turtle.done()` command keeps the Turtle graphics window open until you close it.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ovxe0qrd271e3w7lqn4.jpeg)


Below code, step by step, uses a Turtle to draw a simple smiley face with a yellow circular face, two eyes, and a curved smile.

Step 1 :`import turtle`: This line imports the Turtle graphics library in Python, allowing you to create graphics and drawings.

```javascript
import turtle
```

Step 2 :  `t = turtle.Turtle()`: It creates a Turtle object named 't', which is used to control the turtle's movements and drawings.

```javascript
import turtle
t = turtle.Turtle()
```

Step 3 : `t.fillcolor('yellow')`: Sets the fill color for shapes drawn by the turtle. In this case, the fill color is set to yellow, which will be used for the face and eyes.

```javascript
import turtle
t = turtle.Turtle()
t.fillcolor('yellow')

```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zsprvplrp38vq2dvqu54.jpeg)

Step 4 : `t.begin_fill()`: This command marks the beginning of a shape that you want to fill with the specified color. It prepares the turtle to draw a filled shape.

```javascript
import turtle
t = turtle.Turtle()
t.fillcolor('yellow')
t.begin_fill()



```
Step 5 : `t.circle(100)`: Draws a circle with a radius of 100 units. This is used to create the yellow face of the smiley.

```javascript
import turtle
t = turtle.Turtle()
t.fillcolor('yellow')
t.begin_fill()
t.circle(100)
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8x301cao2ngp4u4xpvq2.jpeg)

Step 6 : `t.end_fill()`: Ends the filling of the shape, which is the yellow circle in this case.

```javascript
import turtle
t = turtle.Turtle()
t.fillcolor('yellow')
t.begin_fill()
t.circle(100)
t.end_fill()
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/umo287phuvr4mxy489sg.jpeg)

Step 7 : `t.penup()`: Raises the turtle's pen, so it won't draw when it moves.

```javascript
import turtle
t = turtle.Turtle()
t.fillcolor('yellow')
t.begin_fill()
t.circle(100)
t.end_fill()
t.penup()
```
Step 8 : `t.goto(-40, 120)`: Moves the turtle to the coordinates (-40, 120) without drawing any lines.

```javascript
import turtle
t = turtle.Turtle()
t.fillcolor('yellow')
t.begin_fill()
t.circle(100)
t.end_fill()
t.penup()
t.goto(-40, 120)
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xfa121uqzp3uf47refw3.jpeg)

Step 9 : `t.pendown()`: Lowers the turtle's pen, so it can start drawing again.

```javascript
import turtle
t = turtle.Turtle()
t.fillcolor('yellow')
t.begin_fill()
t.circle(100)
t.end_fill()
t.penup()
t.goto(-40, 120)
t.pendown()
```

Step 10 :`t.begin_fill()`: Begins the filling of another shape.
```javascript
import turtle
t = turtle.Turtle()
t.fillcolor('yellow')
t.begin_fill()
t.circle(100)
t.end_fill()
t.penup()
t.goto(-40, 120)
t.pendown()
t.begin_fill()
```

Step 11 :`t.circle(10)`: Draws a circle with a radius of 10 units, creating the left eye.

```javascript
import turtle
t = turtle.Turtle()
t.fillcolor('yellow')
t.begin_fill()
t.circle(100)
t.end_fill()
t.penup()
t.goto(-40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pt81wlnvypkasfuq4gqd.jpeg)

Step 12 : `t.end_fill()`: Ends the filling for the left eye, which is also filled with the yellow color.

```javascript
import turtle
t = turtle.Turtle()
t.fillcolor('yellow')
t.begin_fill()
t.circle(100)
t.end_fill()
t.penup()
t.goto(-40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
```
Step 13 : `t.penup()`: Lifts the pen again.

```javascript
import turtle
t = turtle.Turtle()
t.fillcolor('yellow')
t.begin_fill()
t.circle(100)
t.end_fill()
t.penup()
t.goto(-40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
t.penup()
```
Step 14 : `t.goto(40, 120)`: Moves the turtle to the coordinates (40, 120) without drawing.

```javascript
import turtle
t = turtle.Turtle()
t.fillcolor('yellow')
t.begin_fill()
t.circle(100)
t.end_fill()
t.penup()
t.goto(-40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
t.penup()
t.goto(40, 120)
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gu0y9jw2zug09jz4b6mi.jpeg)

Step 15 : `t.pendown()`: Lowers the pen to start drawing.

```javascript
import turtle
t = turtle.Turtle()
t.fillcolor('yellow')
t.begin_fill()
t.circle(100)
t.end_fill()
t.penup()
t.goto(-40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
t.penup()
t.goto(40, 120)
t.pendown()
```

Step 16 : `t.begin_fill()`: Begins filling for the right eye.
```javascript
import turtle
t = turtle.Turtle()
t.fillcolor('yellow')
t.begin_fill()
t.circle(100)
t.end_fill()
t.penup()
t.goto(-40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
t.penup()
t.goto(40, 120)
t.pendown()
t.begin_fill()
```
Step 17 : `t.circle(10)`: Draws a circle with a radius of 10 units, creating the right eye.

```javascript
import turtle
t = turtle.Turtle()
t.fillcolor('yellow')
t.begin_fill()
t.circle(100)
t.end_fill()
t.penup()
t.goto(-40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
t.penup()
t.goto(40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/otmkcri3d40ckdnq932c.jpeg)

Step 18 :t.end_fill(): Ends the filling for the right eye.

```javascript
import turtle
t = turtle.Turtle()
t.fillcolor('yellow')
t.begin_fill()
t.circle(100)
t.end_fill()
t.penup()
t.goto(-40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
t.penup()
t.goto(40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
```
Step 19 :`t.penup()`: Lifts the pen.
```javascript
import turtle
t = turtle.Turtle()
t.fillcolor('yellow')
t.begin_fill()
t.circle(100)
t.end_fill()
t.penup()
t.goto(-40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
t.penup()
t.goto(40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
t.penup()
```

Step 20 :**t.goto(-30, 85)**: Moves the turtle to the coordinates (-30, 85) without drawing.
```javascript
import turtle
t = turtle.Turtle()
t.fillcolor('yellow')
t.begin_fill()
t.circle(100)
t.end_fill()
t.penup()
t.goto(-40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
t.penup()
t.goto(40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
t.penup()
t.goto(-30, 85)
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pkdoyrrmn5154vynwuyz.jpeg)

Step 21 : **t.pendown()**: Lowers the pen to start drawing again.
```javascript
import turtle
t = turtle.Turtle()
t.fillcolor('yellow')
t.begin_fill()
t.circle(100)
t.end_fill()
t.penup()
t.goto(-40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
t.penup()
t.goto(40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
t.penup()
t.goto(-30, 85)
t.pendown()
```
Step 22 :**t.right(90)**: Rotates the turtle 90 degrees to position it correctly for drawing the smile.
```javascript
import turtle
t = turtle.Turtle()
t.fillcolor('yellow')
t.begin_fill()
t.circle(100)
t.end_fill()
t.penup()
t.goto(-40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
t.penup()
t.goto(40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
t.penup()
t.goto(-30, 85)
t.pendown()
t.right(90)
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j14p3v7qsby7c9bp1ub1.jpeg)
Step 23 : **t.circle(30, 190)**: Draws a curved line to create the smile using a part of a circle with a radius of 30 and an angle of 190 degrees.

```javascript
import turtle
t = turtle.Turtle()
t.fillcolor('yellow')
t.begin_fill()
t.circle(100)
t.end_fill()
t.penup()
t.goto(-40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
t.penup()
t.goto(40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
t.penup()
t.goto(-30, 85)
t.pendown()
t.right(90)
t.circle(30, 190)
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t99qeaqoy4wx7rvgii0d.jpeg)

Step 24 : **turtle.done()**: Keeps the Turtle graphics window open until you click on it to close the program.

```javascript
import turtle
t = turtle.Turtle()
t.fillcolor('yellow')
t.begin_fill()
t.circle(100)
t.end_fill()
t.penup()
t.goto(-40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
t.penup()
t.goto(40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
t.penup()
t.goto(-30, 85)
t.pendown()
t.right(90)
t.circle(30, 190)
turtle.done()
```


It creates a yellow circle for the face, two smaller circles for the eyes, and a curved line for the smile. The smiley face will be displayed in a Turtle graphics window, and the program will finish when you close the window.

Here's a breakdown of what each part of the code does:

- `t = turtle.Turtle()`: Creates a Turtle object named t.
- `t.fillcolor('yellow')`: Sets the fill color to yellow for filling the shapes.
- `t.begin_fill()`: Begins filling the shape with the specified color.
- `t.circle(100)`: Draws a circle with a radius of 100, creating the face of the smiley face.
- The following `penup()`, `goto()`, and `pendown()` commands are used to move the turtle to the desired positions for drawing the eyes and smile.
- `t.right(90) `rotates the turtle to the right by 90 degrees, so it can draw the curved smile.
- `t.circle(30, 190)`draws a part of a circle, creating the smile.
- The `turtle.done()` command is used to keep the Turtle graphics window open until you close it. When you close the window, the program finishes.

Your code should successfully draw a smiley face when executed.

[Click here to understand the program and the result](https://replit.com/@vayolapradeep00/how-to-draw-smiley#main.py)