Convention dictates that everyone's first program should be hello world
. While this seems of limited use it is really important on several counts:
Input the follwing code and press SHIFT+ENTER to execute.
print("Hello world!")
As a result:
print('Hello Drew!')
executes correctly but:
print("Hello anyone!')
and:
print "Hello anyone!"
raise SyntaxError
s. The messages are Python's way of trying to tell you what it thinks is wrong with the code you have written. Sometimes these messages can be quite long, so generally it is best read them from bottom to top.
In the first case the error message is EOL while scanning string literal
, which needs some explanation.
EOL
means End of line
and in this context means Python reached the end of the line (while scanning) without having read a complete command.string literal
references that Python, and programming languages in general refer to text as strings (of characters). We will use this term a lot.strings
must be contained in matching pairs of quotes, the first indicates the start of the string, the second its end.In the second case, Python has done a really good job of telling us it thinks the problem is. Missing parentheses in call to 'print'. Did you mean print("Hello anyone!")?
, yes we did forget the parentheses and the suggested corrections is what we meant to type.
After saying hello to the world let's see if we can use Python to perform some calculations, afterall this is what computers are supposed to be good for. Let's try a couple of examples:
print(4+2)
print(3-1)
print(5*3)
print(6/3)