Overview

Python is the programming language used to control VEX IQ robots in VEXcode IQ. It is one of the most popular languages in the world — used by scientists, game developers, and engineers everywhere.

Why does this matter?

Understanding Python basics lets you write programs that make your robot move, react to sensors, and complete autonomous routines. Once you understand these building blocks, you can build almost anything.

Variables

A variable is like a labeled box that holds a value. You can use that value later in your program, or change it whenever you need to.

motor_speed = 75      # stores the number 75
target_distance = 24  # stores 24 (inches)
is_running = True     # stores True or False
  • Use descriptive names — motor_speed is clearer than x
  • Python variables can hold numbers, text, or True/False values

Data Types

TypeWhat it storesExample
intWhole numbersspeed = 80
floatDecimal numbersdistance = 12.5
boolTrue or Falseis_pressed = False
strTextname = "left motor"

Functions

A function is a block of code you name and reuse. Instead of writing the same instructions five times, write them once as a function and call it whenever you need it.

def drive_forward(speed, duration):
    left_motor.spin(FORWARD, speed, PERCENT)
    right_motor.spin(FORWARD, speed, PERCENT)
    wait(duration, SECONDS)
    left_motor.stop()
    right_motor.stop()

Now you can call drive_forward(75, 2) anywhere in your program to drive forward at 75% speed for 2 seconds.

Key parts of a function:

  • def tells Python you are defining a function
  • The name comes next (drive_forward)
  • Parameters in parentheses let you pass in values (speed, duration)
  • The indented code is the function body — it runs when you call the function

Conditionals (if/else)

Conditionals let your robot make decisions based on sensor readings or other values.

if bumper.pressing():
    left_motor.stop()
    right_motor.stop()
else:
    left_motor.spin(FORWARD, 75, PERCENT)
    right_motor.spin(FORWARD, 75, PERCENT)

Translation: “If the bumper is pressed, stop. Otherwise, drive forward.”

You can chain multiple conditions with elif (short for “else if”):

if distance_sensor.object_distance(MM) < 100:
    # Object is very close — stop
    left_motor.stop()
elif distance_sensor.object_distance(MM) < 200:
    # Object is nearby — slow down
    left_motor.spin(FORWARD, 30, PERCENT)
else:
    # Nothing close — full speed
    left_motor.spin(FORWARD, 80, PERCENT)

Loops

A loop repeats code automatically. In robotics, loops are used constantly because your robot needs to keep checking sensors and updating motors over and over.

while loop — repeats as long as a condition is true:

while True:
    # This code runs forever (driver control loop)
    left_speed = controller.axis3.position()
    right_speed = controller.axis2.position()
    left_motor.spin(FORWARD, left_speed, PERCENT)
    right_motor.spin(FORWARD, right_speed, PERCENT)
    wait(20, MSEC)

for loop — repeats a set number of times:

for i in range(5):
    drive_forward(75, 1)
    turn_right(90)

This would drive forward and turn right 5 times — drawing a pentagon!

The VEX IQ Motor API

The API (Application Programming Interface) is the set of commands VEXcode IQ gives you to control your robot’s hardware. Here are the most important motor commands:

# Spin a motor continuously
left_motor.spin(FORWARD, 75, PERCENT)
left_motor.spin(REVERSE, 50, PERCENT)

# Stop a motor
left_motor.stop()
left_motor.stop(BRAKE)   # locks the motor in place

# Spin for a set time, then stop automatically
left_motor.spin_for(FORWARD, 2, SECONDS, 75, PERCENT)

# Spin to a specific position (in degrees)
left_motor.spin_to_position(180, DEGREES, 50, PERCENT)

# Read the motor's current position
position = left_motor.position(DEGREES)

# Read the motor's current speed
speed = left_motor.velocity(PERCENT)

Displaying Information on the Brain Screen

Use the Brain screen to show live data during a run — this is very helpful when debugging.

brain.screen.clear_screen()
brain.screen.print("Speed: ", left_motor.velocity(PERCENT))
brain.screen.next_row()
brain.screen.print("Position: ", left_motor.position(DEGREES))

Putting It All Together — A Simple Autonomous Routine

def autonomous():
    # Drive forward 2 feet
    drive_forward(75, 2)

    # Turn right 90 degrees
    turn_right(90)

    # Drive forward another foot
    drive_forward(75, 1)

Functions make your autonomous routine easy to read — each line says exactly what the robot is doing, like a recipe.

Good Coding Habits

  • Test one thing at a time. If you change motor speed and also add a sensor check, you will not know which change caused a different result.
  • Use descriptive variable names. lift_speed is much clearer than s.
  • Start simple. Get the robot moving forward first. Add turns and sensors after that works.
  • Record your results. Write down what the robot actually did in your engineering notebook.

Common Mistakes

  • Indentation errors — Python uses indentation to know what code belongs inside a function or loop. If your spaces are inconsistent, the program will not run.
  • Wrong units — Make sure you are using the right units (PERCENT vs. RPM, SECONDS vs. MSEC) in every function call.
  • Assuming it works after one test — Always run your program several times to make sure the behavior is consistent.