Python OOP
Today's class started off sort of slow. We did an algorithm practice sheet for an hour and went through all the problems. I'm still getting used to writing in Python and these are some of the things I kept tripping up on:
- for i in range (#, #): the upper limit is NOT inclusive. Meaning, if you want something from say, 1 to 10, you have to write (1, 11) because Python goes up to but not including 11.
- When writing for i in list: this for loop runs all the values inside your list. I kept mixing that up as list[i] which is not the case, therefore if I wanted to change the actual values in the list at a certain index (list[i] = new_number), then I would either create another for loop for that purpose or, as Maric pointed out: create a counter variable outside of the for loop and do count += 1 inside the loop to note what index the for loop is on.
- I need to get used to writing things in functions.
- A blueprint of a barrack (not an actual barrack) is the class.
- Blueprint in the sense that the actual barrack doesn't exist but if it were to, it would come from the class of barracks.
- Attributes like health of the building etc. are a part of the class.
- Methods or functions of the building (like creating soldiers) are a part of the class.
- This leads to the creation of the barracks (or an instance/object) and this barrack will inherit all the attributes or methods that the class of barrack has, but only if these attr/methods are public.
We then learned more about the syntax that sets up the class in Python like __init__. A tricky part about OOP is that when you first start learning it, a lot of the objects that are being passed in the parameters of classes or functions will make your head spin. It can get really confusing really easily as you stack more functions in a class and when you start creating more classes that use functions in another class. Here is an example:
Definitely the biggest head scratcher for today was the idea of singly and doubly linked lists.
Basic objects and classes |
...lol doubly linked lists |
Definitely the biggest head scratcher for today was the idea of singly and doubly linked lists.
Singly linked lists are nodes that have a head portion (that contains the value) and the tail portion (which points towards to another node). If there's only one node or if there is not another node to point to, the tail section will become null. Doubly linked lists are the same as a singly linked but has two tail portions which can point in a bidirectional manner instead of a single direction like singly linked lists. I won't focus too much on it since it's optional and is mostly to ready a person for a technical interview but it's definitely a CS theory that I should get down by the end of this bootcamp.
No comments:
Post a Comment