02 The first Python program
16 Jan 2018In your work directory, create a directory ~/PHY494/02_python
and go
to this directory:
Hello World
Using you favorite editor, create a text file named hello.py
in the
directory ~/PHY494/02_python
1 with the
following content:
Execute ("run") your hello.py
program with the python
program:
Python interpreter
Python is an interpreted language; you can
think of python
reading each line in an input file and executing
it. Start python
on its own:
You should see something like
Python 3.5.3 |Anaconda custom (x86_64)| (default, Mar 6 2017, 12:15:08)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
The Python prompt is >>>
.
Type commands and execute them with Enter.
Like the shell, Python is a REPL, a "Read-eval-print-loop".
Activity: hello, line by line
Type the commands from hello.py
into the interpreter, hitting Enter
after every line, and execute your program step by step.
Exit with quit()
or Ctrl + d
Basic plotting
Create a file motion.py
with content
You should create a graph similar to the following:
Interactive Python with ipython
The ipython interpreter is like python
but with
lots of improvements such as TAB
-completion, help with command?
(one question mark directly following a command) and source code with
command??
(two question marks), command line history, and many
additional shell-like commands (so-called "magic" commands such as
%cd
, %ls
, %pwd
, %run
, %time
and %timeit
— see %magic
for help).
Start it with
It should look like
Python 3.5.3 |Anaconda custom (x86_64)| (default, Mar 6 2017, 12:15:08)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.1.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]:
where In [1]:
is the prompt.
Just use ipython
instead of python
for interactive work.
ipython
basics
- To exit, give the
exit()
command or^D
(Control
+D
). - Use arrow keys to move up and down through the history or through multi-line commands.
- Use TAB-completion.
- Use
command?
or?command
(question mark directly before or after a command or object) to get help %run FILE
executes FILE similarly topython FILE
.2
ipython
with matplotlib
In the basic plotting example we used the matplotlib
library for
plotting and wrote a figure to a file. ipython
can show figures
interactively but the details depend on your operating system and
installed packages. Try the following and see if the figure is
displayed on your screen when you run motion.py
:
Windows
Run ipython qtconsole
to show graphics inline:3
In ipython
macOS
Run ipython
In ipython
Linux
Run ipython
(If gtk3
does not work, try qt5
, qt
, wx
, tk
, auto
, or just
--matplotlib
.)
In ipython
Footnotes
(If `qt5` does not work, try `qt`, `wx`, `tk`, `auto`, or just
`--matplotlib`.)
However, this does not always work and might require additional
packages. Please share any insights and solutions!
-
If you can call your editor from the command line then this is as easy as, for example with
atom
(ornano
),cd ~/PHY494/02_python atom hello.py
This will open a new file with name
hello.py
or open an existing file if it is present.However, if you use an editor that you have to open from, say, the Windows Start menu, then you need to first open the editor and then use a menu command such as File → New to create a blank document (use or File → Open to open an existing file). Once you have written content you need to save it in the correct location (usually, or File → Save). To find the
02_python
directory, first navigate to your home directory:-
This can be tricky on Windows : In your shell (not in your editor), type
cd; pwd
to learn the path to your home directory. In your editor's file system dialog window, start from the system disk (normallyC:
under Computer) and look forC:\Users\YOUR_USERNAME
. -
On Apple Mac OS X, your home directory is
/Users/YOUR_USERNAME
. -
On typical Linux distributions it is
/home/YOUR_USERNAME
.
In your home directory, locate the
PHY494
folder, click on it to find the02_python
folder inside and click on the latter. Provide the file name ("hello.py") and save the file. (In the shell, make sure that the file is in place where you expect it to be, i.e.,ls -la
.) ↩ -
-
%run
is an ipython "magic" command, see help on those commands with%magic
. ↩ -
If you don't want to see graphics inline but as a separate window you can try ↩