Coding Path Questions?

Hello @mtf,

Thank you for the encouraging words. I for sure will be staying the course just had a frustrating moment last night trying to code the items listed below:

> authors = “Audre Lorde,Gabriela Mistral,Jean Toomer,An Qi,Walt Whitman,Shel Silverstein,Carmen Boullosa,Kamala Suraiyya,Langston Hughes,Adrienne Rich,Nikki Giovanni” > author_names = authors.split(‘,’) > print(author_names, “\n”) > author_last_names = > for name in author_names: > author_last_names.append(name.split(" ")[-1]) > print(author_last_names, “\n”) > print(authors[-1], “\n”)

The line of code that got me was :

for name in author_names:
author_last_names.append(name.split(" ")[-1])

I had no idea that I could use name.split()[-1]

I think I understand it now. Correct me if I am wrong…I had a split list and was able to get this item in the list due to it being in the -1 spot of the index?

Thank you again for your patience. I really do appreciate your time.

Best Regards

1 Like

We know that str.split() returns a list with the space character as the default delimiter. We also know that a negative index means right to left. [-1] is the index of the last item (from the left) and first item from the right. Keep at it.

Hello @mtf,

Thank you again for your explanation. It is much appreciated. Still keeping at it. Working on split and join at the moment.

Best Regards.

1 Like

The range object to validate all user supplied indices (subscripts) is built upon the len() function:

    iterable = ?
    i = ?
    n = len(iterable)
    r = range(-n, n)
    v = i in r    #  will be True if i is in range

This is stuff that you will know in your sleep. Don’t make it harder to learn than it actually is.

Hello @mtf

Thank you once again for your help in the explanation you provided. I am always grateful.

I was wondering if you could help me understand why the code below is using a "toomer bio = " I don’t understand the = \ part of the code.

toomer_bio = \ """ Nathan Pinchback Tomer, who adopted the name Jean Tomer early in his literary career, was born in Washington, D.C. in 1894. Jean is the son of Nathan Tomer was a mixed-race freedman, born into slavery in 1839 in Chatham County, North Carolina. Jean Tomer is most well known for his first book Cane, which vividly portrays the life of African-Americans in southern farmlands. """

Thank you for your time and help.

1 Like

Outside of triple quotes (multiline strings) line breaks cause strings to be unreadable and trigger a syntax error or an exception. Try removing the \ and run the code. Is it a syntax error (pre-compile) or an exception (runtime error)?

In Python, \ at the end of a line just before a line break signals, ‘continuation’ to the interpreter, and thus the line break is ignored. Technically, line 2 above will raise an error given it has a newline (line break) without the continuation operator.

1 Like

Hello @mtf,

Thank you so much for your comments in regards to the line of code with the \ syntax. When I remove the \ character I get the following error message:

File “script.py”, line 1
daily_sales =
^
SyntaxError: invalid syntax

As soon as I add the \ character to the string with the “”" characters I don’t get the syntax error message any further.

Good news is I finished up the string project that had thread count, name of customer and sales amount. I did pretty good throughout most of the project. I needed some help of course on some of the items but my mind is starting to work through some of the concepts that I am struggling with. I of course am no master at python but I am showing up each day to try and learn more. I am about to start another project which most likely I will do tomorrow.

The one .method that is messing me up is the .format method. I am still trying to understand it better.

Thank you as always for your time and help.

Best Regards,

1 Like

str.format() is well documented online. Look for pyformat and give it a complete read. The oldest form of formatting is modulo (nickname for %). You don’t need to get into the weeds on the oldest form, just skim the surface and move on to .format().

The thing to keep note of is the directives that can be applied to placeholder expressions. They are explained in the % early days in terms of type, and expanded in the .format() literature. I won’t get into it, here but leave you to do the reading.

When you have a good handle on .format() and feel comfortable playing around with it, migrate that understanding to the newest form of string formating, f-string which expands on the directives even further. Don’t skip the .format reading. It is essential to getting a handle on this new method. In the end, you will find it easy to adopt and use and never look back on the older methods. One thing at a time so you become totally immersed in the whole string formating idea.

1 Like