Python Kholase
Python Kholase
Giovanni Perteghella
1
this week
Python, but particularly, why Python?
Practice!
installing and first steps
Practice! variables, data structures and constructs
& functions and object-oriented programming
Practice! time to practice even more!
[Link]
2
aword about editors
3
Python Programming Language
High-level Programming Language
Multi Platform
Windows, Linux, Unix (macOS)
Off-side-rule based
i.e., no brackets nor semicolon required
4
Named
after the
(Monty)
Python
5
why Python?
software quality
developer productivity
focus on readability 1/3 to 1/5 over Java, C++, C Code
readability counts (from the Zen of Python)
minimalist programming approach
there’s just one obvious way to do it libraries support
great standard library
program portability
component integration
(most) Python programs ruin unchanged on all
mayor computer platforms glue language (e.g. data science)
support for portable GUI, DB Access, web-based
can be easily integrated with C/C++ Code
systems, …
6
still, why
Python?
7
is Python a Scripting Language?
8
is Python a Scripting Language?
9
Python main features
interactive shell
10
compile interpret
d vs ed
pros pros
• faster execution • steep learning curve
• can produce a • takes automatically care
distributable executable of memory usage
standalone file • allows fast prototyping
cons cons
• more complicated to • usually slower
build (many files) • does not produce
• user must administrate standalone programs
memory usage
11
don¶ttry
# Anaconda Python Distribution this at
home!!
by Continuum Analytics.
12
don¶ ttry
# getting Anaconda Miniconda this at
home!!
[Link]
instead of installing the full Anaconda distribution, we will save space installing the
Miniconda version
conda
Python
pip
and a few others
13
and now?
Type python in a shell (or python3)
Caret
distribution platform
14
our first Python program …
what happened?
15
… and our first Python package
[Link]
16
don¶ttry
# (conda) environment(s) this at
home!!
Python libraries can be easily installed with different package managers (pip,
conda, etc.)
projects are usually organized in different environments (with related packages &
versions)
let’s run conda create --name spam python=3
18
variables
naming rules
variables can only contain letters, numbers, and
underscores.
names can start with letters or underscores, but not
with numbers.
spaces are not allowed in variable names, use underscores!
19
strings
20
strings
21
numbers
integers (int) floating points (float)
22
numbers
integers (int) floating points (float)
23
# comments
It explains your thinking to others who will work with your code
It explains particularly difficult sections of code in detail.
24
the Zen of Python aka
keep it simple if you can; if not, do not complicate stuff even more
Always code as if the guy who ends up maintaining your code will be a
violent psychopath who knows where you live. Code for readability.
-John F. Woods, 1991
your code cannot be perfect, but it doesn’t mean that you cannot make it work, for now
25
exercises! #1
26
exercises! #1
+ bonus question:
can you multiply a
String by an
integer?
27
a word about types
dynamictyping
strong typing
• once the type has been inferred, it cannot change without explicit cast
28
a word about types
duck typing!
strong typing
29
logical tests
30
logical tests
31
the if, the elseand the.. offside
anif statement tests for a condition, and then
responds to that condition.
if the condition is true, then whatever action is listed
next gets carried out.
you can test for multiple conditions at the same time,
and respond appropriately to each condition.
32
the if, the elseand the.. offside
anif statement tests for a condition, and then
responds to that condition.
if the condition is true, then whatever action is listed
next gets carried out.
you can test for multiple conditions at the same time,
and respond appropriately to each condition.
1 6
3
2 4 7
3 5
34
but, what is True?
1 6
3
2 4 7
3 5
35
built-in data structures
iterables: lists, tuples, sets, dictionaries
36
[lists]
a list is a collectionof items, that is stored in a variable.
37
[lists] square brackets
define lists
a list is a collectionof items, that is stored in a variable.
38
enumerate a list
what if you want to print the index and the value at that
index in a list?
just keep a counter variable i and use value[i], right?
39
common list operation
40
more common list operation
42
exercises! #2
43
slicing a list[:]
44
deep vs shallow copy
usernames
copied
usernames
usernames
copied
usernames
[Link] 45
[numerical lists]
aka do not expect anything special, apart from range,min,maxand sum
46
[list comprehensions]
47
exercises! #3
48
“strings” + “(again?)”
we can use them as lists, and we can convert them to lists
49
“strings” + “(again?)” + “(again?)”
50
exercises! #4
51
Python for genetics! [Link]
52
(tuples,)
aka immutable list
53
while loop:
54
while loop: more
55
historical & mathematical exercise!
56
{dictionaries: µwhat are they¶}
57
{dictionaries: ‘how to iterate’}
58
{more: dictionaries}
59
looping through dictionaries
60
exercises! #5
61
functions()
62
functions(more)
63
function(arguments)
64
*arguments of variable length
65
**keyword arguments of variable length
you can consider
**kwargs as a
**kwargslets you accept a variable dictionary
number of keyword arguments
66
class:
67
class(inheritance): from module math
import function
sqrt
remember: a Shuttle
can quack like a Rocket
68
from module import classes
wait, modules?
modulesare nothing more than python scripts containing
reusable code (classes, functions, etc.)
modulesare organized in packages
69
open(files)
70
Time to code ! Look at example folder
71
simple http server
72
simple http server
73
simple http server
74
simple http server
@[Link]("/")
add a new routeto this function
75
simple http server
@[Link]("/")
add a new routeto this function
76
simple http server
@[Link]("/")
add a new routeto this function
77
ipgeo location
we need the geoip2==2.9.0 package (pip installgeoip2==2.9.0) and the GeoLite2 database
83
ipgeo location
84
ipgeo location
85
ipgeo location
city = [Link]
extract the interesting fields
country = [Link]
longitude = [Link]
lat = [Link]
86
port scanner
87
port scanner
88
port scanner
t_ip = [Link](t_host)
extract the ip from the hostname
ask the user for a port to scan
89
port scanner
t_ip = [Link](t_host)
extract the ip from the hostname
90
port scanner
t_ip = [Link](t_host)
extract the ip from the hostname
91
wireshark log parser
read a .pcapfile
create a [Link]
92
wireshark log parser
create a [Link]
93
wireshark log parser
94
wireshark log parser
95
wireshark log parser
96
other libraries and tools that could be handy
97