formations/python/python3/en/getting-started.txt

135 lines
2.4 KiB
Plaintext
Raw Normal View History

2018-08-21 10:25:44 +02:00
Getting started
===============
Installation
-------------
Have a look at the python_ website.
.. _python: http://www.python.org
Hello world
-----------
::
python -c "print 'hello'"
Opening a python prompt::
>>> print("hello world")
hello world
.. code-block:: python
:caption: hello.py
:name: hello-py
import sys
name = sys.argv[1]
print("Hello, {0}".format(name))
starting a web server::
python3 -m http.server 8080
importing a module
>>> import this
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
use the builtin help
>>> help(function)
Configure your prompt
---------------------
Set your path or install a good prompt like `ipython <https://ipython.org/ipython-doc/2/install/install.html>`_
.. envvar:: PYTHONPATH
defaults on the current directory
you can add a path to point to some other library
put this in your `.bashrc` (under linux), under windows... I don't know:
::
export PYTHONPATH:`pwd`
export PYTHONPATH=$PYTHONPATH:'/usr/share':'/toto/titi/tata'
alias pyenv='export PYTHONPATH=`pwd`:$PYTHONPATH'
export PYTHONSTARTUP='/home/gwen/.pystartup'
.. envvar:: PYTHONSTARTUP
the pystartup enables us to set the
- autocompletion
- the history of the commands
example of `.pystartup`
::
# Store the file in ~/.pystartup, and set an environment variable to point to
# it, e.g. "export PYTHONSTARTUP=/max/home/itamar/.pystartup" in bash.
#
# Note that PYTHONSTARTUP does *not* expand "~", so you have to put in the full
# path to your home directory.
import rlcompleter
import readline
readline.parse_and_bind("tab: complete")
import os
histfile = os.path.join(os.environ["HOME"], ".pyhist")
try:
readline.read_history_file(histfile)
except IOError:
pass
import atexit
atexit.register(readline.write_history_file, histfile)
del os, histfile
# enhanced completion
#import rlcompleter2
#rlcompleter2.setup()
Editors
---------
IDLE
https://docs.python.org/3/library/idle.html
un framework de développement intégré : :term:`IDLE`
.. glossary::
IDLE
IDLE_ is an IDE (Integrated Development Environnement)
.. _IDLE: http://docs.python.org/3/library/idle.html
Thonny_
.. _Thonny: https://thonny.org/
Otherwise, your preferred editor...