Still working on this CS50 assignment. I’ve finished porting to Python some of the first programs we wrote in C. There are some really cool things Python does out of the gate that made me a little bit jealous! For example:
We did a couple of cipher programs to change a message from the user into a coded message. To write this in C it requires a for loop to manually go letter by letter:
// prompt user for word to encode
The way that loops are written in Python makes it so much simpler! And being able to do this for a string, not just an array or object, is pretty cool if you ask me:
# prompt user for word to encode
One thing that I like in C that doesn’t work in Python is the fact that characters (letters, numbers, etc.) are equivalent to their ASCII value, so
a == 97,
A == 65, etc. This made it pretty straightforward in the cipher exercises because you can just do simple addition or subtraction to shift the letters. In Python you have to explicitly change characters into numbers with
ord(char), do the manipulation, and then change it back to a letter with
chr(num). I think it’s because Python forces type without needing the programmer to explicitly define type…so with characters they must be stored as strings? Anyway not the end of the world but I thought this was interesting.
I’m getting familiar with the syntax of writing objects in Python, for example:
Classes require an initialization function so will always start with the
__init__ method. They use the
self parameter so that methods can be called on the class;
self will always be the first parameter, and there will always be at least this one parameter.
I also learned the difference between a
set in figuring out how to load the positive and negative word lists being used to analyze the tweets:
List: Items are kept in order as values
Dict: Items are a key-value pair
Set: Values only, order is not maintained, and duplicates are not allowed.
So in this case
set is best, and has the added benefit of a speedier search since it’s not necessary to read through the whole list—it will just stop when the relevant word is found.