I'm new to python and learning how to code. I'm printing last element of my list and sum of the list as-
print list[-1],sum
But the output is separated by " " and not separated by ",". Any idea how to separate it by comma?
I'm using Python 2.7
Use the sep keyword argument:
print(list[-1], sum, sep=',')
print list[-1],sum is clearly using Python2.Include it in quotes, like this:
print str(list[-1]) + "," + str(sum)
Enclosing them in str() is unnecessary if list[-1] and sum are strings.
In general, symbols are interpreted as Python symbols (for example, names like sum are interpreted as variable or function names). So whenever you want to print anything as is, you need to enclose it in quotes, to tell Python to ignore its interpretation as a Python symbol. Hence print "sum" will print the word sum, rather than the value stored in a variable called sum.
sum as a variable name as it's already the sum function.list as a variable name as it's already the list constructor :)