Friday, June 6, 2014

Module 3: Python Fundamentals - Part 1

This week's assignment was to become familiar with Python scripting language.  We used Python to perform a few basic functions and methods.  We were instructed to start with a string object of our full name and write a script that ultimately printed only our last name and the number of letters in our last name multiplied by three.  The output of my script is seen above.

This was multi-step process in which we had to isolate each name in a list (e.g. first, middle, last), select only our last name, then calculate the number of letters in it.  This was achieved as follows:

1.       I used my full name “Philip Michael Coppola” as the original string and split this string into a list, which I called “listName” indexed as 0 (Philip), 1 (Michael), 2 (Coppola).
2.       I then used the script:
>>>lastNameLen = len(listName[-1])
>>>print lastNameLen

This gave a result of ‘7’, the length of “Coppola”.  Due to the requirement that another person should be able to use this code to print their last name, I had to use the [-1] index.  This references the last item in a list, whereas using [2] would only work if the other person put three names.

No comments:

Post a Comment