Using geogebra.org/python to learn basics in Python in four lessons

This text explores how GeoGebra’s Python extension can be used both to learn the fundamentals of the Python programming language and to gain a deeper understanding of mathematical analysis and GeoGebra itself. By working with programming in a mathematical context, participants develop computational thinking alongside core mathematical concepts. Presentation slides from 2026 Latvia conference. 1. Introduction Python, developed in the late 80’s has become the most used programming language (Tiobe, 2026-01-20). It is an easy-to-understand programming language which suits beginners and professionals alike. It’s a versatile language that can be used both for home projects, in school and by big companies. Python is widely used in science, not only mathematics, but in other sciences as well, by being a useful instrument for simulation, visualization and data analyzing. Giving students a basic knowledge in the language is both beneficial for the students’ futures, but computational thinking is also part of many countries curricula for upper secondary school mathematics (for example Sweden (2026-01-20)). GeoGebra now provides a Python platform, so there is no need to install neither Python, nor an IDE, to be able to make smaller programs in Python. We can also interact with GeoGebra, and have this platform function as a bridge between mathematics and programming.
2. Learning Python with GeoGebra in four sessions The core of the seminar consists of four structured sessions. The first three sessions focus on fundamental Python concepts, with mathematics and GeoGebra used primarily for short applied examples.The fourth session introduces object-oriented programming, where GeoGebra’s Python classes play a central role. Lesson 1 - Core concepts (print, variables, expressions, calculations) Print and types First, I show the students the IDE, in this case PyGgb, how to create, save and run scripts. Then I introduce them to the print function, for example with the “Hello, World” program. The most classic program in the world to make the first time you make a program is “Hello, World!”, which just prints “Hello, World!” to the terminal. This is a good way to show the print-function, and how it works. print("Hello, World!") and what it does. This also leads directly into the next segment about variables. Variables Printing “Hello, World!” works because it is a string (str). print(Hello, World!) doesn’t work because it is neither a string nor a number. This is a good way to instruct the students on differences between str, int, float, variables and expressions. String (str): word or sentences Integer (int): whole numbers Float (float): a number with decimal points Boolean (bool): True or False, denotes if a statement is true or false Variable (var): A container of a str, int, float, bool, function or other expressions A variable is a text that is declared with an equal sign. x = 5 y = 2 A variable is a text that is declared with an equal sign. Means that x is 5 and y is 2. We see here that neither x nor y has citation marks, making them NOT strings but variables. Both x and y here are ints and ints can be printed with print(). Here it can be good to comment on “ and ‘ being equal in terms of citation. Exercise: Try these commands, and analyze the output: print(2) print("2") print('2') print(two) print("two") a = 2 print(a) print(2 + 3) b = 3 print(a + b) print(c) c = 2 + 3 print(c) Error If the program ends and we are given an error, this means we have done something wrong and need to correct it. The output will tell us what is wrong, and where the error is, so we can correct the code. Operations All the usual operations are possible in Python, but some look a little different than for example in GeoGebra. Try it out: a = 9 b = 2 print(a + b) # Addition print(a - b) # Subtraction print(a * b) # Multiplication print(a / b) # Division, always a float print(a // b) # Floor division, always rounded down to int print(a ** b) # a to the power of b print(a % b) # modulo, outputs the remainder of the division Formulas and expression Using words as variables makes it easy to keep track of our variables. Remember that the only special sign we can use in variables, and numbers can only be used together with other signs. Try: width = 4 height = 6 rectangle_area = width * height print(rectangle_area) 4 = "four" 4 = four four = 4 four_4 = 3 _ = "underscore" Input The input function is an import, built in, function that takes values from the user, in the terminal. We can then save these and use them. Inputs are always strings, but can be converted if the string is compatible. If not saved, the value can not be used anywhere, so we have to catch it with a variable. Try: input("Write something here: ") a = input("Write something here: ") print(a) r = input("Radius: ") area = 3.14159 * r ** 2 print(area) r = float(input("Radius: ")) area = 3.14159 * r ** 2 print(area) r = float(input("Write a word, not a number: ")) GeoGebra We will now incorporate GeoGebra with the teaching, using points. A point in PyGgb takes two coordinates, an x and a y, either as floats or ints. Try: A = Point(0, 0) B = Point(4.6, -2) C = Point(4 + 3, 3-1) See how these points are now drawn in the GeoGebra window. Suggested exercises:
  • Calculate the area of a circle
  • Convert temperature with input
  • Draw three points forming a triangle
Lesson 2 – Conditions, Loops, Comments and Imports Mathematics focus: Comparisons, repeated calculations, number sequences In this lesson, we introduce structures that allow a program to make decisions, repeat calculations, and remain readable even as it becomes longer. These ideas correspond directly to mathematical reasoning, where we work with cases, repeated steps, and clear notation. Conditions A condition allows the program to choose between different paths of execution. This is done by evaluating a comparison, which always results in either True or False. Based on this result, the program decides which code to run next. Try: x = int(input("Write a whole number: ")) if x > 0: # Check if the input data is more than 0 print("Positive") elif x < 0: # Check if the input data is less than 0 print("Negative") else: # This equals to x == 0 print("Zero") The program begins by asking the user to input a whole number. Since input always returns a string, the value must be converted to an int (or float) before any comparison can be made. The program first checks whether the number is greater than zero. If that condition is true, the print statement is executed and the rest of the conditions are skipped. If the condition is false, the program moves on to check whether the number is less than zero. If that is also false, the only remaining possibility is that the number is exactly zero, and the final block is executed. This structure ensures that exactly one outcome is chosen. x == 0 means that x is exactly 0, but this is unnecessary to check if we already checked if it’s more or less than 0, we just use else. Tests to try after this section include changing the comparison operators and observing how the output changes. Try: Replace x > 0 with x >= 0 and see which cases are now classified differently. Remove the elif and write a second if instead, then test what happens when the number is negative. Try entering non-numerical input and observe the error message produced by Python. Absolute Value Conditions can also be used to modify values safely. A common mathematical operation is the absolute value, which ensures that a number is never negative. Try: x = int(input("Write a whole number: ")) if x < 0: absolute = -x print(f"The absolut of {x} is {absolute}") This code checks whether x is negative. If it is, the value is multiplied by -1, turning it into a positive number. If x is zero or already positive, the condition is false and the value remains unchanged. After this code runs, x represents the absolute value of the original number. Tests to try here include printing the value of x before and after the condition to clearly see the change. Try assigning different values to x, such as negative numbers, zero, and positive numbers, and verify that the result is always non-negative. Combine this code with user input so that the absolute value is computed for numbers entered in the terminal. For-loops A for-loop is used when the same calculation needs to be repeated many times with different values. This is especially common in mathematics when working with sequences, tables, or repeated evaluations of a formula. Try: for i in range(1, 11): print(7 * i) Here, the loop variable i takes on the values from 1 to 10 (the last value is always 1 less than the chosen number). For each value, the expression 7 * i is evaluated and printed. The loop stops automatically once the range is done. This makes a multiplication table from 7 to 70. Tests after this section include changing the range to start at 0 or to stop at a different number and observing the effect. Replace 7 * i with another expression, such as i * i, and interpret the output as a sequence of squares. Try printing both i and the result on the same line to better see the relationship between input and output values. Sums Loops become even more powerful when combined with variables that store intermediate results. This is often called an accumulator. s = 0 for i in range(1, 101): s += i print(s) The variable s is initialized to zero and represents the current sum. Each time the loop runs, the value of i is added to s. This continues until all numbers from 1 to 100 have been processed. When the loop finishes, s contains the total sum. This is a direct computational version of a mathematical summation. Tests to try include printing the value of s inside the loop to see how it grows step by step. Change the range so that the sum starts at a different number or ends earlier. Modify the expression so that only even numbers are added and verify the result. Comments Comments are lines in the program that are meant to explain the code to humans. Python ignores them completely when running the program. # This is a comment Comments can be used to describe what a program does at the top of a file, to explain the purpose of a specific variable, or to clarify why a certain solution was chosen. They are also useful for temporarily disabling code without deleting it, which is helpful when testing or debugging. For example, a comment can be placed above a block of code to explain its role: # Calculate the sum of numbers from 1 to 100 s = 0 for i in range(1, 101): s += i print(s) Comments can also be placed at the end of a line to explain a specific operation: s += i # add current value of i to the sum Another important use of comments is to document assumptions and limitations. For example, a comment can state that a variable is assumed to be an integer or that a certain input must be positive. This makes the code easier to understand and maintain. Tests to try include adding comments that explain every line of a short program, then running the program to confirm that comments do not affect the output. Comment out a line of code and observe how the program behavior changes. Write misleading comments and discuss why accurate comments are important. PyGgb Enhancement Loops can be combined with GeoGebra to visualize mathematical ideas. for x in range(-5, 6): Point(x, x ** 2) For each value of x between −5 and 5, a point is created with y-coordinate equal to x². The collection of points forms a parabola. Each iteration of the loop corresponds to evaluating the function for a specific input value, just like filling out a value table in mathematics. Tests here include changing x**2 to another expression, such as 2*x + 1, and observing the new shape. Increase the range to include more points and see how the graph becomes clearer. Try using floating-point values by manually specifying points instead of relying only on integers. Suggested Exercises Write a program that calculates the factorial of a number using a loop and includes comments explaining each step. Write a program that generates the Fibonacci sequence and comment on how each new number is constructed.
Lesson 3 – Lists and Functions Mathematics focus: Datasets, functions, statistics In this lesson, we move from working with single values to working with collections of values. At the same time, we introduce functions, which allow us to describe mathematical relationships in a structured and reusable way. Together, lists and functions make it possible to work with data, formulas, and statistics in a clear and organized manner. Lists A list is a variable that can store multiple values at once. Instead of having many separate variables, a list keeps related data together in a specific order. Lists are written using square brackets, and the elements are separated by commas. Try the following code. What does it do? numbers = [3, 7, 2, 9, 4] # This is a list! print(sum(numbers) / len(numbers)) Here, the variable numbers contains five integers. The function sum(numbers) adds all the values in the list, and len(numbers) counts how many values the list contains. Dividing the sum by the length gives the average of the dataset. This mirrors how averages are calculated in mathematics, but the list allows the program to handle any number of values without changing the formula. Tests to try at this point include changing the values in the list and observing how the average changes. Add more numbers to the list and confirm that the same code still works. Try printing numbers itself to see how lists are displayed. Try printing len(numbers) alone to verify that it represents the number of data points. We can create a a list from a range, like this:  my_list = list(range(1,100,2)) print(my_list) What did the range create here? Functions A function in programming is similar to a function in mathematics. It takes an input, performs a calculation, and produces an output. Functions allow us to define a rule once and then reuse it many times. #This is a function def f(x): return 2*x + 3 This code defines a function named f. The variable x is the input, and the expression 2*x + 3 is the output. The return statement sends the result back to wherever the function was called. The function itself does not do anything until it is used. Tests after defining the function include calling it with specific values, such as print(f(0)), print(f(1)), and print(f(-2)). Compare the output to the values obtained by calculating the expression manually. Try changing the formula inside the function and observe how all outputs change consistently. Function and Loop Functions become especially powerful when combined with loops. This allows us to evaluate the same function for many input values automatically. for x in range(-5, 6): print(f(x)) In this loop, the variable x takes values from −5 to 5. For each value, the function f(x) is evaluated and printed. This produces a sequence of function values and is equivalent to creating a table of values in mathematics. The loop ensures that the same rule is applied systematically to each input. Tests here include printing both x and f(x) on the same line to clearly see the input-output relationship. Change the range to include more values or fewer values and observe how the sequence changes. Try using a different function definition and reuse the same loop without modifying it. Function and List Functions can also be written to work directly with lists. This is useful for statistical calculations and data analysis. def average(lst): return sum(lst) / len(lst) This function takes a list as its input and returns the average of its elements. The variable name lst represents the entire list, not a single value. The function works for any list of numbers, regardless of its length, as long as it contains at least one element. Tests after this section include calling the function with different lists, such as average([1, 2, 3]) or average(numbers). Try passing a list with only one value and confirm that the average is that value. Try passing an empty list and observe the error that occurs, then discuss why dividing by zero is not allowed. PyGgb Enhancement Lists can also store geometric objects, not just numbers. This is often the first time students store objects inside a list. points = [] for x in range(-5, 6): p = Point(x, f(x)) points.append(p) The variable points starts as an empty list. Each time the loop runs, a new point is created using the function f(x) to determine the y-coordinate. The point is then added to the list using append. By the end of the loop, the list contains all points that represent the graph of the function. At this stage, students use objects without discussing object-oriented programming explicitly. Tests here include printing the points list to see that it contains point objects. Try changing the function and rerunning the code to generate a different graph. Increase the range to include more points and observe how the visual representation becomes smoother. Suggested Exercises Write a program that calculates basic statistics, such as average and maximum value, for a set of measurement data stored in a list. Draw two different functions using points and compare their shapes visually. Create a function that finds the maximum or minimum value of a dataset and show these values using points in PyGgb.
Lesson 4 – Classes (focus on PyGgb) Up to this point, we have used objects such as points, circles, and segments without discussing how they are constructed internally. In this lesson, we take a closer look at what these objects really are. The key idea is that every object we draw in PyGgb is created from something called a class. A class describes what kind of object something is, what data it contains, and what actions it can perform. Class, Object, Attribute, and Method Consider the following code: A = Point(3, 4) This code creates a point at the coordinates (3, 4). The name Point refers to a class. The variable A refers to an object that was created from that class. The object A represents one specific point, while the class Point describes what all points have in common. Once the object exists, we can access information stored inside it. Try: A = Point(3, 4) print(A.x) Here, x is an attribute of the point. Attributes store data that belongs to the object. In this case, the attribute x stores the x-coordinate of the point. Objects can have many attributes, and each object has its own values. Attributes can also be changed after the object has been created. A.color = "#FF0000" This changes the color attribute of the point A to red. The object updates visually because the attribute controls how it is displayed. Tests to try here include printing both A.x and A.y to confirm the coordinates. Change the color to a different value and observe the visual result. Try deleting the point and then attempting to access one of its attributes to see what happens. More Examples with Objects The same ideas apply to other geometric objects. c = Circle(A, 5) print(c.radius) This code creates a circle using the point A as its center and 5 as its radius. The variable c refers to the circle object. The attribute radius stores the size of the circle, and printing it shows that the value is stored inside the object. s = Segment(A, Point(0, 0)) print(s.length) Here, a segment is created between two points. The segment object has an attribute called length, which is calculated from the positions of its endpoints. Tests after this section include changing the radius of the circle and observing the visual difference. Try creating segments between different points and printing their lengths. Delete one of the defining points and observe how it affects the connected objects. Comparison with Your Own Class To better understand how PyGgb objects work, it is useful to compare them with a class we write ourselves. class Point2D: def __init__(self, x, y): self.x = x self.y = y This code defines a class named Point2D. The method __init__ is called automatically when a new object is created from the class. The variable self refers to the object being created. When we write self.x = x, we are creating an attribute named x inside the object and assigning it a value. This is directly comparable to how a PyGgb point works. Writing self.x inside the class corresponds to accessing A.x outside the class. The object stores its own data, and attributes are accessed using the dot notation in both cases. Tests to try here include creating an object using P = Point2D(3, 4) and printing P.x and P.y. Change the values and confirm that they belong to the specific object. Create two different Point2D objects and verify that they store independent values. Your Own Mathematical Class Classes can also be used to represent mathematical objects, such as functions. class Quadratic: def __init__(self, a, b, c): self.a = a self.b = b self.c = c def value(self, x): return self.a*x**2 + self.b*x + self.c This class represents a quadratic function of the form ax2+bx+cax^2 + bx + cax2+bx+c. The coefficients are stored as attributes of the object. The method value computes the function value for a given x-value. This keeps the formula and its parameters together in one structured object. Tests after this section include creating a quadratic function with different coefficients and evaluating it for several x-values. Try printing the attributes a, b, and c directly to confirm that they are stored inside the object. Modify the coefficients and observe how the output changes. Connection to PyGgb Once a mathematical class has been defined, it can be used together with geometry. f = Quadratic(1, 0, -4) for x in range(-5, 6): Point(x, f.value(x)) Here, an object representing a quadratic function is created. The loop evaluates the function for x-values from −5 to 5 and creates a point for each value. The result is a visual graph of the function. This shows how classes, functions, loops, and geometry work together to model mathematics programmatically. Tests here include changing the coefficients of the quadratic function and observing how the graph changes. Increase the range to include more points and see how the curve becomes smoother. Create two different quadratic objects and draw both graphs for comparison.
3. Suggestions for further work One important aspect of continuing learning is finding inspiration for new projects. This often begins by observing mathematical problems or patterns and asking whether they can be explored or visualized using code. Even simple ideas, such as modifying a known formula, testing extreme values, or animating a geometric construction, can lead to meaningful projects. Encouraging curiosity and experimentation helps students move from following instructions to creating something of their own. The seminar also highlights examples of what can be built using Python in GeoGebra. By combining programming with geometry, students can construct dynamic figures, visualize functions, simulate mathematical processes, and explore relationships that are difficult to see with static drawings. Revisiting familiar mathematical topics with these tools often leads to deeper understanding, as the program makes abstract ideas concrete and interactive. Finally, attention is given to ways of extending both the mathematical and programming concepts explored during the seminar. Students are encouraged to refine their existing programs, add new features, and connect multiple ideas into larger projects. For example, a simple function graph can be expanded into a full investigation of parameters, or a geometric construction can be enhanced with user input and automated calculations. Through these extensions, students continue to develop logical thinking, mathematical reasoning, and confidence in using programming as a tool for exploration and problem solving. One example is a dynamic function explorer. A program can be written where a function is defined using parameters, and points are generated automatically to show its graph. By changing the parameters, either directly in the code or through user input, students can immediately see how the graph changes. This can be used to explore linear functions, quadratic functions, or more advanced expressions, and to investigate how coefficients affect shape, position, and intersections. Another example is a geometric construction that updates automatically. A triangle can be created where the vertices are defined by variables, and the program can calculate and display properties such as side lengths, angles, perimeter, and area. By moving or redefining the points, the calculations update instantly. This makes it possible to investigate geometric relationships and test conjectures in a visual and interactive way. A further possibility is simulating mathematical sequences and patterns. A program can generate sequences such as arithmetic sequences, geometric sequences, or the Fibonacci sequence, and display them both numerically and graphically. For example, points can be plotted to show how a sequence grows, or line segments can be used to illustrate recursive relationships. Statistical investigations are also well suited for programming. Measurement data can be stored in lists, and programs can be written to compute averages, minimum and maximum values, and simple distributions. These values can then be visualized using points, bars, or dynamically updated text in GeoGebra, helping students connect numerical results to graphical representations. Another example is modeling real-world situations using mathematics. Programs can simulate motion, growth, or decay by repeatedly evaluating functions over time. For instance, a loop can be used to model constant speed motion, or exponential growth, with points or segments showing how quantities change step by step. This helps link mathematical formulas to processes and change. Finally, more open-ended projects can combine several ideas from the seminar. A student might create a small interactive tool where the user inputs values, the program performs calculations using functions and lists, and the results are shown geometrically. Such projects naturally integrate variables, conditions, loops, functions, lists, and classes, reinforcing both programming structure and mathematical reasoning. These examples demonstrate that Python in GeoGebra is not limited to isolated exercises, but can be used as a flexible environment for exploration, visualization, and creative mathematical work.