Introduction to Python Programming Course - Part 10
Just managing to squeeze in a weekday session. Am on holiday from work so time on a Friday! More on control flow. Lots on different for and while loops - will need to keep working on these and using in context as they're conceptually tricky.
Start Time - 10:17
Break, Continue
For more precise loop of when 'Break' is used. Used to end the loop if we detect a condition has been met. Used in for and while.
Cargo weight example - if over a weight, break statement means we stop loading.
Continue - skips just one iteration.
Challenge - this was tricky! I nearly had it -
# HINT: modify the headlines list to verify your loop works with different inputs
headlines = ["Local Bear Eaten by Man",
"Legislature Announces New Laws",
"Peasant Discovers Violence Inherent in System",
"Cat Rescues Fireman Stuck in Tree",
"Brave Knight Runs Away",
"Papperbok Review: Totally Triffic"]
news_ticker = ""
# write your loop here
for headline in headlines:
news_ticker += headline + " "
if len(news_ticker) >= 140:
news_ticker = news_ticker[:140]
break
print(news_ticker)
I didn't know about the news_ticker = news_ticker[:140] line, and started with the break, like with the example in the earlier info. So I had a logic to that!
Not too bad though - I got the concept which was the main thing.
Great practice for prime number finding. I've done this before with Swift and never found a neat code. This looks neat!
## Your code should check if each number in the list is a prime number
check_prime = [26, 39, 51, 53, 57, 79, 85]
## write your code here
## HINT: You can use the modulo operator to find a factor
for number in check_prime:
for i in range(2, number):
if number % i == 0:
print("{} is NOT a prime number, because {} is a factor of {}.".format(number, i, number))
break
if i == number -1:
print("{} IS a prime number.".format(number))
I'm not totally sure about why to use break here, and what the -1 bit is. But I'll get there!
OK without break, it loops through until proving a non-prime is prime. So the break is needed - it means that once one example of factor to show not prime comes up, it goes onto the next number.
The -1 is continuing up to the number I am testing, of possible factors of up to less than one of that number. E.g. for 7, it would test from 2, to 3, 4, 5 and 6. So that's why after that the break is needed.
*I will come back to more practice with these!
Zip and Enumerate
Combining and splitting lists. Zip.
Returns an iterator. You can unzip too.
Enumerate - returns tuples with indices and values. Easier?
Quiz (zip)
I got the solution a bit differently - but can see the proper zip syntax is more elegant. All good!
Other Zip quizzes OK - but wasn't familiar enough with syntax to do them.
Enumerate - difficult! Needed help -
cast = ["Barney Stinson", "Robin Scherbatsky", "Ted Mosby", "Lily Aldrin", "Marshall Eriksen"]
heights = [72, 68, 72, 66, 76]
# write your for loop here
for i, name in enumerate(cast):
cast[i] = name + " " + str(heights[i])
print(cast)
But it makes sense. Just need more practice with these. Something I'll need to do after this section is go back and practice all the different loops and aspect of this. Will definitely help!
Finish Time - 11:02 (approx 45 minutes)
A bit more of control flow to finish, then I definitely need a summary of going over each part with practice again.
Comments
Post a Comment