Circuit Python tutorials in Robotics II
View the Project on GitHub MrPrattASH/Robotics-II-Circuit-Python
A variable is a “bucket” that we can use to store data. In Python, we can save data like numbers, words, letters, True/False, or more types of values inside of variables. Much like in math, we also use variables in programming.
# In math:
x = 5
y = 2
z = x + y
# In Programming:
distance = 12
speed = 95.5
name = "rover_1"
is_driver_controlled = True
The main difference between variables in math, and in programming, are that:
power = 5
light_intensity = 84.8
rover_name = "Jenkins"
has_rc_control = True
print(power)
print(light_intensity)
print(rover_name)
print(has_rc_control)
print("power")
print("light_intensity")
print("rover_name")
print("has_rc_control")
See below for legal vs “illegal” variable names in Python
image credit
Another convention in naming variables in python is that all names are in lower case. If we have to have multiple words, we_separate_them_with_an_underscore. This is called “snake_case”, a well fitting name for Python.
distance = 50
print(distance)
distance = 12
print(distance)
Just like in math, we can also use arithmatic operators on variables.
+ add
- subtract
* multiplication
/ division
// integer division (no decimals)
% Modulus (remainder division)
Try this:
time = 10
speed = 40
distance = time * speed
print(distance)
Copy and paste the following code inside code.py. Then, follow the #code comments to practice arithmatic operators in Python:
# Note: any line that starts with a "#" hashtag, is a special line in Python
# it is a "code comment", something that is useful for you, but the computer ignores
# don't change these variables
wallet = 25
bank_account = 50
kaas_broodje = 2.25
# assign total money to the sum of wallet and bank_account
total_money =
# You now purchase a kaas broodje from your wallet. subtract this value from total_money and form your wallet.
total_money = total_money -
wallet = wallet -
#print all 3 values. Don't change these print statements. They'll print first the string, then the value of the variable listed after the comma
print("Total sum in wallet:", wallet)
print("Total sum in Bank:", bank_account)
print("Total money all together:", total_money)
Changing variables is critical in computer programs, and one of the most important things we can do. Imagine we have a distance sensor, that only reads the distance to an object once. Not a very useful design! We always need to be constantly re-writing the value of distance in order to update our code. Let’s practice
Here’s a series of calculations using variables. Your task is to determine the final values of boxes
and items
.
boxes = 3
items = boxes * 5 # items = ?
items = items - 4 # items = ?
boxes += 2 # boxes = ?
boxes *= 2 # boxes = ?
# Use arithmetic operators for changing values and shorthand for assignments such as += and -=.
boxes = 10
items = 11
Calculate the total price. Copy and paste the following code in, and modify this code.
# Variables for item prices
item1 = 10
item2 = 20
tax = 0.08
# Don't change the above code ^^^
# Assign total_price to the sum of item1 and item2
total_price =
# Assign tax_amount to the total_price multiplied by tax
tax_amount =
# Assign final_price to the total_price plus the tax_amount
final_price =
# Here we're printing the final values, don't change the code below!
print("Total Price:", total_price)
print("Tax Amount:", tax_amount)
print("Final Price:", final_price)
# Use basic arithmetic to calculate totals and percentages.
Total Price: 30
Tax Amount: 2.4
Final Price: 32.4
again, copy and paste the following code into your code.py file.
# Dimensions of a rectangle
length = 15
width = 10
# Don't change the above code ^^^
# Assign area to the product of length and width
area =
# Assign perimeter to the sum of all sides (2 * length + 2 * width)
perimeter =
# Here we're printing the final values, don't change the code below!
print("Area:", area)
print("Perimeter:", perimeter)
# For area, use multiplication, and for perimeter, account for both pairs of sides.
Area: 150
Perimeter: 50