Introduction to Python Programming Course - Part 12
More consolidation needed! I went over section 1 (Data Types and Operations) now need to do the same with structures, so all of that is clear. THEN go through control flow again. After that, I'll try those end of unit challenges. If I still can't get them, then it's fine - it's just to make sure I've got everything clear up to that point!
Data Structures
Lists
These are essentially arrays from Swift. Same in syntax and use of certain methods e.g. append and += to add an element.
This is a container of data. Lists are ordered. Using the index - starting from 0. All that is fine.
-1 of the index would be the last element of the list. Lists can also be of multiple types e.g. you can use an Int, a string etc.
Slicing in lists - this is using a colon in the index part. E.g. [:3]. That would go up to the fourth index but essentially have the first three items. *Need to try out this with examples!
OK I've done some more practice! :2 would be the first two. Or the items BEFORE index 2 (e.g. index 0 and index 1). 2: would be the values AFTER the second element. That makes more sense. OR FROM Index 2 - from the third element.
That clarifies this one!
Mutability and Order -
My take is that mutable means values can be changed; order means that there is an index system. Let me test...
What I've tested DOES let me change the values of strings. So they are mutable, not immutable? Not sure on that.
Right I've played around a bit more with the slicing. Making more sense!
Other methods...
Join - put in what to separate, then the list with that method. With len, max, min, sorted etc. all good. List needs to go inside the method.
That's all I can do with lists. Did some practice with character names - all seems to work.
Tuples
So key difference with lists is that they are immutable. They are in parentheses or none. Tuple unpacking - naming each element after setting the values...
So after using a characters list, then a tuple to have stats, here's the code I've tested to try out format...
print("{}'s stats are strength: {}, power: {} and skill: {}".format(characters[0], strength, power, skill))
Wednesday's stats are strength: 7, power: 8 and skill: 6
And it works! Above is the print output. I can then do a different character name and other attributes. All good!
These seem fine. Nothing else here. One thing that isn't clear is how to access elements of the tuple. Will check that out...
OK found that out - it's just the index system in []. Same as lists, easy!
But if they are unpacked, you can use that label as it's clearer e.g. strength instead of attributes[0].
Sets
Now for these, I don't think I'll ever really use them in practice. But you never know! So let's test these out!
OK I've tested. All good - I know what to do with these but again, can't see sets being used much in practice!
Dictionaries
Key-value pair - all good with this side of it.
Get method - this is to retrieve a value from putting in the key e.g. -
driver_wins = {"Schumacher": 91, "Alonso": 32, "Vettel": 53}
print(driver_wins.get("Vettel"))
53
That's the extra practice!
Testing 'in' is just for a bool.
Cool - is and is not make sense. This is to compare a value to a key. Easy.
Compound Data Structures
These are the nested ones - dicts within dicts etc.
One thing here - when calling for a specific value, we need two consecutive keys in a row with square brackets for each. Let's practice!
Cool. If you do try to print the dict, it will work but the whole thing gets printed. Makes sense.
Last bit is having a look at the practice questions - these were good to look at to see some applied learning. I had done it before.
Here were the challenges:
1. Split a string into a list of words
*This was using the 'split' method
2. Converting the list to a set so it has unique elements
*Just putting the answer of 1. into a set basically
3. Length of container
*Doing the len method
Then the hardest challenges -
1. Finding number of unique words in the dict
*This was len method
2. Seeing if a certain value was in there
To see if that value was 'in' (last time I did 'get' - 'in' was better! - get actually retrieves a value).
3. First element, when sorted by keys
*For this, I would use sorted method, then use the 0 index to access
4. Word with highest value
Use 'max' as that means alphabetically later.
*One thing I got wrong was create a list, getting the keys from the dictionary. I missed that but makes sense - had to use 'sorted', then put in .keys as accessing those. So those took the keys from the dict.
And that's the lot! All data structures gone over.
No need to do another summary - will do that on the 'Sticky' summary. Next time, Control Flow deep dive!
Comments
Post a Comment