▲ 6 r/pygame+1 crossposts

Cupta serpant added by Tony

Tony added the Cupta unit, a serpent of dark magic who returned from the caverns and tries to siphon your life essence, with this git commit.

Tony also changed the control of player's direction from keyboard to mouse, making the player easier to control. But, this now only works when the mouse pointer is inside the game window. If it's outside the direction is still controlled by keyboard as before. So you can now choose the direction control that you prefer.

See the PythonLearningGame repo to add your own game elements to this collaborative project, or simply tell us what you would like to see added to the game.

previous PythonLearningGame post

u/Sea-Ad7805 — 1 day ago

New Putler unit added by Anonymous-Me-Me-Me

Anonymous-Me-Me-Me added a new Putler unit with this git commit and message:

Putler unit added, any likeness to real people is completely coincidental. It wants you to sign its contract, but if you get too close it tries to run to its bunker. Best to zero it before it goes into history for destroying an "empire".

We did had to add sunglasses for copyright and identity protection reasons.

PythonLearningGame is a collaborative Python project for fun and educational purposes. To add your own game elements see the PythonLearningGame repo.

previous PythonLearningGame post

u/Sea-Ad7805 — 5 days ago

Adding objects to set or dictionary: equality and hashing

An exercise to help build the right mental model for Python data.

# Output of this Python Program?
def main():
    o1, o2 = MyClass(1), MyClass(1)
    myset = {o1}
    print(o2 in myset, end=' ')
    o1.set_value(1000)
    print(o1 in myset, end=' ')

class MyClass:
    def __init__(self, v):
        self.v = v
    def set_value(self, v):
        self.v = v

main()

class MyClass:
    def __init__(self, v):
        self.v = v
    def set_value(self, v):
        self.v = v
    def __eq__(self, other):
        return self.v == other.v
    def __hash__(self):
        return hash(self.v)

main()

# --- possible answers ---
# A) TypeError: unhashable type: 'MyClass'
# B) True False False False
# C) True False False True
# E) False True True True
# D) False True True False
# See "Solution" for correct answer.
  • Solution
  • More exercises
  • Explanation: "User-defined classes have __eq__() and __hash__() methods by default (inherited from the object class); with them, all objects compare unequal (except with themselves) and x.__hash__() returns an appropriate value such that x == y implies both that x is y and hash(x) == hash(y)."
reddit.com
u/Sea-Ad7805 — 6 days ago

Adicionando objetos a um conjunto ou dicionário: igualdade e hashing

Um exercício para ajudar a construir o modelo mental correto sobre os dados em Python.

  • Solução
  • Mais exercícios
  • Explicação: "User-defined classes have __eq__() and __hash__() methods by default (inherited from the object class); with them, all objects compare unequal (except with themselves) and x.__hash__() returns an appropriate value such that x == y implies both that x is y and hash(x) == hash(y)."
u/Sea-Ad7805 — 7 days ago

Adding objects to set or dictionary: equality and hashing

An exercise to help build the right mental model for Python data.

# Output of this Python Program?
def main():
    o1, o2 = MyClass(1), MyClass(1)
    myset = {o1}
    print(o2 in myset, end=' ')
    o1.set_value(1000)
    print(o1 in myset, end=' ')

class MyClass:
    def __init__(self, v):
        self.v = v
    def set_value(self, v):
        self.v = v

main()

class MyClass:
    def __init__(self, v):
        self.v = v
    def set_value(self, v):
        self.v = v
    def __eq__(self, other):
        return self.v == other.v
    def __hash__(self):
        return hash(self.v)

main()

# --- possible answers ---
# A) TypeError: unhashable type: 'MyClass'
# B) True False False False
# C) True False False True
# E) False True True True
# D) False True True False
# See "Solution" for correct answer.
  • Solution
  • More exercises
  • Explanation: "User-defined classes have __eq__() and __hash__() methods by default (inherited from the object class); with them, all objects compare unequal (except with themselves) and x.__hash__() returns an appropriate value such that x == y implies both that x is y and hash(x) == hash(y)."
reddit.com
u/Sea-Ad7805 — 7 days ago

Adding objects to set or dictionary: equality and hashing

An exercise to help build the right mental model for Python data.

  • Solution
  • More exercises
  • Explanation: "User-defined classes have __eq__() and __hash__() methods by default (inherited from the object class); with them, all objects compare unequal (except with themselves) and x.__hash__() returns an appropriate value such that x == y implies both that x is y and hash(x) == hash(y)."
u/Sea-Ad7805 — 7 days ago
▲ 48 r/pygame+1 crossposts

Battle Tail, show what you can do

A Battle Tail is added with this git commit.

PythonLearningGame is a collaborative Python project using PyGame, just for fun and purely educational. We are looking for collaborators.

Surely some of you think you are pretty good with Python, then this is your chance to show us what you can really do besides writing know-it-all comments. Maybe add:

  • health pickups
  • shield
  • laser weapon
  • lock-on missiles
  • mines to drop
  • units shooting back
  • ...

See the PythonLearningGame repo for instructions.

previous PythonLearningGame post

u/Sea-Ad7805 — 8 days ago
▲ 841 r/pycharm+6 crossposts

Teaching Python the right way

Programming courses often focus heavily on understanding code, while paying far less attention to understanding the program state. But code does not exist in isolation. Its main goal is to change the program state, before ultimately producing some output.

To develop an accurate mental model of program execution, students need to understand both:

  • the instructions being executed
  • the values, references, and data structures those instructions create and modify

Reading code alone does not always reveal how the program state changes during execution. That is why I created 𝗺𝗲𝗺𝗼𝗿𝘆_𝗴𝗿𝗮𝗽𝗵: a tool that visualizes the state of a Python program as it changes, step by step.

It can help explain a wide range of introductory Python topics. Here are just a few examples:

Instead of reconstructing the program state from print statements, students can now watch it change as each line executes. This makes unfamiliar concepts easier to understand and bugs easier to fix.

Help your students learn Python programming more thoroughly and easily.

See: more examples

u/EmadFahim134 — 10 days ago
▲ 20 r/pygame+2 crossposts

Troll unit added by Joseph

Joseph added a dangerous Troll unit, that kills everything with just one touch, with this git commit. It pretends to mind its own business but then suddenly rushes towards you making the game a lot more difficult. Nice work, thanks Joseph.

See our collaborative PythonLearningGame repo to play the game or add your own unit type or game dynamics.

What should be the next addition to our game?

previous PythonLearningGame post

u/Sea-Ad7805 — 17 days ago
▲ 23 r/PythonLearnersHub+2 crossposts

Better collision handling

In this git commit we switch from simply swapping the speed of two colliding units to elastic collision handling for more realistic collisions. The linear algebra implementation does add complexity to our code but, as it's all confined to a single function, it should not hamper further development of our collaborative PythonLearningGame project.

  • Who has other fun ideas we could add, maybe more unit types?
  • Who wants to commit his/her own code changes?

See the submit changes instructions.

previous PythonLearningGame post

u/Sea-Ad7805 — 21 days ago
▲ 25 r/learnbioinformatics+1 crossposts

Visualizing Python for bioinformatics students

Learning Python becomes much easier when students can see how variables, values, and data structures change while a program runs.

🧬 Consider this simple k-mer indexing example.

Although the code is relatively short, a beginner needs to understand several concepts at once:

  • DNA sequence slicing
  • k-mer generation
  • dictionaries and membership tests
  • lists stored as dictionary values
  • repeated positions and list mutation

With open-source 𝗺𝗲𝗺𝗼𝗿𝘆_𝗴𝗿𝗮𝗽𝗵, students can now easily step through a program and see these concepts in real-time, helping them to more easily get to the right mental model to think about Python code execution.

u/Sea-Ad7805 — 22 days ago

Copying an object in different ways

An exercise to help build the right mental model for Python data. What is the output of this Python Program?

import copy

class Coord:

    def __init__(self, x, y, z):
        self.c = [x, y, z]

    def __str__(self):
        return str(self.c)[1:-1]

coord = Coord(0, 0, 0)
c1 = coord
c2 = copy.copy(coord)
c3 = copy.deepcopy(coord)
c1.c[0] = 1
c2.c[1] = 2
c3.c[2] = 3

print(coord)
# --- possible answers ---
# A) 0, 0, 0
# B) 1, 0, 0
# C) 1, 2, 0
# D) 1, 2, 3
reddit.com
u/Sea-Ad7805 — 23 days ago
▲ 81 r/PythonBrasil+2 crossposts

Copying an object in different ways

An exercise to help build the right mental model for Python data. The “Solution” link uses memory_graph to visualize execution and reveals what’s actually happening:

u/Sea-Ad7805 — 23 days ago
▲ 46 r/LeetcodeChallenge+5 crossposts

Two different Binary Tree implementations side by side

The same tree represented in two very different ways visualized using 𝗺𝗲𝗺𝗼𝗿𝘆_𝗴𝗿𝗮𝗽𝗵: Binary Trees

🔗 Binary Tree as Nodes: The tree is built out of multiple node objects. Each node stores its value and two references, one to its left child and one to its right child.

📦 Binary Tree as List: The tree is stored in a single list or array. Instead of references, indices represent the relationships between nodes. For a node at index, its children can be found using a simple calculation:

  • Left child: 2 * index + 1
  • Right child: 2 * index + 2

So, when should you use which representation?

The node-based version makes the structure explicit and intuitive. It is great for education: students can clearly see how nodes are connected while practicing classes and references/pointers. It is flexible and works particularly well for irregular or sparse trees. It is the clearest representation when learning how trees work.

The list-based version can be very efficient for (nearly) balanced homogeneous trees. It does not need to store child references, requires fewer separate memory allocations, and keeps values close together in memory for improved cache performance.

The same abstract data structure, but with very different trade-offs in clarity, flexibility, and performance.

Which representation would you use?

u/Sea-Ad7805 — 29 days ago

Solve Python LeetCode problems with help of Visualizer

Solving Python LeetCode problems gets easier with the help of open-source memory_graph visualizer. Run the Binary Tree live demo with just one click. It visualizes the state of any Python program in each step so concepts like:

  • references
  • aliasing: variables sharing values
  • shallow vs deep copy
  • recursion
  • data structures

become concrete, clear, and easy to debug.

reddit.com
u/Sea-Ad7805 — 1 month ago

Creating our own game with the PythonLearning community

I thought it might be fun to build our own Python game together as members of the PythonLearning community. What else were you planning to do this summer anyway? We can use this simple game in this git repo as starting point:

PythonLearningGame

It's purely educational, a great opportunity to learn about:

  • Python
  • PyGame
  • (quick and dirty) Object Oriented Programming
  • Teamwork using GIT

This should be accessible for Python students who are comfortable with loops, functions, and classes.

Get creative and make your contribution to this game, maybe:

  • change behavior
  • add a new unit type
  • add special effects
  • add new game dynamics
  • add better graphics
  • ... (what ever you like, but keep it civilized)

Drop a comment if you feel this might be good educational fun and you might want to contribute.

See the PythonLearningGame repo for instructions. If things are not clear or you get stuck, ask AI or ask questions in the comments.

u/Sea-Ad7805 — 1 month ago

I want your help to make a fun Python game

I want your help to make a simple game into a really fun Python game. It's purely educational, an opportunity to learn about:

  • Python
  • (quick and dirty) Object Oriented Programming
  • PyGame
  • Teamwork using GIT

This should be accessible for Python students how are comfortable with loops, functions, and classes. If you want to help see the instructions on this PythonLearningGame git repo.

Make your changes to this already working game, maybe:

  • change behavior
  • add a new unit type
  • add special effects
  • add new game dynamics
  • ...

If things are not clear or you get stuck, ask AI or add questions in the comments.

u/Sea-Ad7805 — 1 month ago