Introduction of Python Programming
Introduction of Python Programming
Introduction to
Programming
(with Python)
Tariq Rashid, July-August 2015
Ambition
Confidence
to explore further,
understand suppliers and techies
The Journey
data science?
languages
tech overview
That’s it!
There is no other magic.
It’s all numbers ...
Networks
It’s all numbers ...
Desktops
Applications
It’s all numbers ...
Smartphone Apps
Android / iOS
It’s all numbers ...
Digital Movies
Digital Music
It’s all numbers ...
on / off
lightbulb
switch
battery
The 1s and 0s
on / off
input lightbulb output
switch
battery
The 1s and 0s
0
on / off
switches
lightbulb 0
0
battery
The 1s and 0s
1
on / off
switches
lightbulb 0
0
battery
The 1s and 0s
1
on / off
switches
lightbulb 1
1
battery
The 1s and 0s Comput
ation:
Only wh
en both inp
uts are
on
Turn on
the outpu
t light
1
on / off
switches
lightbulb 1
1
battery
The 1s and 0s Comput
ation:
Only wh
en both inp
uts are
on
Turn on
the outpu
t light
e :
Recip
IF
1 r e i s low
rat u
tempe s after 8pm
on / off ND it’ lightbulb 1
switches
A
THEN ating
1 n t he he
o
Turn
battery
Programming
Programming
“Recipes”
Ye Olde Programming!
… live demo!
Loads of Programming Languages!
…. Go Python!
Enough Theory!
Let’s code!
Hello World!
Let’s Decode Some Python ...
Play button
awaiting your next instruction
“Execute my command with extreme
prejudice”
Have a Play!
… is 6
3
Data Nitty Gritty
data
memory
Variable - the name of a storage box
3
a
variable name
Variable - the name of a storage box
5
3
b
a
variable names
Just like school maths
In maths we write:
3
a=3
a
Data Nitty Gritty
In Python we also
write:
3
a=3
a
… try it!
Python Variables
code starting with the hash
symbol # are ignored
we can use them as comments
shortcut!
Changing Data is Useful
7
3
a replace the 3 with a 7
Changing Data
before ... a is 3
after ... a is 7
Arithmetic on Variables
a=7
b=a*3
print (b)
Arithmetic on Variables - Try It!
Different Kinds of Data
Numbers:
a=7 Decimals:
b = 3.14159
Text:
c = “cat”
Different Kinds of Data
Numbers:
b = 3.14159
Text:
“integer”
c = “cat”
“floating point”
Data Types - Try it!
A Collection of Data is Useful
9 3 4 8 1 5
Python Lists
List
9 3 4 8 1 5
0 1 2 3 4 5 index
a= 9 3 4 8 1 5
0 1 2 3 4 5 index
variable name
a[3] = 8
a[0] = 9
Making a Python List
a= 9 3 4 8 1 5
0 1 2 3 4 5
a = [ 9, 3, 4, 8, 1, 5 ]
Python Lists - Try it!
make the list using
square brackets
a = [ 9, 3, 4, 8, 1, 5 ]
a= 9 3 4 8 1 5
0 1 2 3 4 5
a[2] = 0
a= 9 3 0 8 1 5
0 1 2 3 4 5
Changing List Data
a = [ 9, 3, 4, 8, 1, 5 ]
a= 9 3 4 8 1 5
0 1 2
i t!
3 4 5
Try
a[2] = 0
a= 9 3 0 8 1 5
0 1 2 3 4 5
Oops!
error message!
Reaching Too Far Out!
a= 9 3 4 8 1 5
0 1 2 3 4 5 index
a[3] = 8
a[0] = 9
Classic Coder Errors
Software Bug!
Buffer Overflow!
Computers Love Repetitive Work!
Computers don’t mind doing lots of
repetitive number calculations.
it’s called
iterating
over a list
Working Through a List - Try it!
woah!
l
w
In Python …
v=a*b*c
Our First Function
return v
… finally returns an
answer ...
Our First Function - Block Volume
l
v=w*l*h v
reusable code
reused here!
reusable code
Hurrah!
e a t e d s ome
u ’ ve j us t cr reused here!
Yo l e c o de!
reu s a b
radius
circumference = 2 * pi * radius
Functions - Puzzle 2
Functions - Puzzle 2
palindrome !
Google is a Coder’s Best Friend!
Linux
And how Open Source projects often share cool useful stuff.
Let’s Use Somebody Else’s Work
Libraries - extend your Python’s powers with
somebody else’s code
importing
my Python libraries somebody else’s module
https://docs.python.org/3/library/math.html
List 9 3 4 8 1 5
9 3 4 8 1 5
Array
4 2 5 0 0 1
2 7 3 1 4 0
Python Arrays
index
Array
0 9 3 4 8 1 5
a= 1 4 2 5 0 0 1
2
2 7 3 1 4 0
variable name 0 1 2 3 4 5 index
a[1,3] = 0
a[2,0] = 2
Python Arrays
index
Array
array [ ro
w, colum
0 9 3 4 8 n1 ] 5
a =in fact 4
1 2 5 0 0 1
yo u can ha
ve many
dimensio
2
2 7 3 1 4 0 ns ...
array [dim
1, dim2, d
variable name 0 1 2 3
im3 4…] 5 index
a[1,3] = 0
a[2,0] = 2
We need HELP!!
Numpy to the Rescue!
a = np.array ( )
it’s a function
just like we saw
before
Numpy Arrays
a = np.array (
[ ]
arrays are enclosed by
) square brackets
Numpy Arrays
a = np.array (
[ [1, 2, 3],
3 by 3
array of numbers
[7, 8, 9] ]
)
Numpy Arrays - Try It!
import numpy library
but we’ll call it np from
now on
powerful efficient
so can write concise clean code for working on large data
Array Functions - Try it!
sine function …
(trigonometry from school)
Graphics! … What Does This Do?
Graphics! … What Does This Do?
… and this?
making a List … remember
them !
http://scipy-lectures.github.io/intro/matplotlib/matplotlib.html
http://matplotlib.org/basemap/users/examples.html
Image processing
with Python is easy
Web Application - we’ll make our own little app ... (not just a static page)
Web Application - we’ll make our own little app ... (not just a static page)
t h e se w ork?
Ho w do
u nde r t he hood?
What’s
o v i ng parts?
nm
What ar e the mai
Anatomy of (many) Web Applications
browser
internet
user
web application
Anatomy of (many) Web Applications
web application server
logic
data storage
Anatomy of (many) Web Applications
web application server
lue and
g
on
coordinati n
front end ual desig
visbuilder
logic
customer
records business
process
data storage
Talking to a Web Application
GET /contact
OK … stuff ...
browser
talking in a
language called
HTTP
web application
Demo - Let’s Talk to a Web Server
request a page
response code; OK
content of response
GET /contact
OK … stuff ...
handlers
<h2>Hello</h2>
<hr>
Twitter Clone - The Logic
URL is /
URL is /submit
form to submit new
tweet
submit
add tweet to list
list of
redirect back to / previous tweets
Twitter Clone - Python Code
import Flask
list of tweets
app = flask.Flask(__name__)
<style>
tweets.append(tweet)
<h2>Submit a Tweet</h2>
</form>
<h2>Previous Tweets</h2>
XXXX
</body>
</html>
"""
Twitter Clone - Let’s Try It!
Twitter Clone - Let’s Try It!
SUCC
ESS!
Spy Challenge!
Encrypt:
Encrypt
A p p l E z
a p p l e z
key is 2
c r r n g b
Decrypt
do something
do more
return the_answer
Competition!
TEAM 1 TEAM 2
encrypt decrypt
encrypted
message message
message
In just 4 sessions we’ve …
data science?
languages
tech overview
meetup.com
Link to These Slides
https://goo.gl/6eQaMi