In our Learn To Build A Hello World Python GUI In A Delphi Windows App post, we already demonstrate the combining strength of Delphi and Python for your applications. We learn about all the components involved in the demo, the implementation details, and run “Hello World!” and other Python mathematical operations for the sanity test.
This post will guide you on how to run another Python feature in Python4Delphi with RAD Studio: Variables and Data Types in Python GUI and gets the output.
Prerequisites: Download and install the latest Python for your platform. Follow the Python4Delphi installation instructions mentioned here. Alternatively, you can check out this video Getting started with Python4Delphi.
First, run the Python GUI in the Demo1 App:
Let’s dive in to the Python Variables and Data Types!
Table of Contents
1. What is Python Variables?
A variable is a named location used to store data in the memory, or in another words: Containers for storing data values. In Python, variables can be changed or re-assign later in the program.
1.1. Variable Assignment & Re-Assignment
Example of variable assignment & re-assignment:
1 2 3 4 5 6 7 |
# First variable assignment number = 10 print(number) # Second variable assignment (change the first one) number = 10.011 print(number) |
See it in actions, with Python4Delphi:
1.2. Assigning Multiple Values to Multiple Variables
Here is the example if you want to sign multiple values to multiple variables:
1 2 3 4 5 |
a, b, c = 10, 10.5, "Hello Python4Delphi!" print (a) print (b) print (c) |
See it in actions, with Python4Delphi:
1.3. Variable Casting
We can specify the data type of a variable with casting:
1 2 3 4 5 6 7 |
x = str(7) # x will be '7' y = int(7) # y will be 7 z = float(7) # z will be 7.0 print(x) print(y) print(z) |
See it in actions, with Python4Delphi:
1.4. Case-Sensitive
Variable naming in Python is case-sensitive:
1 2 3 4 5 6 |
a = 42 A = "Python" #A will not overwrite a print(a) print(A) |
See it in actions, with Python4Delphi:
2. What is Python Data Types?
In computer science and computer programming, a data type or simply type is an attribute of data which tells the compiler or interpreter how the programmer intends to use the data. A data type constrains the values that an expression, such as a variable or a function, might take. This data type defines the operations that can be done on the data, the meaning of the data, and the way values of that type can be stored. A data type provides a set of values from which an expression (i.e. variable, function, etc.) may take its values.
Listed below are Python built-in data types by default:
Text Type: | str |
Numeric Types: | int , float , complex |
Sequence Types: | list , tuple , range |
Mapping Type: | dict |
Set Types: | set , frozenset |
Boolean Type: | bool |
Binary Types: | bytes , bytearray , memoryview |
2.1. Get the Data Type
We can learn about any available Python data types, using print(type(var)). Examples:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# Str x = "Hello World" print(type(x)) # Int y = 20 print(type(y)) # Float z = 20.5 print(type(z)) # Complex a = 1j print(type(a)) # List b = ["apple", "banana", "cherry"] print(type(b)) # Tuple c = ("apple", "banana", "cherry") print(type(c)) # Range d = range(6) print(type(d)) # Dict e = {"name":"John", "age":36} print(type(e)) # Set f = {"apple", "banana", "cherry"} print(type(f)) # Frozenset g = frozenset({"apple", "banana", "cherry"}) print(type(g)) # Bool h = True print(type(h)) # Bytes i = b"Hello" print(type(i)) # Bytearray j = bytearray(5) print(type(j)) # Memoryview k = memoryview(bytes(5)) print(type(k)) |
See it in actions, in PythonGUI:
Congratulations, now you have learned Python Variables and Data Types in Python GUI in A Delphi Windows App!
From now, the applications are endless and the only limit is your imagination.
Check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi.