In this table I will compare some basic programming syntax and conventions between the Python and Perl programming languages.
Programming element | Python | Perl |
---|---|---|
Creating a variable that contains an int: | age = 25 |
%age = 25; |
Creating a variable that contains a float: | probability = 0.62 |
%probability = 0.62; |
Creating a variable that contains a string: Python supports single, double, and triple quotes. Perl supports only double quotes. |
state = 'North Dakota' state = "North Dakota" state = """North Dakota""" |
%state = "North Dakota"; |
Denoting string literals: Python uses an ‘r’ in front of the string. Single quotes always mean string literal in Perl. Double quotes in Perl allow for interpolations such as ‘\n’, ‘\t’, etc.. |
path = r'\path\to\some\files' path = r"\path\to\some\files" path = r"""\path\to\some\files""" |
%path = '\path\to\some\files'; |
String Concatenation: | greeting = "Hello" + " world" |
$greeting = "Hello" . " world"; |
Creating a list or array: | fruit_list = ['apple', 'banana', 'cherry'] |
@fruit_list = ("apple", "banana", "cherry"); |
Getting the length of a list or array: | length = len(fruit_list) |
$length = @fruit_list; |
Creating a dictionary or hash: | fruit = {'a':'apple', 'b':'banana', 'c':'cherry'} |
%fruit = ("a", "apple", "b", "banana", "c", "cherry"); %fruit = ("a" => "apple", "b" => "banana", "c" => "cherry"); |
Getting keys and values from a dictionary or hash: | fruit_keys = fruit.keys() fruit_values = fruit.values() |
@fruit_keys = keys %fruit; @fruit_values = values %fruit; |
Adding and deleting key-value pairs to a dictionary or hash: | fruit['o'] = 'orange' del fruit['c'] |
$fruit{'o'} = "orange"; delete $fruit{'c'}; |
Python was invented in the late 1980s (1989) by Guido van Rossum. Ideas for Python were adopted from Modula-3 and Lisp. Perl was also invented in the late 1980s (1987) by Larry Wall as a general-purpose Unix scripting language to make report processing easier. Perl is an acronym that stands for Practical Extraction Report Language.