Python data types
Python, like all programming languages, uses data types to classify types of information. The specific data type you use determines the values you can assign to it, how you can store it, and what you can do to it (including the operations you can perform on it).
You deal with written information all the time and probably don’t think about the difference between numbers and text (that is, letters and words). But there’s a big difference between numbers and text in a computer because with numbers, you can do arithmetic (add, subtract, multiple, divide). For example, everybody knows that 1+1 = 2. The same doesn’t apply to letters and words. The expression A+A doesn’t necessary equal B or AA or anything else because unlike numbers, letters and words aren’t quantities. You can buy 12 apples at the store, because 12 is a quantity, a number — a scalar value in programming jargon. You can’t really buy a snorkel apples because a snorkel is a thing, it’s not a quantity, number, or scalar value.
Strings are sort of the opposite of numbers. With numbers, you can add, subtract, multiply, and divide because the numbers represent quantities. Strings are for just about everything else. Names, addresses, and all other kinds of text you see every day would be a string in Python (and for computers in general). It’s called a string because it’s a string of characters (letters, spaces, punctuation marks, maybe some numbers). To us, a string usually has some meaning, like a person’s name or address, but computers don’t have eyes to see with or brains to think with or any awareness that humans even exist, so if it’s not something on which you can do arithmetic, it’s just a string of characters.
The following list shows the seven most common Python data types:
integer
: These are whole numbers, positive or negative (including 0). Example:100
.float
: Floating-point numbers are real numbers, rational or irrational. In most cases, this means numbers with decimal fractions. Example:123.45
.string
: Strings are sequences of characters, or text, enclosed in quotes. Example:"any text"
.boolean
: Can be one of two values, true or false. Example:True
orFalse
.list
: An ordered sequence of elements. With a list, the order of the elements can be changed. Example: [value1
,value2
, … ].tuple
: An unchangeable ordered sequence of elements. Example: (value1
,value2
, …).dictionary
: This is Python’s mapping data type. Dictionaries map keys to values as a means of storing information. Example: {key1
:value1,key2
:value2, …}.
Python operators and special characters
With Python and for computers in general it helps to think of information as being one of the following data types: number, string, or Boolean. You also use computers to operate on that information, meaning to do any necessary math or comparisons or searches or whatever to help you find information and organize it in a way that makes sense to you.
Python offers many different operators and special characters for working with and comparing types of information. Here we just summarize them all for future reference, without going into great detail.
Special Characters
# |
comment |
""" |
string literal |
\n |
new line |
\char |
escape character |
Numeric Operators (in Order of Precedence)
() |
grouping |
** |
exponent |
– |
negation |
* |
multiplication |
/ |
division |
% |
modulus |
// |
floor division |
+ |
addition |
– |
subtraction |
Comparison Operators
== |
equal |
!= |
not equal |
> |
greater than |
< |
less than |
>= |
greater than or equal to |
<= |
less than or equal to |
and |
logical and |
or |
logical or |
not |
logical not |
f strings
number |
width |
.2f |
1234.56 |
,.2f |
1,234,56 |
.1% |
6.5% |
: |
align left |
^ |
center |
Python functions for working robots
The basic components of robots can be controlled with Python functions and can work together to accomplish robotic tasks. One way to do so is through the use of a python class file called RobotInterface.py
. Here is a list of the functions that are defined in the RobotInterface.py
library:
- Utilities
Color( red, green, blue, white = 0)
Converts the provided red, green, blue color to a 24-bit color value. Each color component should be a value 0–255, where 0 is the lowest intensity and 255 is the highest intensity.
allLEDSOff()
Turns all the LEDs off on the robot.
centerAllServos()
Moves all servos to their center position.
- Front LEDs
set_Front_LED_On(colorLED)
One of these constants: RobotInterface.left_R
, RobotInterface.left_G
, RobotInterface.left_B
, RobotInterface.right_R
, RobotInterface.right_G
, RobotInterface.right_B
.
set_Front_LED_Off(colorLED)
One of these constants: RobotInterface.left_R
, RobotInterface.left_G
, RobotInterface.left_B
, RobotInterface.right_R
, RobotInterface.right_G
, RobotInterface.right_B
.
- Strip LEDs
rainbowCycle( wait_ms = 20, iterations = 3)
Cycles the Pixel LEDs through the rainbow for at least three iterations.
colorWipe
( color)
Changes all the Pixel LEDs to the same color.
setPixelColor( pixel, color, brightness)
Sets an individual Pixel to a specific color; brightness is for the whole string.
- Ultrasonic Sensor
fetchUltraDistance()
Returns the distance in front of the sensor in centimeters (cm).
motorForward( speed, delay)
Moves the robot forward at speed
(0–100) for delay
seconds.
motorBackward( speed, delay)
Moves the robot backwards at speed
(0–100) for delay
seconds.
stopMotor()
Immediately stops the drive motor.
- Head Turn Servo
headTurnLeft()
Turns head all the way left.
headTurnRight()
Turns head all the way right.
headTurnMiddle()
Turns the head to the middle.
headTurnPercent( percent)
Turns the head as a percent 0–100, where 0 is all the way left and 100 is all the way right.
- Head Tilt Servo
headTiltDown()
Tilts the head all the way down.
headTiltUp()
Tilts the head all the way up.
headTiltMiddle()
Tilts the head to the middle.
headTiltPercent( percent)
Tilts the head as a percent 0–100, where 0 is all the way up and 100 is all the way down.
- Front Wheel Servo
wheelsLeft()
Turns the wheels all the way to the left.
wheelsRight()
Turns the wheels all the way to the right.
wheelsMiddle()
Turns the wheels to the middle.
wheelsPercent(percent)
Turns the wheels as a percent 0–100, where 0 is all the way left and 100 is all the way right.