Python Variables

A variable is a placeholder to store values.  It is just like high school math class “x = 2.”
example:
myName = “Ferguson”
print(name)
You can choose any variable name that you want.  Freedom is great, but with that freedom comes responsibility.  If you choose “x” to be your variable it provides no description of what it represents and thus become very confusing after just a few variables are added to a program.  On the other hand you do not want you variable to be too long “the_number_received_from_accounting_end_of_the_month.”  While this is very descriptive it is long and hard to type and hard to read.  A good in between might be “numAccounting.”
A variable name can contain letters, numbers, or _, but can’t start with a number.  These are all OK:
my_name
myName
my1Name
my2Name
Open IDLE and enter these commands
>>>fname = “Sam”
>>>mname = “I”
>>>lname = “Am”
>>>print(fname, mname, lname)
output:  Sam I Am
>>>pennies = 100
>>>pennies = pennies + 50
>>>print(pennies)
output:  150
Variables can also be Lists, Dictionaries (aka Maps), Tuples (unchangeable lists)
#making a list
>>>myFruit = [“Apple”, “Banana”, “Grape”]
>>>print(myFruit)
output:  [“Apple”, “Banana”, “Grape”]
#making a dictionary (values and keys) of my friends and nicknames
myFriends = {“Maxwell”:”Mad-max”, “Isabelle”:”The Bellster”, “Jeff”:”El Jefe”}
>>>print(myFreinds)
output:  {“Maxwell”:”Mad-max”, “Isabelle”:”The Bellster”, “Jeff”:”El Jefe”}
#making a tuple – tuples do not change their values
pi_tuple = (3, 1, 4, 1, 5, 9)
>>>print(pi_tuple)
output:  (3, 1, 4, 1, 5, 9)
From here in we will not be using the IDLE for our coding but instead will use a text editor or PyCharms.  We will save our files as .py extensions and then run them from the command prompt of PyCharms directly.
Lets use an adventure gaming theme to keep things relatable.  We will use variables with strings and integers, lists and dictionaries (keys and values).
Copy this code below into PyCharms or a text editor and then run it.
See if you can follow along with what happens one day 1 and day 2.  In essence we are just modifying variables, lists and dictionaries.
#THE GREAT ADVENTURE GAME!

#using adventure gaming theme to explain variables
#day1
playerName = "Xorg"
health = 100
weapons = ["sword", "bow", "axe"]  #this is a list
money = {"gold":50, "silver":20, "copper":5}  #this is a map or dictionary (keys and values)

#player wishes to change her name to "Xorg the Great!"
playerName = "Xorg the Great!"

#player gets hit in the head and loses 20 health points
health = health - 20  #health will be equal to current health minus 20

#player finds a dagger in a cave
weapons.append("dagger")

#player slays a dragon and gets 20 gold coins
money["gold"] = money["gold"] + 20


print(playerName)
print(health)
print(weapons)
print(money)

#day2

#player heals naturally 1pt of health
health = health + 1

#player finds one ruby laying on the ground
money["ruby"] = 1

#player gave her axe to a friend
del weapons[2]   #deletes the weapon in the 3rd location in the list (Python counts from zero)

#a thief stole the players bow
weapons.remove("bow")  #does the same thing as "del" above but uses the name not location

#lets sort our weapons alphabetically
weapons.sort()

#let's try reverse order
weapons.reverse()

# Get a list of dictionary keys
print(money.keys())

# Get a list of dictionary values
print(money.values())

#Make the output a little pettier.  A cooma prints to different items, a plus sign combines like items
print()
print()
print("Name: " , playerName)
print("Health: " , health)
print("Weapons: " , weapons)
print("Gold: " , money["gold"])
print("Silver: " , money["silver"])
print("Copper: " , money["copper"])
print("Rubies: " , money["ruby"])
 Fantastic work!  Let’s keep moving forward!