Variables#
When we program, we want to store information in places that we can access later. We are going to call these places ‘variables’ for now. (later you will learn that we actually store things in registers which can be accessed via their location, but that is for a more advanced programming class).
We assign values to variabels using =. We can store a bunch of different types of information or data, which can be found on the datatypes page.
a = 1+2 #storing a numeric value in 'a' (will equal 3)
print(a)
# We have some shortcuts we can use for the general math operators
# By placing the operator infront of the = sign durign the assignment, that operation will be performed with the value after the sign
a += 3 #adding 3 to a (will now equal 6)
print(a)
a -= 3 #subtracting 3 from a (will now equal 3 again)
print(a)
a *= 3 #multiplying 3 from a (will now equal 9)
print( a)
a /= 3 # dividing a by 3 (will now equal 3 again)
print(a)#notice that a is now a float instead of an integer (it has a decimal place after it, and before it did not)
a **= 3 # a^3 (will now equal 27)
print(a)
a //= 3 # floor division by 3 (rounding down, now equal to 9)
print( a)
a %= 3 # modular devision by 3 (finding the remainder, now equal to 0)
print(a)
# You can also assign variable values based off of other variables
a = 3
b = a+3 #b will now be assigned to 6
print(b)
c = "Hello!" #storing a string in variable 'c'
print(c)
3
6
3
9
3.0
27.0
9.0
0.0
6
Hello!
If you are already familiar with programming, you may know that some programming languages have a ++ and a -- operator that allow you to increment or decrease the specific variable by 1. Python does not have this, so keep that in mind.
Global and Local Variables#
# A GLOBAL variable, accessible everywhere.
menu_item = "egg and spam"
def order():
# This is a LOCAL variable. It only exists inside this function.
customer_item = "spam, egg, sausage, and spam"
print(f"Customer: I'll have the {customer_item}.")
print(f"Waitress: We've only got {menu_item}, sir. Waiting on more spam")
print("Customer: Oh, alright then.")
def serves():
# The waitress uses the GLOBAL variable because that's what's available
# to the whole restaurant.
print(f"Waitress serves: Here's your {menu_item}, sir. The one from the menu.")
def request():
menu_item = "spam, egg, spam, spam, spam, bacon, and spam"
print(f"Customer: How much spam does {menu_item} have in it?")
print(f"Waitress: Not as much as spam, spam, spam, spam, spam, baked beans, spam and spam has.")
def delivery():
# This function uses the 'global' keyword to change the GLOBAL menu_item.
# It's a special delivery of 'spam spam spam spam'.
global menu_item
menu_item = "spam, spam, spam, spam, egg and spam"
print("We got more spam!")
# Here's what happens:
print(f"The menu has {menu_item}, and that's it")
print("\nA customer comes in and places an order.")
order()
print("\nThe waitress serves from the menu again.")
serves()
print("\nThe new spam delivery arrives!")
delivery()
print("\nNow, let's see what's on the menu after the delivery.")
serves()
print("\nPerhps something a little off menu?")
request()
#Moral of the story: Don't modify global varables inside of functions if you can avoid it...
#Globals are great for:
#Pins on microcontrollers
#motor assignments
#Set variables that a lot of places will use, but don't need to be changed
#Don't use globals for:
#Literally anything else...
The menu has egg and spam, and that's it
A customer comes in and places an order.
Customer: I'll have the spam, egg, sausage, and spam.
Waitress: We've only got egg and spam, sir. Waiting on more spam
Customer: Oh, alright then.
The waitress serves from the menu again.
Waitress serves: Here's your egg and spam, sir. The one from the menu.
The new spam delivery arrives!
We got more spam!
Now, let's see what's on the menu after the delivery.
Waitress serves: Here's your spam, spam, spam, spam, egg and spam, sir. The one from the menu.
Perhps something a little off menu?
Customer: How much spam does spam, egg, spam, spam, spam, bacon, and spam have in it?
Waitress: Not as much as spam, spam, spam, spam, spam, baked beans, spam and spam has.
#Sandbox Area