Finished this section of Python but have some questions about it


#1

Hi there everyone, I managed to finish this section of Python but I do have some questions about it.

  1. When creating dictionaries, does it matter if I place a comma at the end of the last value?

prices = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3 <-------- (would it make a difference if we place a comma after this?)
}

  1. We used the "total = 0" value a few times, can someone tell me the significance of this?

#2

would it make a difference if we place a comma after this?

Nope. But without the comma it looks nicer, don't you think?

We used the "total = 0" value a few times, can someone tell me the significance of this?

I don't really understand your question, sorry. We use 0 as a value because when you go to the shop you don't want to pay for the purchases of the previous clients, right?

Without defining total we would get the error saying that total is undefined.


#3

My question being what is the point of us using "total = 0", like what is that whole statement there for? What if we decided not to use it? What is the importance of that statement?


#4

This statement is used to assign value 0 to variable total. And as I said before - we use total to store the result of our calculations (summing) so we have to define this variable and set it to 0 because this is identity element for addition in real numbers.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.