Introduction to Python

Python provides by default:

Basic operations

+  Addition
-  Subtraction
*  Multiplication
/  Division
** Exponentiation
abs(x) Absolute value of x
%   Remainder of a/b
//  Integer part of a/b

Basic High-Level Data Types

  • Numbers: 10, 10.0, 1.0e+01, ( 10.0+3j )
  • Strings, Unicode: “Hello World!”, u”Hello World!”
  • Lists: [‘abc’, 3, “x”]
  • Tuples: (‘abc’, 3, ‘x’)
  • Dictionnaries: {‘key1’:’abc’, ‘key2’: 3, ‘key3’:’x’}

Exercise 1

Use Python as a simple calculator. Try the previous operations on different numbers (integers, floats, …).

Define an array a = [1, 2, 3] What is the result of 2a[2] ? What is the result of 2a ?

Combine operations and data types and comment your findings.


In [1]:
a = [1,2,3]
print(2*a[2])
print(2*a)


6
[1, 2, 3, 1, 2, 3]

Conclusion

Without additional libraries, Python is almost useless for scientific computing.

Go to the introduction to Numpy.


In [2]:
%pylab


Using matplotlib backend: TkAgg
Populating the interactive namespace from numpy and matplotlib

In [3]:
import scipy.misc

In [6]:
l=scipy.misc.lena??

In [ ]:
l=scipy.misc.lena

In [ ]:
l=scipy.misc.lena

In [7]:
imshow(l,cmap="gray")


Out[7]:
<matplotlib.image.AxesImage at 0x41d62d0>

In [ ]: