Introduction to Python Programming Course - Part 14

Final bit of consolidation. The rest of Control Flow. Last time I really got to grips with creating a new list from existing and modifying one. The extra practice is worth it - need to ensure I do the same here and not just rush through!

Start Time - 16:44

So Udacity is not working! I've tried various times and it simply won't open. So I'm not going to give up and have hunted around for some other Control Flow stuff. Going to go over this for around half an hour, then hope Udacity is working tomorrow. If not, time for a new course!

https://pynative.com/python-control-flow-statements/

For loops

These are iterated a fixed number of times. You need to know that number of times. For an unknown number, use while loops.

Also good for when doing something with values in a list.

Challenge - I overcomplicated by doing the % 2 == 0 - in range, I can just do a step of 2!

Break statement - this terminates the loop. Then it goes onto the next line of code.

Or the innermost loop in nested loops. 

Continue statement - skips that iteration and moves onto the next. Did an example - using continue in the example (if a letter is in a string) - use continue for when it is NOT in the string. Then do the adding to count etc. 

Pass statement - don't know this! This is for empty code, but could get added to at some point. So you could go back in and do something.

Else - can also be used after the code in for loop is run. E.g. printing first five numbers, then else is a text of 'Done'. Or something like that. It's AFTER the code.

Break statement can override this and go to the end so the else statement was never actually used.

Reverse loop - this is new! 

list1 = [10, 20, 30, 40] for num in reversed(list1): print(num)

Sorry that looks messy - copied and pasted! Basically the 'reversed' keyword is used before the list. Can also be done using neg numbers in the range.

Nested for loops - complex stuff! Let's do a practice here...

characters = ["Wedesday", "Enid", "Bianca"]


for i in range(1, 10):

    print(i)

    for name in characters:

        print(name)


That's just to visually see it - not that the above code is useful!


Another example - 

characters = ["Wedesday", "Enid", "Bianca"]


count = 0


for i in range(1, 10):

    print(i)

    

    if count >= len(characters):

        break

    else: 

        name = characters[count]

        count +=1

        print(name)


And one more!

characters = ["Wedesday", "Enid", "Bianca"]


tags = ["Legend", "Werewolf", "Siren"]


count = 0


for character in characters:

    for tag in tags:

        if count >= len(characters):

            print("No more characters")

            break

        else:

            print("{} is a {}".format(characters[count], tags[count]))

            count += 1

Nothing exciting but basically the idea of starting a loop, then running the mini loop within it.  The idea is to see the nested part. 


Finish time - 17:15 (approx 30 minutes).

So a few points here, which I'll put into the summary. Next time, hopefully Udacity will be working. If it doesn't, then it will be time to find a new course. The good point here is that over the last month, the Udacity course has definitely sparked enough interest for me to WANT to continue learning python! Small, steady progress!



Comments