Introduction to Python Programming Course - Part 7

Right here we go for the weekday coding! This section is all about control flow. Last time it was the basics of if statements and conditionals - certain keywords, use of operators etc. Let's go!

Start Time - 19:28

For Loops

These are for iterating - do something repeatedly. 

An iterable - something that can return one element at a time. So it could be items in a list. 

I remember these from before - useful for when going through a sequence of numbers. For strings in this list example, using the title method just gives the name. An empty list, then for something in something - add them to this. Again with the title - property?

Range

So with range, it is the number of elements that are then accessed. Or it can be specific items. Or in range - just numbers up to that point.

OK this stuff is tricky. The hardest up to this point - I need practice with this and NOT rush through it!

Indentation - important here. If not indented, then it means that this is separate. Will explain more.

Start, stop, step - a specific method - it shows from which index, the number AFTER the last one to stop at (need to check this) and how much to go up by is step. OK.

Did the challenges. Good, am used to a basic iteration and a basic range. Cool!

SMASHED IT! Needed stack overflow to help but it suggested that and I still needed to figure the logic. 


names = ["Joey Tribbiani", "Monica Geller", "Chandler Bing", "Phoebe Buffay"]

usernames = []


# write your for loop here


for name in names:

    name_lower = name.lower()

    usernames.append(name_lower.replace(' ', '_'))


print(usernames)


In this, I got the idea of appending to a new list, but needed to separate out the two methods. Worked a treat and all makes sense!

ONE thing is I can put both methods on one line, rather than a separate bit for lower. Makes sense but good to try it the 'longer' way!

Wow. Nailed the next bit too! Needed to check syntax but here is how I could do the same thing but with the range function:

for name in range(len(usernames)):
    usernames[name] = usernames[name].lower().replace(" ", "_")


print(usernames)


Again. Got another one - used stack overflow for the 'contains' but actually it was just that character 'in' the item. 

tokens = ['<greeting>', 'Hello World!', '</greeting>']
count = 0

# write your for loop here

for i in tokens:
    if "<" and ">" in i:
        count += 1

print(count)

Don't understand the last quiz - let's see the solution!


Alternative for the token one - 

tokens = ['<greeting>', 'Hello World!', '</greeting>']

count = 0
for token in tokens:
    if token[0] == '<' and token[-1] == '>':
        count += 1

print(count)
I have to say it. My code is neater! I prefer how more succinct mine is.

Finish Time - 20:23 (55 minutes).


That was TOUGH! Good to have done this so thoroughly as I feel so much more confident with for loops than I did about 45 minutes ago! Range makes sense and learned some other methods - the lower and replace. It was fun but tricky!






Comments