At My Fingertips

Rapid Playground

Documentation

Rugby Ball

In this activity you learn how to create ellipses and colors, and you learn how to define your own names for things.

Asking for a Graphic

Assume you have a friend who does not see the figure below:

image.png

You might have said something like "a magenta ellipse with a width of 80 and a height of 160". Not bad!

Use the function named PyTamaro iconellipse and the color named PyTamaro iconmagenta to get Python to create the above ellipse for you:

Loading...

Click on the "RUN" button above to execute the above code cell. Python will consult the pytamaro libary to learn what the names ellipse, magenta, and show_graphic mean. Then it will call the ellipse function, passing three arguments that define exactly how the ellipse is to look like (its width should be 80, the height 160, and the color magenta). Finally, it will pass the created ellipse to the show_graphic function, which will make it visible.

Naming Things

Above you created a graphic and immediately passed it as an argument to show_graphic.

There is another way to achieve the same goal. It takes an extra step, but it has the advantage that you can use a name to refer to the ellipse you created:

Loading...

The above code creates a yellow ellipse (are rugby balls yellow???) and assigns the created ellipse to the name rugby_ball.

From there on, you can write rugby_ball and Python will use the ellipse you previously created.

Thus, the above code produces the exact same output as:

Loading...

The previous two code cells produced the same output. But their code differs somewhat.

The difference is that the first approach defines a name, rugby_ball, and assigns the ellipse to that name.

Thanks to that name we now can just say "Hey, Python, show the rugby ball!", in code:

show_graphic(rugby_ball)

We can forget about the details (the fact that it is an ellipse, its color, its width and height). Python knows that rugby_ball means the ellipse you created.

Colors

We really would like to create a brown rugby ball. The pytamaro library contains various colors: you just imported two of them, magenta and yellow. Maybe there is a name for the brown color as well? Try! Import brown, and then create a brown ellipse.

What happens when you run your code?

Loading...

You should have gotten an error:

ImportError: cannot import name 'brown' from 'pytamaro'

Why? Because the pytamaro library does not define what brown means!

The library only defines a small number of colors, specifically:

PyTamaro iconred, PyTamaro icongreen, PyTamaro iconblue, PyTamaro iconcyan, PyTamaro iconmagenta, PyTamaro iconyellow, PyTamaro iconblack, and PyTamaro iconwhite

Did you fix the code? (If not, you will bump into problems below.) You should see a black rubgy-ball-y ellipse.

Creating Our Own Color

If you want to use another color, you have to define what it means yourself. One way to do that is to call the function named PyTamaro iconrgb_color to create a color. You have to provide three numbers to that function: how much red light, how much green light, and how much blue light it should mix together to produce your color. Each number is in the range between 0 (nothing) to 255 (a lot).

Brown is quite close to red, so let's take quite a bit of red (100), and then some green (60), and a little bit of blue (30). Run the code below to see the result:

Loading...

This brown looks a bit off. Can you add a bit more red? Change the first argument to rgb_color from 100 to 150, and rerun the code cell.

This looks more like it!

What You Learned

You now know how to create ellipses of different colors and sizes, you know about the small number of colors predefined by the pytamaro library, and you know that there is a function rgb_color to create your own colors.

In terms of programming, you learned how to define your own names: for graphics like rugby_ball = ellipse(200, 100, brown) or for colors like brown = rgb_color(150, 60, 30).

You will get to play with these and many more concepts in most PyTamaro activities.


This activity has been created by LuCE Research Lab and is licensed under CC BY-SA 4.0.

Rugby Ball

Logo of PyTamaro

PyTamaro is a project created by the Lugano Computing Education Research Lab at the Software Institute of USI

Privacy PolicyPlatform Version 96857c4 (Sat, 04 May 2024 09:49:57 GMT)