Skip to main content

Command Palette

Search for a command to run...

Day 18: 100 Days of Code

Python

Updated
2 min readView as Markdown
Day 18: 100 Days of Code

All the ways to import module

1.Basic import

Keyword module_name

import turtle
tim = turtle.Turtle()

2.from...import...

keyword module_name keyword thing_in_module

from turtle import Turtle
tim = Turtle()

3.from...import *

keyword module_name keyword every thing.

forward()

Note: It is very confusing not preferred in python community. Python programmers sometimes use first way or second way of importing modules.

Aliasing modules

import turtle as t
tim = t.Turtle()

Installing modules

If module or package is not already installed into the pycharm IDE then you have to install it. You can do it by going to the project setting then interpreter and there you can write the name of package you need and install it. Then you can import the modules.

Named colors vs random colors

# Named Colors
tim.color("red")
# Random Colors

import turtle as t
import random
tim = t.Turtle()
screen = t.Screen()
t.colormode(255)

def random_color():
    r = random.randint(0, 255)
    g = random.randint(0, 255)
    b = random.randint(0, 255)

    rand_color = (r, g, b)
    return rand_color

tim.pencolor(random_color())
screen.exitonclick()

Python Tuples:

my_tuple = (1, 3, 8)
my_tuple[2] #Access the element from tuple

Tuple vs List: In tuples we can not make changes i.e., edit, delete. Tuples are Immutable. You have to convert that into list list(my_tuple) to make changes in it.

my_tuple[2] = 12

Output:
Error

Challenges

1.Draw a rectangle

Screen Shot 2022-02-05 at 12.50.40 PM.png

2.Draw a dotted line

Screen Shot 2022-02-05 at 12.51.14 PM.png

3.Draw different shapes

Screen Shot 2022-02-05 at 12.55.13 PM.png

4.Random Walk

Screen Shot 2022-02-05 at 12.56.21 PM.png

5.Draw a Spirograph Screen Shot 2022-02-05 at 12.57.01 PM.png

Code on Github

Project

The Hirst Painting Project. Github

Screen Shot 2022-02-05 at 12.49.12 PM.png

Turtle Module Documentation