In this tutorial notebook, you will learn how to do programming in python and Jupyter (formerly named as ipython). Open a new python notebook, try to copy
and paste
the following codes to play with.
When you read the following codes, you will see how simple and clear Python syntax is.
In [1]:
# make a list
students = ['boy', 'boy', 'girl', 'boy', 'girl', 'girl', 'boy', 'boy', 'girl', 'girl', 'boy', 'boy']
boys = 0; girls = 0
for s in students:
if s == 'boy':
boys = boys +1
else:
girls+=1
print("boys:", boys)
print("girls:", girls)
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!')
SHIFT+ENTER
on your keyboard or press the play button () in the toolbar above.You may read the following tutorial for more information: https://www.datacamp.com/community/tutorials/tutorial-jupyter-notebook
Hello World!
statementprint("Hello World!")
SHIFT+ENTER
on your keyboard!
In [ ]:
In [2]:
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
""")
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.
In [4]:
%timeit sum(range(100000))
Python has a lot of 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 [5]:
import numpy
In [6]:
numpy
Out[6]:
In [7]:
import numpy as np
In [8]:
np
Out[8]:
In [9]:
# Declare x=0
x=0
In [10]:
x += 2 # shorthand for x = x + 2
In [11]:
"""
Multi line Strings can also be used as comment (a docstring)
"""
x += 2
In [12]:
x = 1 + 2 + 3 + 4 + 5 \
+ 6 + 7 + 8
It is also possible to continue expressions on the next line within parentheses, without using the \
marker:
In [13]:
x = (1 + 2 + 3 + 4 +
5 + 6 + 7 + 8)
Sometimes it can be useful to put multiple statements on a single line.
In [14]:
boys = 0; girls = 0
# Python 2 only!
>> print "first value:", 1
first value: 1
# Python 3 onwards
>> print("first value:", 1)
first value: 1
In order to use print function in python 2, you may execute at the beginning
from __future__ import print_function
In [15]:
for s in students:
if s == 'boy':
boys = boys +1
else:
girls+=1
// C code
for(int i=0; i<100; i++)
{
// curly braces indicate code block
total += i;
}
In Python, code blocks are denoted by indentation:
for i in range(100):
# indentation indicates code block
total += i
This is a very brief exploration of the essential features of Python syntax; its purpose is to give you a good frame of reference for when you're reading the code in later sections. The most widely used style guide in Python is known as PEP8
, and can be found at https://www.python.org/dev/peps/pep-0008/.