So, to get started today I want to introduce one more data type and one simple command. But first, comments.
Comments inside of Python are a way to add descriptive text to your script and have it be ignored by the interpreter. So you don’t have to worry about Python freaking out when you need to explain why or how you did something in your script.
To write a single line comment simply put a “#” in front of it
#this is a comment
The boolean data type has only one of two values. True or False. Older versions of Python don’t have True or False as defined keywords but the concept still works. Each data type has its equivalent version of True or False.
With Numbers 0 is the equivalent of False, and any other value is interpreted as being True.
And, oddly, an empty string is not the equivalent of False. But if you check for the truthfulness of an empty string it’ll return False. I’ll show you in an example later.
This will come in handy with conditional statements. But before we get into If statements we’ll look at our first real command print.
Remember that Python is a case-sensitive language. So it’s always “print” and never “Print.”
print is a simple command that does one thing, it “prints” whatever text you want to the screen. Or, in the case of PaintShop Pro, to the Script Output Palette.
print is a bit different from other commands in Python. Namely that when you usually call a command in Python you need to include all parameters for that command between parentheses. Take for example round(). round() takes two parameters, one is optional, a Real number and a precision number and returns a float.
And so now I’ve introduced you to round instead of print, let’s get back on track. As you can see the two parameters 2.623… and 2 are passed to the round command between (). This is normal. print doesn’t follow this convention though. In Python3 they change this but since PSP is still using Python 2.7 our version of print does not use parentheses.
print can take any number of parameters, just make sure to separate each with a comma. If the parameter is not text print will attempt to convert it to text automatically. But only if it’s not in the same parameter as some text data. Let me show you.
#This is an example of printing various data types
print "This is a string"
print 2.0, True, "String #2"
The output looks something like this.
>>> print “This is a string”
This is a string
>>> print 2
>>> print 2.0
>>> print 2.0, True, “String #2”
2.0 True String #2
>>> print int
Alright, now let’s look at conditions and the “if” statement.
Conditionals are any statement that produces a True or False result. This is done with the use of comparison operators. Similar to performing math with a variable or piece of data on one side of the operator, these operators always produce either True or False.
== – Equal to – checks if the two pieces of data are equal to each other.
!= – Not Equal To – checks if two pieces of data are different returns True if they are, False if they are the same
<> – Also Not Equal To – far as I know it’s just another way to write the same thing
> – Greater than – checks to see if the left side is greater than the right, returns True if it is
>= – Greater than or equal to – checks to see if the left side is greater than or equal to the right side
< – Less Than – checks to see if the left side is less than the right
<= – Less Than or equal to – checks to see if the left side is less than or equal to the right
So let’s go over a few quick examples
print 1 == 1 #should print True
print 1 == 2 #should print False
print 2 < 1 #should print False
print "1" 1 #should print True because "1" is not the same as 1
print 0 == False #should return True
print '' == False #should return False
if statements are an awesome tool in programming that allows you to perform actions based on conditions. For example, if you only wanted to perform an action if a person’s age was above a certain level you can use an if statment to check if my_Age > 32. And if that condition returns true, perform some additional steps.
To understand how if statements work I need to introduce you to indentation and code blocks.
A code block is a small section code that is separated from the rest of your script and only run under specific conditions. Code blocks are used with custom commands you build yourself, with if statements, loops, classes, etc. Any time we need a small bit of code to operate semi-independently from the rest of the script we use code blocks. And code blocks are defined by their indentation.
I’ve mentioned this a few times but Python really really cares about how much indentation you use, and what types. You can use as much or as little indentation as you want, but you have to be consistent. A tab may indent you the equivalent of 4 spaces visually, but a tab is not the equivalent of 4 spaces. So when you create code blocks by using tabs, then every line of that code block must be defined using tabs. If you try to mix and match tabs and spaces Python will throw a fit wondering where the extra indentation came from. Why did you change? Now it doesn’t know what you’re doing and it’s going to error out.
This is why, when setting up Notepad++ and VS Code we made sure to convert tabs into spaces. It just makes life easier.
The if statement is the most common way you’ll create code blocks in Python. And it’s the first step to making your scripts “smarter” in PSP. Here is the format of the if statement.
#begin code block
Let's say that you have a program that asks for input, and you want to react to their input.
#ignore this command, for now, it won't work in PSP, it simply gets text from the user.
cmd = input('Do you want to Proceed Y/N')
if cmd == "Y": #if :
print "Great! Glad to hear it" #indent is 4 spaces here
elif and else
It’s also possible to respond to multiple possibilities with the same if statement by using elif and else.
else is a catch all for any condition we don’t check. So if our first condition is false, else will run instead of the if statement. Take our example above, it only responds to an input of “Y”. But it doesn’t respond to “N” and it definitely doesn’t respond if someone inputs an incorrect value like “2.” If we were to add an else statement we could catch both of those examples and respond with the same thing.
cmd = input ("Do you want to Proceed Y/N")
if cmd == "Y":
print "Great! Glad to hear it."
print "That's fine, ending transmission." #notice my indent matches
So, now if the user types “Y” and nothing else it’ll print “Great! Glad to hear it.” But any other value, “N” or “2” or “What the heck is this?” will all respond with “That’s fine, ending transmission.”
Next let’s talk about elif.
elif is short for Else If. elif lets you check a secondary condition. What this means is that if the first condition is False you can check another specific condition before defaulting to else.
So let’s pick up our example again. Let’s say we want three different messages. if they say yes “Great! Glad to hear it.” if they say no, “Understood, ending transmission.” And if they say anything else, “I”m sorry, I didn’t catch that.”
cmd = input("Do you wish to Proceed? Y/N")
if cmd == "Y":
print "Great! Glad to hear it."
elif cmd == "N":
print "Understood, ending transmission"
print "I'm sorry, I didn't catch that."
Although this seems rather simplistic here in a PSP script this is very powerful as we can decide how to proceed based on various things. Such as whether or not there is an open image.
if App.TargetDocument == None: #no image is open for us to use
return #simply quit the script
Or if we want to know if we need to resize the image or not
if App.TargetDocument.Width < 800: #if image is wider than 800 pixels
#perform resize command here
I realize App.TargetDocument looks weird compared to everything else we've done so far but I promise I'll get to that soon.
As you can see, this one command offers us the ability to not just look at data, but respond to it. To make intelligent decisions based on what's going on.
Next tutorial I'm going to introduce you to the basics of custom commands or functions as they're called. How to make your own and how to import libraries of functions that other people have made to extend the functionality of Python.