Introduction to Python Programming Course - Part 5

OK, I'm doing this gradually. Things are soaking in and making sense. Nothing too hard has come up. Let's do a recap before I crack on...

Last time it was about data structures rather than types. So things like lists, sets. These can be of different types but are their own structures - they follow a certain syntax and have different functions. Tuples too. 

Start time - 17:20

Dictionaries

These are a mutable type. Keys to values - same as Swift. Curly brackets. Keys don't even need to be the same type; as long as it's consistent. 

Use 'in' as keyword to check if a value is there.

'Get' method - but returns none if a value is not. Need to look more at that.

OK using the square bracket lookup can lead to crashes. Get is safer.

Is and is not - So using is followed by 'none' or is not followed by 'none' would lead to a true or false

Quiz - all good. I get the basics of the key/value pair and trying to access. 

A point on == and is. Is means the SAME OBJECT. Whereas == means have the same value. So they're not the same thing basically. 

Got the indexing bit - the challenge was lists as the values. But not as the key!

PAsued at 17:45

Continued at 19:17

Quiz so far.

Tuples - ordered and immutable. Can be indexed and sliced up like a list.

Set - mutable, unordered and only contains unique items

Sets and Dicts both use curly braces

Dicts - mutable and unordered; keys are unique but have to be a data type, not struct

*Only bit I got wrong is that any type can be a key (specifically it is not structs). Has to be IMMUTABLE (which means tuples can work, right?).

Yes! That makes sense. When calling the key in the print statement, then square brackets are still used. 


Compound Data Structures 

This means a nested dictionary. One within another.

Elements - hydrogen and helium. Dicts within a dict.

SO it would be [] followed straight by [] when accessing values.

Quiz - I thought I nailed it but not. Will check the solution. 

*Paused for 15 mins

Sets - unordered, mutable and use .add, NOT sortable and unique elements only

Right, for the solution, mine DID work and I put it into the original bit of code. In the solution they added in NEW code. Clumsier I reckon!

Did the next bit a bit differently - did 'get' instead of 'contains'. And did the 'max' instead of the [-1] for the highest value. Makes sense though!

Conclusion - 

List, set, tuple and dictionary. Got it. Mutable - list and set; Ordered - list and tuple. List uses [], tuple (), set uses {} or set(), dicts use {} with key/value pairs - keys have to be immutable.

That's the end of this section! I don't need to go over specific bits again but they key point is that ordering, mutability and constructor (e.g. what sort of brackets) are what distinguish the different structures. Each have different methods, specific for that data struct. The mutability and ordering is making more sense.

Total time - approx 1 hour.


Comments