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.
In [1]:
print "Hello World!"
PROGRAM HELLO
WRITE (*,100)
STOP
100 FORMAT (' Hello World! ' /)
END
(print "Hello World")
#include <iostream.h>
main()
{
cout << "Hello World!" << endl;
return 0;
}
class HelloWorld {
static public void main( String args[] ) {
System.out.println( "Hello World!" );
}
}
disp('Hello World');
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"
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
'''
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.
In [4]:
time.sleep(0.5);
print "Too bad"
In [5]:
import time
time.sleep(0.5);
print "Now its work"
In [6]:
print "We delete the time object to unload it from memory"
del time
time.sleep(0.5);
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
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
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
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
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
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")
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]
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(',')
Use %s to act as a placeholder in output
print "........%s......." % your-variables
In [7]:
print "A %s C" % "F"
Use a bracket at the end if you use two or more placeholders
In [8]:
print "A %s C %s" % ("B","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)
Same for float numbers, but the formats depends
In [10]:
print "A %s C %s" % (1e-8,2e6)
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
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
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
In [25]:
print('Hello World!')
In [26]:
from __future__ import print_function
print('Hello World!')