CoCalc Shared Filespython_course.sagewsOpen in CoCalc with one click!
Authors: Tim Clemans SMC, John Jeng, Christine Miles, Harald Schilly, ℏal Snyder, Hal Snyder, William A. Stein
Views : 10

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

print "Hello world"

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.

#type the code underneath this comment:
%md 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.

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.

%md ### 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 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.

Interact: please open in CoCalc

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 ' in isn'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."'
Interact: please open in CoCalc