Python to Pascal

A programmer is expected to be able to program in more than one language. He or she might need to convert an existing program from one language to another or be required to switch to a different language for a new project. This tutorial should be helpful both for students wanting to convert a Python script to Pascal and for Python programmers learning the Pascal language. We tested our Python code with (1) IDLE, (2) the educational version of PyCharm or (3) the Python 3.3 interpreter run directly from the command line. The equivalent Pascal code was compiled and executed by Lazarus.

Some Differences between Python and Pascal

  • In Pascal the formatting with indents aids readability but is not required for successful compilation.
  • Pascal is not case sensitive.
  • Python code is usually interpreted (although you can compile it) whereas Pascal is compiled.
  • The statement separator in Pascal is the semicolon.
  • You use single quotes to delimit a string (instead of either single or double quotes in Python).
  • You must declare the type of a variable before its first use in Pascal.
  • The assignment operator is := in Pascal and = in Python.
  • The test for equality is = in Pascal instead of == in Python.
  • Pascal has the handy repeat .. until loop for iteration and case statement for selection.
  • For calling a routine that takes no parameters, a Pascal identifier does not require the appendage ().
  • Assignment to a name inside a Python function marks the name as local by default. A global variable that you assign in a Pascal routine will be changed as expected (without declaring it within the routine as being global).
  • You can use a record in Pascal instead of a tuple in Python.
  • In Python, a variable belonging to a class is known as an attribute and when you refer to it you prefix its identifier with self.. In Pascal, a variable declared in a class is usually called a field (and may be suitably named e.g. FSpeed). You can prefix the identifier with Self., but this is usually unnecessary.
  • In a Python class, an attribute used without the self. prefix is a class variable. If it is mutable (e.g. a list) you need to be aware that a change made by one object will change the data that all objects in this class will reference. A field is unique to each object of a Pascal class, however. (If you want a class variable, declare it with the class var keywords).
  • In Python you use the __init__ function as a class constructor whereas in Pascal you actually use the keyword constructor together with its identifier, which is usually Create.

Follow the numbered links below to other sections of this tutorial. Follow the first link to see equivalent code for commonly used language features.

Programming - a skill for life!

Pascal Programming Tutorials