Download Python for perl programmers

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Pythonforperlprogrammers
FernandoPineda
140.636
Similari;esbetweenPerl&Python
•  Likeperl,pythonis:
–  A‘scrip;ng’languagebutalsoageneralpurpose
language.
–  Interpreted
–  Commentsstartwith‘#’
–  Mul;pleprogrammingparadigmsaresupported
•  impera;ve
•  object-based
•  func;onal
–  Dynamicallytyped
–  Largepackagerepositories(CPAN,PyPI)
Packages&repositories
•  ComprehensivePerl
ArchiveNetwork(CPAN)
•  ~175KpackagesinCPAN
•  cpan
•  cpanm
•  use
•  PythonPackageIndex
(PyPI)
•  ~90KpackagesinPyPI
•  easy_install
•  pip
•  import
Differencesbetweenperl&python
•  Python(likeCandJava)isstronglytyped
•  Pythoncodeblocksdelimitedviaindenta;on
ratherthancodeblocks
•  Builtininterac;ve-shell(perlhase.g.Devel::REPL)
•  InpythonEVERYTHINGisanobject
Built-inTypes
Perl
•  scalar
•  array
•  hash
*immutablepythonobjects
hfps://docs.python.org/2/library/types.html
Python
• 
• 
• 
• 
• 
• 
• 
• 
• 
int,long
float,complex
string*
list
tuple*
set
frozenset*
dict
...etc.
Codeblocks
perl
foreach my $x in (1..9) {
print “$x\n”;
}
python
for x in range (1,10)
print(x)
versions,programs&scripts
perlscript
pythonscript
perl --version
python --version
#!/usr/bin/env perl
#!/usr/bin/env python
... perl statements ...
‘’’ python statements ...
raw input()
#!/usr/bin/env python
# raw)input() returns a string object
age = raw_input("What is your age? ")
# print() prints the string
print (“your age: “+ age )
# type() returns a type ‘type’
# print() prints the type
print(type(age))
Anonymousfunc;ons
perl
my $f= sub {
my $x = shift;
return 2*$x;
}
python
f = lambda x:
return 2*x
print(f(2))
print $func_ref->(2)
my $f= sub { 2*shift}
print $func->(2)
f= lambda x: 2*x
print (f(2))
Inpythoneverythingisanobject
•  type()showsthetypeofanobject
•  dir() showsthemethodsinanobject
type(“hello”)
dir(“hello”)
“hello”.__add__(“ world”)
type(9/8)
print(9/8)
Crea;ngyourownobjects
perl
Package MyClass;
sub new {
return bless{x=>undef}
}
$obj = MyClass->new();
perlwithMoo
Package MyClass;
use Moo;
has x => {is => ‘rw’}
python
class MyClass:
def __init__(self):
self.x = None
obj = MyClass
Map
perl(map)
my @items= 1..9;
@squared = map {$_**2} @items
python(map)
items= range(1,10)
squared = list(map(lambda x: x**2, items))
python(listcomprehension)
list= range(1,10)
squared = [x**2 for x in list]
Reduce
perl(reduce)
use List::Util (‘reduce’);
my @items= 1..9;
my $sum= reduce { $a + $b } @items;
python(reduce)
from functions import reduce
items= range(1,10)
sum = reduce(lambda x,y: x+y, items))
Troublewithversions
•  Python2.xvsPython3.x
–  v.3releasedinDec.2008
–  Notbackwardcompa;blewithPython2.x
–  Manyprojectss;llinPython2.x
–  PyPIhas25,948v.5packagesportedtov.6(outof90839)
•  Perl5.xvsPerl6.x
–  v.6releasedinDec.2015(announcedin2000)
–  Notbackwardcompa;blewithPerl5.x
–  CPANhas173,63v.5packagesbutdoesnotyetsupportv.6
packages
Ra;onalizinglanguageissues
•  PrependtoPython2.xmodules
from __future__
from __future__
from __future__
from __future__
...etc.
import
import
import
import
absolute_import
division
print_function
unicode_literals
print 8/7 # with and with ‘division module’
•  PrependtoPerl2.xscriptsandmodules
use Modern::Perl;
use Moo;
# simplifies object creation/usage
Addi;onalmaterial
•  hfps://docs.python.org
•  hfps://pypi.python.org
•  hfp://modernperlbooks.com/books/
modern_perl_2016
Related documents