Advantages and disadvantages of Python and Perl, and advantages and disadvantages of pythonperl

Source: Internet
Author: User
Tags perl script

Advantages and disadvantages of Python and Perl, and advantages and disadvantages of pythonperl

I. Comparison between Python and Perl

(1) The initial purpose of designing a language determines the built-in functions of the language:

Perl was initially created by Larry to format and process text, so there is a built-in regular expression. In python, a plural type is created. It is assumed that Guido initially created python for numerical calculation.Therefore, perl is good at text processing and python is good at numerical processing.

(2) Different application fields and requirements:

At the beginning, Perl was designed to facilitate the compilation of complex and efficient system scripts.It is also the most widely used scripting language. It is equivalent to the Swiss army knife in programming. It has a strong processing capability for character and text files. Previously, it required shell + sed + awk + C to complete the task, and only a perl script is required. In addition, the application field has been extended to support object-oriented programming.

Python object-oriented dynamic public language, suitable for Script Programming and rapid developmentIt is a bridge between a compilation language (such as C) and a scripting language (such as perl, scalability and object-oriented features make it a tool for large-scale application development.

(3) about strong types

In Perl, the Data Type depends on the context of the data.

In Python, the data type is determined by the data itself. Therefore, Python is generally considered a strongly-typed language, while Perl is not.

(4) about built-in Basic Types

The basic type of Perl is scalar to distinguish it from array and hash. Scalar can be a number or a string. Basically, scalar is either a number or a string. Whether scalar is a string or a number depends on the context of scalar. If it is a function that processes a string, it is a string. If it is a function that processes numbers, it is a number. Perl will make every effort to complete the conversion between them, no matter how absurd it seems to you. In Perl, all scalar starts with $, and all scalar starts with $.

The basic type of Python. Likewise, it is not a number or a string. However, it cannot be a number or a string. Python determines whether the variable is a number or a string to choose how to interpret the function. If it cannot find a proper explanation, Python will throw an exception. In general, this strategy can please some programmers and make others feel uncomfortable.

(5) about composite types

Perl has two composite types: array and hash. Python has three composite types: tuple, list, And dict. The tuple + list in Python corresponds to the array in Perl. Therefore, there is no such problem as who provides richer types.

(6) list as a whole

In Perl, the name starting with @ indicates the entire array. It is said that @ is the array header. However, according to the Perl principle, arrays such as @ foo can also be used in an environment requiring scalar. Perl will try its best to convert @ foo into a scalar. Generally, this scalar is the length of @ foo. In Python, you can directly use the variable name to obtain the entire list (or tuple ).

Ii. Exploration of Python

(1) Let's step by step into the world of Python

(2) Tips

1 --- run the python script, which is run in cmd below

2 --- input and output on the python Console

3-The ":" is too awesome to replace {}. The statement organization depends on Indentation rather than the begin/end block. Therefore, the indentation must be well controlled;

4 -- use it without variable or parameter Declaration

5 -- Python can write programs that are very tight and readable. Programs Written in Python are usually much shorter than those written in the same C or C ++ program.

6 -- run at the primary prompt. The primary prompt is usually identified as threeBig Yu (">>> "); The subsequent part is called the subordinate prompt, which is identified by three vertices ("... ").After two consecutive carriage returns, you can end the secondary prompt and change it to a common identifier >>>

7 -- the Python script can be directly executed like a Shell script. You only need to write a command line at the beginning of the script file to specify the file and mode:

#! /Usr/bin/env python

(Notify the interpreter of the user path) "#!" It must be the first two characters of the file. On some platforms, the first line must end with a Unix-style line terminator ("\ n, the Mac ("\ r") or Windows ("\ r \ n") Terminator cannot be used.Note:"# "Is a line comment in PythonStart character.

8 --

Python source files can be encoded using character sets other than ASCII. The best practice is in #! A special comment line is followed by a line to define the character set.

#-*-Coding: iso-8859-1 -*-

According to this Declaration, Python will convert the characters in the file from the specified encoding to Unicode as much as possible, and in this example, this character set is a iso-8859-1. InYou can find the available encoding list in the Python Library Reference Manual.Cp-936 or UTF-8, does not directly support GB, GBK, GB-18030 or ISO-10646-Translator's note ).

9 -- the pass statement does not do anything. If exp: elif exp: else:

It is used in scenarios where there must be any statement in syntax, but there is nothing to do in the program, for example:

>>> while True:...        pass # Busy-wait for keyboardinterrupt... if n > 0:    sum = 1; elif n == 0:    sum = 0 else:    sum = -1;

10 -- Define a function
The keyword def introduces a function definition. It must be followed by parentheses with function names and formal parameters. The function body statement starts from the next line and must be indented. The first line of the function body can be a string value. This string is the document string of the function, also known as docstring.
Some document string tools can process or print documents online, or allow users to browse the code interactively. Adding document strings to your code is a good practice and should become a habit.

11 --PassLambda keywords. You can create small anonymous functions.

PassLambda keywords can be used to create small anonymous functions. Here is a function that returns the sum of its two parameters: "lambdaa, B: a + B ".Lambda can be used in any function object. Due to syntax restrictions, they can only have one separate expression. In terms of semantics, they are just a syntax Technique in the definition of common functions.

12 -- DelStatement

There is a way to delete the specified index element from the linked list: del statement. This method can also delete slices from the linked list (we previously assigned an empty linked list to slices ). For example:

>>> A = [-1, 1, 66.6, 333,333,123 4.5] >>> del a [0] >>> a [1, 66.6, 333,333,123 4.5] >>> del a [] >>> a [1, 66.6, 1234.5] del can also be used to delete the entire variable: >>> del

Iii. Practice

# Coding = UTF-8 #! /Usr/bin/pythonimport xml. sax # inherited syntax class derived class name (base class name )://... in the base class name writing brackets, the basic class is specified in the tuples when the class is defined. Class MovieHandler (xml. sax. contentHandler): def _ init _ (self): self. currentData = "" self. type = "" self. format = "" self. year = "" self. rating = "" self. stars = "" self. description = "" # element start event processing def startElement (self, tag, attributes): self. currentData = tag if tag = "movie": print "****** Movie *****" title = attributes ["title"] print "Title :", title # def endElement (self, tag): if s Elf. currentData = "type": print "Type:", self. type elif self. currentData = "format": print "Format:", self. format elif self. currentData = "year": print "Year:", self. year elif self. currentData = "rating": print "Rating:", self. rating elif self. currentData = "stars": print "Stars:", self. stars elif self. currentData = "description": print "Description:", self. description self. currentData = "" # Content event processing def characters (self, content): if self. currentData = "type": self. type = content elif self. currentData = "format": self. format = content elif self. currentData = "year": self. year = content elif self. currentData = "rating": self. rating = content elif self. currentData = "stars": self. stars = content elif self. currentData = "description": self. description = content if (_ name _ = "__ Main _ "): # create an XMLReader parser = xml. sax. make_parser () # turn off namepsaces parser. setFeature (xml. sax. handler. feature_namespaces, 0) # override ContextHandler Handler = MovieHandler () parser. setContentHandler (Handler) parser. parse ("movies. xml ") def foo (bar = []): # bar is an optional parameter. If not specified, the default value is [] bar. append ("MKY"); # However, this line is faulty... Return bar; print foo () odd = lambda x: bool (x % 2) nums = [n for n in range (10)] nums [:] = [n for n in nums if not odd (n)] # Ah, this beautiful print nums
This example is mainly used for reading SAX_XML, defining the default values of parameters of the function, and lambda anonymous functions.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.