Python and Perl are both popular scripting languages that are interpreted, general purpose, and support multiple programming paradigms. While they have similar capabilities, Python emphasizes readability and has a stronger community around its development. Python also has a large standard library and is used in many popular projects. However, Python is still working through issues related to the transition from Python 2 to Python 3.
4. EASY TO READ
Source: https://leetcode.com/discuss/34793/my-10-line-unreadable-python-solution
import collections
class Solution:
def canFinish(self, n, prs):
m = collections.defaultdict(lambda: collections.defaultdict(set))
map(lambda pr: m[0][pr[0]].add(pr[1]) or (m[1][pr[1]].add(pr[0])), prs)
m[2][0].update(set(filter(lambda c: not m[0][c], xrange(n))))
while m[2][0]:
v = (lambda x: (m[2][1].add(x) or x))(m[2][0].pop())
m[2][0].update(filter(lambda o: m[0][o].discard(v) or
(not m[0][o] and o not in m[2][1]), m[1][v]))
return len(m[2][1]) == n
13. >>> import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
14. use Acme::this;
The Zen of Perl, by bellaire
Beauty is subjective.
Explicit is recommended, but not required.
Simple is good, but complex can be good too.
And although complicated is bad,
Verbose and complicated is worse.
Brief is better than long-winded.
But readability counts.
So use whitespace to enhance readability.
Not because you're required to.
Practicality always beats purity.
In the face of ambiguity, do what I mean.
There's more than one way to do it.
Although that might not be obvious unless you're a Monk.
At your discretion is better than not at all.
Although your discretion should be used judiciously.
Just because the code looks clean doesn't mean it is good.
Just because the code looks messy doesn't mean it is bad.
Reuse via CPAN is one honking great idea -- let's do more of that!
Credit: bellaire
Source: http://www.perlmonks.org/?node_id=752029
16. INSTALL PYTHON TO PLAY AROUND
virtualenv is a tool to to create isolated Python environments
$ virtualenv my_python
Using base prefix '/usr'
New python executable in my_python/bin/python3
Also creating executable in my_python/bin/python
Installing setuptools, pip, wheel...done.
$ source my_python/bin/activate
(my_python)$ which python
/home/mruiz/my_python/bin/python
(my_python)$ deactivate
$ which python
/usr/bin/python
17. WHERE IS MY CPAN?!?!
Fear not! PyPI to the rescue
The official third-party software repository for Python
The Python Package Index
Not to beconfused with PyPy (theimplementation and JIT compiler)
18. LET'S INSTALL SOME SHIT
pip vs. easy_install
pip (2008) easy_install (2004)
Install from Wheels Yes No
Uninstall Packages Yes (pip uninstall) No
Dependency Overrides Yes No
List Installed Packages Yes (pip list) No
PEP438 Support Yes No
Installation format 'Flat' packages Encapsulated Egg
sys.path modification No Yes
Install from Eggs No Yes
pylauncher support No Yes
Multi-version Installs No Yes
Source: https://packaging.python.org/en/latest/pip_easy_install.html
21. SYNTAX OVERVIEW
x = 0
while x < 10:
print(x)
x += 1
def sum(x, y):
return x + y
print(sum(2, 3))
my $x = 0;
while ($x < 10) {
say $x++;
}
sub sum($x, $y) {
return $x + $y;
}
say sum(2, 3);
27. GENERATORS (STATE VARIABLES / CORO)
def gen():
i = 0
while True:
yield str(i)
i = i + 1
x = gen()
for i in range(0, 10):
print(next(x))
use 5.010;
sub gen {
state $i = 0;
return $i++;
}
say gen() for (0..9);
28. LIST COMPREHENSIONS (MAP, GREP)
list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
squared_even = [x ** 2 for x in list if x % 2 == 0]
my @list = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
my @squared_even = map { $_ ** 2 } grep { $_ % 2 == 0 } @list;
29. MORE STUFF
built in exceptions (Try::Tiny)
built in REPL (re.pl)
huge standard library (cpan)
32. VERSIONING SHITSHOW
Python 3 released in December 2008
Breaks backwards compatibility with Python 2.x
No new features will go into 2.x branch
3.x does not offer enough coolness to encourage migration
Lots of projects still in 2.x
13779 PyPI ported packages out of 65396 (source:
caniusepython3.com)
33. HOW TO WRITE PORTABLE CODE?
Prepend this to your Python 2 modules
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
34. FURTHER READING
Python Official Docs:
Python Package Index:
Python Enhancement Proposals:
Python Packaging Authority:
Python 3 is killing Python:
https://docs.python.org
https://pypi.python.org/pypi
https://www.python.org/dev/peps
https://www.pypa.io
http://blog.thezerobit.com/2014/05/25/python-3-is-
killing-python.html