Introduction to Python Programming Course - Part 3
Getting some momentum going! And not that many more parts to this types and operations bit of the course. Last time it was all about Ints, Floats, Bools and Strings - different types and ways of using different operators with them.
Start Time - unknown
22. String Methods Practice
\n - special way of putting in a line break.
OK learned some new methods!
verse_length = len(verse)
print(verse_length)
print(verse.find("and"))
print(verse.index("and"))
print(verse.rfind("you"))
print(verse.count("you"))
So find and index - at what character index does 'and' first appear; rfind - last appear for "you" and count is of a specific string within a string "you". Had to look them up but that's good - that was the point of this challenge!
One thing I didn't do was the descriptive message. For that it is using the 'format' function.
Use of {} for the missing values. Let's just practice that...
This is what they did -
message = "Verse has a length of {} characters.\nThe first occurence of the \
word 'and' occurs at the {}th index.\nThe last occurence of the word 'you' \
occurs at the {}th index.\nThe word 'you' occurs {} times in the verse."
length = len(verse)
first_idx = verse.find('and')
last_idx = verse.rfind('you')
count = verse.count('you')
print(message.format(length, first_idx, last_idx, count))
Something to get used to is using {}, then doing a specific sequence so that they match up. Interesting!
Paused after approx. 20 minutes
Continued next day
23. Solution
All good
24. There's a bug in my code
Errors -
Division by zero (seen this one already)
Syntax - parsing EOF - something missing like closing parentheses or ending quotes
Type error - len - could be used for many functions - not the right number or arguments given
There are other ones too
A top tip is to search for the error message on google. Also print statements - these then tell you if there is an error or not.
26. Summary
SO, what we have learned in this section of the course..
Data types - int, float, string and bool
Operators - four main types plus exponentiation, modulo, integer division
Assignment - = but also += etc
Comparisons - < > != etc
Logical - keywords of or, and not
There was more - not in the summary.
Use of strings and performing functions e.g. len for length. Combining values e.g. strings together.
Use of _ for separating words, various other functions with strings. Use of the {} in a value, then putting in the values after. Then as long as number of arguments match, it will work!
Finished after approx. 20 minutes.
Total time - 40 minutes
Enough for now. Will do a bit more at the weekend, which will be 'Concepts'. This will be going in deeper but I'm confident I'll keep developing.
Comments
Post a Comment