This article will focus on the basic building blocks of the Python programming language that are core to interacting with programs, including strings, integers, floats, and booleans.
After we have defined the basic steps for creating the python development environment, the logical next step for many new students in learning the Python programming language is to understand the basic components that will be used to create their first program. However, as you may shortly experience, there is often a plethora of jargon surrounding these base concepts that serve to add confusion to the already challenging task of learning a new programming language.
Object Types vs. Data Types
The key to navigating the terminology is to broadly accept that everything in Python is considered an object or a thing on which we can perform operations. Thus, these objects can be classified into different buckets based on their classification and how we can use them. This is the type of the object.
The confusion usually sets in when the term data type is thrown into the mix. It usually doesn’t help that the term object types and data types are often interchanged in many training guides or courses.
An object type can refer to any object in a Python program such as a function, class, or module. These are more intermediate or advanced topics, but they will be covered later in this tutorial series.
A data type, while technically an object type, often refers to lower-level or primitive items that are built-in to the Python language. The terminology may be intimidating, but you’ve likely encountered these data types in your lifetime before:
- String
- Integer
- Float
- Boolean
To kick us off, we will focus on these four primitive data structures listed above.
Figure 1. Python, like the polka dot bag, contains different object types that we can perform different tasks with.
Strings in Python
Strings are text-based information readable to humans, so they provide a great data type to start with (in opposition to the way we might define data types for PLCs, which are targeted to machines). In Python, strings are denoted by either single quotes or double quotes. Some examples are below from the pycharm editor:
These are relatively simple strings, and in some cases you may find yourself needing multiple lines of text within a string. This can be accomplished by using triple quotes:
If you execute any of the above strings, you’ll notice the program doesn’t accomplish much aside from indicating the program executed with no errors:
Figure 2. Execution of a multi-line string.
The print() Function and Variables in Python
In order to see or log the previous multiline string to the terminal, we must use Python’s print() function. Functions are objects as well, but they behave similarly to an algebraic function such as y = f(x). The function takes an input x, does something with x, and then returns a value y. The print function takes an object supplied to it, such as our multiline string, and logs that string to the console as such:
Figure 3. Using the print() function to output a multi-line string.
You’ll notice that, in Figure 3, the multi-line string format within the parenthesis of the print function is somewhat difficult to type out manually. Furthermore, what if we wanted to reference this string again later in the code? Wouldn’t that be a lot of work to type all those characters again? This is where variables come into play. Variables can be assigned to represent an object. For example, let’s assign our multi-line string to a variable called “plc” and print the plc variable:
Figure 4. Assigning variables.
If you’ve used other programming languages before, you’ll notice we just did something strange. We did not need to declare a plc variable and its associated data type prior to assigning the plc variable to the multi-line string. Essentially, variable data types are inferred by the interpreter at run time based on the value assigned to the variable. This rapidly speeds up development time and reduces repetitive error-prone tasks such as declaring variables.
F Strings in Python
The name may sound funny, but now that you know about variables, a very handy tool for working with strings and variables are f strings. Let’s say you want to print a variable’s assigned value within a string to log a message. We can accomplish this by wrapping our variable in curly brackets and prefixing the string with the letter f:
Figure 5. The usage of an f string to display a variable nested inside of a string.
String Slicing
Another core concept regarding strings is the ability to pull select character(s) from a string; this is known as string slicing. To pull certain values from a string, an offset or position must be specified within square brackets. For example, pulling the first character from the following string:
Figure 6. Positive index string slicing.
The indexing generally moves from left to right of the string with positive indices. However, it’s also possible to move from right to left.
Figure 7. Negative index string slicing.
Lastly, it is also possible to extract a multi-character substring from the string. Let’s extract the “.com” from “test.com” using two methods that yield the same result:
Figure 8. Substring extraction using string slicing.
Integers in Python
Integers represent whole numbers with no fractional component. Like algebra, we can perform the typical math operations on numbers in Python: addition, subtraction, and multiplication:
Figure 9. The integer data type and some supported operations.
Floats in Python
Floats are the common counterpart to integers and represent fractional values. Similar operations to integers can be performed on floats:
Figure 10. The float data type and some supported operations.
Boolean Data Types in Python
A great conceptual analogy for a Boolean data type is a low or high bit (BOOL) in a PLC program. The bit is often used to detect or toggle a physical entity on or off, open or closed such as a valve. Only two states can exist for a boolean, True or False:
Next Steps
Now that the basic data types have been explained, the next objective will be understanding the use of data structures and lists. Stay tuned for the next (forthcoming) article in the Python tutorial series.
Copyright Statement: The content of this website is intended for personal learning purposes only. If it infringes upon your copyright, please contact us for removal. Email: admin@eleok.com