python: an introduction for php webdevelopers
TRANSCRIPT
![Page 1: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/1.jpg)
PYTHON: AN INTRODUCTION FOR PHP DEVELOPERS
![Page 2: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/2.jpg)
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 !
![Page 3: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/3.jpg)
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.
![Page 4: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/4.jpg)
Things that ruleIt’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
![Page 6: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/6.jpg)
Things that can biteUses 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
![Page 7: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/7.jpg)
Who uses it ?
![Page 8: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/8.jpg)
In BelgiumNot 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)
![Page 9: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/9.jpg)
Some web frameworks
https://github.com/vinta/awesome-python
BeautifulSoup
![Page 10: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/10.jpg)
Work project: GhelamcoCSV 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)
![Page 11: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/11.jpg)
From CSV data
![Page 12: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/12.jpg)
To generate doctrine entity fields
… now imagine if you need to do that manually…Average runtime: less than a second…
![Page 13: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/13.jpg)
Personal project: WebGL baking tool
http://www.simplicity.be/improving-realism-in-webgl-scenes-by-using-texture-baking/
![Page 14: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/14.jpg)
Now for the code stuff…
![Page 15: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/15.jpg)
IDEPython 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
![Page 16: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/16.jpg)
PyCharm CE
![Page 17: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/17.jpg)
Running python• Through the interactive python shell
$ pythonPython 2.7.9 (default, Dec 19 2014, 06:00:59) [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwinType "help", "copyright", "credits" or "license" for more information.>>> print "hello world"hello world>>>
![Page 18: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/18.jpg)
Running python• By running a .py file
hello.py
print "hello world"
$ python hello.pyhello world
command
![Page 19: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/19.jpg)
2 very handy methodsdir
list attributes and methods
help
shows builtin help
![Page 20: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/20.jpg)
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']
![Page 21: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/21.jpg)
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)
![Page 22: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/22.jpg)
CommentsA comment in python start with a hash (#) symbol
No multiline comments
use multiple #
![Page 23: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/23.jpg)
Comments (examples)// hello world
/** * hello * world **/
PHP
# hello world
# hello# world
Python
![Page 24: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/24.jpg)
VariablesPython 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
![Page 25: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/25.jpg)
Mutable vs immutableMutable: 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!
![Page 26: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/26.jpg)
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
![Page 27: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/27.jpg)
Standard data typesNumber
integer
float
long
complex
String
List
Tuple
Dictionary
![Page 28: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/28.jpg)
Variables declarations (examples)
$var_1 = 1; // Integer $var_2 = 3.14; // Float$var_3 = "hello"; // String$var_4 = 'hello'; // String
PHP
var_1 = 1 # Integervar_2 = 3.14 # Floatvar_3 = "hello" # Stringvar_4 = 'hello' # Stringvar_1 = 'abc' # Is now a string
Python
![Page 29: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/29.jpg)
Math operatorsArithmetic operators are the same as PHP
Notation Description
+ add
- subtract
* multiply
/ division
% modulo
** power operator** the ** operator is introduced in PHP 5.6
![Page 30: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/30.jpg)
Assignment operatorsNotation 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
![Page 31: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/31.jpg)
Comparison operatorsNotation Description
== is equal*
!= not equal
<> not equal
> bigger than
< smaller than
>= bigger than or equal
<= smaller than or equal* doesn’t have the === operator
![Page 32: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/32.jpg)
Logical operators
Notation Description
and and
or or
not not
![Page 33: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/33.jpg)
Chained comparisons
# chainedif 5 < x < 7:
print 6
# same as if x > 5 and x < 7: print 6
![Page 34: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/34.jpg)
Strings (examples)$var_1 = 'Thunderstruck';$var_2 = "Ain't no sunshine";$var_3 = 'Ain\'t no sunshine';$var_3 = "this hasmultiplelines";
PHP
Python
var_1 = 'Thunderstruck'var_2 = "Ain't no sunshine"var_3 = 'Ain\'t no sunshine'var_3 = """this hasmultiplelines"""
![Page 35: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/35.jpg)
print ('Thunderstruck');echo 'Thunderstruck';
PHP
print ('Thunderstruck'); # python 3.xprint 'Thunderstruck'; # python 2.x
Python
![Page 36: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/36.jpg)
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)
![Page 37: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/37.jpg)
SlicesStrings are a sequence of characters
A subsequence of a sequence is called a slice
The operation that extracts a subsequence is called slicing
Notation Descriptiona[start:end] items start through end-1
a[start:] items start through the rest of the arraya[:end] items from the beginning through end-1
a[:] a copy of the whole arraya[start:end:step] start through not past end, by step
![Page 38: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/38.jpg)
Slicesa[-1] # last item in the arraya[-2:] # last two items in the arraya[:-2] # everything except the last two items
FROM HTTP://WWW.NLTK.ORG/BOOK/CH03.HTML
![Page 39: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/39.jpg)
NoneNone 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.
![Page 40: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/40.jpg)
None (example)
db_con = None # Try to connecttry: 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
![Page 41: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/41.jpg)
BooleansSimple same as in PHP but case sensitive!
True
False
1
0
![Page 42: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/42.jpg)
Booleans (examples)$a = True;$b = FALSE;
if ($a === true) { echo "Party!"; }
PHP
a = Trueb = False
if a is True:print “party"
if a == True:print "party"
Python
![Page 43: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/43.jpg)
SequencesDifficult 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
![Page 44: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/44.jpg)
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]
![Page 45: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/45.jpg)
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]
![Page 46: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/46.jpg)
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')
![Page 47: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/47.jpg)
Dictionaries
person = {'Name': 'Tom', 'Age': 27};
print person['Name']; # Tomprint person['Age']; # 27
Python
$person = array('Name' => 'Tom', 'Age' => 27);
print $person['Name']; print $person['Age'];
PHP
![Page 48: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/48.jpg)
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
![Page 49: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/49.jpg)
Membership operators (example)
>>> a = (1,2,4,8)>>> 1 in aTrue>>> 3 not in aTrue>>> 3 in aFalse
>>> word = "boat">>> 'e' in wordFalse>>> 'a' in wordTrue
![Page 50: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/50.jpg)
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 '
![Page 51: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/51.jpg)
Whitespace - indentationInstead 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.
![Page 52: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/52.jpg)
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 stuffelif (other_statement): # do other stuffelse: # do other stuff
Python
![Page 53: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/53.jpg)
Iterations: for loopPython 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.
![Page 54: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/54.jpg)
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
![Page 55: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/55.jpg)
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
![Page 56: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/56.jpg)
Iterations: rangeIf 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()
![Page 57: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/57.jpg)
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
![Page 58: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/58.jpg)
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.
![Page 59: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/59.jpg)
FunctionsFunction 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)
![Page 60: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/60.jpg)
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
![Page 61: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/61.jpg)
Function argumentsYou 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
![Page 62: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/62.jpg)
Required argument (example)
>>> def hello(name): print "hello" + name
>>> hello("glenn")glenn
Python
function hello($name) { echo "hello" . $name;}
hello("glenn");
PHP
![Page 63: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/63.jpg)
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)
![Page 64: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/64.jpg)
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
![Page 65: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/65.jpg)
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 )
![Page 66: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/66.jpg)
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: 1hello(1, 2, 3); // I was called with 3 arguments: 1,2,3
PHP >= 5.6 using the . . . (splat) operator
![Page 67: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/67.jpg)
ClassesIn 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.
![Page 68: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/68.jpg)
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
![Page 69: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/69.jpg)
Classes: inheritanceSupports 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.
![Page 70: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/70.jpg)
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
![Page 71: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/71.jpg)
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
![Page 72: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/72.jpg)
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
![Page 73: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/73.jpg)
__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.
![Page 74: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/74.jpg)
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.xfrom __future__ import division
>> 1 / 2 0.5
>> 1 // 2 # you will need the special // division # operator to get an integer back
![Page 75: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/75.jpg)
File I/O (example)
# Reading a filewith open('myfile.txt', 'r') as f: for line in f: print f
# Writing a filewith open('myfile.txt', 'w') as f: f.write('Hello')
![Page 76: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/76.jpg)
Lambda functions# Simple lambda examplef = 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]
![Page 77: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/77.jpg)
The proof is in the pudding…
![Page 78: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/78.jpg)
Minimal flask (example)from flask import Flaskapp = 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
![Page 79: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/79.jpg)
Minimal scraper 1/2 (example)<div title="buyer-name">Carson Busses</div><span class="item-price">$29.95</span>
HTML
from lxml import htmlimport 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 pricesprices = tree.xpath('//span[@class="item-price"]/text()')
print 'Buyers: ', buyersprint 'Prices: ', prices
Python
![Page 80: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/80.jpg)
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/
![Page 81: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/81.jpg)
Bayesian text classifierfrom sklearn.feature_extraction.text import CountVectorizerfrom sklearn.pipeline import Pipelinefrom sklearn.naive_bayes import MultinomialNBimport numpy
# docsdocs = numpy.array(["Buy viagra", "Hello frank", "The servers are down", "Do you want a rolex", "Cheap airplane tickets"])
# labels 1: spam, 0: hamlabels = numpy.array([1,0,0,1,1])
# pipelinesteps = [('vectorizer', CountVectorizer()), ('clf', MultinomialNB())]
pipe = Pipeline(steps)pipe.fit(docs, labels)
# test datatest_data = numpy.array([ "Hello koen", "Cheap viagra"])
print pipe.predict(test_data)
HTTP://SCIKIT-LEARN.ORG/STABLE/
![Page 82: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/82.jpg)
Automate with fabric# Import Fabric's API modulefrom 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/
![Page 83: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/83.jpg)
Some interesting resourcesDive 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/
![Page 84: Python: an introduction for PHP webdevelopers](https://reader035.vdocuments.site/reader035/viewer/2022062522/58a999f31a28abc2518b572f/html5/thumbnails/84.jpg)
Thank you for listening…(any questions ?)