0% found this document useful (0 votes)
2 views92 pages

Python Kholase

This document serves as an introduction to Python programming, covering its advantages, installation, and basic concepts such as variables, data structures, and functions. It emphasizes Python's readability, ease of use, and versatility across platforms, while also providing practical exercises and resources for further learning. The document highlights key features of Python, including dynamic typing and built-in data structures like lists and dictionaries.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views92 pages

Python Kholase

This document serves as an introduction to Python programming, covering its advantages, installation, and basic concepts such as variables, data structures, and functions. It emphasizes Python's readability, ease of use, and versatility across platforms, while also providing practical exercises and resources for further learning. The document highlights key features of Python, including dynamic typing and built-in data structures like lists and dictionaries.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Intro to Python

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

ƒ feel free to use whatever you prefer


ƒ be sure to use syntax color highlighting
ƒ Online editor [Link]
ƒ I personally use PyCharm
ƒ but options are Visual Studio Code, Atom, Sublime Text 2, Notepad++,etc..

3
Python Programming Language
ƒ High-level Programming Language

ƒ Very shallow learning curve

ƒ Multi Platform
ƒ Windows, Linux, Unix (macOS)

ƒ Off-side-rule based
ƒ i.e., no brackets nor semicolon required

[Link] ƒ Language of the choice in:

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?

enjoyment the act of programming


is more pleasure than chore

7
is Python a Scripting Language?

ƒ Python is a general-purpose programming language

ƒ often applied in scripting roles


ƒ general purpose programming language that blends procedural functional and object-oriented
paradigms

ƒ but what do people think about it?


ƒ shell tools
ƒ control language
ƒ ease of use

8
is Python a Scripting Language?

ƒ shell tool ƒ ease of use


ƒ control language
ƒ code operating-system- ƒ simple language for quick
ƒ “glue layer” to control other
oriented scripts application components coding tasks
ƒ it can!just like dozens of ƒ it can!but it isn’t a control ƒ it is! but it isn’t only for
other application domains language simple tasks!

9
Python main features

ƒ succinct yet very readable syntax


ƒ great introspection (saves tons of time)
ƒ great standard library
ƒ built-in data structure (Map, Immutable List)
ƒ language batteries included

ƒ 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!!

ƒ we use Python 3 included in the Anaconda distribution

by Continuum Analytics.

ƒ Anaconda is a completely free enterprise-ready Python


distribution for large-scale data processing, predictive
analytics, and scientific computing.

ƒ apart from that, Anaconda ships with easy-to-use


installers for almost every platform, that would
drastically reduce the burden of setting up the
environment (exp. on Windows)

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

ƒconda install command installs 720+ additional conda packages

13
and now?
ƒ Type python in a shell (or python3)

Read-eval-print loop (REPL)


version

Caret

distribution platform

14
our first Python program …

ƒ open a text editor

ƒ type print(“Hello World!”)


ƒ save the file as hello_world.py
ƒ now in the shell type python3 hello_world.py

ƒ what happened?

ƒ how long compared to other languages?

15
… and our first Python package

ƒ in the shell, type pip3 install ipython


ƒpip3 is a Python package manager
ƒipython is a better version of the usual Python shell

[Link]

16
don¶ttry
# (conda) environment(s) this at
home!!

ƒ the Python standard library is great, but it’s not enough

ƒ 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

and install Python3 in it


conda, create an environment
called spam
ƒ and now, activateit!
17
time to become pythonist
Let’s meet variables, types, and the Zen

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!

ƒ you cannot use Pythonkeywordsas names


ƒ but you can overwrite function or class names!

ƒ variable names should be descriptive

19
strings

single or double quote!

it’s easy to transform a string!

20
strings

single or double quote!

it’s easy to transform a string!

21
numbers
integers (int) floating points (float)

22
numbers
integers (int) floating points (float)

pretty similar to different from


other languages! other languages?

23
# comments

Whatmakes a good comment?

ƒ It is short and to the point, but a complete thought


ƒ It explains your thinking for then you return to the code later

ƒ It explains your thinking to others who will work with your code
ƒ It explains particularly difficult sections of code in detail.

When should you write comments?

ƒ When you have to thinkabout code beforewritingit.


ƒ When you are likely to forget later how you approached a problem.

ƒ When there is more than oneway to solve a problem.

ƒ When others are unlikelyto anticipateyour way of thinking

24
the Zen of Python aka

Python programmers recognize that good code can actually be beautiful

it’s always better to be clear

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

pro tip: Python error messages are your friends

26
exercises! #1

+ bonus question:
can you multiply a
String by an
integer?

pro tip: Python error messagesare your friends

27
a word about types

dynamictyping

• it is not required to specify the types of variables/functions


• it is automatically inferred by operations

strong typing

• once the type has been inferred, it cannot change without explicit cast

28
a word about types
duck typing!

dynamictyping ƒ if it walks like a duck

ƒ and it quacks like a duck


ƒ it is not required to specify the types of ƒ then it must be a duck
variables/functions
ƒ it is automatically inferred by operations
but also…

strong typing

ƒ once the type has been inferred, it cannot change


without explicit cast

29
logical tests

ƒ wait, did we say anything about the bool type?


ƒ no, but bool corresponds to int in Python
ƒ in addition there are True and False ;as we’ve
already seen)
ƒ we can also use the next conditional operators to
compare variables

ƒ remember: equality means having the same value!

30
logical tests

ƒ wait, did we say anything about the bool type?


ƒ no, but bool corresponds to int in Python
ƒ in addition there are True and False ;as we’ve
already seen)
ƒ we can also use the next conditional operators to
compare variables

ƒ remember: equality means having the same value!

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.

what about this?


the offside rule is used to
decide what’s inside the
block

it applies to all blocks!


conditionals, loops and
functions!

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.

what about this?


the offside rule is used to
decide what’s inside the
block

it applies to all blocks!


conditionals, loops and
functions!
never forget about elif!
33
but, what is True?

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.

ƒ the itemsshould be relatedin some way

ƒ but there are no restrictions on what can be stored in a list.


ƒ since lists are collection of objects, it is good practice to give them a
plural name.
ƒ if each item in your list is a car, call the list 'cars'.

37
[lists] square brackets
define lists
ƒ a list is a collectionof items, that is stored in a variable.

ƒ the itemsshould be relatedin some way

ƒ but there are no restrictions on what can be stored in a list.


ƒ since lists are collection of objects, it is good practice to give them a
plural name.
ƒ if each item in your list is a car, call the list 'cars'.

for is a keyword for the for (-each) loop

student is a temporary variable

the nested block is created with the offside rule

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?

ƒ or maybe use the function enumerate!

39
common list operation

40
more common list operation

Sorted doesn’t sort the list in-place!


41
more more common list operation

42
exercises! #2

Don’ƚ forget to save everything in scripts


files (.py) and execute them with
python3 working_list.py

Hey ! some of you may want to use Python


functions: you can if you’re brave enough!

43
slicing a list[:]

ƒ a list is a collection of items and we should be


able to get any subset
ƒ we can use the notation
list[start_index:end_index]to
select all the elements from index
start_index (included) to end_index
(excluded)

44
deep vs shallow copy

ƒ usernames is not really a list: under the


hood is a pointer to a list in memory
ƒ if we want to make a deep copy of the list, we
can slice all the elements out of it

usernames

copied
usernames
usernames

copied
usernames
[Link] 45
[numerical lists]
aka do not expect anything special, apart from range,min,maxand sum

mini exercise: mini exercise:


can you type just range(11)? can you type just range(21,2)?
mini exercise:
what if you don’t use list?

46
[list comprehensions]

it allow us to collapse the first three lines of code


into one line:

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

ƒ listsare quite dynamic; they can grow and shrink

ƒ you can modify any element you want to in a list.


ƒ sometimes we like this behaviour, but other times we may want to
ensure that no user or nopart of a program can change a list.
ƒ that's what tuples are for

53
while loop:

54
while loop: more

55
historical & mathematical exercise!

56
{dictionaries: µwhat are they¶}

ƒ dictionaries are a way to store information that is connected in some way.


ƒ dictionaries store information in key-valuepairs,

ƒ any piece of information is connected to at least one other piece of


information.
ƒ dictionaries do not store their information in any particular order

57
{dictionaries: ‘how to iterate’}

58
{more: dictionaries}

59
looping through dictionaries

accessing to key-value pairs accessing to values accessing to keys

60
exercises! #5

61
functions()

ƒ functions are a set of actions that we group together and


give a name to.
ƒ you have already used several functions (or methods) from
the core Python language, such
as [Link]()and [Link]().
ƒ we can define our own functions, which allows us to
"teach" Python new behaviour.

62
functions(more)

ƒ functions are a set of actions that we group together and


give a name to.
ƒ you have already used several functions (or methods) from
the core Python language, such
as [Link]() and [Link]().
ƒ we can define our own functions, which allows us to "teach"
Python new behaviour.
ƒ by default functions return None, we can change this
behaviour using the returnkeyword

63
function(arguments)

default arguments keyword arguments

64
*arguments of variable length

*argslets you you can consider


accept a variable *args as a list
number of
arguments

65
**keyword arguments of variable length
you can consider
**kwargs as a
**kwargslets you accept a variable dictionary
number of keyword arguments

66
class:

ƒ classes are a way of combining information and behaviour.


ƒ let's consider what you'd need to do if you were creating a rocket
ship in a game:
ƒ you'd want to track are the x and y coordinates of the rocket

the objective is not to learn about


oop design’s principle, but to get class constructor
used to Python syntax for classes!
instance method
self is the keyword
for the instance itself

67
class(inheritance): from module math
import function
sqrt

ƒ the class Rocket is our base class


ƒ we can create other classes that are based on it

ƒ for example, we can create a reusable rocket, or


a Shuttle
ƒ this child class will have access to the
move_rocket and get_distance
functions

remember: a Shuttle
can quack like a Rocket

68
from module import classes

ƒ we can save the Rocket and theShuttle


classes in a module(python script) called
[Link]
ƒ now we can import those classes from the
rocketmoduleand reuse them

ƒ wait, modules?
ƒ modulesare nothing more than python scripts containing
reusable code (classes, functions, etc.)
ƒ modulesare organized in packages

69
open(files)

Reading from a file


Mode Short for Meaning

r read Read from an existing file.

w write Write to a file. If the file exists,

its old content is removed.

Writing to a file a append Append text to the end of a file

keeping the old content.

70
Time to code ! Look at example folder

1) simple http server


2) ip geo location
3) port scanner
4) wireshark log parser

71
simple http server

ƒ we need the flask package

ƒ create a flask app

ƒ define a functionthat will be our endpoint

ƒ add a new routeto this function

ƒ start the server

ƒ connect to the server through the browser and


Python

72
simple http server

ƒ we need the flask package

ƒ create a flask app app = Flask(__name__)

ƒ define a functionthat will be our endpoint

ƒ add a new routeto this function

ƒ start the server

ƒ connect to the server through the browser and


Python

73
simple http server

ƒ we need the flask package

ƒ create a flask app app = Flask(__name__)

ƒ define a functionthat will be our endpoint def hello():


return "Hello, World!"

ƒ add a new routeto this function

ƒ start the server

ƒ connect to the server through the browser and


Python

74
simple http server

ƒ we need the flask package

ƒ create a flask app app = Flask(__name__)

ƒ define a functionthat will be our endpoint def hello():


return "Hello, World!"

@[Link]("/")
ƒ add a new routeto this function

ƒ start the server

ƒ connect to the server through the browser and


Python

75
simple http server

ƒ we need the flask package

ƒ create a flask app app = Flask(__name__)

ƒ define a functionthat will be our endpoint def hello():


return "Hello, World!"

@[Link]("/")
ƒ add a new routeto this function

ƒ start the server FLASK_APP=examples/hello_server.py flask run

ƒ connect to the server through the browser and


Python

76
simple http server

ƒ we need the flask package

ƒ create a flask app app = Flask(__name__)

ƒ define a functionthat will be our endpoint def hello():


return "Hello, World!"

@[Link]("/")
ƒ add a new routeto this function

ƒ start the server FLASK_APP=examples/hello_server.py flask run

ƒ connect to the server through the browser and [Link]('[Link]


.text
Python

77
ipgeo location

ƒ we need the geoip2==2.9.0 package (pip installgeoip2==2.9.0) and the GeoLite2 database

ƒ load the database

ƒ query the database

ƒ extract the interesting fields

83
ipgeo location

ƒ we need the geoip2==2.9.0 package (pipinstallgeoip2==2.9.0) and the GeoLite2 database

ƒ load the database reader = [Link]("./GeoLite2-


City_20191001/[Link]")

ƒ query the database

ƒ extract the interesting fields

84
ipgeo location

ƒ we need the geoip2==2.9.0 package (pipinstallgeoip2==2.9.0) and the GeoLite2 database

ƒ load the database reader = [Link]("./GeoLite2-


City_20191001/[Link]")

ƒ query the database response = [Link](ip)

ƒ extract the interesting fields

85
ipgeo location

ƒ we need the geoip2==2.9.0 package (pipinstallgeoip2==2.9.0) and the GeoLite2 database

ƒ load the database reader = [Link]("./GeoLite2-


City_20191001/[Link]")

ƒ query the database response = [Link](ip)

city = [Link]
ƒ extract the interesting fields
country = [Link]
longitude = [Link]
lat = [Link]

86
port scanner

ƒ ask the user for a hostname

ƒ extract the ip from the hostname

ƒ ask the user for a port to scan

ƒ create a socket and try to connect

ƒ hint: try the socket library

87
port scanner

t_host = str(input("Enter the host to be scanned: "))


ƒ ask the user for a hostname

ƒ extract the ip from the hostname

ƒ ask the user for a port to scan

ƒ create a socket and try to connect

ƒ hint: try the socket library

88
port scanner

t_host = str(input("Enter the host to be scanned: "))


ƒ ask the user for a hostname

t_ip = [Link](t_host)
ƒ extract the ip from the hostname
ƒ ask the user for a port to scan

ƒ create a socket and try to connect

ƒ hint: try the socket library

89
port scanner

t_host = str(input("Enter the host to be scanned: "))


ƒ ask the user for a hostname

t_ip = [Link](t_host)
ƒ extract the ip from the hostname

t_port = int(input("Enter the port: "))


ƒ ask the user for a port to scan
ƒ create a socket and try to connect

ƒ hint: try the socket library

90
port scanner

t_host = str(input("Enter the host to be scanned: "))


ƒ ask the user for a hostname

t_ip = [Link](t_host)
ƒ extract the ip from the hostname

t_port = int(input("Enter the port: "))


ƒ ask the user for a port to scan
ƒ create a socket and try to connect
sock = [Link]()
[Link](10)

res = [Link]((t_ip, t_port))


print(f"Port {t_port}: Open")
ƒ hint: try the socket library
[Link]()

91
wireshark log parser

ƒ we need the dpkt package and .pcapfile: [Link]

ƒ read a .pcapfile

ƒ create a [Link]

ƒ iterate over packets and convert them to


Ethernet

ƒ extract packet’s source and destinations IPs

92
wireshark log parser

ƒ we need the dpkt package and .pcapfile: [Link]

ƒ read a .pcapfile f = open(pcap_fname, "rb")

ƒ create a [Link]

ƒ iterate over packets and convert them to


Ethernet

ƒ extract packet’s source and destinations IPs

93
wireshark log parser

ƒ we need the dpkt package and .pcapfile: [Link]

ƒ read a .pcapfile f = open(pcap_fname, "rb")

ƒ create a [Link] pcap = [Link](f)

ƒ iterate over packets and convert them to


Ethernet

ƒ extract packet’s source and destinations IPs

94
wireshark log parser

ƒ we need the dpkt package and .pcapfile: [Link]

ƒ read a .pcapfile f = open(pcap_fname, "rb")

ƒ create a [Link] pcap = [Link](f)

ƒ iterate over packets and convert them to forts, buf in pcap:


eth = [Link](buf)
Ethernet

ƒ extract packet’s source and destinations IPs

95
wireshark log parser

ƒ we need the dpkt package and .pcapfile: [Link]

ƒ read a .pcapfile f = open(pcap_fname, "rb")

ƒ create a [Link] pcap = [Link](f)

ƒ iterate over packets and convert them to forts, buf in pcap:


eth = [Link](buf)
Ethernet

ƒ extract packet’s source and destinations IPs if isinstance([Link], [Link]):


ip = [Link]
src = socket.inet_ntoa([Link])
dst = socket.inet_ntoa([Link])

96
other libraries and tools that could be handy

the data scientist new home


jupyter notebook write Python and tell a story

lab notebooks improved

server centralized notebooks/lab


scientific Python libraries
& google’s colab! notebooks in the cloud
numpy multi-dimensional arrays

sklearn data mining and data analysis


the wonderful world of (data) viz
matplotlib basic library for plotting pandas data frames (i.e. tables) support

seaborn fancier plots

bokeh fancier and interactive plots

97

You might also like