HomeTurtlegraficsGPanelRobotics WebTigerPython
 Python - Online
Deutsch   English   

5. Variables

 

 

YOU LEARN HERE...

 

that variables in computer science are understood as placeholders for values that can change during program execution. A variable has a name and a value. You are free to choose the variable name. Key words, special characters and names beginning with a number are not permitted.

 

 

EXAMPLES

 

Examle 1: Drawing a pyramid
To draw the pyramid on the right, you use two variables. The variable w for the width and y for the y-coordinate of the center of the rectangles. The variable w is initially assigned the value 40, which is reduced by 4 each time the slice is passed. To do this, the program retrieves the old value w and subtracts 4 from it. This new value is stored in w again. You write: w = w - 4. This is therefore not an equation in the mathematical sense, but an assignment.

Similarly, with y = y + 2 the value of the variable y is increased by 2 after each rectangle drawn. Instead of y = y + 2, you can write y += 2 for short.

Program:     

# Gp5a.py
from gpanel import *

makeGPanel(-20, 20, -20, 20)

setColor("blue")
w = 36
y = 0
repeat 9:
    pos(0, y)
    fillRectangle(w, 2)
    w = w - 4
    y = y + 2
► Copy to clipboard
 




Example 2
: Drawing a color gradient
The figure below is created by 25 concentric squares that get smaller and darker. The largest square has a side length of 50, the next side length is 2 smaller and so on. At the same time, the green color component is reduced by 10 with each square drawn. After 25 squares have been drawn, the color is almost black.

Program:     

# Gp5b.py
from gpanel import *

makeGPanel(-30, 30, -30, 30)
g = 255
size = 50

repeat 25:
    setColor(0, g, 0)
    fillRectangle(size,size)
    g = g - 10
    size = size - 2
    delay(100)
► Copy to clipboard
 

 

Example 3: Reading in the value of the variable interactively and drawing random points
You can use an input dialog to enter the value of a variable interactively.

When calling
n = inputInt("Number of random points?")
an input dialog opens where you can enter a number. When you press the OK button, the dialog box closes and the entered value is assigned to the variable n.
 

In the repeat loop, this value is used as the number of repetitions.

Program:      

# Gp5c.py
from gpanel import *
from random import randint

n = inputInt("Anzahl Zufallspunkte?")
makeGPanel(0, 100, 0, 100)

setColor("lime")

repeat n:
    pos(randint(0, 100), randint(0, 100))
    fillCircle(2)
► Copy to clipboard
 

 

 

REMEMBER YOU...

 

You use variables for values that change during program execution. Each variable has a name and a value.

With x = x + 1 (short x = += 1) , the value of the variable x is increased by 1,
x = x - 1 (short x = -= 1) decreases the value of the variable x by 1.

 

 

TO SOLVE BY YOURSELF

 

1)


Use the repeat loop to draw 20 concentric circles.

 
 

2)


Use the command fillArc(radius, startangle, stopangle) and draw smaller and darker semicircles..

 


 

3)


Use the input dialogc = inputString(“Enter a color”) to enter an X11 color name and save it in the variable c. A filled circle is then drawn with the color c.