CH 1 Syntax

In this tutorial notebook, you will learn how to do programming in python and jupyter. Open a new notebook in ipython or https://try.jupyter.org/, try to "copy" and "paste" the following codes.

Jupyter

- 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 ()

Simple statements : Hello World

  1. The first thing we do is to print a phrase "Hello World!" using python
  2. The "print" command with an "object" print your object to the terminal
  3. Noted that "print" command can only used in python 2. In python 3 you have to use "print(your-object)" function

In [1]:
print "Hello World!"


Hello World!

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!')

In [2]:
print "Tips 1: And Enter makes a new line in a cell"
print "Tips 2: Print also output line by line"


Tips 1: And Enter makes a new line in a cell
Tips 2: Print also output line by line

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
'''


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

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.

Example: Time module


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



NameErrorTraceback (most recent call last)
<ipython-input-4-ef309ab19b8b> in <module>()
----> 1 time.sleep(0.5);
      2 print "Too bad"

NameError: name 'time' is not defined

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


Now its work

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


We delete the time object to unload it from memory

NameErrorTraceback (most recent call last)
<ipython-input-6-148481f0da4d> in <module>()
      1 print "We delete the time object to unload it from memory"
      2 del time
----> 3 time.sleep(0.5);

NameError: name 'time' is not defined

Integer

Declaring variable is simple, just put an equal sign to a variable name. Print it with the variable name:


In [16]:
a = 10
print a


10

You can print different variables at the same time with comma-separated, and the different arthimatic operations are showned


In [17]:
a = 10
b = 15
print a + b , a - b , a * b , a / b


25 -5 150 0

Float

f1 miss the zero, f2 is a usual float number, f3 and f4 use scientific notation, all are float numbers


In [18]:
f1 = 10.
f2 = 10.2
f3 = 1e9
f4 = 1e-9
f5 = .1
print f1,f2,f3,f4,f5


10.0 10.2 1000000000.0 1e-09 0.1

And the different arthimatic operations of floats are showned


In [19]:
f1 = 1e9
f2 = 1e-9
print f1 + f2 , f1 - f2 , f1 * f2 , f2 / f1


1000000000.0 1000000000.0 1.0 1e-18

String

s1 use double quote, s2 use single quote, s3 concatenate s1 and s2


In [20]:
s1 = "HK"
s2 = 'U'
s3 = s1 + s2
print s1,s2,s3


HK U HKU

The dot replace function replace a word "Hong Kong" in s1 to "Pok Fu Lam"


In [21]:
s1 = "The University of Hong Kong"
print s1
print s1.replace("Hong Kong","Pok Fu Lam")


The University of Hong Kong
The University of Pok Fu Lam

Each character can be obtained by giving an index of the string, noted that it starts from zero


In [22]:
s1 = "The University of Hong Kong"
print s1[0]
print s1[4]
print s1[18]
print s1[23]


T
U
H
K

Split is a powerful function that split a string with a character into a list (array)


In [23]:
s1 = "The University of Hong Kong"
print s1.split()
s2 = "11 12 13 14 15"
print s2.split()
s3 = "11.12,13.14,15"
print s3.split(',')


['The', 'University', 'of', 'Hong', 'Kong']
['11', '12', '13', '14', '15']
['11.12', '13.14', '15']

More about string in a statement

Use %s to act as a placeholder in output

print "........%s......." % your-variables

In [7]:
print "A %s C" % "F"


A F C

Use a bracket at the end if you use two or more placeholders


In [8]:
print "A %s C %s" % ("B","D")


A B C D

If your input is not a string, in this case it is an integer, it will convert to string automatically


In [9]:
print "A %s C %s" % (1,2)


A 1 C 2

Same for float numbers, but the formats depends


In [10]:
print "A %s C %s" % (1e-8,2e6)


A 1e-08 C 2000000.0

If you need precision in the printed float, use


In [11]:
print "1 Digit: %.1f" % 3.1415929
print "2 Digit: %.2f" % 3.1415929
print "3 Digit: %.3f" % 3.1415929
print "4 Digit: %.4f" % 3.1415929


1 Digit: 3.1
2 Digit: 3.14
3 Digit: 3.142
4 Digit: 3.1416

If you need precision in the scientific notation, use


In [12]:
print "1 Digit: %.1e" % 3.1415929
print "2 Digit: %.2e" % 299792458
print "3 Digit: %.3e" % 299792458
print "4 Digit: %.4e" % 6.62607004e-34


1 Digit: 3.1e+00
2 Digit: 3.00e+08
3 Digit: 2.998e+08
4 Digit: 6.6261e-34

Boolean

Boolean can be defined by True or False, and support basic logical operation.


In [24]:
b1 = True
b2 = False
b3 = (b1 and b2) or (b1 or b2)
print b1,b2,b3


True False True

Example: Input from future

If you need the print() function in python 3, input it from future


In [25]:
print('Hello World!')


Hello World!

In [26]:
from __future__ import print_function
print('Hello World!')


Hello World!