DFIR Python Study Group: Class 2
Learning a programming language is similar to learning piano for me (I WILL play Dance for Me Wallis by Abel Korzeniowski one day!). It doesn’t come easy, and I get really sick, really fast, of hot cross buns. I want to be a master…yesterday.
I’m a little ahead of the second study group class video. Class 2 starts by going over the 'beer song'. The beer song teaches us how to use range. I really like how Alexis explains it – he is straight to the point, easy to understand >
range(x, x, x)
The first parameter is your starting point, the second is your stopping point, and the third is the direction you’re moving. The beer song, “99 Bottles of beer on the wall…” goes down by one. In our code, this looks like this: range(99, 0, -1).
- == is comparing
- = is assigning
- Everything in Python is an object
Chapter 2: List Data: Working with Ordered Data
The last Chapter touched on the variety of data structures Python accepts. The book states that Python comes with four built-in data structures – list, tuple, dictionary, and set. (When something is built into Python, it doesn’t need to be imported.)
- List = ordered, defined by brackets, mutable, can be changed/dynamically shrink and grow
- Tuple = defined by parentheses, immutable, cannot be changed
- Dictionary = unordered set of key/value pairs – can dynamically shrink and grow. Each key has a value.
- Set = collection of related objects, no duplicates allowed
Chapter 2 has us focusing on lists. We used a list in our coding project, specifically, the variable “odds”. Odds was a list of odd numbers that was called upon in our code.
I’m hoping a few concepts make sense as time goes on. It is wonderful that lists are mutable, but why wouldn’t a programmer just go in, find the list, and edit the list instead of changing it down the line?
Lists are enclosed in square brackets. Below we practice using a list.
len() = a built-in function that tells us how many objects are in an object
Halfway through Chapter 2 we start using input. Input, for me, is so much fun. I enjoy creating code that the user can interact with. It makes it come to life and is a bit more exciting.
We also start learning about remove, pop, extend and insert.
- remove = removes the data specified (only the first instance found).
- pop = removes and returns an object from an existing list. It will remove the indexed position of the item.
- extend = adds onto an existing list.
What is interesting about this one is that you need to remember to put in the brackets. You’re providing a list to be added to the list. If you don’t, you’ll get an error.
As you can see, these methods have been adding data to the right and left of our original list. With insert, we are able to specify where we want our new data to go.
- insert = inserts an object before a specified value. When using insert, we will use two objects within our parenthesis. The first object is the where you want your object to go (before the current object in that spot), and the second object is the object we want to insert. The book really highlights BEFORE. Remember this.