Variables are great but they don't hold very much information. That's where lists can help. Lists hold multiple variables in one "list" of variables. In other programming languages they're used to hold only one data type.
Line 1: Declares a list of pokemon
Line 3: outputs the first pokemon in the list so Bulbasaur
Line 4: outputs the last pokemon in the list so Charmander
Line 6: outputs the whole list
Note that the accessors [] work the same as string so -1 is the last element and you can use [5:] to list from the 5th element to the end.
If we want to print the list we could loop through it to print each one individually:
Line 3: loops through the list, i takes on the value of each item in the list
Line 4: print's i
We can also update individual items in the list:
Line 3: updates the last element and replaces Charmander with Pikachu
We can also add to the list:
Line 3: uses the append function to add pikachu to the end of the list.
We can also pop an item location:
Line 3: removes the element in the 1 position
There's many functions for lists that can be found here:
List methods
Dictionaries and lists are great if you're only making one instance of them, like a deck of cards, a list of names, etc. If we want to create a template and then make different instances of said template to save time we'd use classes and objects. Classes and objects are used a lot in programming, here's an example:
Line 1: define the NPC class
Line 2: set the name attribute to blank
Line 3: set health to 0
Line 5: made a slime an object of the NPC class, it by default has 0 health and no name, so we set those.
Line 6: set slime health =2
Line 7: set slime name to be Jerry
Line 8: we can add attributes that aren't in the class by default, I added species and set it to slime
Line 10: outputs Jerry the slime has 2HP
Line 11: outputs Do you attack Jerry
So we see this is pretty powerful since we can make a lot of objects of the NPC class, or we can make multiple classes to make more specialized templates. We could even do Inheritance to make subclasses but I'll let you research that on your own.
The initialization function allows you to build an object of the class prior without having to manually reset the default values to what you want. Here's an example of modified code from our last program:
Line 1: defines the NPC class
Line 2: declares the __init__ function to take in the object, a name, and health
Line 3: sets name to the name attribute that gets passed in
Line 4: sets the health attribute to the health number that gets passed in
Line 7: sets up our slime object named Jerry
Line 8: there was no species attribute in the init function so we still had to update it manually.
The init function is powerful and should be used to cut down on programming time wherever possible.
Here's how to set up your class(es) in another file:
This is in the SmithLib file. I declare a class for smithBot
I import the SmithLib so that I can use the smithBot class.
Sample output. All files must be in the same folder to accomplish this.
Lists, variables, and objects are the fastest data types but sometimes it makes more sense to use one over another. Classes and objects help organize your code to make it easily readable. Dictionaries can be used in place but are not as nice to use.
Remember that functions are efficient for memory but the tradeoff is time to run since they have to do calculations. It's sometimes better to just use a variable if you're going to be using the result of a function multiple times in the code. Functions are a great way to organize your code.
Imports should be at the top of your code, followed by functions, then your main code segments. There are exceptions to this rule in later grades.
For projects you should now be using multiple files, one to hold your functions, another should hold your classes if you're using them. It is fine on daily assignments to use one file unless otherwise instructed.
Methods are like functions, except they're tied to a class or an object of that class. Methods can be used to check themselves or others. This makes it so you can check collisions of a list of objects in a video game very easily.
This section is optional. We likely will use classes before we ever consider using dictionaries. However, dictionaries are structured very similarly to JSON files, so that is some very useful knowledge if you want to learn about noSQL Databases.
Lists are fantastic for making a numbered lists of items, but what if we want to make the indexes words with definitions? This is where the dictionary data type can come in handy.
Line 1: declares the dictionary with the keys: name, strength, and health. These will be used to access them and update them. All keys must be strings.
Line 2: name is set to Sphen
Line 3: strength is 3, notice it's an integer, the values don't have to be just string.
Line 4: health is 10
Line 7: outputs the elf name, some text, and the elf's strength. Like lists we use [brackets] to access items in the dictionary, but unlike lists we use strings for the keys instead of integers.
We can also update these values:
Line 9: prints that Sphen tripped and took 3 damage
Line 10: updates Sphen's health by subtracting 3 from it.
Line 11: prints Sphen now has 7 hp
Dictionaries are great for items and their descriptions, designing an inventory, or if you want to make a list searchable by string key. We can add to the dictionaries too:
Line 13: adds a bow as a weapon
Line 14: prints out the elf and all it's keys with definitions