At My Fingertips

Rapid Playground

Documentation

Rectangles

In this activity you learn how to create rectangles, and you learn how to use some key features of this web page.

Asking for a Graphic

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

Alt text

Which of the following descriptions is most comprehensive?

  1. A rectangle
  2. A red rectangle
  3. A red rectangle that is wider than high
  4. A red rectangle with a width of 600 and a height of 100

In Python, like in any programming language, we need to be unambiguous when describing what we want. The fourth description above is the most complete. It provides the information we need.

But how exactly do we tell Python that we need "a red rectangle with a width of 600 and a height of 100"? Python has a pretty peculiar grammar. Here is the above request in Python code:

rectangle(600, 100, red)

We ask for a rectangle by calling the function named PyTamaro iconrectangle. You can hover with your mouse over the colored function name to learn more about that function. If you do, you find out that the function requires three arguments: the rectangle's width, height, and color. The arguments have to be provided in parentheses. They have to be separated by commas. And they have to be provided in that specific order; this way it is clear that the first number specifies the width and the second one the height.

The name PyTamaro iconred denotes the color. Hover over the colored name to see its documentation. Yes, it's quite brief.

Using a Library

Python knows very little. It has a super small vocabulary. And the names rectangle and red are not part of its initial vocabulary!

So if you just tell Python to produce a rectangle(600, 100, red), even though this is grammatically correct, Python won't know how to do that. It will complain that it does not know the names rectangle and red.

If you want Python to know what those names mean, you have to teach it! Don't worry, you don't need to teach it yourself. We've already done that for you. We put the definitions of those names, and many more, in a library. Anyone with access to that library can use them. This specific library, with lots of names that have to do with graphics, is called pytamaro.

Here is a code cell, with one line of code. It tells Python that it should go look up the names rectangle and red in the pytamaro library:

Loading...

Click on the "RUN" button above to execute the above code cell. If all goes well, Python will have consulted the library and learned what the names rectangle and red mean. The button will turn grey and say "DONE", and next to the button you will see a green checkmark.

You can edit the above code. For example, you can ask Python to also import the name show_graphic from the pytamaro library by writing:

from pytamaro import rectangle, red, show_graphic

As soon as you change the above code cell, its button will turn blue again, so you can run the changed code.

Every time you run code, the button will turn grey, and every time you edit code, the button will turn blue again.

Let's do one more edit, and this time we make a mistake. Again in the above code cell, enter:

from pytamaro import rectangle, red, show_graphic, sing

Once you run this code, you get an error message, right below the code cell. It says:

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

What went wrong? The pytamaro library does not contain any definition for the name sing. PyTamaro does not know how to sing!

Remove the name sing, and the trailing comma, from the import statement, and run the code cell once more. The button should be grey, and no error should be shown.

Creating Rectangles

Now run the following code cell. It should produce a 600-by-100 red rectangle.

Loading...

The above code cell will call the rectangle function to create a rectangle.

However, nowhere in the code does it say that this rectangle should be shown to you!

If you run a program, that program may produce a value (like a rectangle, or a number), but if you don't ask for it, the value will not be output.

If you want to see the rectangle, or any other graphic, you have to tell Python to show it.

Showing Graphics

To actually see the rectangle we have to do two things:

  1. Call the rectangle function to produce the rectangle
  2. Call the show_graphic function to display the rectangle on the screen

What does show_graphic do? Do you see the colored names at the very top of the code cell below? Right after the word "Docs"? The top of each code cell lists the names that code cell imports from the pytamaro library, so you can hover over them to see their documentation. Hover over show_graphic. See, it... shows a graphic.

So, you can pass your rectangle to the show_graphic function, and the function will output it.

show_graphic(...)  # shows whatever you give as an argument (instead of the ...)

Run the code below, and it should create a rectangle, and then it should show that rectangle right below the code cell.

Loading...

Different Rectangles

In the following code cell, create a blue rectangle with width 100 and height 150, and show it.

First import the definition of the name blue, and then replace the ... with the appropriate rectangle call.

Loading...

Now create a green square with side length 200 (call rectangle with width and height both specified as 200, don't forget to import green).

Loading...

What You Learned

You now know how to create rectangles of different colors and sizes.

In terms of programming, you had your first encounter with quite a few fundamental concepts:

  • numeric literals (numbers like 600 or 150)
  • functions (like rectangle and show_graphics)
  • names (for functions, but also for other kinds of values, like the colors red, green, or blue)
  • passing arguments to parameters of functions (like the argument 600 to the parameter width)
  • function calls (to get the function to do something, like rectangle(600, 100, red))
  • libraries (which contain definitions for names, like pytamaro, which defines lots of names for composing graphics)
  • import statements (to importing names from libraries, like from pytamaro import red)
  • output (the show_graphic function will output the graphic you pass it as an argument)

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.

Rectangles

Logo of PyTamaro

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

Privacy PolicyPlatform Version d900fe4 (Fri, 26 Apr 2024 16:23:18 GMT)