Introduction to Python Programming Course - Part 13

 More consolidation time! Control flow here we go...

Start Time - 19:48

Conditional Statements

If - this uses colon. Checks a condition. Elif - instead of else if. Else - all others. 

It skips code if a condition is not true. 

Indentation - this is more than a visual. It determines what lines of code are then run.

*SOmething I've noticed is that it's better to put a whole string, then use the format method, than it is to add individual Int or other values. Makes more sense and easier. 

Boolean Expression

Next bit now. All good so far. 

This could be using and, or, not. Or more complex expressions.

Never use 'true' or 'false'. If statement always evaluated to true - no point!

'Not' operator to see if something is false. 

Use 'None' for an no value. Not 'nil' like that is in Swift. 

This is how you use a range of values:

elif 151 <= points <= 180:

Elif could be if or else (irrelevant). But we have the number, the comparison, then the variable, then the next comparison, then the number. 

For Loops

This is when is starts getting harder!! 

Iterable - returns one element at a time. Can be in lists, dictionaries or other basic types like strings. Sequence types - string, list, tuple; non-sequence - dict and (files?).

In lists - easy. 

Range - this is harder! For just a number in brackets, then it just uses that number of iterations. Then there is (start, stop, range). That is what number to start at, where to stop and what increment. 

You can have (start, stop) as well with the increment set as 1.

Or you can have (stop) which is all nos. up to that one. From 0.

OK. That ALL makes more sense!

Creating new lists...

So this is all about using a previous list, then having an empty new one, then using a for loop to add each item to the new one!

The point? Well, there would be a string method e.g. 'title' to capitalise each item. 

Modifying...

Harder! Needs the range and other function - in the example below - len. Need to practise this...

cities = ['new york city', 'mountain view', 'chicago', 'los angeles']

for index in range(len(cities)):
    cities[index] = cities[index].title()

So we have range as from 0 to the last item basically. Having len gives us the length of the list. So it will go from default 0 up to the length of the list (4 items). Then the last part iterates through the index of 0 each time, to the last item. Index - 1? No apparently not! Making more sense...

*A key point I didn't get before is that there is a difference in creating a new list and modifying an existing one. In the former, a new empty list needs to be ready, for then the existing one to be added each item (with a function e.g. lowercase) to change each item. A modifying needs the 'range' function to then make changes to what is already there. More code needed in the latter. 

Practice...

OK. The modifying a list is making much more sense. Having done some practise with character names and f1 drivers! Basically, I need to use the range function, as I'm going to be iterating through each item and changing it. To get ALL items, I need the len function around the list. Then in the iterations, I need the index of the 'driver' or 'element' basically part for the list, then have that equal to how I am modifying it. That makes sense to me!

Last thing is practice with making a new list from existing...

YES! I've got it. Simple - a new empty list is created. Then I run a for loop with the existing one, then set a new variable for each element, with any changes I want to make (the changes could be done later but seems to make more sense to do so here!). Then append the NEW variable to the NEW list. 

Cool!

Finish Time - 20:49 (approx 1 hour)

This was so worth doing. I really skimmed over this and was so concerned in just getting the challenges right (after basically checking the answers then editing my code!) rather than putting in the practice to UNDERSTAND the code. 

Now I do and am confident I could apply that to any list - for creating new or modifying with a for loop and all of that conceptual understanding!


Comments