Nov 21, 2024

Wiki

Python

Aide

edit SideBar

Search

First steps in Python


By way of introduction

General presentation

Python is a relatively young programming language (about twenty years old). It is often presented as a scripting language, but has since evolved a lot: it presents all the possibilities offered by other languages (object programming, lambda computation, pattern design, etc.)

It is an interpreted language: you do not have to compile the program before executing it. It gains multi-platform programs, easier debugging, but it loses speed.

Python allows you to do complicated things easily and with a minimum of lines of code. In addition, you can mix python code with C/C++, or Java. So,

  • difficult parts can be made in python,
  • when the parts requiring speed will be made in C/C++.

By its clear and concise syntax, python is a good pedagogical tool. In addition, it is rich in scientific libraries, and is increasingly used by the scientific community.

The Python language is based on a community development model: a band of cheerful developers, forming a nucleus around the BDFL (Le Benevolent Dictator For Life, Guido van Rossum), proposes evolutions by writing PEPs: the Python Enhancement Proposal.

These mini specifications are subject to approval

  • first, from all Python developers, to be refined or rejected,
  • and then accepted or not by the BDFL, and provided for in the schedule for completion in a future version.
  • These improvements can be followed on page What's new in python.

The interest of this model is the speed with which the language evolves, compared to others that experience greater inertia, due to economic and strategic considerations induced by the companies that support them.

It should also be noted that, in these TP rooms, we use the python branch 2, which is destined to disappear: branch 3 was released 2-3 years ago, it is incompatible with branch 2 (the developers wanted to correct python youth errors). However, moving from branch 2 to branch 3 requires updating all existing libraries, which takes time. This is why most GNU/Linux distributions, including Debian, still use branch 2 by default.

To conclude, it is a rapidly developing language, which is increasingly used for all its qualities (NASA, Google, Ubuntu, etc.)

It is installed as a base in GNU/Linux distributions. For other OSes, you can go to http://www.python.org.

How to launch the interactive mode

In a terminal, type (without the dollar)

  $ python

to start the interactive mode.

Test the following commands (without the >>>), and study the results:

  >>> 2+3
  >>> print "result =",2+3
  >>> chr(97)
  >>> range(5)

Once these commands have been tried, press the up arrow key to see that python manages a buffer.

You can run python in interactive mode at any time, for example to test a command line. Always remember to launch an interactive console when programming in python.

Type Control-D to exit the interactive mode.

Outside the interactive mode

In a terminal, type

  $ gedit prog1.py&

(or use the text editor of your choice).

Then, copy the following code into your editor:

  #-*-coding:utf8-*-
  n=input('How old are you?')
  print "you have", n, "years."

Two remarks:

  • We don't have >>> when we write a python program in a text editor
  • Important: do not put spaces at the beginning of the line, never.

Finally, when you return to your terminal, type

  $ python prog1.py 

...and the expected result is achieved without any problems!

Inputs-Outputs

The basic methods

input()
Allows you to read an integer. Can be used, for example, as follows
  >>> n=input()
input(string)
Displays the character string, then reads the integer entered at the end of the line. For example,
  >>> n=input('Your age? ').
raw_input()
Same as input() but by reading strings.

Practical work

  1. Make a program that displays "Hello!", and execute it.
  2. Ask the user his name and age, and post Hello, so-and-so, you're so old. (by replacing so-and-so and so much with the right values).
  3. Write a program that asks the user to enter a number and returns its double and triple.

Comments in Python

The base

They start with a # in python, so everything that follows this symbol is ignored. You can use this to document your code.

For comments on several lines, start and end them with ''' (three quotes, or three quotation marks, it's the same).

There are some comments with particular syntax, which provide information about the encoding of characters, or about the location of the python interpreter....

Encoding commentary

The first line of the previous program is mandatory (it can also be placed in the second line, but no further). It provides information on the choice of character encoding, which could also have been, for example:

  #-*-coding:Latin-1-*-

Interpreter's commentary

If you put in the first line of your python file toto (a text file, therefore) :

  #!/usr/bin/python

and you authorize the execution of your file:

  $ chmod u+x toto

then you can run toto as follows

  $ ./toto

Practical work

Add comments to programs you have previously made, whether on one or more lines.

Python on the net

  1. Read the Python presentation page on wikipedia.
  2. Browse the official Python page: http://www.python.org, then familiarize yourself with the following pages :
    1. Official documentation
    2. Python package index : more than 20000 modules.
  3. Take note of the following useful sites, browse them
    1. PyMOTW : a module presented in detail, each week.
    2. Association francophone Python
    3. Learn to program with Python : Book by G. Swinnen, under free license.
    4. A bite of python : Book introducing Python, under free license and in French.

Page Actions

Recent Changes

Group & Page

Back Links