Screen boundry in pygame

Hello i try to learn pygame and python in general and I got this code:
there is section of code that I don’t understand but know the function is to give boundary so the robot cannot go off the screen:

import pygame
 
pygame.init()
width, height = 640, 480
screen = pygame.display.set_mode((width, height))
 
robot = pygame.image.load("robot.png")
x = width/2-robot.get_width()/2
y = height/2-robot.get_height()/2
 
to_right = False
to_left = False
to_up = False
to_down = False
 
clock = pygame.time.Clock()
 
while True:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                to_left = True
            if event.key == pygame.K_RIGHT:
                to_right = True
            if event.key == pygame.K_UP:
                to_up = True
            if event.key == pygame.K_DOWN:
                to_down = True
 
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                to_left = False
            if event.key == pygame.K_RIGHT:
                to_right = False
            if event.key == pygame.K_UP:
                to_up = False
            if event.key == pygame.K_DOWN:
                to_down = False
 
        if event.type == pygame.QUIT:
            exit()
 
    if to_right:
        x += 2
    if to_left:
        x -= 2
    if to_up:
        y -= 2
    if to_down:
        y += 2
 
    x = max(x,0)
    x = min(x,width-robot.get_width())
    y = max(y,0)
    y = min(y,height-robot.get_height())
 
    screen.fill((0, 0, 0))
    screen.blit(robot, (x, y))
    pygame.display.flip()
 
    clock.tick(60)

Please help me to understand this section from model solution:

    x = max(x,0)
    x = min(x,width-robot.get_width())
    y = max(y,0)
    y = min(y,height-robot.get_height())

my solution for this problem was to check the coordinates of the robot:

    if to_left and x > 0:
        x -= speed
    if to_right and x + robot.get_width() < width:
        x += speed
    if to_up and y > 0:
        y -= speed
    if to_down and y + robot.get_height() < height:
        y += speed

What is the function of mix() and max() in the model solution?
Thank you

In Python, the min() and max() functions return the lowest and highest values, respectively, from a set of values or an iterable.

x = max(x,0)
x = min(x,width-robot.get_width())
y = max(y,0)
y = min(y,height-robot.get_height())

This code first uses max() to ensure that x is not less than 0, and then uses min() to ensure that x is not greater than width - robot.get_width() . Similarly, max() is used to ensure that y is not less than 0, and min() is used to ensure that y is not greater than height - robot.get_height().