Sage is math software that uses the world's fourth most popular programming language, Python, see http://www.tiobe.com/tiobe_index Unlike the first three most popular programming languages Python can be used interactively. Also the commerical math software systems like Mathematica and MATLAB use their own languages. By using Sage you're using/learning Python which will be useful in general.
This intro to Python for users of Sage on the SageMathCloud. Therefore we will do all the coding inside a Sage worksheet.
In this lesson you will learn:
Click under this box where it says
print "Hello world" and hit SHIFT-ENTER at the same time.
The light gray bar on the side will become green. Sage will run the program, and the results will show underneath.
Defining print - the way that python displays something
Defining syntax - the set of rules that the computer follows to run code.
In lesson one you did the classical Hello World that any coding tutorial will have you do. Now we're going to debilerately execute invalid code to learn about syntax errors.
Type the code from line 37 below this box. Remove the quotes. Execute the code using what you learned in the first box. Do not copy and paste as this is an exercise in the importance of syntax.
You should have gotten your first error. You will see errors a lot when you code. As you learn from examples you'll need to copy the examples exactly or you'll get an error because Python has a very strict syntax.
The error you got should look like:
Error in lines 1-1 Traceback (most recent call last): File "/projects/sage/sage-6.10/local/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 905, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "<string>", line 1 print Hello world ^ SyntaxError: invalid syntax
Notice that the error doesn't say what you're missing, but it does show you where the error is by using a ^. In this case, the error is in print hello world. Because of this you're going to have to memorize Python's basic syntax.
A string is a list of characters. This could be one character, a word, a sentence, a paragrah, or longer. It could have numbers or special characters as well. When you want to print a string, you need to start that string with either a
" and end it which ever you started it with.
There are special characters that we sometimes need to deal with in order to create a valid string. We are now going to look at some invalid strings, then discuss how to change them.
These are not valid strings:
t her name.'because Python in this case thinks the string stops at the ' in
He's going home."
"because Python in this case thinks the string stops at the " found in the dialogue
"He's going home".
To make a valid string, you need to change the string. Below are ways to change invalid strings. One way of doing this is adding a
\. This is called escaping.
't her name.'
Sarah isn't her name."
For the second example, there are also two fixes:
"that show dialogue
"He's going home.</span>"
'then put a \ in front of the
He said "He</span>
's going home."'