At My Fingertips
Rapid Playground
In this activity you learn how to create ellipses and colors, and you learn how to define your own names for things.
Assume you have a friend who does not see the figure below:
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 ellipse and the color named blue to get Python to create the above ellipse for you:
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.
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:
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:
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.
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?
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:
red, green, blue, cyan, magenta, yellow, black, and white
Did you fix the code? (If not, you will bump into problems below.) You should see a (light) green watermelon-y ellipse.
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 rgb_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:
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
PyTamaro is a project created by the Lugano Computing Education Research Lab at the Software Institute of USI
Privacy Policy ⢠Platform Version 1cd5229 (Tue, 05 Nov 2024 16:55:57 GMT)