Python Reference PDF

Summary

This document is a Python reference guide for VEX robotics. It includes descriptions and examples of various Python statements, comments, and control statements used in VEX code.

Full Transcript

Python® Reference Statements Comments Description # a helpful comment A comment begins with the # character and should be used to help readers of your code understand what it is doing. The program ignores comments when it runs. Control st...

Python® Reference Statements Comments Description # a helpful comment A comment begins with the # character and should be used to help readers of your code understand what it is doing. The program ignores comments when it runs. Control statements Description if condition: A control statement that performs instructions... based on whether a condition is true. The condition must evaluate to a true or false value. if condition: An if/else tests for both true and false results of... a conditional expression. When the condition else: evaluates to true, the code below the if and... before the else executes; when the condition evaluates to false, the code in the else executes. if condition1: A nested if statement is one if statement if condtion2: indented in another. The second if statement... with condtion2 will only execute when the first condition1 is true. You can also nest an if statement in an else clause. if condition1:... else: if condtion2:... wait(time, units) Pauses program execution for the specified time. while True: A control statement that executes indefinitely.... Input & Output Devices Additional help is in codev5.vex.com. In the Code panel, select the help icon next to each device statement. Bumper Switch Description Bumper(port) Connects to a bumper switch at the specified 3-wire port, such as such as brain.three_wire_port.c. bumper_X.pressing() With the bumper switch connected to port X, reports a 1 if the switch is pressed, 0 otherwise. Distance Sensor Description Distance(port) Connects to a distance sensor at the specified port, such as Ports.PORT3. distance_N.object_distance(units) With distance sensor connected to port N, reports the distance to the nearest object in the specified units. distance_N.object_velocity() With distance sensor connected to port N, reports the velocity of the nearest object in meters/second. distance_N.is_object_detected() With the distance sensor connected to port N, reports true if the sensor detects an object, false otherwise. LED Description Led(port) Connects to an LED aat the specified 3-wire port, such as brain.three_wire_port.a. led_X.on() With servo connected to port X, turn the LED on. led_X.of() With servo connected to port X, turn the LED on. Limit Switch Description Limit(port) Connects to a limit switch at the specified 3-wire port, such as such as brain.three_wire_port.b. limit_switch_X.pressing() With the limit switch connected to port X, reports a 1 if the switch is pressed, 0 otherwise. Motor Description Motor(port, gear_ratio, reverse) Connects to a motor using the specified configuration variables. motor_N.spin(direction) Turns on the motor connected to port N and spins it in the specified direction, either FORWARD or REVERSE. motor_N.stop() Turns the motor off. Optical Sensor Description Optical(port) Connects to an optical sensor at the specified port, such as Ports.PORT4. optical_N.brightness() With the optical sensor connected to port N, reports the amount of light detected within the range 0 to 100. optical_N.color() With the optical sensor connected to port N, reports a color value using named color such as Color.RED or Color.BLUE. optical_N.hue() With the optical sensor connected to port N, reports a color value in degrees, between 0 and 359. optical_N.is_near_object() With the optical sensor connected to port N, reports true if an object is close enough to sense, false otherwise. optical_N.set_light(state) With the optical sensor connected to port N, turn its led on or off. The value for state can be either LedStateType.ON or LedStateType.OFF. Potentiometer Description Potentiometer(port) Connects to a potentiometer at the specified 3-wire port, such as such as brain.three_wire_port.a. potentiometer_X.angle(PERCENT) With potentiometer connected to port X, reports the angular rotation in terms of a percentage, 0 to 100. Servo Description Servo(port) Connects to a servo motor at the specified 3-wire port, such as such as brain.three_wire_port.a. servo_X.set_position(0,DEGREES) With servo connected to port X, move the servo to the specified position in a range of -45° to 55°. Operators Comparison Operators Description ab greater than: evaluates to true if a is greater than b a = b greater than or equal to: evaluates to true if a is greater than or equal to b a == b equal to: evaluates to true if a is equal to b a != b not equal to: evaluates to true if a is not equal to b Mathematical Description operators a + b Addition; adds two values. a - b Subtraction; subtracts two values. a * b Multiplication; multiplies two values. a / b Division; divides two values. a // b Floor division; divides two values and drops the decimal portion of the returned value. a%b Modulus; returns the remainder of a division operation. a**b Exponent; raises a value to the power of another value.

Use Quizgecko on...
Browser
Browser