Introduction to Python Programming Course - Part 4

Onto the next section - concepts!

Data Structures. I remember doing 'structs' and 'classes' with Swift and these were always tricky.

Yes this is all about grouping together different types. Also going to learn about membership and identity - not sure what those terms mean, so am looking forward to finding out!

2. Lists and Membership Operators

Containers - contain data types and other containers!

Lists - I think these are the equivalent of arrays

E.g. months of the years - contain strings (the months). Square brackets. Ordered - yep the same as array. Indexes etc. First element is 0. Think of it as how far away from the beginning of the list. 

-1 index is the last index item, -2 is second to last etc. Not -0!

List index exception - error to show if trying to access item that is not there - will show you how far off etc.

3. Quiz - lists and membership operators and 4. solution

One of those terms. Membership makes sense - items that are members in a list. 

Nailed question about the number of days - did the index for month - 1 in the square brackets.

Only one I did not get was when printing out multiple elements - 

print(eclipse_dates[-3:])

So doing the colon means everything from that to the end will get printed e.g. -3, -2, -1. That make senses and is something new!

Approx. 20 minutes

Continued next day!

5. Why do we need lists

Lists - real world example with MSFT then the vanguard one. 506 holdings - then can be accessed through the index etc.

Using the 'in' keyword returns a bool (t/f).

6. List Methods

Lists are mutable. Different to strings when values have been changed. This is for reassigning.  Need to look into that more... 

There are certain methods e.g. len, max. Max depends on the object type, with Int/Float it would be biggest. For strings it would be latest in alphabet. Interesting...

You can do lists with multiple types. But max could not compare them. Min - opposite of max.

Sorted - sends a copy from smallest to largest, leaving the original unchanged.

Join method - Puts together string items into one string. The 'join' bit is what goes between each item e.g. new line or a hyphen etc. You can only join strings.

Append - adds element to the end of the list

7. Quiz

All good. And some extra practice - 

f1_drivers = ["Alonso", "Leclerc", "Verstappen", "Hamilton"]

print(sorted(f1_drivers))


f1_drivers.append("Perez")

print(f1_drivers)


print(max(f1_drivers))


f1_drivers_joined = " & ".join(f1_drivers)

print(f1_drivers_joined)


8. Check for understanding lists


Yes - got the main idea. That Data structures contain different data types, that structs are types but not the other way around. 

Lists - mutable and ordered

Slicing the elements - harder. Need to use the : So e.g. 2:6 gives the 3rd to 7th items. Fine. But then :3 gives first three items, not 4. And 4: gives from the fifth item. That colon before the number is the confusing bit, looking at it. But I'll get it!

9. Tuples

I know these. Two or more values put together in one object. 

We are calling these CONTAINERS. Related pieces of info. Immutable and ordered.

Lat and Lon example. Accessed by indices. Not like lists - cannot add or remove items, sort them or that.

Two or more values so closely related. So length, width, height. Parentheses are optional for these... So brackets not needed.

Tuple unpacking - putting in the assignment labels for each value, can be done after. Or that can be left and accessed using curly brackets, then format, then putting the labels in. A bit of syntax there!

10. Quiz 

All good. More practice...

position = 4, 6

print("The latitude is " ,position[0], " and the logitude is ",position[1])

vettel, hamilton, alonso = 4, 7, 2


print("The three most successful championship totals are: {}, {} and {}".format(hamilton, vettel, alonso))

With this, I didn't even need the name of the tuple, just the variables. In the first example, the commas were crucial, to separate out the string from the tuples. 

All good!

11. Sets

Last bit for now!

These are mutable and unordered. So like lists but not ordered. 

You can print unique values. So you could record and collect multiple repeated info. You can add to sets and use the 'add' method, not append.

Pop method to remove. No last element etc. Or index. Mathematical sets.

Union, intersection and difference - these are all unique for sets.


12. Sets Quiz

A note here. You can make a specific set by using the set keyword if changing it from a list to a set. Also if curly brackets are used, then that does define a set and not a list.

Pop removes a random element from a set. 


Finished there. Total time - approx. 1 hour.

So I've learned a lot so far in data strcutures. The structs I've looked at have been lists, tuples and sets. They are either mutable/immutable and ordered/unordered. They have different methods and syntax and functionality. A key thing is that they are all structs which means that they use different TYPES. There's a good distinction. All good so far!


Comments