At My Fingertips

Rapid Playground

Documentation

Watermelon

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:

ellipse

You might have said something like "a blue ellipse with a width of 600 and a height of 100". Not bad!

Use the function named PyTamaro iconellipse and the color named PyTamaro iconblue 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, blue, 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 600, the height 100, and the color blue). 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 watermelons yellow???) and assigns the created ellipse to the name watermelon.

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

Thus, the above code has the exact same effect 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, watermelon, and assigns the ellipse to that name.

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

show_graphic(watermelon)

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

Colors

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

What happens when you run your code?

Loading...

You should have gotten an error:

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

Why? Because the pytamaro library does not define what dark_green 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 (light) green watermelon-y ellipse.

Creating Our Own Color

The green color provided by PyTamaro isn't really close to a watermelon green! 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).

Watermelon green is similar to the default green, but it's darker, and it isn't pure green. Let's use a little bit of red (45), quite a bit of green (152), and also a bit of blue (72). Run the code below to see the result:

Loading...

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 watermelon = ellipse(90, 110, dark_green) or for colors like dark_green = rgb_color(45, 152, 72).

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.

Watermelon

Logo of PyTamaro

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

Privacy PolicyPlatform Version d693e64 (Sun, 05 May 2024 13:57:26 GMT)