SlideShare a Scribd company logo
1 of 84
Download to read offline
PYTHON: AN
INTRODUCTION FOR
PHP DEVELOPERS
What will we talk about?
Some general information about Python

Some code - enough to be able to read and understand
python code

Some minimal practical examples of python code and that
based on things that I picked up @ Dx-Solutions.
Any questions ? Feel free to interrupt !
What is python ?
Python is a widely used general-
purpose, high-level programming
language. 

Its design philosophy emphasises code
readability.

Python supports multiple programming
models, including object-oriented,
procedural and functional
programming.

Has a large and comprehensive
standard library.

Python can be run on many operating
systems and (embedded) platforms.
Things that rule
It’s easy, flexible and powerful.
Good to have in your toolbox!

String handling is fantastic

A wide range of good third party
libraries (because it has a close
relationship with C)

It is extremely versatile (web, gui
apps, arduino yun, scripting …) 

Great prototype language

It’s really fun
HTTP://XKCD.COM/353/
Things that can bite
Uses whitespace (tabs) to delimit
program blocks (personal
preference) 

It isn’t the fastest language (but
projects like cython, pypy, …
counters that) 

Python 3 is not backward
compatible unfortunately. In this
presentation we will focus on
Python 2.x.

Package management could be
better and isn’t that good like in
node.js or PHP
Who uses it ?
In Belgium
Not a lot of big players

Mobile Vikings

Belgacom BIC

GDF Suez

Belfius

…

Lot of misconceptions

(toy language, only for experiments, small startups, …) 

Starting to get some traction in higher education (university of Gent,
Leuven, Antwerp)
Some web frameworks
https://github.com/vinta/awesome-python
BeautifulSoup
Work project: Ghelamco
CSV fields -> doctrine generator.
Really speeded up the process to
create entities.

SQL generator to automatically
generate dump queries

Tool to dump table column names
(for example to use in certain
queries)

RAW dump importer (that served
as a prototype for a PHP symfony
command)
From CSV data
To generate doctrine entity fields
… now imagine if you need to do that manually…
Average runtime: less than a second…
Personal project: WebGL baking tool
http://www.simplicity.be/improving-realism-in-webgl-scenes-by-using-texture-baking/
Now for the
code stuff…
IDE
Python comes with a builtin IDE “Idle” which feels
a bit dated and isn’t that good

PHPStorm lovers will be glad to know that
jetbrains also has a python IDE called PyCharm

Community edition is free

But the web support isn’t in the free edition

But in reality every text editor will suffice
PyCharm CE
Running python
• Through the interactive python shell
$ python

Python 2.7.9 (default, Dec 19 2014, 06:00:59) 

[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on
darwin

Type "help", "copyright", "credits" or "license" for more
information.

>>> print "hello world"

hello world

>>>
Running python
• By running a .py file
hello.py
print "hello world"
$ python hello.py

hello world
command
2 very handy methods
dir

list attributes and methods

help
shows builtin help
dir (example)
>>> dir("dx-solutions")
['__add__', '__class__', '__contains__', '__delattr__', '__doc__',
'__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__',
'__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__',
'__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__',
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__',
'__rmul__', '__setattr__', '__sizeof__', '__str__',
'__subclasshook__', '_formatter_field_name_split',
'_formatter_parser', 'capitalize', 'center', 'count', 'decode',
'encode', 'endswith', 'expandtabs', 'find', 'format', 'index',
'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle',
'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition',
'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit',
'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase',
'title', 'translate', 'upper', 'zfill']
help (example)
>>> help(“dx-solutions”.upper)
Help on built-in function upper:
upper(...)
S.upper() -> string
Return a copy of the string S converted to uppercase.
(END)
Comments
A comment in python start with a hash (#) symbol

No multiline comments 

use multiple #
Comments (examples)
// hello world
/**
* hello
* world
**/
PHP
# hello world
# hello
# world
Python
Variables
Python is a dynamically typed language. So you
do not need to declare variables before using
them or declare their type (like PHP)

Every variable in Python is an object…

…that has an id

…that is of a certain standard data type

…that has a mutable or immutable value
Mutable vs immutable
Mutable: you can change an object without
changing its identity. So in other words when you
alter an object its id will be the same. 

Immutable: you can not change an object without
changing its identity. So when you alter an object
its id will change!
Mutable vs immutable (examples)
>>> myval = "dxsolutions"
>>> id(myval)
4538483456
>>> myval = "dx-solutions"
>>> id(myval)
4538475888
Immutable
>>> mylist = []
>>> id(mylist)
4538197848
>>> mylist.append("hello")
>>> id(mylist)
4538197848
Mutable
Standard data types
Number

integer

float

long

complex

String

List

Tuple

Dictionary
Variables declarations (examples)
$var_1 = 1; // Integer
$var_2 = 3.14; // Float
$var_3 = "hello"; // String
$var_4 = 'hello'; // String
PHP
var_1 = 1 # Integer
var_2 = 3.14 # Float
var_3 = "hello" # String
var_4 = 'hello' # String
var_1 = 'abc' # Is now a string
Python
Math operators
Arithmetic operators are the same as PHP
Notation Description
+ add
- subtract
* multiply

/ division
% modulo
** power operator*
* the ** operator is introduced in PHP 5.6
Assignment operators
Notation Description
= simple assignment
+= add and assign
-= subtract and assign

*= multiply and assign
/= divide and assign
%= modulo and assign
**= exponent and assign*
//= floor division and assign*
* not available in PHP
Comparison operators
Notation Description
== is equal*
!= not equal
<> not equal
> bigger than
< smaller than
>= bigger than or equal
<= smaller than or equal
* doesn’t have the === operator
Logical operators
Notation Description
and and
or or
not not
Chained comparisons
# chained
if 5 < x < 7:
print 6
# same as
if x > 5 and x < 7:
print 6
Strings (examples)
$var_1 = 'Thunderstruck';
$var_2 = "Ain't no sunshine";
$var_3 = 'Ain't no sunshine';
$var_3 = "this has
multiple
lines";
PHP
Python
var_1 = 'Thunderstruck'
var_2 = "Ain't no sunshine"
var_3 = 'Ain't no sunshine'
var_3 = """this has
multiple
lines"""
Print
print ('Thunderstruck');
echo 'Thunderstruck';
PHP
print ('Thunderstruck'); # python 3.x
print 'Thunderstruck'; # python 2.x
Python
String formatting (examples)
>>> "Hello %s %s" % ("Glenn", "De Backer")
'Hello Glenn De Backer'
C syntax
>>> "Hello {0}, {1}".format("Glenn", "De Backer")
'Hello Glenn, De Backer’
>>> "Hello {firstname}, {name}".format(firstname="Glenn",
name="De Backer")
'Hello Glenn, De Backer'
Advanced string formatting (PEP 3101) (PEP = Python Enhancement Proposals)
Slices
Strings are a sequence of characters

A subsequence of a sequence is called a slice

The operation that extracts a subsequence is called slicing
Notation Description
a[start:end] items start through end-1
a[start:] items start through the rest of the array
a[:end] items from the beginning through end-1

a[:] a copy of the whole array
a[start:end:step] start through not past end, by step
Slices
a[-1] # last item in the array
a[-2:] # last two items in the array
a[:-2] # everything except the last two items
FROM HTTP://WWW.NLTK.ORG/BOOK/CH03.HTML
None
None is the pythonic way of defining NULL. 

Evaluates to False

Is an object (NoneType)

Because it is an object, we cannot use it to check
if a variable exist.
None (example)
db_con = None
# Try to connect
try:
db_con = MysqlDB(db_host, db_user, db_password, db_database)
db_con = database.connect()
except DatabaseException:
pass
if db_con is None:
print('Could not connect')
else:
# do dbase stuff
Booleans
Simple same as in PHP but case sensitive!

True

False

1

0
Booleans (examples)
$a = True;
$b = FALSE;
if ($a === true) {
echo "Party!";
}
PHP
a = True
b = False
if a is True:
print “party"
if a == True:
print "party"
Python
Sequences
Difficult to compare as PHP only has associative arrays
( hashmap )

List: is just a list of items

Tuples: are like lists but you cannot change their values. 

months

blood types

…

Dictionary: hashmap / associative arrays

Also supports slices and slicing
List (example 1/2)
>>> mylist = []
>>> mylist.append(123)
>>> mylist.append('dxsolutions')
>>> mylist.append(2)
>>> print mylist
[123, ‘dxsolutions’, 2]
>>> mylist.sort()
>>> print mylist
[2, 123, ‘dxsolutions']
>>> mylist.reverse()
>>> print mylist
['dxsolutions', 123, 2]
List (example 2/2)
>>> mylist.pop()
2
>>> print mylist
['dxsolutions', 123]
>>> mylist.extend([1,2,3])
>>> print mylist
['dxsolutions', 123, 1, 2, 3]
>>> mylist.remove(2)
>>> print mylist
['dxsolutions', 123, 1, 3]
Tuples (example)
>>> days = (‘monday’,'tuesday','wednesday','thursday',
'friday','saterday','sunday')
>>> days[2]
'wednesday'
>>> days[2:]
('wednesday', 'thursday', 'friday', 'saterday',
'sunday')
>>> days[:3]
('monday', 'tuesday', ‘wednesday')
>>> days[0:2]
('monday', 'tuesday')
Dictionaries
person = {'Name': 'Tom', 'Age': 27};
print person['Name']; # Tom
print person['Age']; # 27
Python
$person = array('Name' => 'Tom', 'Age' => 27);
print $person['Name'];
print $person['Age'];
PHP
Membership operators
Notation Description
in Evaluates to true if in sequence
not in Evaluates to true if not in sequence
Python has membership operators, which test for membership
in a sequence, such as strings, lists, or tuples
Membership operators (example)
>>> a = (1,2,4,8)
>>> 1 in a
True
>>> 3 not in a
True
>>> 3 in a
False
>>> word = "boat"
>>> 'e' in word
False
>>> 'a' in word
True
Add / multiply operators and
sequences
>>> a = [ 1, 2 ]
>>> b = [ 3, 4 ]
>>> a + b
[1, 2, 3, 4]
>>> a * 4
[1, 2, 1, 2, 1, 2, 1, 2]



>>> a = "hello "
>>> b = "world"
>>> a + b
'hello world'
>>> a * 4
'hello hello hello hello '
Whitespace - indentation
Instead of using curly braces ( { ) to delimit
program blocks, Python uses indentation 

It is mandatory, no way around it

It is a very controversial feature that some really
hate…

… but it does improve readability… 

… and after a while you totally forget about it.
Conditional: if…else…el(se)if (example)
if (statement) {
// do stuff
} elseif (other_statement) {
// do other stuff
} else {
// do other stuff
}
PHP
if (statement):
# do stuff
elif (other_statement):
# do other stuff
else:
# do other stuff
Python
Iterations: for loop
Python for statement has a lot in common with
PHP foreach

Supports break and continue which works more
or less the same as in PHP

You can loop over strings, lists, dictionaries, … as
they are sequences.
Iterations: for loop (example)
$words = array('cat', 'window', 'door');
foreach ($words as $word) {
echo $word;
}
PHP
words = ['cat', 'window', 'door']
for word in words:
print word
>>> cat, window, door
for word in words[1:3]:
print word
>>> window, door
Python
Iterations: for loop (example)
$persons = array('name' => 'glenn', 'job' => 'developer');
foreach ($persons as $key => $value) {
// process person
}
PHP
persons = {'name': 'glenn', 'job': 'developer'}
for key in persons.keys():
# process key
for value in persons.values():
# process value
for key, value in persons.items():

# process value
Python
Iterations: range
If you do need to iterate over a sequence of numbers, you
can use the range() method.

You have also xrange:

if you do range(1, 10000) it will create a list of 10000
elements in memory

when using xrange you create a sequence which
evaluates lazy. This is faster and uses less memory.

In Python 3.x range() -> xrange()
Iterations: range (example)
// array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
$arr = range(1,10);
// array(1, 2, 3, 4, 5, 6,)
foreach (range(1, 6) as $number) {
    echo $number;
}
PHP
>>> range(10) # python 2.x
>>> list(range(10)) # python 3.x
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> for number in range(1,6):
print number
>>> 1, 2, 3, 4, 5
Python
Iterations: enumerate (example)
>>> # bad example!
>>> words = ['Mary', 'had', 'a', 'little', 'lamb']
>>> for i in range(len(words)):
print i, words[i]
Bad
>>> # if you need indices use enumerate
>>> words = ['Mary', 'had', 'a', 'little', 'lamb']
>>> for index, value in enumerate(words):
print index, value
Good
For example: when you want index -> value from a list.
Functions
Function blocks begin with the keyword def followed
by the function name and parentheses (( ))

The code block within every function starts with a
colon (:) and is indented.

Functions can have docstring. Accessible through
function.__doc__ or help(function)
Functions (example)
function hello() {
    echo "hello world";
}
PHP
def hello():
"Prints hello world"

echo "hello world"
>>> help(hello)
Help on function hello in module __main__:
hello()
Prints hello world
Python
Function arguments
You can call a function by using the following types
of formal arguments:		 

Required arguments

Default arguments

Keyword arguments

Variable-length arguments (Variadic function)

PHP >= 5.6 splat operator (…)

Like PHP it doesn’t support function overloading
Required argument (example)
>>> def hello(name):
print "hello" + name
>>> hello("glenn")
glenn
Python
function hello($name) {
    echo "hello" . $name;
}
hello("glenn");
PHP
Required argument (example)
>>> def hello(name):
print "hello" + name
>>> hello()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: hello() takes exactly 1 argument (0 given)
Default argument (example)
>>> def hello(name="glenn"):
print "hello" + name
>>> hello()
hello glenn
Python
function hello($name="glenn") {
    echo “hello” . $name;
}
hello();
>>> hello glenn
PHP
Keyword argument (example)
>>> def sum(a=1, b=2):
print a + b
>>> sum()
3
>>> sum(a=5)
7
>>> sum(b=4)
5
>>> sum(a=2, b=4)
6
Python
( There isn’t a PHP equivalent )
Variable length argument (example)
def manyArgs(*arg):
print "I was called with", len(arg), "arguments:", arg
>>> manyArgs(1)
I was called with 1 arguments: (1,)
>>> manyArgs(1, 2, 3)
I was called with 3 arguments: (1, 2, 3)
Python
function manyArgs(...$arg) {
echo "I was called with " . count($arg) . " arguments: " 
implode($arg);
}
hello(1); // I was called with 1 arguments: 1

hello(1, 2, 3); // I was called with 3 arguments: 1,2,3
PHP >= 5.6 using the . . . (splat) operator
Classes
In Python everything is an object (again)

Class blocks begin with the keyword class followed by
the class name and a colon (:)

Doesn’t have private / public concept

There is a convention that is followed by most Python
code: a name prefixed with an underscore (e.g.
_spam) should be treated as a non-public…

…but it is still accessible

Classes can also have docstring.
Classes (example)
class Employee:
def __init__(self, name, salary):
self.name = name
self.salary = salary
e = Employee("mike", "1500")
Python
class Employee {
function __construct($name, $salary) {
$this->name = $name;
$this->salary = $salary;
}
}
$e = new Employee("mike", "1500");
PHP
Classes: inheritance
Supports multiple inheritance (PHP lacks that)

Method overloading works the same as in PHP

You can (optionally) make your base class inherit
from the class object. Matter of style.
Classes: inheritance (example)
class Vehicle {
// vehicle methods and members
}
class Car extends Vehicle {
// car methods and members
}
PHP
class Vehicle:
# vehicle methods and members
class Car(Vehicle):
# car methods and members
Python
Classes: overloading 1/2 (example)
class Parent(object):
def __init__(self):
self.value = 5
def get_value(self):
return self.value
class Child(Parent):
pass # is a null operation
>>> c = Child()
>>> c.get_value()
5
Classes: overloading 2/2 (example)
class Parent(object):
def __init__(self):
self.value = 5
def get_value(self):
return self.value
class Child(Parent):
def get_value(self):
return self.value + 1
>>> c = Child()
>>> c.get_value()
6
__init__ /__keyword__
The __keyword__ are also called dunder methods
(thunder in West Flemish or double under)

They are also sometimes called “magic” methods

They are roughly the same as inheriting things

In the case of __init__ in the background Python will do
automatically the calls to init and new

They also make it possible (but are not limited) to change
how operators (for example +, / , ..) behave.
from __future__ import division
(example)
# in python 2.x if you divide an integer you
# will get an integer back
>> 1 / 2
0
# In python 2.7 future = Python 3.x
from __future__ import division
>> 1 / 2
0.5
>> 1 // 2 # you will need the special // division
# operator to get an integer back
File I/O (example)
# Reading a file
with open('myfile.txt', 'r') as f:
for line in f:
print f
# Writing a file
with open('myfile.txt', 'w') as f:
f.write('Hello')
Lambda functions
# Simple lambda example
f = lambda x, y : x + y
>> f(2,4)
6
# Map -> lambda (divide by 2)
>>> a = [2, 4, 8, 16]
>>> map ( lambda x: x/2, a)
[1, 2, 4, 8]
# Filter -> lambda (even numbers)
>>> a = [1, 2, 3, 4, 5, 6 , 7, 8, 9, 10]
>>> filter (lambda x: x % 2 == 0, a)
[2, 4, 6, 8, 10]
The proof is
in the pudding…
Minimal flask (example)
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
@app.route("/name/<name>")
def hello(name=None):
return “Hello “ + name
if __name__ == "__main__":
app.run()
HTTP://FLASK.POCOO.ORG
Minimal scraper 1/2 (example)
<div title="buyer-name">Carson Busses</div>
<span class="item-price">$29.95</span>
HTML
from lxml import html
import requests
page = requests.get('http://econpy.pythonanywhere.com/ex/001.html')
tree = html.fromstring(page.text)
#This will create a list of buyers:
buyers = tree.xpath('//div[@title="buyer-name"]/text()')
#This will create a list of prices
prices = tree.xpath('//span[@class="item-price"]/text()')
print 'Buyers: ', buyers
print 'Prices: ', prices
Python
Minimal scraper 2/2 (example)
Buyers: ['Carson Busses', 'Earl E. Byrd', 'Patty Cakes',
'Derri Anne Connecticut', 'Moe Dess', 'Leda Doggslife', 'Dan Druff',
'Al Fresco', 'Ido Hoe', 'Howie Kisses', 'Len Lease', 'Phil Meup',
'Ira Pent', 'Ben D. Rules', 'Ave Sectomy', 'Gary Shattire',
'Bobbi Soks', 'Sheila Takya', 'Rose Tattoo', 'Moe Tell']
Prices: ['$29.95', '$8.37', '$15.26', '$19.25', '$19.25',
'$13.99', '$31.57', '$8.49', '$14.47', '$15.86', '$11.11',
'$15.98', '$16.27', '$7.50', '$50.85', '$14.26', '$5.68',
'$15.00', '$114.07', '$10.09']
Result
HTTP://DOCS.PYTHON-GUIDE.ORG/EN/LATEST/SCENARIOS/SCRAPE/
Bayesian text classifier
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.pipeline import Pipeline
from sklearn.naive_bayes import MultinomialNB
import numpy
# docs
docs = numpy.array(["Buy viagra", "Hello frank", "The servers are down", "Do
you want a rolex", "Cheap airplane tickets"])
# labels 1: spam, 0: ham
labels = numpy.array([1,0,0,1,1])
# pipeline
steps = [('vectorizer', CountVectorizer()), ('clf', MultinomialNB())]
pipe = Pipeline(steps)
pipe.fit(docs, labels)
# test data
test_data = numpy.array([ "Hello koen", "Cheap viagra"])
print pipe.predict(test_data)
HTTP://SCIKIT-LEARN.ORG/STABLE/
Automate with fabric
# Import Fabric's API module
from fabric.api import *
env.hosts = [‘server.domain.tld’, ‘another_server.domain.tld’]
def update_upgrade():
"""
Update the default OS installation's
basic default tools.
"""
run("aptitude update")
run("aptitude -y upgrade")
def install_memcached():
""" Download and install memcached. """
run("aptitude install -y memcached")
def update_install():
# Update
update_upgrade()
# Install
install_memcached()
HTTP://WWW.FABFILE.ORG/
Some interesting resources
Dive into python -http://www.diveintopython.net/

The standard python library - http://effbot.org/zone/librarybook-
index.htm

How to think like a computer scientist with python - http://
www.greenteapress.com/thinkpython/thinkCSpy.pdf

PyVideo (indexes a lot of python talks) - http://pyvideo.org/ 

80 best python resources - http://www.fromdev.com/2014/03/
python-tutorials-resources.html

10 myths of enterprise python - https://www.paypal-
engineering.com/2014/12/10/10-myths-of-enterprise-python/

Python success stories - https://www.python.org/about/success/
Thank you for listening…
(any questions ?)

More Related Content

What's hot

What's hot (20)

Why Python (for Statisticians)
Why Python (for Statisticians)Why Python (for Statisticians)
Why Python (for Statisticians)
 
Python
PythonPython
Python
 
python.ppt
python.pptpython.ppt
python.ppt
 
Elegant Solutions For Everyday Python Problems - Nina Zakharenko
Elegant Solutions For Everyday Python Problems - Nina ZakharenkoElegant Solutions For Everyday Python Problems - Nina Zakharenko
Elegant Solutions For Everyday Python Problems - Nina Zakharenko
 
Python for Linux System Administration
Python for Linux System AdministrationPython for Linux System Administration
Python for Linux System Administration
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
Php Extensions for Dummies
Php Extensions for DummiesPhp Extensions for Dummies
Php Extensions for Dummies
 
AmI 2015 - Python basics
AmI 2015 - Python basicsAmI 2015 - Python basics
AmI 2015 - Python basics
 
Php extensions
Php extensionsPhp extensions
Php extensions
 
Boost Maintainability
Boost MaintainabilityBoost Maintainability
Boost Maintainability
 
Learn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesLearn 90% of Python in 90 Minutes
Learn 90% of Python in 90 Minutes
 
Python for text processing
Python for text processingPython for text processing
Python for text processing
 
An introduction to Python for absolute beginners
An introduction to Python for absolute beginnersAn introduction to Python for absolute beginners
An introduction to Python for absolute beginners
 
The Sincerest Form of Flattery
The Sincerest Form of FlatteryThe Sincerest Form of Flattery
The Sincerest Form of Flattery
 
Python introduction
Python introductionPython introduction
Python introduction
 
Scalar data types
Scalar data typesScalar data types
Scalar data types
 
OpenGurukul : Language : Python
OpenGurukul : Language : PythonOpenGurukul : Language : Python
OpenGurukul : Language : Python
 
Matlab and Python: Basic Operations
Matlab and Python: Basic OperationsMatlab and Python: Basic Operations
Matlab and Python: Basic Operations
 
Gcrc talk
Gcrc talkGcrc talk
Gcrc talk
 
Python made easy
Python made easy Python made easy
Python made easy
 

Viewers also liked

Python for PHP developers
Python for PHP developersPython for PHP developers
Python for PHP developersbennuttall
 
String Interpolation in Scala
String Interpolation in ScalaString Interpolation in Scala
String Interpolation in ScalaKnoldus Inc.
 
Appendex d
Appendex dAppendex d
Appendex dswavicky
 
WE1.L10 - GRACE Applications to Regional Hydrology and Water Resources
WE1.L10 - GRACE Applications to Regional Hydrology and Water ResourcesWE1.L10 - GRACE Applications to Regional Hydrology and Water Resources
WE1.L10 - GRACE Applications to Regional Hydrology and Water Resourcesgrssieee
 
PHP 5.3 Part 1 - Introduction to PHP 5.3
PHP 5.3 Part 1 - Introduction to PHP 5.3PHP 5.3 Part 1 - Introduction to PHP 5.3
PHP 5.3 Part 1 - Introduction to PHP 5.3melechi
 
Groundwater Research and Technology, Stefan Schuster
Groundwater Research and Technology, Stefan SchusterGroundwater Research and Technology, Stefan Schuster
Groundwater Research and Technology, Stefan SchusterTXGroundwaterSummit
 
Chapter 9 Asynchronous Communication
Chapter 9 Asynchronous CommunicationChapter 9 Asynchronous Communication
Chapter 9 Asynchronous CommunicationPatty Ramsey
 
Offshore pipelines
Offshore pipelinesOffshore pipelines
Offshore pipelineshaiifa25
 
Guidelines for Modelling Groundwater Surface Water Interaction in eWater Source
Guidelines for Modelling Groundwater Surface Water Interaction in eWater SourceGuidelines for Modelling Groundwater Surface Water Interaction in eWater Source
Guidelines for Modelling Groundwater Surface Water Interaction in eWater SourceeWater
 
Final morris esri_nwgis_lidar
Final morris esri_nwgis_lidarFinal morris esri_nwgis_lidar
Final morris esri_nwgis_lidarEric Morris
 
Ch5(ms access with php)
Ch5(ms access with php)Ch5(ms access with php)
Ch5(ms access with php)Chhom Karath
 
eMail 101 (4) Class for Self help Virtual Senior Center
eMail 101 (4) Class for Self help Virtual Senior Center eMail 101 (4) Class for Self help Virtual Senior Center
eMail 101 (4) Class for Self help Virtual Senior Center SnowSugar Video
 
Drought: Looking Back and Planning Ahead, Todd Votteler
Drought: Looking Back and Planning Ahead, Todd VottelerDrought: Looking Back and Planning Ahead, Todd Votteler
Drought: Looking Back and Planning Ahead, Todd VottelerTXGroundwaterSummit
 
Introduction to PHP - SDPHP
Introduction to PHP - SDPHPIntroduction to PHP - SDPHP
Introduction to PHP - SDPHPEric Johnson
 
Appendex c
Appendex cAppendex c
Appendex cswavicky
 
Appendex e
Appendex eAppendex e
Appendex eswavicky
 

Viewers also liked (20)

Python for PHP developers
Python for PHP developersPython for PHP developers
Python for PHP developers
 
String Interpolation in Scala
String Interpolation in ScalaString Interpolation in Scala
String Interpolation in Scala
 
Ch07
Ch07Ch07
Ch07
 
Appendex d
Appendex dAppendex d
Appendex d
 
WE1.L10 - GRACE Applications to Regional Hydrology and Water Resources
WE1.L10 - GRACE Applications to Regional Hydrology and Water ResourcesWE1.L10 - GRACE Applications to Regional Hydrology and Water Resources
WE1.L10 - GRACE Applications to Regional Hydrology and Water Resources
 
PHP 5.3 Part 1 - Introduction to PHP 5.3
PHP 5.3 Part 1 - Introduction to PHP 5.3PHP 5.3 Part 1 - Introduction to PHP 5.3
PHP 5.3 Part 1 - Introduction to PHP 5.3
 
Groundwater Research and Technology, Stefan Schuster
Groundwater Research and Technology, Stefan SchusterGroundwater Research and Technology, Stefan Schuster
Groundwater Research and Technology, Stefan Schuster
 
Chapter 9 Asynchronous Communication
Chapter 9 Asynchronous CommunicationChapter 9 Asynchronous Communication
Chapter 9 Asynchronous Communication
 
Offshore pipelines
Offshore pipelinesOffshore pipelines
Offshore pipelines
 
Voice
VoiceVoice
Voice
 
Guidelines for Modelling Groundwater Surface Water Interaction in eWater Source
Guidelines for Modelling Groundwater Surface Water Interaction in eWater SourceGuidelines for Modelling Groundwater Surface Water Interaction in eWater Source
Guidelines for Modelling Groundwater Surface Water Interaction in eWater Source
 
Final morris esri_nwgis_lidar
Final morris esri_nwgis_lidarFinal morris esri_nwgis_lidar
Final morris esri_nwgis_lidar
 
Ch5(ms access with php)
Ch5(ms access with php)Ch5(ms access with php)
Ch5(ms access with php)
 
eMail 101 (4) Class for Self help Virtual Senior Center
eMail 101 (4) Class for Self help Virtual Senior Center eMail 101 (4) Class for Self help Virtual Senior Center
eMail 101 (4) Class for Self help Virtual Senior Center
 
Intermediate PHP
Intermediate PHPIntermediate PHP
Intermediate PHP
 
Drought: Looking Back and Planning Ahead, Todd Votteler
Drought: Looking Back and Planning Ahead, Todd VottelerDrought: Looking Back and Planning Ahead, Todd Votteler
Drought: Looking Back and Planning Ahead, Todd Votteler
 
Introduction to PHP - SDPHP
Introduction to PHP - SDPHPIntroduction to PHP - SDPHP
Introduction to PHP - SDPHP
 
Appendex c
Appendex cAppendex c
Appendex c
 
Emoji International Name Finder
Emoji International Name FinderEmoji International Name Finder
Emoji International Name Finder
 
Appendex e
Appendex eAppendex e
Appendex e
 

Similar to Python: an introduction for PHP webdevelopers

Programming Under Linux In Python
Programming Under Linux In PythonProgramming Under Linux In Python
Programming Under Linux In PythonMarwan Osman
 
What we can learn from Rebol?
What we can learn from Rebol?What we can learn from Rebol?
What we can learn from Rebol?lichtkind
 
php&mysql with Ethical Hacking
php&mysql with Ethical Hackingphp&mysql with Ethical Hacking
php&mysql with Ethical HackingBCET
 
An Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAn Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAdam Getchell
 
Python (3).pdf
Python (3).pdfPython (3).pdf
Python (3).pdfsamiwaris2
 
First Steps in Python Programming
First Steps in Python ProgrammingFirst Steps in Python Programming
First Steps in Python ProgrammingDozie Agbo
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojureAbbas Raza
 
name name2 n
name name2 nname name2 n
name name2 ncallroom
 
name name2 n
name name2 nname name2 n
name name2 ncallroom
 
name name2 n
name name2 nname name2 n
name name2 ncallroom
 
name name2 n2.ppt
name name2 n2.pptname name2 n2.ppt
name name2 n2.pptcallroom
 

Similar to Python: an introduction for PHP webdevelopers (20)

Programming Under Linux In Python
Programming Under Linux In PythonProgramming Under Linux In Python
Programming Under Linux In Python
 
What we can learn from Rebol?
What we can learn from Rebol?What we can learn from Rebol?
What we can learn from Rebol?
 
php&mysql with Ethical Hacking
php&mysql with Ethical Hackingphp&mysql with Ethical Hacking
php&mysql with Ethical Hacking
 
An Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAn Overview Of Python With Functional Programming
An Overview Of Python With Functional Programming
 
Python for dummies
Python for dummiesPython for dummies
Python for dummies
 
Python (3).pdf
Python (3).pdfPython (3).pdf
Python (3).pdf
 
First Steps in Python Programming
First Steps in Python ProgrammingFirst Steps in Python Programming
First Steps in Python Programming
 
Introduction to php basics
Introduction to php   basicsIntroduction to php   basics
Introduction to php basics
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojure
 
ppt7
ppt7ppt7
ppt7
 
ppt2
ppt2ppt2
ppt2
 
name name2 n
name name2 nname name2 n
name name2 n
 
test ppt
test ppttest ppt
test ppt
 
name name2 n
name name2 nname name2 n
name name2 n
 
ppt21
ppt21ppt21
ppt21
 
name name2 n
name name2 nname name2 n
name name2 n
 
ppt17
ppt17ppt17
ppt17
 
ppt30
ppt30ppt30
ppt30
 
name name2 n2.ppt
name name2 n2.pptname name2 n2.ppt
name name2 n2.ppt
 
ppt18
ppt18ppt18
ppt18
 

Recently uploaded

"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 

Recently uploaded (20)

"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 

Python: an introduction for PHP webdevelopers

  • 2. What will we talk about? Some general information about Python Some code - enough to be able to read and understand python code Some minimal practical examples of python code and that based on things that I picked up @ Dx-Solutions. Any questions ? Feel free to interrupt !
  • 3. What is python ? Python is a widely used general- purpose, high-level programming language. Its design philosophy emphasises code readability. Python supports multiple programming models, including object-oriented, procedural and functional programming. Has a large and comprehensive standard library. Python can be run on many operating systems and (embedded) platforms.
  • 4. Things that rule It’s easy, flexible and powerful. Good to have in your toolbox! String handling is fantastic A wide range of good third party libraries (because it has a close relationship with C) It is extremely versatile (web, gui apps, arduino yun, scripting …) Great prototype language It’s really fun
  • 6. Things that can bite Uses whitespace (tabs) to delimit program blocks (personal preference) It isn’t the fastest language (but projects like cython, pypy, … counters that) Python 3 is not backward compatible unfortunately. In this presentation we will focus on Python 2.x. Package management could be better and isn’t that good like in node.js or PHP
  • 8. In Belgium Not a lot of big players Mobile Vikings Belgacom BIC GDF Suez Belfius … Lot of misconceptions (toy language, only for experiments, small startups, …) Starting to get some traction in higher education (university of Gent, Leuven, Antwerp)
  • 10. Work project: Ghelamco CSV fields -> doctrine generator. Really speeded up the process to create entities. SQL generator to automatically generate dump queries Tool to dump table column names (for example to use in certain queries) RAW dump importer (that served as a prototype for a PHP symfony command)
  • 12. To generate doctrine entity fields … now imagine if you need to do that manually… Average runtime: less than a second…
  • 13. Personal project: WebGL baking tool http://www.simplicity.be/improving-realism-in-webgl-scenes-by-using-texture-baking/
  • 14. Now for the code stuff…
  • 15. IDE Python comes with a builtin IDE “Idle” which feels a bit dated and isn’t that good PHPStorm lovers will be glad to know that jetbrains also has a python IDE called PyCharm Community edition is free But the web support isn’t in the free edition But in reality every text editor will suffice
  • 17. Running python • Through the interactive python shell $ python Python 2.7.9 (default, Dec 19 2014, 06:00:59) [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> print "hello world" hello world >>>
  • 18. Running python • By running a .py file hello.py print "hello world" $ python hello.py hello world command
  • 19. 2 very handy methods dir list attributes and methods help shows builtin help
  • 20. dir (example) >>> dir("dx-solutions") ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
  • 21. help (example) >>> help(“dx-solutions”.upper) Help on built-in function upper: upper(...) S.upper() -> string Return a copy of the string S converted to uppercase. (END)
  • 22. Comments A comment in python start with a hash (#) symbol No multiline comments use multiple #
  • 23. Comments (examples) // hello world /** * hello * world **/ PHP # hello world # hello # world Python
  • 24. Variables Python is a dynamically typed language. So you do not need to declare variables before using them or declare their type (like PHP) Every variable in Python is an object… …that has an id …that is of a certain standard data type …that has a mutable or immutable value
  • 25. Mutable vs immutable Mutable: you can change an object without changing its identity. So in other words when you alter an object its id will be the same. Immutable: you can not change an object without changing its identity. So when you alter an object its id will change!
  • 26. Mutable vs immutable (examples) >>> myval = "dxsolutions" >>> id(myval) 4538483456 >>> myval = "dx-solutions" >>> id(myval) 4538475888 Immutable >>> mylist = [] >>> id(mylist) 4538197848 >>> mylist.append("hello") >>> id(mylist) 4538197848 Mutable
  • 28. Variables declarations (examples) $var_1 = 1; // Integer $var_2 = 3.14; // Float $var_3 = "hello"; // String $var_4 = 'hello'; // String PHP var_1 = 1 # Integer var_2 = 3.14 # Float var_3 = "hello" # String var_4 = 'hello' # String var_1 = 'abc' # Is now a string Python
  • 29. Math operators Arithmetic operators are the same as PHP Notation Description + add - subtract * multiply / division % modulo ** power operator* * the ** operator is introduced in PHP 5.6
  • 30. Assignment operators Notation Description = simple assignment += add and assign -= subtract and assign *= multiply and assign /= divide and assign %= modulo and assign **= exponent and assign* //= floor division and assign* * not available in PHP
  • 31. Comparison operators Notation Description == is equal* != not equal <> not equal > bigger than < smaller than >= bigger than or equal <= smaller than or equal * doesn’t have the === operator
  • 33. Chained comparisons # chained if 5 < x < 7: print 6 # same as if x > 5 and x < 7: print 6
  • 34. Strings (examples) $var_1 = 'Thunderstruck'; $var_2 = "Ain't no sunshine"; $var_3 = 'Ain't no sunshine'; $var_3 = "this has multiple lines"; PHP Python var_1 = 'Thunderstruck' var_2 = "Ain't no sunshine" var_3 = 'Ain't no sunshine' var_3 = """this has multiple lines"""
  • 35. Print print ('Thunderstruck'); echo 'Thunderstruck'; PHP print ('Thunderstruck'); # python 3.x print 'Thunderstruck'; # python 2.x Python
  • 36. String formatting (examples) >>> "Hello %s %s" % ("Glenn", "De Backer") 'Hello Glenn De Backer' C syntax >>> "Hello {0}, {1}".format("Glenn", "De Backer") 'Hello Glenn, De Backer’ >>> "Hello {firstname}, {name}".format(firstname="Glenn", name="De Backer") 'Hello Glenn, De Backer' Advanced string formatting (PEP 3101) (PEP = Python Enhancement Proposals)
  • 37. Slices Strings are a sequence of characters A subsequence of a sequence is called a slice The operation that extracts a subsequence is called slicing Notation Description a[start:end] items start through end-1 a[start:] items start through the rest of the array a[:end] items from the beginning through end-1 a[:] a copy of the whole array a[start:end:step] start through not past end, by step
  • 38. Slices a[-1] # last item in the array a[-2:] # last two items in the array a[:-2] # everything except the last two items FROM HTTP://WWW.NLTK.ORG/BOOK/CH03.HTML
  • 39. None None is the pythonic way of defining NULL. Evaluates to False Is an object (NoneType) Because it is an object, we cannot use it to check if a variable exist.
  • 40. None (example) db_con = None # Try to connect try: db_con = MysqlDB(db_host, db_user, db_password, db_database) db_con = database.connect() except DatabaseException: pass if db_con is None: print('Could not connect') else: # do dbase stuff
  • 41. Booleans Simple same as in PHP but case sensitive! True False 1 0
  • 42. Booleans (examples) $a = True; $b = FALSE; if ($a === true) { echo "Party!"; } PHP a = True b = False if a is True: print “party" if a == True: print "party" Python
  • 43. Sequences Difficult to compare as PHP only has associative arrays ( hashmap ) List: is just a list of items Tuples: are like lists but you cannot change their values. months blood types … Dictionary: hashmap / associative arrays Also supports slices and slicing
  • 44. List (example 1/2) >>> mylist = [] >>> mylist.append(123) >>> mylist.append('dxsolutions') >>> mylist.append(2) >>> print mylist [123, ‘dxsolutions’, 2] >>> mylist.sort() >>> print mylist [2, 123, ‘dxsolutions'] >>> mylist.reverse() >>> print mylist ['dxsolutions', 123, 2]
  • 45. List (example 2/2) >>> mylist.pop() 2 >>> print mylist ['dxsolutions', 123] >>> mylist.extend([1,2,3]) >>> print mylist ['dxsolutions', 123, 1, 2, 3] >>> mylist.remove(2) >>> print mylist ['dxsolutions', 123, 1, 3]
  • 46. Tuples (example) >>> days = (‘monday’,'tuesday','wednesday','thursday', 'friday','saterday','sunday') >>> days[2] 'wednesday' >>> days[2:] ('wednesday', 'thursday', 'friday', 'saterday', 'sunday') >>> days[:3] ('monday', 'tuesday', ‘wednesday') >>> days[0:2] ('monday', 'tuesday')
  • 47. Dictionaries person = {'Name': 'Tom', 'Age': 27}; print person['Name']; # Tom print person['Age']; # 27 Python $person = array('Name' => 'Tom', 'Age' => 27); print $person['Name']; print $person['Age']; PHP
  • 48. Membership operators Notation Description in Evaluates to true if in sequence not in Evaluates to true if not in sequence Python has membership operators, which test for membership in a sequence, such as strings, lists, or tuples
  • 49. Membership operators (example) >>> a = (1,2,4,8) >>> 1 in a True >>> 3 not in a True >>> 3 in a False >>> word = "boat" >>> 'e' in word False >>> 'a' in word True
  • 50. Add / multiply operators and sequences >>> a = [ 1, 2 ] >>> b = [ 3, 4 ] >>> a + b [1, 2, 3, 4] >>> a * 4 [1, 2, 1, 2, 1, 2, 1, 2]
 
 >>> a = "hello " >>> b = "world" >>> a + b 'hello world' >>> a * 4 'hello hello hello hello '
  • 51. Whitespace - indentation Instead of using curly braces ( { ) to delimit program blocks, Python uses indentation It is mandatory, no way around it It is a very controversial feature that some really hate… … but it does improve readability… … and after a while you totally forget about it.
  • 52. Conditional: if…else…el(se)if (example) if (statement) { // do stuff } elseif (other_statement) { // do other stuff } else { // do other stuff } PHP if (statement): # do stuff elif (other_statement): # do other stuff else: # do other stuff Python
  • 53. Iterations: for loop Python for statement has a lot in common with PHP foreach Supports break and continue which works more or less the same as in PHP You can loop over strings, lists, dictionaries, … as they are sequences.
  • 54. Iterations: for loop (example) $words = array('cat', 'window', 'door'); foreach ($words as $word) { echo $word; } PHP words = ['cat', 'window', 'door'] for word in words: print word >>> cat, window, door for word in words[1:3]: print word >>> window, door Python
  • 55. Iterations: for loop (example) $persons = array('name' => 'glenn', 'job' => 'developer'); foreach ($persons as $key => $value) { // process person } PHP persons = {'name': 'glenn', 'job': 'developer'} for key in persons.keys(): # process key for value in persons.values(): # process value for key, value in persons.items():
 # process value Python
  • 56. Iterations: range If you do need to iterate over a sequence of numbers, you can use the range() method. You have also xrange: if you do range(1, 10000) it will create a list of 10000 elements in memory when using xrange you create a sequence which evaluates lazy. This is faster and uses less memory. In Python 3.x range() -> xrange()
  • 57. Iterations: range (example) // array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) $arr = range(1,10); // array(1, 2, 3, 4, 5, 6,) foreach (range(1, 6) as $number) {     echo $number; } PHP >>> range(10) # python 2.x >>> list(range(10)) # python 3.x [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> for number in range(1,6): print number >>> 1, 2, 3, 4, 5 Python
  • 58. Iterations: enumerate (example) >>> # bad example! >>> words = ['Mary', 'had', 'a', 'little', 'lamb'] >>> for i in range(len(words)): print i, words[i] Bad >>> # if you need indices use enumerate >>> words = ['Mary', 'had', 'a', 'little', 'lamb'] >>> for index, value in enumerate(words): print index, value Good For example: when you want index -> value from a list.
  • 59. Functions Function blocks begin with the keyword def followed by the function name and parentheses (( )) The code block within every function starts with a colon (:) and is indented. Functions can have docstring. Accessible through function.__doc__ or help(function)
  • 60. Functions (example) function hello() {     echo "hello world"; } PHP def hello(): "Prints hello world"
 echo "hello world" >>> help(hello) Help on function hello in module __main__: hello() Prints hello world Python
  • 61. Function arguments You can call a function by using the following types of formal arguments: Required arguments Default arguments Keyword arguments Variable-length arguments (Variadic function) PHP >= 5.6 splat operator (…) Like PHP it doesn’t support function overloading
  • 62. Required argument (example) >>> def hello(name): print "hello" + name >>> hello("glenn") glenn Python function hello($name) {     echo "hello" . $name; } hello("glenn"); PHP
  • 63. Required argument (example) >>> def hello(name): print "hello" + name >>> hello() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: hello() takes exactly 1 argument (0 given)
  • 64. Default argument (example) >>> def hello(name="glenn"): print "hello" + name >>> hello() hello glenn Python function hello($name="glenn") {     echo “hello” . $name; } hello(); >>> hello glenn PHP
  • 65. Keyword argument (example) >>> def sum(a=1, b=2): print a + b >>> sum() 3 >>> sum(a=5) 7 >>> sum(b=4) 5 >>> sum(a=2, b=4) 6 Python ( There isn’t a PHP equivalent )
  • 66. Variable length argument (example) def manyArgs(*arg): print "I was called with", len(arg), "arguments:", arg >>> manyArgs(1) I was called with 1 arguments: (1,) >>> manyArgs(1, 2, 3) I was called with 3 arguments: (1, 2, 3) Python function manyArgs(...$arg) { echo "I was called with " . count($arg) . " arguments: " implode($arg); } hello(1); // I was called with 1 arguments: 1
 hello(1, 2, 3); // I was called with 3 arguments: 1,2,3 PHP >= 5.6 using the . . . (splat) operator
  • 67. Classes In Python everything is an object (again) Class blocks begin with the keyword class followed by the class name and a colon (:) Doesn’t have private / public concept There is a convention that is followed by most Python code: a name prefixed with an underscore (e.g. _spam) should be treated as a non-public… …but it is still accessible Classes can also have docstring.
  • 68. Classes (example) class Employee: def __init__(self, name, salary): self.name = name self.salary = salary e = Employee("mike", "1500") Python class Employee { function __construct($name, $salary) { $this->name = $name; $this->salary = $salary; } } $e = new Employee("mike", "1500"); PHP
  • 69. Classes: inheritance Supports multiple inheritance (PHP lacks that) Method overloading works the same as in PHP You can (optionally) make your base class inherit from the class object. Matter of style.
  • 70. Classes: inheritance (example) class Vehicle { // vehicle methods and members } class Car extends Vehicle { // car methods and members } PHP class Vehicle: # vehicle methods and members class Car(Vehicle): # car methods and members Python
  • 71. Classes: overloading 1/2 (example) class Parent(object): def __init__(self): self.value = 5 def get_value(self): return self.value class Child(Parent): pass # is a null operation >>> c = Child() >>> c.get_value() 5
  • 72. Classes: overloading 2/2 (example) class Parent(object): def __init__(self): self.value = 5 def get_value(self): return self.value class Child(Parent): def get_value(self): return self.value + 1 >>> c = Child() >>> c.get_value() 6
  • 73. __init__ /__keyword__ The __keyword__ are also called dunder methods (thunder in West Flemish or double under) They are also sometimes called “magic” methods They are roughly the same as inheriting things In the case of __init__ in the background Python will do automatically the calls to init and new They also make it possible (but are not limited) to change how operators (for example +, / , ..) behave.
  • 74. from __future__ import division (example) # in python 2.x if you divide an integer you # will get an integer back >> 1 / 2 0 # In python 2.7 future = Python 3.x from __future__ import division >> 1 / 2 0.5 >> 1 // 2 # you will need the special // division # operator to get an integer back
  • 75. File I/O (example) # Reading a file with open('myfile.txt', 'r') as f: for line in f: print f # Writing a file with open('myfile.txt', 'w') as f: f.write('Hello')
  • 76. Lambda functions # Simple lambda example f = lambda x, y : x + y >> f(2,4) 6 # Map -> lambda (divide by 2) >>> a = [2, 4, 8, 16] >>> map ( lambda x: x/2, a) [1, 2, 4, 8] # Filter -> lambda (even numbers) >>> a = [1, 2, 3, 4, 5, 6 , 7, 8, 9, 10] >>> filter (lambda x: x % 2 == 0, a) [2, 4, 6, 8, 10]
  • 77. The proof is in the pudding…
  • 78. Minimal flask (example) from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "Hello World!" @app.route("/name/<name>") def hello(name=None): return “Hello “ + name if __name__ == "__main__": app.run() HTTP://FLASK.POCOO.ORG
  • 79. Minimal scraper 1/2 (example) <div title="buyer-name">Carson Busses</div> <span class="item-price">$29.95</span> HTML from lxml import html import requests page = requests.get('http://econpy.pythonanywhere.com/ex/001.html') tree = html.fromstring(page.text) #This will create a list of buyers: buyers = tree.xpath('//div[@title="buyer-name"]/text()') #This will create a list of prices prices = tree.xpath('//span[@class="item-price"]/text()') print 'Buyers: ', buyers print 'Prices: ', prices Python
  • 80. Minimal scraper 2/2 (example) Buyers: ['Carson Busses', 'Earl E. Byrd', 'Patty Cakes', 'Derri Anne Connecticut', 'Moe Dess', 'Leda Doggslife', 'Dan Druff', 'Al Fresco', 'Ido Hoe', 'Howie Kisses', 'Len Lease', 'Phil Meup', 'Ira Pent', 'Ben D. Rules', 'Ave Sectomy', 'Gary Shattire', 'Bobbi Soks', 'Sheila Takya', 'Rose Tattoo', 'Moe Tell'] Prices: ['$29.95', '$8.37', '$15.26', '$19.25', '$19.25', '$13.99', '$31.57', '$8.49', '$14.47', '$15.86', '$11.11', '$15.98', '$16.27', '$7.50', '$50.85', '$14.26', '$5.68', '$15.00', '$114.07', '$10.09'] Result HTTP://DOCS.PYTHON-GUIDE.ORG/EN/LATEST/SCENARIOS/SCRAPE/
  • 81. Bayesian text classifier from sklearn.feature_extraction.text import CountVectorizer from sklearn.pipeline import Pipeline from sklearn.naive_bayes import MultinomialNB import numpy # docs docs = numpy.array(["Buy viagra", "Hello frank", "The servers are down", "Do you want a rolex", "Cheap airplane tickets"]) # labels 1: spam, 0: ham labels = numpy.array([1,0,0,1,1]) # pipeline steps = [('vectorizer', CountVectorizer()), ('clf', MultinomialNB())] pipe = Pipeline(steps) pipe.fit(docs, labels) # test data test_data = numpy.array([ "Hello koen", "Cheap viagra"]) print pipe.predict(test_data) HTTP://SCIKIT-LEARN.ORG/STABLE/
  • 82. Automate with fabric # Import Fabric's API module from fabric.api import * env.hosts = [‘server.domain.tld’, ‘another_server.domain.tld’] def update_upgrade(): """ Update the default OS installation's basic default tools. """ run("aptitude update") run("aptitude -y upgrade") def install_memcached(): """ Download and install memcached. """ run("aptitude install -y memcached") def update_install(): # Update update_upgrade() # Install install_memcached() HTTP://WWW.FABFILE.ORG/
  • 83. Some interesting resources Dive into python -http://www.diveintopython.net/ The standard python library - http://effbot.org/zone/librarybook- index.htm How to think like a computer scientist with python - http:// www.greenteapress.com/thinkpython/thinkCSpy.pdf PyVideo (indexes a lot of python talks) - http://pyvideo.org/ 80 best python resources - http://www.fromdev.com/2014/03/ python-tutorials-resources.html 10 myths of enterprise python - https://www.paypal- engineering.com/2014/12/10/10-myths-of-enterprise-python/ Python success stories - https://www.python.org/about/success/
  • 84. Thank you for listening… (any questions ?)