Python-Pascal Comparison Table

Language Feature Python Pascal
Statement separator End of line (or for ; see Python: Myths about Indentation) ;


Comment


# Single line comment or after code

// instead of #
{Comment spanning
lines}


Declaring constants

VAT_RATE = 0.175
(VAT_RATE treated as a variable by interpreter. Capitals indicate that value should not be changed).
const VAT_RATE = 0.175;
(Compilation will fail if you attempt to change the value).


Declaring variables


Variable type not declared but inferred from first use.

var
  Forename, Surname : string; 
  PaysTax: Boolean;
  TotalHits: integer;
  AverageScore: double;


  (same as AverageScore: real;)
  Months: array [1..12] of
           string;
  Rainfall: array [2004 .. 2010,
              1..12] of integer;
  AgeRecords: array[1..1000] of
               TRec;

  
Also char (same as ansiChar), wideChar, byte, single, currency, TDateTime ...  


Assigning variables

surname = 'Morrison'
average_score = 24.56

import datetime
date_submitted = datetime.date(2007, 5, 1) 
                       # year, month, day

months[0] = 'January'
rainfall[0][0] = 15
Surname := 'Morrison'; 
AverageScore := 24.56;
DateSubmitted := StrToDateTime(
                  '1/5/2007'); 
Months[1] := 'January';
Rainfall[2000, 1] := 15;
or
Rainfall[2000][1] := 15;

If statement
  One line
                  
                  
  More than one line




  Else

if current_num > max:
    max = current_num


if current_num > max:
    max = current_num
    print('max increased')


if current_num > max:
    max = current_num
else:
    print('max unchanged')
if CurrentNum > Max then
  Max := CurrentNum;
                
if CurrentNum > Max then
  begin
    Max := CurrentNum;
    writeln('Max increased');
  end;

if CurrentNum > Max then
  Max := CurrentNum
else
  writeln('Max unchanged');


If-Elif / Case statement

if i_choice == 1:
    maths()
elif i_choice == 2:
    string_manipulation()
case iChoice of
  1 : Maths;
  2 : StringManipulation;
end;


For loop

for i in range(10):
    lines of code



for i in range(10, 0, -1):
    lines of code

for i := 1 to 10 do
  begin
    lines of code
  end;
  
for i := 10 downto 1 do
  begin
    lines of code
  end;


repeat loop

while not finished:
    lines of code
repeat
  lines of code
until finished = true;


While loop

while amount > 0:
    lines of code
while Amount > 0 do
  begin
    lines of code
  end;


Use of tuples/records

MyRec = ('Gibbs', 18)
type
  AgeRec = record
    Surname: string;
    Age: integer;
  end;
var
  MyRec: AgeRec = (Surname: 'Gibbs'; Age: 18);     

Operators for:
  addition
  subtraction
  multiplication
  real division
  integer division
  remainder
  exponentiation
                


+
-
*
\
\\
%
**


+
-
*
/
DIV
MOD

(Use the power function)


Procedure without parameters

def list_records():
    lines of code
To call the procedure:
list_records()


procedure ListRecords;
begin
  lines of code
end;


To call the procedure:
ListRecords;


Procedure with parameter passed by value

def listrec(rec_no):
    lines of code
To call the procedure for the fifth record:
listrec(5):


procedure ListRec(RecNo: Integer);
begin
  lines of code
end;


To call the procedure for the fifth record:
ListRec(5);


Procedure with parameter passed by reference


Complicated

procedure Calc(var Total:
              integer);
begin
  lines of code
end;


Function

def cube(num):
    return num ** 3
function Cube(Num: double):
         double;
begin
   Cube := Num * Num * Num;
end;

Inbuilt string functions
  Type conversions
                  
                  
  



  String manipulation


str() converts numeric value to string
                                    
                  
                  
int() converts string to integer
                  


len() returns length of string
                  
.find() looks for one string within another and returns the position of the first matching character.


String slicing such as my_string[:2], my_string[2:4] and my_string[-2:] returns the start, middle and end of the string.




.upper() returns a string in upper case.                  
.lower() returns a string in lower case.


IntToStr converts integer to string
FloatToStr converts real to string
FloatToStrf converts real to formatted string
DateTimeToStr converts from TDateTime to string
StrToInt converts string to integer  
                  


Length returns length of  string

Pos looks for one string within another
and returns the position of the first matching character.

(StrUtils Unit)
MidStr returns the required number of characters
from any position in the string
LeftStr returns the required number of characters
from the front of the string
RightStr returns the required number of characters
from the back of the string

UpperCase  returns a string in upper case
LowerCase returns a string in lower case

Sets
  initialization
  add member
  remove member

  intersection
  difference
  union

  membership test

set1 = {1, 2, 4, 5}
set1.add(3)
set1.discard(5)


set3 = set1.intersection(set2)
set4 = set1.difference(set2)
set5 = set1.union(set2)

if num in set1:
Set1: set of 1..6 = [1, 2, 4, 5];
include(Set1, 3);
exclude(Set1, 5);


Set3 := Set1 * Set2;
Set3 := Set1 - Set2;
Set3 := Set1 + Set2;

if Num in Set1 then    

Output to console
  without newline
  with newline

print(forename, end='')
print(forename)
write(Forename);
writeln(Forename);


Keyboard input

forename = input('Please enter your forename: ')
write('Please enter your forename: ');
readln(Forename);


Writing a text file

my_file = open('F:\line.txt', 'w')
my_file.write('One line only \n')
my_file.close()


var MyFile: TextFile;
begin
  assignFile(MyFile,
            'F:\line.txt');
  rewrite (MyFile);
  writeln('One line only');
  closeFile (MyFile);
end;


Reading a text file

my_file = open('F:\line.txt', 'r')
my_message = my_file.readline()
my_file.close()


var
  MyMessage: String;
  MyFile: TextFile;
begin
  assignFile(MyFile,
             'F:\line.txt');
  reset(MyFile);
  readln(MyFile, MyMessage);
  closeFile(MyFile);
end;

OOP    
First line of class definition
class Player:
TPlayer = class(TObject)


Constructor

def __init__(self, name):
    self.forename = name
In class definition:
public
  constructor Create(name: string);
implementation:
constructor TPlayer.Create(name: string);
begin
  Forename := name;
end;
Class variable/field/attribute
total = 0
class var Total;


Creating an object

player1 = Player('Jim')
Player1 := TPlayer.Create('Jim');     
Programming - a skill for life!

Converting from Python to Pascal