Python Syntax

Introduction

In this tutorial notebook, you will learn how to do programming in python and Jupyter (formerly named as ipython). Syntax of python is cosidered as "clean" compared to other languages.

Open a new python notebook, try to copy and paste the following codes to play with. The following codes show an example of a woring python code that calculate the

Introduction to ipython/jupyter

The Jupyter notebook is getting more attentions in the natural Science field in the past years. The development of jupyter notebook makes it more advance and stable. This is an article about

http://www.nature.com/news/interactive-notebooks-sharing-the-code-1.16261

Basic operations:

  1. Click on the cell to select it.
  2. Press SHIFT+ENTER on your keyboard or press the play button () in the toolbar above.
  3. Open new cells using the plus button ()

Let's us print a Hello World! statement

  1. Write
    print("Hello World!")
    
    into the cell below:
  2. Press SHIFT+ENTER on your keyboard!

In [ ]:

Hello World! in other language

Fortran

PROGRAM HELLO
    WRITE (*,100)
    STOP
100 FORMAT (' Hello World! ' /)
    END

Lisp

(print "Hello World!")

C++

#include <iostream.h>
main()
{
    cout << "Hello World!" << endl;
    return 0;
}

Java

class HelloWorld {
  static public void main( String args[] ) {
    System.out.println( "Hello World!" );
  }
}

Matlab

disp('Hello World!');

JavaScript

document.write('Hello World!')

Screen Output

To enter more than one line in a cell, press Enter. The print function will print all the objects seprated by a comma , on the same line, the output will have . For example, print "Hello", "World!" will have a space between Hello and World!.


In [2]:
print "Hello", "World!"


  File "<ipython-input-2-cc9c04ed789a>", line 1
    print "Hello", "World!"
                ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("Hello", "World!")?

In [3]:
print "Tips 3: Use \ to escape an characters like \""
print "Tips 4: Use \\n \n to make a newline character"
print '''Tips 5: Use three \' to 
make 
multiple 
line
'''


  File "<ipython-input-3-8d88a3308fc6>", line 1
    print "Tips 3: Use \ to escape an characters like \""
                                                        ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("Tips 3: Use \ to escape an characters like \"")?

The magic command (ipython specific)

Any command starts with % are magic command in ipython notebook. These % command can only be used in ipython instant. A full list of magic command can be found here:

http://ipython.readthedocs.io/en/stable/interactive/magics.html.

These commands are particularly useful in developing and debugging your program.

Import package and library

Python is rich in library. Use expression like

import xxxxxxx

or

from xxxxxxx import yyyyyy

to import library provided that you know the name of your library xxxxxx and objects yyyyyyy in the library xxxxxxx.

Package is not loaded at the beginning, you need to import it before using it.

Object-oriented programme

Example: Time module


In [4]:
time.sleep(0.5);
print "Too bad"


  File "<ipython-input-4-1a028bee3cb0>", line 2
    print "Too bad"
                  ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("Too bad")?

In [5]:
import time
time.sleep(0.5);
print "Now its work"


  File "<ipython-input-5-c68cb05575ef>", line 3
    print "Now its work"
                       ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("Now its work")?

In [6]:
print "We delete the time object to unload it from memory"
del time
time.sleep(0.5);


  File "<ipython-input-6-49c19e8d45cc>", line 1
    print "We delete the time object to unload it from memory"
                                                             ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("We delete the time object to unload it from memory")?

Markdown

Sometimes you may need to write down some notes for yourself. The Jupyter notebook provides convenient ways for you to describe the notes in a Markdown mark-up language. To learn this language, you can look at the following page: https://guides.github.com/features/mastering-markdown/ . GitHub utilizes Markdown extensively.

To change the purpose of the cell, you can look up a widget like this:

and select Markdown


In [ ]: