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 magenta ellipse with a width of 80 and a height of 160". Not bad!
Use the function named ellipse and the color named magenta 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
, 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.
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 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:
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.
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?
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:
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 black rubgy-ball-y ellipse.
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).
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:
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!
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
PyTamaro is a project created by the Lugano Computing Education Research Lab at the Software Institute of USI
Privacy Policy ⢠Platform Version c08406b (Wed, 20 Nov 2024 12:30:00 GMT)