Python Cool Tricks - How to beat color names in Python

You have a list of colors L (for example 630 colors), but you need to display something with even more colors (for example 1700). When you have used all the colors from the list L, then you will start again from the beginning and so on until you reach the required number of colors, using the following trick:

import turtle as tu
# L is a list of 630 colors
for i in range(1700):
    # You don't need to know how many colors are in the list!
    tu.color(L[i%len(L)])     # i%len(L) is the color number from list L
    # ... Drawing with the selected color ...

The remainder when dividing the current index (i) and the maximum number of colors in the list L (len(L)) is always less than the maximum number of colors in the list L. This allows you to always pick a color from the list no matter how big the i index is!

In fact, you are spinning your entire color palette in circles, as many times as you need to.

Of course, you will use L[i%len(L)] in all other situations when a list with a certain number of members should be used for a much larger group of members.

- In Python, you represent a color by a color name using one of the following strings:

- Light colors in order - white, yellow, red, green, blue, purple, orange, pink:

"white", "yellow", "red", "green", "blue", "magenta", "orange", "pink"

- Dark colors in order - black, gold, dark red, dark green, dark blue, dark purple, dark orange, dark pink:

"black", "gold", "darkred", "darkgreen", "darkblue", "darkmagenta", "darkorange", "violet"

- Colors are also represented by other strings such as:

"silver", "chocolate", "tomato", "olive", "plum", "coral", "azure", ...

- Apart from using strings, you can also represent the color using numbers. Your computer can distinguish 16777216 colors (256**3), which are represented by 3 bytes. It is the so-called RGB color space of shades of colors created using 3 basic colors: red, green and blue.

- The following commands represent the same background color (gold) for drawing in turtle graphics in different ways:

import turtle
turtle.bgcolor ("gold")      # A color represented by a string
turtle.colormode (1.0)       # Colors are now represented using the range 0..1
turtle.bgcolor(1.00, 0.84, 0.00)  # Color represented by 3 real numbers
turtle.colormode (255)      # Colors are now represented using the range 0..255
turtle.bgcolor (255, 215, 0)  # Color represented by 3 bytes (decadal)
turtle.bgcolor ("#FFD700")    # Color represented by 3 bytes (hexadecimal)
turtle.bgcolor ((255, 215, 0))  # A color represented by an tuple

You need to know that the Python programming language distinguishes between lowercase and uppercase letters in constant names (e.g. False you must not write FALSE or false), variable names (when you write x and X in two places, they are two different variables), command names (you must write print, not Print or PRINT), etc.

In this regard, you may have noticed that in the name of the same color both upper and lower case letters are used, as necessary. For example, color represented by name (string): in the HTML color table - 'LightSteelBlue', and in the SVG color table 'lightsteelblue'. You could even write 'LIGHTSTEELBLUE' or some combination of lowercase and uppercase letters. Therefore, it is allowed to use both lower and upper case letters in the name of the colors, as desired.

In the HTML color table, Python does not recognize only one color by name: 'RebeccaPurple'. In the SVG color table, Python recognizes all colors, as well as in the X11 color table.

The following are the tables with the names of the colors and the numbers by which the colors are represented:


Of course, you can create your own color list, but you can also use the 5 color palettes from the module gcolor.py:

# gcolor.py

# A palette of 10 basic bright rainbow colors - the background should be 'Black' 
pcolor1 = [
'White', 'Pink', 'Red', 'Orange', 'Yellow', 'SandyBrown', 'Lime',
'Aqua', 'DeepSkyBlue', 'Violet']

# A palette of 10 basic dark rainbow colors - the background should be 'White' 
pcolor2 = [
'Black', 'MediumVioletRed', 'DarkRed', 'DarkOrange', 'Gold', 'Chocolate', 'DarkGreen',
'Teal', 'DarkBlue', 'DarkViolet']

# A palette of 140 colors according to the HTML color scheme (HTML colors) - the background should be 'Black'
pcolor3 = [
# Pink colors
'Pink', 'LightPink', 'HotPink', 'DeepPink', 'PaleVioletRed', 'MediumVioletRed',    
# Red colors
'LightSalmon', 'Salmon', 'DarkSalmon', 'LightCoral', 'IndianRed', 'Crimson',    
'FireBrick', 'DarkRed', 'Red',    
# Orange colors
'OrangeRed', 'Tomato', 'Coral', 'DarkOrange', 'Orange',    
# Yellow colors
'Yellow', 'LightYellow', 'LemonChiffon', 'LightGoldenrodYellow', 'PapayaWhip', 'Moccasin',    
'PeachPuff', 'PaleGoldenrod', 'Khaki', 'DarkKhaki', 'Gold',    
# Brown colors
'Cornsilk', 'BlanchedAlmond', 'Bisque', 'NavajoWhite', 'Wheat', 'BurlyWood',    
'Tan', 'RosyBrown', 'SandyBrown', 'Goldenrod', 'DarkGoldenrod', 'Peru',    
'Chocolate', 'SaddleBrown', 'Sienna', 'Brown', 'Maroon',    
# Green colors
'DarkOliveGreen', 'Olive', 'OliveDrab', 'YellowGreen', 'LimeGreen', 'Lime',    
'LawnGreen', 'Chartreuse', 'GreenYellow', 'SpringGreen', 'MediumSpringGreen', 'LightGreen',    
'PaleGreen', 'DarkSeaGreen', 'MediumAquamarine', 'MediumSeaGreen', 'SeaGreen', 'ForestGreen',    
'Green','DarkGreen',    
# Cyan colors
'Aqua', 'Cyan', 'LightCyan', 'PaleTurquoise', 'Aquamarine', 'Turquoise',    
'MediumTurquoise', 'DarkTurquoise', 'LightSeaGreen', 'CadetBlue', 'DarkCyan', 'Teal',    
# Blue colors
'LightSteelBlue', 'PowderBlue', 'LightBlue', 'SkyBlue', 'LightSkyBlue', 'DeepSkyBlue',    
'DodgerBlue', 'CornflowerBlue', 'SteelBlue', 'RoyalBlue', 'Blue', 'MediumBlue',    
'DarkBlue', 'Navy', 'MidnightBlue',    
# Purple/Violet/Magenta colors
'Lavender', 'Thistle', 'Plum', 'Violet', 'Orchid', 'Fuchsia',    
'Magenta', 'MediumOrchid', 'MediumPurple', 'BlueViolet', 'DarkViolet', 'DarkOrchid',    
'DarkMagenta', 'Purple', 'Indigo', 'DarkSlateBlue', 'SlateBlue', 'MediumSlateBlue',    
# White colors
'White', 'Snow', 'Honeydew', 'MintCream', 'Azure', 'AliceBlue',    
'GhostWhite', 'WhiteSmoke', 'Seashell', 'Beige', 'OldLace', 'FloralWhite',    
'Ivory', 'AntiqueWhite', 'Linen', 'LavenderBlush', 'MistyRose',    
# Gray/Black colors
'Gainsboro', 'LightGrey', 'Silver', 'DarkGray', 'Gray', 'DimGray',    
'LightSlateGray', 'SlateGray', 'DarkSlateGray', 'Black']    

# A palette of selected 359 colors in the X11 color scheme - the background should be 'Black'
pcolor4 = [
'snow', 'ghostwhite', 'whitesmoke', 'gainsboro', 'floralwhite', 'oldlace',
'linen', 'antiquewhite', 'papayawhip', 'blanchedalmond', 'bisque', 'peachpuff',
'navajowhite', 'lemonchiffon', 'mintcream', 'azure', 'aliceblue', 'lavender',
'lavenderblush', 'mistyrose', 'darkslategray', 'dimgray', 'slategray',
'lightslategray', 'gray', 'lightgrey', 'midnightblue', 'navy', 'cornflowerblue', 'darkslateblue',
'slateblue', 'mediumslateblue', 'lightslateblue', 'mediumblue', 'royalblue',  'blue',
'dodgerblue', 'deepskyblue', 'skyblue', 'lightskyblue', 'steelblue', 'lightsteelblue',
'lightblue', 'powderblue', 'paleturquoise', 'darkturquoise', 'mediumturquoise', 'turquoise',
'cyan', 'lightcyan', 'cadetblue', 'mediumaquamarine', 'aquamarine', 'darkgreen', 'darkolivegreen',
'darkseagreen', 'seagreen', 'mediumseagreen', 'lightseagreen', 'palegreen', 'springgreen',
'lawngreen', 'mediumspringgreen', 'greenyellow', 'limegreen', 'yellowgreen',
'forestgreen', 'olivedrab', 'darkkhaki', 'khaki', 'palegoldenrod', 'lightgoldenrodyellow',
'lightyellow', 'yellow', 'gold', 'lightgoldenrod', 'goldenrod', 'darkgoldenrod', 'rosybrown',
'indianred', 'saddlebrown', 'sandybrown',
'darksalmon', 'salmon', 'lightsalmon', 'orange', 'darkorange',
'coral', 'lightcoral', 'tomato', 'orangered', 'red', 'hotpink', 'deeppink', 'pink', 'lightpink',
'palevioletred', 'maroon', 'mediumvioletred', 'violetred',
'mediumorchid', 'darkorchid', 'darkviolet', 'blueviolet', 'purple', 'mediumpurple',
'thistle', 'snow2', 'snow3',
'snow4', 'seashell2', 'seashell3', 'seashell4', 'AntiqueWhite1', 'AntiqueWhite2',
'AntiqueWhite3', 'AntiqueWhite4', 'bisque2', 'bisque3', 'bisque4', 'PeachPuff2',
'PeachPuff3', 'PeachPuff4', 'NavajoWhite2', 'NavajoWhite3', 'NavajoWhite4',
'LemonChiffon2', 'LemonChiffon3', 'LemonChiffon4', 'cornsilk2', 'cornsilk3',
'cornsilk4', 'ivory2', 'ivory3', 'ivory4', 'honeydew2', 'honeydew3', 'honeydew4',
'LavenderBlush2', 'LavenderBlush3', 'LavenderBlush4', 'MistyRose2', 'MistyRose3',
'MistyRose4', 'azure2', 'azure3', 'azure4', 'SlateBlue1', 'SlateBlue2', 'SlateBlue3',
'SlateBlue4', 'RoyalBlue1', 'RoyalBlue2', 'RoyalBlue3', 'RoyalBlue4', 'blue2', 'blue4',
'DodgerBlue2', 'DodgerBlue3', 'DodgerBlue4', 'SteelBlue1', 'SteelBlue2',
'SteelBlue3', 'SteelBlue4', 'DeepSkyBlue2', 'DeepSkyBlue3', 'DeepSkyBlue4',
'SkyBlue1', 'SkyBlue2', 'SkyBlue3', 'SkyBlue4', 'LightSkyBlue1', 'LightSkyBlue2',
'LightSkyBlue3', 'LightSkyBlue4', 'SlateGray1', 'SlateGray2', 'SlateGray3',
'SlateGray4', 'LightSteelBlue1', 'LightSteelBlue2', 'LightSteelBlue3',
'LightSteelBlue4', 'LightBlue1', 'LightBlue2', 'LightBlue3', 'LightBlue4',
'LightCyan2', 'LightCyan3', 'LightCyan4', 'PaleTurquoise1', 'PaleTurquoise2',
'PaleTurquoise3', 'PaleTurquoise4', 'CadetBlue1', 'CadetBlue2', 'CadetBlue3',
'CadetBlue4', 'turquoise1', 'turquoise2', 'turquoise3', 'turquoise4', 'cyan2', 'cyan3',
'cyan4', 'DarkSlateGray1', 'DarkSlateGray2', 'DarkSlateGray3', 'DarkSlateGray4',
'aquamarine2', 'aquamarine4', 'DarkSeaGreen1', 'DarkSeaGreen2', 'DarkSeaGreen3',
'DarkSeaGreen4', 'SeaGreen1', 'SeaGreen2', 'SeaGreen3', 'PaleGreen1', 'PaleGreen2',
'PaleGreen3', 'PaleGreen4', 'SpringGreen2', 'SpringGreen3', 'SpringGreen4',
'green2', 'green3', 'green4', 'chartreuse2', 'chartreuse3', 'chartreuse4',
'OliveDrab1', 'OliveDrab2', 'OliveDrab4', 'DarkOliveGreen1', 'DarkOliveGreen2',
'DarkOliveGreen3', 'DarkOliveGreen4', 'khaki1', 'khaki2', 'khaki3', 'khaki4',
'LightGoldenrod1', 'LightGoldenrod2', 'LightGoldenrod3', 'LightGoldenrod4',
'LightYellow2', 'LightYellow3', 'LightYellow4', 'yellow2', 'yellow3', 'yellow4',
'gold2', 'gold3', 'gold4', 'goldenrod1', 'goldenrod2', 'goldenrod3', 'goldenrod4',
'DarkGoldenrod1', 'DarkGoldenrod2', 'DarkGoldenrod3', 'DarkGoldenrod4',
'RosyBrown1', 'RosyBrown2', 'RosyBrown3', 'RosyBrown4', 'IndianRed1', 'IndianRed2',
'IndianRed3', 'IndianRed4', 'sienna1', 'sienna2', 'sienna3', 'sienna4', 'burlywood1',
'burlywood2', 'burlywood3', 'burlywood4', 'wheat1', 'wheat2', 'wheat3', 'wheat4', 'tan1',
'tan2', 'tan4', 'chocolate1', 'chocolate2', 'chocolate3', 'firebrick1', 'firebrick2',
'firebrick3', 'firebrick4', 'brown1', 'brown2', 'brown3', 'brown4', 'salmon1', 'salmon2',
'salmon3', 'salmon4', 'LightSalmon2', 'LightSalmon3', 'LightSalmon4', 'orange2',
'orange3', 'orange4', 'DarkOrange1', 'DarkOrange2', 'DarkOrange3', 'DarkOrange4',
'coral1', 'coral2', 'coral3', 'coral4', 'tomato2', 'tomato3', 'tomato4', 'OrangeRed2',
'OrangeRed3', 'OrangeRed4', 'red2', 'red3', 'red4', 'DeepPink2', 'DeepPink3', 'DeepPink4',
'HotPink1', 'HotPink2', 'HotPink3', 'HotPink4', 'pink1', 'pink2', 'pink3', 'pink4',
'LightPink1', 'LightPink2', 'LightPink3', 'LightPink4', 'PaleVioletRed1',
'PaleVioletRed2', 'PaleVioletRed3', 'PaleVioletRed4', 'maroon1', 'maroon2',
'maroon3', 'maroon4', 'VioletRed1', 'VioletRed2', 'VioletRed3', 'VioletRed4',
'magenta2', 'magenta3', 'magenta4', 'orchid1', 'orchid2', 'orchid3', 'orchid4', 'plum1',
'plum2', 'plum3', 'plum4', 'MediumOrchid1', 'MediumOrchid2', 'MediumOrchid3',
'MediumOrchid4', 'DarkOrchid1', 'DarkOrchid2', 'DarkOrchid3', 'DarkOrchid4',
'purple1', 'purple2', 'purple3', 'purple4', 'MediumPurple1', 'MediumPurple2',
'MediumPurple3', 'MediumPurple4', 'thistle1', 'thistle2', 'thistle3', 'thistle4',
'gray1', 'gray2', 'gray3', 'gray4', 'gray5', 'gray6', 'gray7', 'gray8', 'gray9', 'gray10',
'gray11', 'gray12', 'gray13', 'gray14', 'gray15', 'gray16', 'gray17', 'gray18', 'gray19',
'gray20', 'gray21', 'gray22', 'gray23', 'gray24', 'gray25', 'gray26', 'gray27', 'gray28',
'gray29', 'gray30', 'gray31', 'gray32', 'gray33', 'gray34', 'gray35', 'gray36', 'gray37',
'gray38', 'gray39', 'gray40', 'gray42', 'gray43', 'gray44', 'gray45', 'gray46', 'gray47',
'gray48', 'gray49', 'gray50', 'gray51', 'gray52', 'gray53', 'gray54', 'gray55', 'gray56',
'gray57', 'gray58', 'gray59', 'gray60', 'gray61', 'gray62', 'gray63', 'gray64', 'gray65',
'gray66', 'gray67', 'gray68', 'gray69', 'gray70', 'gray71', 'gray72', 'gray73', 'gray74',
'gray75', 'gray76', 'gray77', 'gray78', 'gray79', 'gray80', 'gray81', 'gray82', 'gray83',
'gray84', 'gray85', 'gray86', 'gray87', 'gray88', 'gray89', 'gray90', 'gray91', 'gray92',
'gray93', 'gray94', 'gray95', 'gray97', 'gray98', 'gray99']

# Palette of 100 rainbow colors - background should be 'Black'
pcolor5 = [
# reddish colors
(1.00, 0.00, 0.00),(1.00, 0.03, 0.00),(1.00, 0.05, 0.00),(1.00, 0.07, 0.00),
(1.00, 0.10, 0.00),(1.00, 0.12, 0.00),(1.00, 0.15, 0.00),(1.00, 0.17, 0.00),
(1.00, 0.20, 0.00),(1.00, 0.23, 0.00),(1.00, 0.25, 0.00),(1.00, 0.28, 0.00),
(1.00, 0.30, 0.00),(1.00, 0.33, 0.00),(1.00, 0.35, 0.00),(1.00, 0.38, 0.00),
(1.00, 0.40, 0.00),(1.00, 0.42, 0.00),(1.00, 0.45, 0.00),(1.00, 0.47, 0.00),
# orangey colors
(1.00, 0.50, 0.00),(1.00, 0.53, 0.00),(1.00, 0.55, 0.00),(1.00, 0.57, 0.00),
(1.00, 0.60, 0.00),(1.00, 0.62, 0.00),(1.00, 0.65, 0.00),(1.00, 0.68, 0.00),
(1.00, 0.70, 0.00),(1.00, 0.72, 0.00),(1.00, 0.75, 0.00),(1.00, 0.78, 0.00),
(1.00, 0.80, 0.00),(1.00, 0.82, 0.00),(1.00, 0.85, 0.00),(1.00, 0.88, 0.00),
(1.00, 0.90, 0.00),(1.00, 0.93, 0.00),(1.00, 0.95, 0.00),(1.00, 0.97, 0.00),
# yellowy colors
(1.00, 1.00, 0.00),(0.95, 1.00, 0.00),(0.90, 1.00, 0.00),(0.85, 1.00, 0.00),
(0.80, 1.00, 0.00),(0.75, 1.00, 0.00),(0.70, 1.00, 0.00),(0.65, 1.00, 0.00),
(0.60, 1.00, 0.00),(0.55, 1.00, 0.00),(0.50, 1.00, 0.00),(0.45, 1.00, 0.00),
(0.40, 1.00, 0.00),(0.35, 1.00, 0.00),(0.30, 1.00, 0.00),(0.25, 1.00, 0.00),
(0.20, 1.00, 0.00),(0.15, 1.00, 0.00),(0.10, 1.00, 0.00),(0.05, 1.00, 0.00),
# greenish colors
(0.00, 1.00, 0.00),(0.00, 0.95, 0.05),(0.00, 0.90, 0.10),(0.00, 0.85, 0.15),
(0.00, 0.80, 0.20),(0.00, 0.75, 0.25),(0.00, 0.70, 0.30),(0.00, 0.65, 0.35),
(0.00, 0.60, 0.40),(0.00, 0.55, 0.45),(0.00, 0.50, 0.50),(0.00, 0.45, 0.55),
(0.00, 0.40, 0.60),(0.00, 0.35, 0.65),(0.00, 0.30, 0.70),(0.00, 0.25, 0.75),
(0.00, 0.20, 0.80),(0.00, 0.15, 0.85),(0.00, 0.10, 0.90),(0.00, 0.05, 0.95),
# blueish colors
(0.00, 0.00, 1.00),(0.05, 0.00, 1.00),(0.10, 0.00, 1.00),(0.15, 0.00, 1.00),
(0.20, 0.00, 1.00),(0.25, 0.00, 1.00),(0.30, 0.00, 1.00),(0.35, 0.00, 1.00),
(0.40, 0.00, 1.00),(0.45, 0.00, 1.00),(0.50, 0.00, 1.00),(0.55, 0.00, 1.00),
(0.60, 0.00, 1.00),(0.65, 0.00, 1.00),(0.70, 0.00, 1.00),(0.75, 0.00, 1.00),
(0.80, 0.00, 1.00),(0.85, 0.00, 1.00),(0.90, 0.00, 1.00),(0.95, 0.00, 1.00)
]

def pcolor(p, n):              # Selection of color n from the color palette p
    if p == 1:
        color = pcolor1[n%10]  # For n>=10, the expression n%10 is always < 10
    elif p == 2:
        color = pcolor2[n%10]  # For n>=10, the expression n%10 is always < 10
    elif p == 3:
        color = pcolor3[n%140] # For n>=140, the expression n%140 is always < 140
    elif p == 4:
        color = pcolor4[n%359] # For n>=359, the expression n%359 is always < 359
    elif p >= 5:
        color = pcolor5[n%100] # For n>=100, the expression n%100 is always < 100
    if p < 1:
        color = pcolor2[0]
    return color

def black_white(bgcol):
    if bgcol == 'black':
        return 'white'
    else:
        return 'black'

And finally, of course, dessert - a program that uses these color palettes (the cover image was drawn using this program):

import turtle as tu
from gcolor import *
from math import *
from random import *

# Choice of pen depth, backgr. color and choice of black and white or color drawing
def pen_size_bgcolor(d, bgcol, pencol):  
    tu.hideturtle()    # Hide the drawing pointer (turtle)
    tu.pensize(d)      # Set the pen thickness to d
    tu.bgcolor(bgcol)  # Set default background color
    if not pencol:     # If the drawing is black and white and the background is black,
        tu.color(black_white(bgcol))  # set the pen color to white and vice versa!

# If the drawing is in color, the color is selected
def bw_color(pencol, pal, color):
    if pencol:
        tu.color(pcolor(pal, color))

def GotoCenter():
    tu.penup()
    tu.goto(0, 0)       
    tu.pendown()

#  Common to all effects:
#  d - The thickness of the pencil for drawing,
#  bgcol - Background color on which to draw,
#          if not specified the current background color is assumed,
#          any color can be specified,
#  pencol - If True then color drawing is done (default),
#           if it is False then black and white drawing is done and care is taken
#           that the color of the pen is opposite to the color of the background,
#  pal - A color palette is selected.

def effect1(n, m, f, alfa, a, b, c, d, bgcol=tu.bgcolor(), pencol=True, pal=5):
    pen_size_bgcolor(d, bgcol, pencol)
    for i in range(n):
        for j in range(m):
            bw_color(pencol, pal, i)
            tu.forward(f*i*j)
            tu.left(alfa)                        
        tu.left(a*b/c+b)

def effect2(n, r, alfa, d, bgcol=tu.bgcolor(), pencol=True, pal=5):
    pen_size_bgcolor(d, bgcol, pencol)
    for i in range(n):
        bw_color(pencol, pal, i)
        tu.circle(r)
        tu.left(alfa)

def effect3(n, s, alfa, d, bgcol=tu.bgcolor(), pencol=True, pal=5):    
    pen_size_bgcolor(d, bgcol, pencol)
    for i in range(alfa):
        bw_color(pencol, pal, i)
        tu.forward(i*n/s+1)
        tu.left(alfa/s+1)     

def effect4(n, s, alfa, k, d, bgcol=tu.bgcolor(), pencol=True, pal=5):
    pen_size_bgcolor(d, bgcol, pencol)
    for i in range(n):
        bw_color(pencol, pal, i)
        tu.circle(i+s)
        tu.left(alfa/k)

def effect5(n, k, d, bgcol=tu.bgcolor(), pencol=True, pal=5):
    pen_size_bgcolor(d, bgcol, pencol)
    for i in range(n):
        bw_color(pencol, pal, i)
        tu.forward(i)
        tu.left(360/k)

def effect6(n, alfa, d, bgcol=tu.bgcolor(), pencol=True, pal=5):
    pen_size_bgcolor(d, bgcol, pencol)
    for i in range(n):
        bw_color(pencol, pal, i)
        tu.circle(i)
        tu.left(alfa)

def effect7(n, m, alfa, s, k, d, bgcol=tu.bgcolor(), pencol=True, pal=5):
    pen_size_bgcolor(d, bgcol, pencol)
    for i in range(n):
        for j in range(m):
            bw_color(pencol, pal, i)
            tu.forward(i/s)
            tu.forward(sin(radians(alfa)))
            tu.left(k/s)
            tu.width(i/360+1)
        tu.left(alfa)

def effect8(n, r, alfa, s, k, d, bgcol=tu.bgcolor(), pencol=True, pal=5):
    pen_size_bgcolor(d, bgcol, pencol)
    for i in range(0, n, 2):
        bw_color(pencol, pal, i)
        tu.circle(r)
        tu.left(alfa)
        tu.forward(i/s+k)

def effect9(n, m, alfa, d, bgcol=tu.bgcolor(), pencol=True, pal=5):
    pen_size_bgcolor(d, bgcol, pencol)
    for i in range(n):
        for j in range(m):
            bw_color(pencol, pal, i)
            tu.forward(i)
            tu.left(alfa)

def effect10(n, alfa, s, k, d, bgcol=tu.bgcolor(), pencol=True, pal=5):
    pen_size_bgcolor(d, bgcol, pencol)
    for i in range(n):
        bw_color(pencol, pal, i)
        tu.forward(i*k+s)
        tu.left(alfa)

def effect11(n, s, k1, k2, dp, d, bgcol=tu.bgcolor(), pencol=True, pal=5):
    pen_size_bgcolor(d, bgcol, pencol)
    for i in range(n):
        bw_color(pencol, pal, i)
        tu.forward(i*k1/s+i)
        tu.left(n/s+k2)
        tu.dot(dp)
        tu.pensize(d)        

def effect12(n, s, k, dl, dt, bgcol=tu.bgcolor(), lineel=False,
             el=[0], pencol=True, pal=5):
    # lineel - True, Lines and elements are also drawn,
    #          False, Only lines are drawn.
    # el - List of elements (drawn by repeating elements from the list)
    pen_size_bgcolor(dl, bgcol, pencol)
    for i in range(n):
        bw_color(pencol, pal, i)
        if dt==0: tu.penup()
        tu.forward(i*3/s+i)
        tu.left(n/s+k)
        if dt==0: tu.pendown()
        tu.stamp()
        tu.clearstamp(2)
        tu.clearstamp(-11)
        tu.dot(dt)

def effect13(n, alfa1, alfa2, s, k, 
             d, bgcol=tu.bgcolor(), pencol=True, pal=5, rnd=True):
    pen_size_bgcolor(d, bgcol, pencol)
    for i in range(n):
        bw_color(pencol, pal, i)
        tu.forward(s)
        if tu.xcor() >= k or tu.xcor() <= -k or tu.ycor() >= k   \
           or tu.ycor() <= -k:
            if rnd:
                tu.left(randint(alfa1, alfa2))
            else:
                tu.left(i)           

def effect14(n, m, alfa1, alfa2, k, d, bgcol=tu.bgcolor(), pencol=True, pal=5):
    pen_size_bgcolor(d, bgcol, pencol)
    for i in range(n):
        for j in range(m):
            bw_color(pencol, pal, i)
            tu.forward(i)
            tu.left(alfa1+k)
        tu.right(alfa2)

def effect15(n, m, alfa1, alfa2, k, d, bgcol=tu.bgcolor(), pencol=True, pal=5):
    pen_size_bgcolor(d, bgcol, pencol)
    for i in range(n):
        for j in range(m):
            bw_color(pencol, pal, i)
            tu.forward(i)
            tu.left(degrees(alfa1)+k)
        tu.right(degrees(alfa2))

def effect16(n, m, alfa1, alfa2, k, d, bgcol=tu.bgcolor(), pencol=True, pal=5):
    pen_size_bgcolor(d, bgcol, pencol)
    for i in range(0, n, k):
        for j in range(m):
            bw_color(pencol, pal, i)
            #tu.circle(i+j)
            tu.circle(i)
            tu.forward(i)
            tu.right(atan(alfa1))
        tu.right(degrees(alfa2))

def effect17(n, m, alfa1, alfa2, s, d, bgcol=tu.bgcolor(), pencol=True, pal=5):
    pen_size_bgcolor(d, bgcol, pencol)
    for i in range(n):
        for j in range(m):
            bw_color(pencol, pal, i)
            tu.circle(i/s)
            tu.left(alfa1)
        tu.left(alfa2)

def effect18(n, alfa, s, k, dp, d, bgcol=tu.bgcolor(), pencol=True, pal=5):    
    pen_size_bgcolor(d, bgcol, pencol)
    for i in range(n):
        bw_color(pencol, pal, i)
        tu.forward(i/s+k)
        tu.left(alfa)
        tu.dot(dp)     

def effect19(n, m, alfa1, alfa2, s, dp, d, bgcol=tu.bgcolor(), pencol=True, pal=5):
    pen_size_bgcolor(d, bgcol, pencol)
    for i in range(n):
        for j in range(m):
            bw_color(pencol, pal, i)
            tu.forward(i/s)
            tu.left(alfa1)
            tu.heading()
        tu.left(alfa2)
        tu.dot(dp)

def effect20(n, m, alfa1, alfa2, s, k, d, bgcol=tu.bgcolor(), pencol=True, pal=5):
    pen_size_bgcolor(d, bgcol, pencol)
    for i in range(n):
        for j in range(m):
            bw_color(pencol, pal, i)
            tu.forward(i*j+k)
            tu.left(alfa1)
        tu.penup()
        tu.goto(0, 0)
        tu.forward(s)
        tu.right(alfa2)
        tu.pendown()

def effect21(n, m, alfa1, alfa2, s, k, d, bgcol=tu.bgcolor(), pencol=True, pal=5):
    pen_size_bgcolor(d, bgcol, pencol)
    for i in range(n):
        for j in range(m):
            bw_color(pencol, pal, i)
            tu.forward(i*j*k)
            tu.left(alfa1)
            tu.goto(0, 0)
            tu.forward(s)
        tu.penup()
        tu.goto(0, 0)
        tu.forward(s)
        tu.right(alfa2)
        tu.pendown()

def effect22(n, m, alfa1, alfa2, k, d, bgcol=tu.bgcolor(), pencol=True, pal=5):
    pen_size_bgcolor(d, bgcol, pencol)
    for i in range(n):
        for j in range(m):
            bw_color(pencol, pal, i)
            tu.forward(i*j*k)
            tu.left(alfa1)            
        tu.penup()
        tu.goto(0, 0)       
        tu.pendown()
        tu.left(alfa2)

def effect23(n, m, alfa, k, d, bgcol=tu.bgcolor(), pencol=True, pal=5):
    pen_size_bgcolor(d, bgcol, pencol)
    for i in range(n):
        for j in range(m):
            bw_color(pencol, pal, i)
            tu.forward(i*j*k)
            tu.left(alfa)

def effect24(n, alfa, s, d, bgcol=tu.bgcolor(), pencol=True, pal=5):
    pen_size_bgcolor(d, bgcol, pencol)
    for i in range(n):
        bw_color(pencol, pal, i)
        tu.forward(s)
        tu.right(alfa)
        tu.forward(s/5)
        tu.left(2*alfa)
        tu.forward(s/2)
        tu.right(alfa)    
        tu.penup()
        tu.setposition(0, 0)
        tu.pendown()    
        tu.right(alfa/15)

def effect25(n, s, k, d, bgcol=tu.bgcolor(), pencol=True, pal=5):
    pen_size_bgcolor(d, bgcol, pencol)
    for i in range(n):
        bw_color(pencol, pal, i%s+3)
        tu.forward(i*k/s+i)
        tu.left(360/s+1)
        #tu.width(i*s/200)

def effect26(n, m, s, d1, d2, k, d, bgcol=tu.bgcolor(), pencol=True, pal=5):
    pen_size_bgcolor(d, bgcol, pencol)
    for i in range(n):
        if i%m==0: tu.begin_fill() 
        for j in range(s):        
            tu.fillcolor(pcolor(pal, i))
            bw_color(pencol, pal, i)        
            d1 += d2
            tu.forward(d1)
            tu.right(k*360/s+1)        
        if i%m==0: tu.end_fill()

def effect27(n, m, s, d1, d2, k, d, bgcol=tu.bgcolor(), pencol=True, pal=5):
    pen_size_bgcolor(d, bgcol, pencol)

    tu.penup()
    d1p = d1
    for i in range(n): 
        for j in range(s):
            if d1<=0: break        
            d1 -= d2
            tu.forward(d1)
            tu.right(k*360/s+1)
    d1 = d1p
    tu.left(tu.towards(0, 0))
    tu.goto(tu.xcor(), tu.ycor())
    print(tu.xcor(), tu.ycor())
    tu.pendown()

    for i in range(n):
        if i%m==0: tu.begin_fill() 
        for j in range(s):
            if d1<=0: break
            tu.fillcolor(pcolor(pal, i))
            bw_color(pencol, pal, i)        
            d1 -= d2
            tu.forward(d1)
            tu.right(k*360/s+1)        
        if i%m==0: tu.end_fill()

def effect28(n, r1, r2, d, bgcol=tu.bgcolor(), pencol=True, pal=5):
    pen_size_bgcolor(d, bgcol, pencol)    
    import colorsys
    x=randint(1, r1)
    y=randint(1, r2)
    for i in range(n+1):
        color = colorsys.hsv_to_rgb(i/1000, 1.0, 1.0)
        tu.color(color)
        tu.forward(i)
        tu.right(x)
        tu.left(y)

if __name__ == '__main__':
    #tu.speed(0)
    tu.tracer (10000, 9999)
    #effect1(420, 4, 0.2, 91, 73, 15, 100, 6)
    #effect1(420, 4, 0.2, 315, 73, 15, 100, 1)
    #effect1(420, 4, 0.2, 200, 150, 15, 100, 1)
    #effect1(1000, 4, 0.2, 500, 150, 50,80, 5, bgcol=tu.bgcolor(), pencol=True, pal=4)
    #effect2(100, 100, 6, 1)
    #effect2(1000, 100, 6, 1)        
    #effect2(1000, 100, 15, 1)
    #effect2(1000, 100, 15, 5)
    #effect3(3, 3, 360, 1, pal=5)
    #effect3(3, 3, 360, 1, pencol=False)        
    #effect3(3, 3, 1000, 1, bgcol='white', pencol=False) 
    #effect4(640, 12, 71, 1.2, 1, bgcol='black', pal=5)
    #effect4(320, 50, 71, 1.2, 1, bgcol='black', pal=5)
    #effect5(100*4, 4, 1, pencol=False)
    #effect5(100*4, 4, 1, bgcol='black', pencol=False) 
    #effect5(100*4, 4, 1, bgcol='black', pal=5)    
    #effect5(50*8, 8, 5, bgcol='black', pal=5)
    #effect5(150*8, 8, 5, bgcol='black', pal=5)
    #effect6(900, 358, 1, pencol=False)
    #effect6(300, 35, 1, pencol=False)
    #effect6(900, 45, 1, pal=5)
    #effect6(3000, 15, 1, pal=5)
    #effect6(200, 361, 20, pal=5)
    #effect7(300, 6, 91, 5, 33, 1, bgcol='black', pencol=False)
    #effect7(300, 6, 94, 5, 33, 1, bgcol='black', pencol=False)
    #effect7(300, 6, 280, 5, 33, 1, bgcol='black', pencol=False)
    #effect7(300, 6, 135, 5, 150, 1, bgcol='black', pencol=False)
    #effect7(300, 6, 135, 7, 180, 1, bgcol='black', pencol=False)
    #effect7(1000, 6, 91, 5, 33, 1, bgcol='black', pal=5)        
    #effect8(640, 200, 53, 50, 20, 1, bgcol='black', pal=5)
    #effect8(200, 100, 19, 50, 20, 1, bgcol='black', pal=5)
    #effect8(200, 100, 19, 10, 20, 1, bgcol='black', pal=5)
    #effect8(200, 100, 19, 10, 5, 1, bgcol='black', pal=5)
    #effect8(2000, 100, 19, 10, 5, 1, bgcol='black', pal=5)
    #effect9(300, 24, 33, 1, pencol=False)            
    #effect9(300, 24, 33, 1, bgcol='black', pencol=False)
    #effect9(3000, 24, 33, 1, bgcol='black', pencol=False)
    #effect9(300, 24, 33, 1, bgcol='black', pal=5)
    #effect9(3000, 24, 33, 1, bgcol='black', pal=5)
    #effect9(300, 124, 50, 1, bgcol='black', pal=5)
    #effect9(500, 300, 50, 10, bgcol='black', pal=5)
    #effect9(500, 300, 10, 2, bgcol='black', pal=5)
    #effect10(400, 51, 2, 5, 1, pencol=False)
    #effect10(400, 51, 2, 5, 1, bgcol='black', pal=5)        
    #effect10(400, 200, 2, 5, 1, bgcol='black', pal=5)
    #effect10(400, 220, 2, 5, 1, bgcol='black', pal=5)
    #effect10(400, 280, 2, 5, 1, bgcol='black', pal=5)
    #effect10(400, 80, 2, 5, 1, bgcol='black', pal=5)
    #effect10(1000, 80, 20, 0.5, 1, bgcol='black', pal=5)
    #effect10(1000, 80, 20, 0.5, 10, bgcol='black', pal=5)
    #effect11(400, 4, 3, 0.35, 20, 1, bgcol='black', pal=5)
    #effect11(1000, 4, 3, 0.35, 20, 1, bgcol='black', pal=5)
    #effect11(1000, 2, 3, 0.35, 10, 0.5, bgcol='black', pal=5)
    #effect11(500, 12, 1, 0.35, 50, 0.5, bgcol='black', pal=5)
    #effect11(500, 12, 10, 0.35, 50, 0.5, bgcol='black', pal=5)
    #effect11(1300, 4, 3, 0.35, 20, 1, bgcol='black', pal=5)
    #effect11(360, 3, 2, 1.5, 1, 1, bgcol='black', pal=5)
    #effect11(360, 5, 2, 1.5, 1, 1, bgcol='black', pal=5)
    #effect11(360, 2, 2, 1.5, 1, 1, bgcol='black', pal=5)
    #effect12(1000, 6, 0.350, 1, 5, bgcol='black', pal=5)
    #effect12(1000, 30, 0.950, 3, 50, bgcol='black', pal=5)
    #effect12(2000, 90, 0.950, 9, 100, bgcol='black', pal=5)
    #effect12(3000, 10, 0.350, 19, 100, bgcol='black', pal=5)
    #effect12(10000, 10, 0.350, 19, 100, bgcol='black', pal=5)
    #effect13(400, 90, 180, 40, 10, 1, bgcol='black', pal=5) 
    #effect13(10000, 110, 160, 40, 3, 3, bgcol='black', pal=5, rnd=False)
    #effect13(400, 120, 150, 200, 10, 20, bgcol='black', pal=5)  
    #effect14(500, 3, 33, 46, 30.5, 1, bgcol='black')
    #effect14(500, 3, 200, 200, .5, 1, bgcol='black')
    #effect14(500, 300, 10, 10, .5, 1, bgcol='black')
    #effect14(500, 30, 45, 45, .5, 1, bgcol='black')
    #effect15(500, 3, 33, 46, .5, 1, bgcol='black')
    #effect15(500, 30, 33, 46, .5, 1, bgcol='black')
    #effect15(500, 300, 200, 200, .5, 1, bgcol='black')
    #effect15(500, 300, 10, 10, .5, 1, bgcol='black')
    #effect15(500, 3, 200, 200, 100, 1, bgcol='black')
    #effect15(500, 300, 200, 200, 100, 1, bgcol='black')        
    #effect16(1200, 4, 9, 23, 3, 1, bgcol='black')
    #effect16(100, 10, 90, 90, 1, 1, bgcol='black')
    #effect16(100, 3, 9, 351, 1, 1, bgcol='black')
    #effect16(100, 4, 45, 215, 1, 1, bgcol='black')
    #effect16(100, 4, 45, 215, 1, 7, bgcol='black')
    #effect16(100, 1, 45, 215, 1, 7, bgcol='black')
    #effect16(500, 1, 175, 185, 1, 1, bgcol='black')
    #effect17(2000, 1, 45, 215, 12, 1, bgcol='black')
    #effect17(2000, 3, 175, 185, 12, 1, bgcol='black')
    #effect17(2000, 3, 175, 185, 12, 3, bgcol='black')
    #effect17(2000, 30, 175, 185, 12, 1, bgcol='black')
    #effect18(2620, 115, 3.5, 14, 2, 1, bgcol='black')
    #effect19(360, 6, 23, 185, 6, 6, 1, bgcol='black') 
    #effect19(360, 40, 23, 185, 6, 20, 1, bgcol='black')
    #effect19(1000, 20, 45, 215, 15, 20, 1, bgcol='black')
    #effect20(120, 12, 91, 3, 12, .8, 1, bgcol='black') 
    #effect20(120, 12, 91, 3, 12, .8, 7, bgcol='black') 
    #effect20(120, 12, 91, 300, 12, .8, 1, bgcol='black')
    #effect20(15, 50, 150, 30, 12, .8, 7, bgcol='black')
    #effect20(5, 250, 200, 200, 120, .008, 1, bgcol='black')
    #effect21(420, 4, 65, 82.5, 25, 2, 1, bgcol='black')
    #effect21(200, 40, 45, 215, 2, .05, 1, bgcol='black')        
    #effect21(400, 200, 10, 350, 1, .005, 1, bgcol='black') 
    #effect22(50, 4, 65, 82.5, 2, 1, bgcol='black')
    #effect22(80, 40, 65, 82.5, .1, 1, bgcol='black')
    #effect22(200, 200, 65, 82.5, .01, 1, bgcol='black')
    #effect22(200, 30, 15, 345, .01, 1, bgcol='black')
    #effect22(200, 40, 15, 345, .01, 1, bgcol='black')
    #effect22(200, 150, 15, 345, .01, 1, bgcol='black')
    #effect22(300, 300, 15, 345, .001, 1, bgcol='black')
    #effect23(50, 4, 65, 2, 1, bgcol='black')
    #effect23(50, 40, 65, 2, 1, bgcol='black')
    #effect23(50, 8, 65, 2, 1, bgcol='black')
    #effect23(50, 70, 65, 2, 1, bgcol='black')
    #effect23(50, 70, 65, .1, 1, bgcol='black')
    #effect23(50, 50, 300, .1, 1, bgcol='black')
    #effect23(500, 50, 355, .01, 1, bgcol='black')
    #effect23(5000, 5, 5, .01, 1, bgcol='black')
    #effect24(180, 30, 100, 1, bgcol='black')
    #effect24(180, 30, 200, 1, bgcol='black')
    #effect24(180, 90, 200, 1, bgcol='black')
    #effect24(180, 250, 200, 1, bgcol='black')
    #effect24(180, 350, 200, 1, bgcol='black')
    #effect24(360, 45, 200, 1, bgcol='black', pencol=True, pal=3)
    '''
    effect25(360, 6, 3, 1, bgcol='black', pal=1)
    GotoCenter()        
    effect25(360, 5, 3, 1, bgcol='black', pal=1)
    GotoCenter()        
    effect25(360, 4, 3, 1, bgcol='black', pal=1)
    GotoCenter()        
    effect25(360, 3, 3, 1, bgcol='black', pal=1)
    GotoCenter()        
    effect25(360, 2, 3, 1, bgcol='black', pal=1)
    GotoCenter()        
    effect25(360, 3, 8, 1, bgcol='black', pal=1)
    GotoCenter()        
    effect25(360, 3, 25, 1, bgcol='black', pal=1)
    '''
    #effect26(100, 10, 3, 10, 2, 2, 1, bgcol='black', pal=5)
    #effect26(50, 20, 3, 10, 2, 2, 1, bgcol='black', pal=5)
    #effect27(1000, 10, 5, 800, 2, 2, 1, bgcol='black', pal=5)

    # Each new call to this function draws a different effect!
    effect28(1000, 999999999, 999999999, 1, bgcol='black')            
    #effect28(1000, 999, 999, 1, bgcol='black')
    #effect28(1000, 99, 9999, 1, bgcol='black')
    #effect28(1000, 9, 150, 1, bgcol='black')

    tu.exitonclick()

Note: If you want to see how effects are drawn, just delete # before tu.speed(0) and write # before tu.tracer (10000, 9999).

You can find useful information about Python tricks (and other things) on the site:

Python&Math

0
Subscribe to my newsletter

Read articles from Miodrag Golubović directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Miodrag Golubović
Miodrag Golubović

Electrical engineer. He programs in several programming languages. Python because it's relatively lightweight, calculates with very large numbers, has great support and tons of pre-built stuff that you just need to include in your program.