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)
tokens = ['<greeting>', 'Hello World!', '</greeting>']
count = 0
for token in tokens:
if token[0] == '<' and token[-1] == '>':
count += 1
print(count)
Comments
Post a Comment