Basic Python - Variables and Comments


Learn about the basic Python syntax for declaring variables and assigning values to them. Also learn about adding comments to your scripts.

Video recorded using: Ignition 8.0


(open in window)

[00:00] In this video, I'd like to go over the basics of the Python scripting language, so that you can get an idea of how you can use it within Ignition. As you can see, I'm using the script console to go over some code that I've already written. The first thing you'll notice is that each of the lines of code here have a pound symbol at the front. These are called comments. Comments act like little messages within your code, they don't affect the code's outcome but they do provide a way of reminding either yourself or someone else how this code works. To add a comment to your code simple place the pound sign at the front of the line that you want to be a comment, like these first two lines here. You can use comments throughout all of your code as many times as you would like. You can also comment and uncomment multiple lines by pressing Control + forward slash, you'll notice when I do it, it removes the pound signs at the front of each line, pressing Control + forward slash again adds those pound signs back and makes them a comment again. Next we have variables. Variables allow us to assign a value to particular name. Variable names can be whatever you want. You'll notice on lines five and six, I have variables named X and Y and then I have on line seven a variable called user selection, and on line eight a variable called newVar. In Python you also don't have to declare the data type of the variable. The data type of the variable is determined by whatever value you assign to it. So in this case my variable X is an integer because I'm assigning an integer to it. Variables can also change date type really easily. For example my variable X, if I were to later on assign it a value of 5.5, it now becomes a float. Strings do need to be in quotes but you'll notice on line seven and eight, it doesn't matter which type of quotes I use either single or double quotes. So here on line seven I have a string that says, this is a string within double quotes and on line eight I have also a string in single quotes. Both of these are considered string values. Integer, floats and strings are some of the most basic data types within Python but there are other more complex data types that we'll go over in the next video.

You are editing this transcript.

Make any corrections to improve this transcript. We'll review any changes before posting them.