Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Intro to Python for new users of Sage
Preface
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.
Chapter 1. Getting Python to say things
In this lesson you will learn:
how to execute code within Sage
the importance of syntax
common errors
strings
how to code strings with special characters
Key Terms
Print
Syntax
String
Escaping
Lesson 1. Executing code
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
Lesson 2. The importance of syntax
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:
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.
Lesson 3. Strings
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 '
or "
and end it which ever you started it with.
Lesson 4. Dealing with special characters
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:
'Sarah isn
't her name.'
because Python in this case thinks the string stops at the ' inisn't
."He said
"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.
put a \ in front of the
'
in isn't.'Sarah isn
</span>'t her name.'
OR
change the ' at the beginning and end to
"
"Sarah isn't her name.
"
For the second example, there are also two fixes:
you need to put a </span> in front of the
"
that show dialogue
"He said
</span>"He's going home.
</span>""
OR
change the " at the beginning and end to
'
then put a \ in front of the'
in he's
'He said "He
</span>'s going home."
'