Home > Software engineering >  'Comma Code' Automate the Boring stuff with Python
'Comma Code' Automate the Boring stuff with Python

Time:07-02

Can someone please read through the comments and help me understand the code thoroughly.

I have just started learning coding through Automate with boring stuff with python.

I have some level of understanding but it's hard to put everything together.

# defines the function and sets (someList) as a parameter.
def listTostring(someList):         
    # creates an empty string
    a = '' 
    
    # iterates through the length of each item in (someList).    
    for i in range(len(someList)):   
        # concatenates comma at the end of each item.       
        a  = str(someList[i]   ', ') 
        
        # concatenates 'and'
        a  = str('and '   someList[len(someList)-1])  
        # please explain the above line of code

        print(a) 
    
spam = ['apples', 'bannanas', 'tofu', 'cats'] 

# passes spam as an argument.
listTostring(spam)  

CodePudding user response:

First of all, the operator for strings concatenates 2 strings. So, in your code,

a  = str('and '   someList[len(someList)-1])  

will concatenate the string 'and ' with the last string of someList at the end of a. Considering this, it's not necessarily to call to the function str() because 'and ' someList[len(someList)-1] is already a string. So, your function could be:

def listTostring(someList):         
# creates an empty string
a = '' 

# iterates through the length of each item in 'someList'.    
for i in range(len(someList)):   
    # concatenates comma at the end of each item.       
    a  = someList[i]   ', '
    
    # concatenates 'and'
    a  = 'and '   someList[len(someList)-1]
    # this line will concatenate 'and ' with the last element of someList at the end of a

    print(a)

So, in your case, a will finally be 'apples' ', ' 'bannanas' ', ' 'tofu' ', ' 'cats' ', ' 'and ' 'cats'. That is, 'apples, bannanas, tofu, cats, and cats'. Notice that cats is twice at the end because you did the iteration between all of the elements, instead of all without the last one.

CodePudding user response:

a  = str('and '   someList[len(someList)-1])  

a gets added another string - 'and' plus the last element of someList. str(...) isn't necessary here since and is already a string, and to access the last element of a list we don't need to get it's length first, we can just use negative indexes ! So this line could be rewritten simpler as:

a  = "and"   someList[-1]

CodePudding user response:

as I don't know which part of line exactly is your mean, I explain all of things

a  = str('and '   someList[len(someList)-1])

this code is equivalent to:

a = a   str('and '   someList[len(someList)-1])

and simply concat to string and set it to a

str convert some data to string... for example int (for example: if you want to print 1 1 = 2, you must write: print(str(1) " " str(1) " = " str(2)) because you can't concat str and int), float, and so on, but in this case 'and ' someList[len(someList)-1] is string and don't need any conversion, so you can remove it.

'and '   someList[len(someList)-1]

you use a for loop on your list using range(len(someList)), but it is incorrect and you must use range(len(someList) - 1)

why incorrect? because you loop on all data list, but you must loop on all data except last element and add it with an "and" at last.

using

for i in range(len(someList) - 1):   
    a  = someList[i]   ', '

your a is:

apples, bananas, tofu, 

and you need to add last element: someList[len(someList)-1] (len(someList) is 4 and len(someList) - 1 is 3 and mean 3rd element), simply you can use someList[-1]

and at last:

a = a   'and '   someList[len(someList)-1]

is

a = "apples, bananas, tofu, "   "and "   "cats"

complete code:

def listTostring(someList):
    a = ''
    
    for i in range(len(someList) - 1):
      a  = someList[i]   ', '
    
    a  = 'and '   someList[-1]
    print(a)

spam = ['apples', 'bannanas', 'tofu', 'cats'] 
listTostring(spam)
  • Related