At My Fingertips

Documentation

Did you just start the "Welcome to PyTamaro" curriculum?

If yes, welcome!

If no, you may want to go to the "Welcome to PyTamaro" curriculum and start it, and come to this activity from there. If you start this activity from within a curriculum, you will be nicely guided along a learning path.

About This Activity

This activity takes you on a whirlwind tour through programming with Python and PyTamaro. By the end of this activity you will have seen a program that can produce the coat of arms of Ticino, the beautiful state in the mediterranean part of Switzerland:

ticino coat of arms

You will get a taste of many things, but we will only skim the surface. If at the end of this activity you continue along the path through the Welcome curriculum, we will explain and explore everything in more depth. If you complete the entire Welcome curriculum, you should understand several important foundational concepts of programming, and you should be ready to explore the various more advanced curricula and activities available on this web site.

Programming?

This web site helps you on your journey to learn to program. Programming means to describe structures and processes in a clear way. The description must be unambiguous: there must be only one possible interpretation. The lack of ambiguity allows computers to execute the instructions given in a program.

We write programs to automate things we do not want to do by hand. We might write a program to add numbers for us, or a program to figure out our next move in a chess game, or a program to fly an airplane, or a program to keep track of our grades.

On this web site we will mostly write programs that create graphics. But let's get started with a non-graphical program, a program that helps us with a buying decision. Assume you see two chocolate bars, one costs 2.45 and the other (a fancy one!) costs 4.95. You have 7.90 with you, and you wonder whether that will be enough to buy the chocolate. Of course you could do the math in your head. But let's be lazy, and ask a computer:

Loading...

If you click "RUN", this program will execute. The two numbers will be added, and the result will be compared to 7.90. If you can afford the two bars, the computer will print the word True, otherwise it will print the word False.

We don't print on paper, but we print on the screen. So you should see the result right below the code cell.

Coding?

Programmers are programming. They write programs.

Alternatively you could say:

Coders are coding. They write code.

The term "code" comes from the old days, where programs were written in a form that was hard to understand for humans. Today, programs can be written in a way that is quite easy for humans to understand. We don't need to "encode" or "decode" much. While we don't usually refer to modern programmers as "coders", we still refer to programs as "code", we refer to parts of a program as "pieces of code", and to programming as "coding".

Python?

While for most people, a python is a snake, for programmers, Python is a programming language. Actually, the Python programming language isn't really named after the snake. It's named after Monty Python.

Python. Photo by David Clode on Unsplash

When talking about languages, most people think about natural languages, such as English, Italian, German, French, and many others. Programmers think about programming languages, such as Python, Java, or JavaScript.

Python is one of the most widely used programming languages, and it is often the first text-based programming language taught in school. But beware: Python is not a simple language. Although one can quickly write some Python code, the language is complex, and many things can go wrong. For this reason, the "Welcome to PyTamaro" curriculum guides you carefully along a safe path. Over the course of that curriculum we try to explain everything you encounter, so you always understand what's going on. We don't want that scary Python snake to constrict you!

But for now, we rush through things quickly, just to get a first taste!

A Python Program with Numbers

We found a large metal sheet in the shape of a coat-of-arms, with a total width of 1 meters, and a total height of 1.2 meters. We would like to clean and color the sheet to turn it into a Ticino coat-of-arms.

Metal sheet. Photo by takis politis on Unsplash Can of red color. Photo by benjamin lehman on Unsplash Can of blue color. Photo by benjamin lehman on Unsplash

We need to paint the left half in the official Ticino red color, and the right half in Ticino blue. The two colors have different prices. Ticino red costs 18.50 CHF per square meter, Ticino blue costs 22.45 CHF per square meter. Let's compute how much we have to spend on color. Try to read and understand the following Python code:

Loading...

Click the "RUN" button to have Python compute and output the total cost. Did you expect that the overall cost of paint is almost the same as the cost of blue paint to cover 1 square meter?

Actually, what is the area to be painted in blue? Let's add a new line at the bottom of the above code to find out: add the statement print(half_m2). This will output the area of one half of the coat of arms. Run the code again. You should see two outputs now: one for the overall cost and one for the half the area.

The above Python code does the following:

  • Import the name pi from the math library. That library contains lots of mathematical code, like the definition of the constant 𝛑.
  • Define a function named circle_area that can compute the area of a circle given a radius. The function expects the radius to be provided as a floating-point number (known as a float, e.g., 1.2) and returns the result as a floating-point number (thus the -> float).
  • Define names for the height and width of the coat-of-arms.
  • Compute the radius of the coat-of-arms and assign it to a name.
  • Compute the rectangular area at the top of one side of the coat-of-arms and assign it to the name rectangle_m2.
  • Compute the area of the 90 degree circular sector at the bottom of one side of the coat-of-arms and assign it to the name sector_m2.
  • Compute the total area of one half of the coat-of-arms and assign it to the name half_m2.
  • Define names for the costs (per square meter) of red and blue paint.
  • Compute the cost of paint for the coat-of-arms.
  • Output the computed cost, so we can see it.

PyTamaro?

While Python is a language, PyTamaro is a library. For a natural language, a library may hold thousands of books full of text written in that langauge. For a programming language, a library may hold thousands of "modules" (sorry, we don't use the term "book" in programming) full of text written in that language.

Library. Photo by Susan Q Yin on Unsplash

PyTamaro is a library for Python. PyTamaro was developed at USI in Lugano. While many traditional libraries play with numbers, or with text, PyTamaro plays with graphics. It contains Python code that specifies unambiguously how to create a graphic.

You could show PyTamaro code to a human who knows Python, and they would be able to follow the instructions and to produce the desired graphic (e.g., by cutting out colored paper pieces and composing them into a graphic). Or you could show PyTamaro code to a machine that knows Python, and the machine will do the same (well, it probably won't cut out paper, but it might print on paper, or draw on the screen). The code describes what to do. Unambigously. It can be read and "executed" or "run" by humans, or by machines. Of course we usually want to let machines do the work for us.

The PyTamaro library defines various things that have to do with graphics: it knows about colors, it knows about some basic graphics (like rectangles or ellipses), and it knows about ways to compose basic graphics into more complex graphics.

A Python Program with Graphics

Let's use functionality from the PyTamaro library to calculate with graphics. We want to compose and output (i.e., show on the screen) the Ticino coat-of-arms.

Loading...

This program does not print a text. It shows a graphic. Run the code and look at the output it produced. Then edit the code: in the last line, change the argument passed to the coat_of_arms function (the number 50) to a larger number. Run your changed code; it should output a larger graphic! The code is flexible: it allows you to produce coats of arms of different sizes.

The code above does the following:

  • Import all the names we need from the pytamaro library:
    • Graphic is a type. Rectangles, circular sectors, and many other things are values of type Graphic. While float is the type of floating-point numbers, Graphic is the type of... graphics.
    • rgb_color is a function that can produce a color given an amount of red, green, and blue light.
    • rectangle and circular_sector are two functions that produce graphics.
    • above, beside, and rotate are three functions that compose graphics into more complex graphics.
    • show_graphic is a function that takes a graphic and outputs it on the screen.
  • Define names, ticino_red and ticino_blue, for two colors.
  • Define a function named coat_of_arms that takes a radius and produces a coat of arms (the graphic composed of two rectangles and two circular sectors).
  • Output the graphic on the screen, with a call to show_graphic.

What You Learned

You learned that programming is about unambiguously defining some instructions, so that they can be executed by a machine (or by a human), and it's clear what the result will be.

You learned that Python is a programming language, and that PyTamaro is a library of code for creating graphics in Python.

You got to see a couple of Python programs, and you encountered a lot of concepts that are involved in programming (ideas like names, functions, types, or output).

If you came to this activity via the "Welcome to PyTamaro" curriculum, you should see an icon in the right sidebar:

Icon of "Perform Output" competency

This indicates that in this activity you got to grow your competency to perform output (with a call to print or show_graphic).

If you didn't understand all of the explanation, that's ok. This activity was just the very first time you practiced that competency. If you mark the activity as completed (with the "Mark Actvitiy as Completed" button at the bottom of the page), you will see the "perform output" competency appear in your personal list of competencies in the left sidebar.

Pill of "Perform Output" competency

The number indicates that you completed one activity that covers this competency. As you complete more and more activities, the number will grow, your understanding of what it really means to "perform output" will grow, and your ability to write programs that output whatever you like will grow as well.

If you continue your trip though the "Welcome to PyTamaro" curriculum, you will learn to write your own programs, and you will gain a better understanding of the concepts you briefly encountered in this activity. You will continuously grow your programming competencies, and the information about your competencies will accumulate in your left sidebar.


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

Ticino Coat of Arms

Logo of PyTamaro

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)