Circuit Python tutorials in Robotics II
View the Project on GitHub MrPrattASH/Robotics-II-Circuit-Python
A computer program is a set of instructions that tells a robot how to perform specific tasks or respond to certain conditions. This involves receiving inputs from the environment (such as data from sensors like cameras, microphones, or touch sensors) that provide information about the robot’s surroundings or instructions from human operators. The robot’s computer then processes these inputs and determines appropriate outputs, which are actions or responses executed by the robot. Outputs could involve moving parts of the robot, navigating a space, or communicating information back to humans or other systems. Understanding how programs guide input and output is essential for students interested in robotics, as it defines how robots interact with and respond to the world around them.
There are 2 ways we can write code for our microcontroller. A REPL (Read-Eval-Print Loop) and a Python script saved in a .py
file.
.py
file is used to write a complete set of instructions that can be executed all at once, which is ideal for developing larger programs or projects.
While a REPL offers quick feedback for experimentation, script files allow for the development and execution of more structured and complex code. We’ll be using scripts for the majority of this course.For these tutorials, you’re going to need:
code.py
file on your circuitpy
device, then modify the code to satisfy the requirements.code.py
file.In programming, we need a way to distinguish what is an instruction for our robot, and what is a note for us as programmers. A code comment is a piece of text in a program that is ignored by the computer when the program runs. It is used to explain what certain parts of the code do, making it easier for others (or yourself) to understand when reading the code later. Comments can clarify complex code, indicate the purpose of variables or functions, and remind programmers of things to fix or check. In Python, a single-line comment is created by placing a #
symbol before the text. For example, in Python, you might see:
# This variable stores the user's age. The computer ignores this line
user_age = 25 # (the computer reads all non-green text here!)
In python, just like on your calculator, you can do a lot of math in line. Take a look at some examples:
Operator | Description | Example | Result |
---|---|---|---|
+ |
Addition: Adds two numbers together. | 5 + 3 |
8 |
- |
Subtraction: Subtracts the second number from the first. | 5 - 3 |
2 |
* |
Multiplication: Multiplies two numbers. | 5 * 3 |
15 |
/ |
Division: Divides the first number by the second. | 5 / 3 |
1.6666666666666667 |
// |
Floor Division: Divides and returns the largest integer. | 5 // 3 |
1 |
% |
Modulus: Finds the remainder after division. | 5 % 3 |
2 |
** |
Exponentiation: Raises the first number to the power of the second. | 5 ** 3 |
125 |
Write a Python program to multiply two numbers, 4 and 5, and print the result. Copy and paste the following into your code.py file.
print(4 * 5)
20
Write a Python program to add two numbers, 10 and 15, and print the result.
# Use the + operator to add numbers.
25
In programming, a variable is like a container that holds data. You can think of it as a labeled box where you can store and manage information that your program can use later. The label (the variable name) helps you identify and access the data stored in the box.
5
, -3
, and 100
are integers.true
or false
. Booleans are often used in programming to represent conditions, helping the program make decisions."hello"
, "123 Main St."
, and "Welcome!"
are strings. Strings can include letters, numbers, spaces, and special characters.By using these different types of variables, programmers can handle various kinds of data effectively within their programs.
Define the following variables and print them:
city = "New York"
temperature = 23
rainy = False
# Print the first variable
print(city)
# Now print the other two variables below
# Use the print() function to output variable values.
New York
23
False
Define the following variables and print them:
product = "Laptop"
quantity = 5
price = 999.99
in_stock = True
# Print all variables below
# Remember to print each variable separately.
Laptop
5
999.99
True
Great work! You’ve now got a basic understanding of some simple Python syntax.