Python language syntax
Python: what is it?
“Python is a portable, dynamic, extensible, free language, which allows a modular, and object-oriented approach to programming. Python has been developed since 1989 by Guido van Rossum and several voluntary contributors” (Gerard Swinnen “Learn to program with Python” , pg. 6, 2005).
All the information about this language is accessible on the Python site:
The Python language is interpreted in Flux by Jython.
Additional information about this interpreter is available on the Jython website: http://www.jython.org/Project/ .
File
Python files have the record extension *.py.
General rules
General rules:
- A line should contain only one instruction
- All the comments begin with the character # and continue until the end of the line
- Names of variables must follow some simple rules:
- names should begin with a letter or a _, and may contain letters (accented letters, cedillas, spaces, special characters are prohibited), numbers and the character _
- the case is significant (upper and lower case letters are differentiated)
- Blocks are marked by indentation (in standard version, 4 spaces; do not combine spaces and tabulations for indentation)
Variables and types
Declaration, assignment of variables:
- It is not necessary to declare the variables. A variable is created at its first assignment by means of = operator
- The type of a variable is not explicitly declared and can change in time. The type of a variable is the type of the value that is assigned to it.
Types of standard data:
-
numerical types: integer, real, complex
312 3.13e10 0.1256 3.2+0.5j
-
strings: between apostrophes or quotation mark
'hello' "followed by information"
-
sequences: lists, sets, dictionaries
['a','b','c'] (1,2,3) ('a','b','c') {'jack': 409,'andy': 860}
Tests
The statement if is used to test a value
if test 1 :
# test 1 true
elif test 2 :
# test 2 true
else :
# default
Comparison operators
The condition after the if statement can contain the following comparison operators:
Element | Function |
---|---|
x == y | x is equal to y |
x != y | x is not equal to y |
x > y | x is greater than y |
x < y | x is less than y |
x >= y | x is greater than or equal to y |
x <= y | x is less than or equal to y |
Loops
There are two types of loop:
- for loop, to reiterate on the values of a sequence
for variable in sequence :
# loop block
- while loop, to reiterate as long as a condition is satisfied
while test :
# loop block
range() fonction
The range() function turns out to be very useful to manage iterations in loops. It generates a list of integers
>>> range(8)
[0, 1, 2, 3, 4, 5, 6, 7]
Functions
A new function is defined using the keyword def .
Example :
The add() function , defined below, gives a sum of 2 numbers or concatenates two strings.
def add (a,b) :
return a+b
Error handling
The Python language has built-in system for exception handling. The program can contain various types of errors: syntax errors, semantic errors (of logic) and errors of execution (exceptions). When an exception occurs, the program execution is stopped and the exception is handled.
To handle syntax errors and exceptions, Python uses the following statement:
try :
# instructions to execute (to raise exceptions)
except exception_class :
# instructions to execute if the exception of the above class occurs
else :
# instructions to execute if there is no exception
finally :
# instructions to execute in all the cases
Python enables:
- to catch several types of exceptions in same treatment
- to raise the exceptions with the keyword raise