Need some explanation about syntax


#1



Hi, I don't quite understand the principles of syntax. I mean, why are there spaces between elements of the code while you can write it without spaces as well, e.g. "eggs = 12" has spaces between each element, but I tried "eggs=12" and it also worked, so why bothering put spaces? The other thing is why are there spaces between some lines while there are none between others. For instance, there is no space between "def", "eggs" and "return" lines, but there is one between "return" and "print" line. Why is it so? In another exercise the variable name was something like "name_name", why do we need the "_" sign? Can't we just write it as "name name" with a space between the two "names"? And the last thing about putting four spaces before a line, should there be exactly four spaces or there could be less? If there should be exactly four, why? Instruction says that we need spaces to structure the code, but since we have lines isn't it already structured? what is the reason behind that? Thank you in advance!



Replace this line with your code.


#2

In this case, the spaces are only for readability. But for example here:

def spam

the space is critical to separate def keyword and the name of your definition (spam)

so, spaces and equal signs separate keywords and names.

This choice is for the developer, you want to got for maximum readability, now, with 5 lines this is not very important, but what about 100,000+ lines of code? and its better to start early with good habits.

i personally think you got it right, function in 3 lines, then an empty line, then the function call

again, your choice. You could also do nameName (pretty uncommon in python, this happens more in JS)

no, because spaces will separate them, variable names have to be one word (this is why we use underscore, so you can see the different words, which means you can give your variable longer logic names)

again, free choice. But 4 spaces is very common, and again, being consistent is very important.

with 2 spaces it might be difficult to see how everything is indented, and you don't want to many spaces, this will force the code of your screen


#3

Thank you!!! That was super helpful!! So if I get it right, the main
purpose of these signs is to separate some elements from the others and to
increase overall readability of the code, right?


#4

just like we separate words in sentences by spaces, we need to do the same in programming language. How else do you know where one word ends and the other start? (okay, yes, we humans can, but its just an example)

yes, seems you understand :slight_smile:


#5

Ok, THANK YOU!
Stetim94 codecademy@discoursemail.com於 2017年4月26日 週三,18:19寫道:


#6

Hello, may I ask you a few more questions?
1) what's the difference between a number data type and a string data type? since strings can also contain numbers, so what's the difference between, say, "spam = 5" and "spam = "5""?
2) as a second part of the question above, I don't understand what does the string do. What's it purpose?
3) It is said that string needs to be within quotes. But what the difference does it make when I use quotes (" ") or apostrophe (' ')?
4) "print" command allows us to print out the string. But in some exercises in the print line we write the string itself (e.g. "the_machine_goes = "Ping!", print "Ping!"") while in the others we write the name of the variable (e.g. "the_machine_goes = "Ping!", print the_machine_goes"). Again, is there any difference, if any, and when should we use the former or the latter?
5) Why do the elements that are replaced by the "%" operator have an "s" in the end, as"%s"? What does it stand for?

Will be glad if you could answer this, thank you!


#7
  1. a number is actually a number, and a string is just a piece of text:

print 5 * 5
print "5" * 5
  1. string can be used to stored pieces of text.

  2. You could use both, doesn't matter

  3. Why store a string in a variable if you are then going to print the string? But what if you need user input:

    x = raw_input("enter a string: ")
  4. the s stands for string


#9

Everything is clear except of the 3rd, I still don't understand when we should use which, could you further elaborate on it pls?


#10

well, strings take up memory. So once you have the string in memory (by storing it in a variable) it makes more sense to use the variable, making a new string would take up more memory


#11

ok, thanks, appreciate that!

Stetim94 codecademy@discoursemail.com於 2017年4月28日 週五,15:36寫道:


#12

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