Introduction To Python Programming: Luis Pedro Coelho
Introduction To Python Programming: Luis Pedro Coelho
Luis Pedro Coelho (Programming for Scientists) Python I October 22, 2012 (1 / 26)
Python
Luis Pedro Coelho (Programming for Scientists) Python I October 22, 2012 (2 / 26)
Python Language History
History
Python was started in the late 80s.
It was intended to be both easy to teach and industrial strength.
It is (has always been) open-source.
In the last 10 years, it has become one of the most widely used
languages (top 10).
Luis Pedro Coelho (Programming for Scientists) Python I October 22, 2012 (3 / 26)
Popularity
Luis Pedro Coelho (Programming for Scientists) Python I October 22, 2012 (4 / 26)
Popularity
Luis Pedro Coelho (Programming for Scientists) Python I October 22, 2012 (5 / 26)
Python Versions
Python Versions
The current versions of Python are 2.7 and 3.3
This class assumes you have 2.62.7
There are some small dierences when compared to version 3.x
Luis Pedro Coelho (Programming for Scientists) Python I October 22, 2012 (6 / 26)
What is a Computer?
1. Memory
2. Processor
. Magic
3
Luis Pedro Coelho (Programming for Scientists) Python I October 22, 2012 (7 / 26)
Python Model
1. Objects
2. Operations on objects
3. Magic
Luis Pedro Coelho (Programming for Scientists) Python I October 22, 2012 (8 / 26)
Python Example
p r i n t H e l l o World
Luis Pedro Coelho (Programming for Scientists) Python I October 22, 2012 (9 / 26)
Running Python
1. From a le
2. Interactively
Luis Pedro Coelho (Programming for Scientists) Python I October 22, 2012 (10 / 26)
Computer Program
helloword.py
p r i n t H e l l o World
Luis Pedro Coelho (Programming for Scientists) Python I October 22, 2012 (11 / 26)
Running a Program
1. Shell
2. IDE
Luis Pedro Coelho (Programming for Scientists) Python I October 22, 2012 (12 / 26)
Let me show you a demonstration
Luis Pedro Coelho (Programming for Scientists) Python I October 22, 2012 (13 / 26)
More Complex Example
What is 25 times 5?
Luis Pedro Coelho (Programming for Scientists) Python I October 22, 2012 (14 / 26)
More Complex Example
What is 25 times 5?
p r i n t 25 * 5
Luis Pedro Coelho (Programming for Scientists) Python I October 22, 2012 (14 / 26)
More Complex Example
name = 2
other = 3
y e t a n o t h e r = name + o t h e r
name = 5
p r i n t y e t a n o t h e r + name
Luis Pedro Coelho (Programming for Scientists) Python I October 22, 2012 (15 / 26)
Blackboard demonstration
Luis Pedro Coelho (Programming for Scientists) Python I October 22, 2012 (16 / 26)
Conditionals
i f <c o n d i t i o n>:
<s t a t e m e n t 1>
<s t a t e m e n t 2>
else :
<s t a t e m e n t 3>
Luis Pedro Coelho (Programming for Scientists) Python I October 22, 2012 (17 / 26)
Lists
s t u d e n t s = [ L u i s , Mark , Rita ]
print students [ 0 ]
print students [ 1 ]
print students [ 2 ]
Luis Pedro Coelho (Programming for Scientists) Python I October 22, 2012 (18 / 26)
Loops
s t u d e n t s = [ L u i s , Mark , Rita , . . . ]
for st in students :
print st
Luis Pedro Coelho (Programming for Scientists) Python I October 22, 2012 (19 / 26)
Example
v a l u e s = [ 0 . 11 , - 0 . 23 , - 0 . 16 , 0 . 18 , 0 . 23 , 0 . 19 ]
sum = 0 . 0
sum2 = 0 . 0
for v in values :
sum = sum + v
sum2 = sum2 + v * v
mu = sum/ l e n ( v a l u e s )
mu2 = sum2/ l e n ( v a l u e s )
p r i n t Average : { 0 } . format (mu)
p r i n t Std Dev : { 0 } . format (mu2 - mu*mu)
Luis Pedro Coelho (Programming for Scientists) Python I October 22, 2012 (20 / 26)
Example
v a l u e s = [ 0 . 11 , - 0 . 23 , - 0 . 16 , 0 . 18 , 0 . 23 , 0 . 19 ]
sum = 0 . 0
sum2 = 0 . 0
for v in values :
sum += v
sum2 += v * v
mu = sum/ l e n ( v a l u e s )
mu2 = sum2/ l e n ( v a l u e s )
p r i n t Average : { 0 } . format (mu)
p r i n t Std Dev : { 0 } . format (mu2 - mu*mu)
Luis Pedro Coelho (Programming for Scientists) Python I October 22, 2012 (21 / 26)
Example
v a l u e s = [ 0 . 11 , - 0 . 23 , - 0 . 16 , 0 . 18 , 0 . 23 , 0 . 19 ]
mu = 0 . 0
mu2 = 0 . 0
for v in values :
mu += v
mu2 += v * v
mu /= l e n ( v a l u e s )
mu2 /= l e n ( v a l u e s )
p r i n t Average : { 0 } . format (mu)
p r i n t Std Dev : { 0 } . format (mu2 - mu*mu)
Luis Pedro Coelho (Programming for Scientists) Python I October 22, 2012 (22 / 26)
Example
v a l u e s = [ 0 . 11 , - 0 . 23 , - 0 . 16 , 0 . 18 , 0 . 23 , 0 . 19 ]
mu = 0 . 0
mu2 = 0 . 0
for v in values :
mu += v
mu2 += v * v
mu /= l e n ( v a l u e s )
mu2 /= l e n ( v a l u e s )
p r i n t Average : { 0 } . format (mu)
p r i n t Std Dev : { 0 } . format (mu2 - mu*mu)
Luis Pedro Coelho (Programming for Scientists) Python I October 22, 2012 (23 / 26)
Exercise
Adapt the code to ignore negative numbers.
Luis Pedro Coelho (Programming for Scientists) Python I October 22, 2012 (24 / 26)
Exercise
Adapt the code to ignore negative numbers.
v a l u e s = [ 0 . 11 , - 0 . 23 , - 0 . 16 , 0 . 18 , 0 . 23 , 0 . 19 ]
mu = 0 . 0
mu2 = 0 . 0
n = 0.0
for v in values :
i f v >= 0 . 0 :
mu += v
mu2 += v * v
n += 1
mu /= n
mu2 /= n
p r i n t Average : { 0 } . format (mu)
p r i n t Std Dev : { 0 } . format (mu2 - mu*mu)
Luis Pedro Coelho (Programming for Scientists) Python I October 22, 2012 (24 / 26)
Loops (II)
Greatest Common Divisor (Euclids Method)
a if b = a
gcd(a, b) = gcd(a b, b) if a > b
gcd(a, b a) o.w.
Luis Pedro Coelho (Programming for Scientists) Python I October 22, 2012 (25 / 26)
Loops (II)
Greatest Common Divisor (Euclids Method)
a if b = a
gcd(a, b) = gcd(a b, b) if a > b
gcd(a, b a) o.w.
a = 9344
b = 6497
w h i l e a != b :
if a > b:
a ,b = a-b,b
else :
a ,b = a ,b-a
print a
Luis Pedro Coelho (Programming for Scientists) Python I October 22, 2012 (25 / 26)
For Monday
Install Python(x,y)
(or the equivalent on your platform)
Luis Pedro Coelho (Programming for Scientists) Python I October 22, 2012 (26 / 26)