Level 2: Events - IncrediCoders/Python1 GitHub Wiki
Mrs. Scratcher added this page on March 14, 2025
Hello class! I prepared this bonus article that will help explain Events in Python programming!
In addition to this bonus article, you can find other bonus articles that teach you the topics I covered in Level 2: Variables, Conditional Statements: If Then Else, Initialization Files, Displaying Text, and While Loops.
An event is something that happens, and your code can be set up to respond when it does. Events are like signals or triggers that tell your program. Imagine you hear the school bell ring. That’s an event! It tells you it’s time to head to class.
In coding, events can be things like clicking a button, pressing a key, moving the mouse, or tapping the screen.
Events make your programs interactive! Without events, your code just runs straight through. But with events, your program can wait and respond to what the user does.
That’s how video games work, how apps know when to scroll, and how Paul Python knows where to walk across the map.
Let’s look at a simple example using Paul Python.
Scenario: Paul Python is playing a game where he only starts walking to school when the spacebar is pressed.
key_pressed = "space"
if key_pressed == "space":
print("Paul Python walks to school!")
What does this mean?
If the spacebar is pressed, the program prints a message saying Paul Python is walking.
Even though this is a simple example, it shows how an event works. You can write code that waits for something to happen, then takes action.
Many programming languages use events, but they write them a little differently. No matter the language, the idea is the same: wait for something to happen, then do something.
Let’s look at how three different languages respond to a simple event, pressing a button.
Python
button_pressed = True
if button_pressed:
print("Button was pressed!")
Java
button.addActionListener(e -> {
System.out.println("Button was pressed!");
});
C#
button.Click += (sender, e) => {
Console.WriteLine("Button was pressed!");
};
What’s the Difference?
All three languages can handle events, like pressing a button or a key. But the way you write the code is a little different:
- Python is the easiest to read. It often uses plain words and simple structure, which is great for beginners.
- Java and C# need a bit more setup. They use something called event listeners to wait for actions like button clicks.
- In Java and C#, you have to tell the program exactly what kind of event you're waiting for and how to respond, using a bit more code.
No matter which language you use, the main idea stays the same: your code waits for something to happen, then does something in response.
In the Wiki, we tackle the topic of Events on this page:
- On the Level 5: Help page's Glossary of Terms, we define events in #6.
Next, if you need help completing your Class Introductions assignment from the book, go to Level 2: Help.
After you complete Level 2 in the book, you can take on the two extra challenges to add to your Class Introductions program and learn more! When you're done, you can move on to Level 3, the Classroom Quiz!
-
Challenge 1: Add the introduction for RAM and ROM and one for Amphib Ian, totaling 14 introductions!
-
Challenge 2: Instead of using an if/else ladder, you will put the text and images into a Python list (it's a list of variables).
In addition to this Help page and the instructions for our Level 2 challenges, we also have Online Articles, a Learning Quiz, an Unplugged Activity, and a Rewards article:
-
Level 2: Online Articles - I made you a list of different web pages I found, which will help you learn more about variables, events, and if statements, in addition to what you're learning in our Class Introductions project.
-
Level 2: Learning Quiz - I wrote some questions in case you want to quiz yourself about what you learned. Or you can teach others and quiz them!
-
Level 2: Unplugged Activity - I wrote this page with more details than what you saw in the book. In this game, you'll have one person act as the developer, and one person act as the variable where the developer choose a noun for the variable to say in the story that they tell!
-
Level 2: Rewards - If you completed the Class Introductions project that we covered in class, then I set up this page to act as a reward. You can see some illustrations of me and learn more about who I am! You'll also find the Apple Award digital download, to show off your accomplishment! Make sure you complete the Level 2 project from the book first though!
After you're completely done with Level 2 (did you do the challenges?), then it's time to move on to Level 3! While you read through Level 3 in your book, you can check out the resources from Mrs. Codala, as she teaches you how to build the Classroom Quiz program:
I hope you learned more about Events! They are an essential part of Python programming.
-- Mrs. Scratcher