Python Programming 7
Sometimes you just need a break. This chapter will be much shorter than the others because we are simply reviewing and adding on to some previous knowledge.
We are aware that we can store and print strings in variables. But we can do some string operations that can be very helpful. What if we want to find the number of characters in a sentence or paragraph.
words = "Hi I'm a really long sentence and you should try and guess how long I am."
print len(words)
This will print 73 because there are 73 characters in this sentence. Take say, a deranged individual who for some reason will not stop typing to you IN ALL CAPS. These people needed to have their voices severly dampened on the keyboard because it's so annoying.
annoyingPerson = "BLA BLA BLA ALL I DO IS TYPE IN CAPS"
quietDown = annoyingPerson.lower()
print quietDown
This code will take the string from annoyingPerson and convert it to lowercase letters with the lower() function. Take a good look at how we achieved this and stored the lowercase string in a new variable. You can do the same thing with lowercase letters by adding a .upper() function. You can also figure out if a string starts or ends with a specific word.
name = "Noam Chomsky"
print name.lower().startswith("noam")
This will print True because my name starts with 'noam'. Notice that 'noam' and "Noam" are different because of capitalization. However, this can be fixed by adding the .lower() function. The opposite function to startswith() is of course endswith(). What if I wanted to count the number of words in a sentence? This can be achieved by showing another powerful function in python.
sentence = "The proletariat have nothing to lose but their chains."
num_of_words = sentence.split(" ")
print len(num_of_words)
This will print 9, since there are 9 words in the sentence. Basically, we were able to split the sentence up into all of its words by using the .split() function. This function takes a string and cuts it up by whatever is in the (). In this case we has a space (" "). Which means all the words got cut up by a space. Once cut, we pushed these words into a list which we learned about in the last chapter. Then we simply printed the length of the list.
Boolean Issues....
You can create very complex boolean expressions with some extra tools like the 'and' and 'or'. Instead of having to write multiple if statements (or something) to handle several conditions, you can do it in one line.
x = 5
y = 6
if x == 5 and y ==6:
____print('I shall be printed')
Using the 'and' you can string together conditions. If both of those conditions are true, the print will run. If only one is true, it will fail. This is because both x and y must be true. You can also use the 'or'.
x = 5
y = 7
if x == 5 or y ==6:
____print('I shall be printed')
This will still print because only x or y needed to be true to run the rest of the code.
print not False
This will print True because the keyword not negates the value of the following boolean.
Try to answer the following questions without putting them in PyCharm. Then see if you're right by running them. Does 'g' == 'G'? What will be printed here:
print(True == not False)
print(True != True)
print((not True) == (False and True))
print(("hello" != "HELLO".lower()) and (False or True) and (not(not True)))
print(not(True or False))
print(True != False or False == True)
Write a program letting a user know if their tweet will be short enough to use on Twitter (restrict them to 250 characters ... it is 250 right? I don't use Twitter. Wait, hold on. Google says its 140. That's really dumb honestly. Like who restricts writing to 140 characters. Why is this company worth billions of dollars. The world doesn't make sense at times. Fine just use 140. Or 250. It really is just about the concept. Get to work.)