Python#

The VEX IQ (2nd gen) Python API Reference explains what each command, method, and function does, what inputs it uses, what it returns, and how it can be used in a project.

Use this reference when you want to understand a command before adding it to a project, check what values it accepts or returns, or compare related commands in a category.

Python commands are used in VEXcode IQ (2nd gen) Python projects. Blocks and C++ projects use the other API sections instead.

Note: The IQ (2nd gen) Brain runs Python through a virtual machine with limited memory. While there is a maximum source code size, whether a program will run also depends on its complexity (such as the number of functions, variables, and logic used). As a result, programs that are large or complex may fail to compile or run even if they are under the size limit. C/C++ programs are generally less affected by these limits on IQ (2nd gen).

How to Read a Method Entry#

Most Python entries include the following parts:

  • Command name - The official name of the command, method, or function.

  • Description - Explains what the command does and when it is useful.

  • Usage - Shows the syntax used to write the command in code.

  • Parameters - Lists the inputs the command accepts and explains what each one does.

  • Return value - Explains what the command reports back, if it returns a value.

  • Example code - Shows one way the command can be used in a project.

Common Python API Elements#

Element

What it means

Method

A command called on an object such as drivetrain.drive(…).

Function

A command called directly, such as wait(…) or print(…).

Parameter

A value passed into a method or function to control what it does.

Optional parameter

A parameter that can be left out so the default behavior is used.

Return value

A value a command reports back, such as a number, text, or Boolean.

Basic Python Vocabulary#

Term

What it means

Variable

A name that stores a value, such as speed = 50.

String

Text inside quotes, such as “Hello”.

Integer

A positive or negative number, such as 90.

Float

A number with a decimal, such as 25.5.

Boolean

A value that is either True or False.

Argument

A value passed into a method or function call, such as REVERSE in drivetrain.drive(REVERSE, 20, PERCENT).

Example Method Entry#

drive#

drive moves the robot forward or reverse forever. The robot will continue to move until it is given another action, like turning or stopping.

Usage:
drivetrain.drive(direction, velocity, units)

Parameters

Description

direction

The direction the robot moves: FORWARD or REVERSE.

velocity

Optional. The velocity to drive with from 0% to 100% when using PERCENT, from 0 to 120 rpm when using RPM, or from 0 to 720 degrees per second when using VelocityUnits.DPS. This can be an integer or decimal (float). If no velocity is provided, the robot drives at the current drive velocity.

units

Optional. The velocity unit: PERCENT (default), RPM (rotations per minute), or VelocityUnits.DPS (degrees per second).

# Drive forward then stop
drivetrain.drive(FORWARD)
wait(2, SECONDS)
drivetrain.stop()

# Drive slowly in reverse then stop
drivetrain.drive(REVERSE, 20, PERCENT)
wait(2, SECONDS)
drivetrain.stop()