In the previous video, I showed you how to write a very simple Python script. In this video, I will show you how to work with data types and structures. As you can see, there is a text written at the top of the shell. That is the result of running a script. There is a message that says, this is a puzzle that you have to resolve. How did this message come up?
Try to pause the video and think about it. When you think you have the answer, click on Play and see what code produces this outcome? The answer is that this text appears. If you type print, parenthesis, nd the text written between quotation marks. For this tutorial, we will use idle Python since we will write small codes in interactive shell, we will set a variable called that equals to four, then press enter. After that, type print and between parentheses and hit enter, the result will appear.
Basically the shelf knows what r is from the previous line. Because it is stored in memory, we can do more with this memory data. We can add values, multiply the result and raise it to power. Remember to use parenthesis to group the operations. If you do not use parenthesis, the result is different. There are some rules to set variable.
Rule number one is style. You can use words or strings. To name a variable. lowercase letter, uppercase letters, lowercase strings, uppercase strings, lowercase or uppercase with underscore capitalized words and mixed case rule to make sure to avoid some characters, like lowercase L, uppercase O, uppercase I, as a single character variable names, since in some fonts, these characters are indistinguishable from numbers one and zero. Use uppercase l instead of lowercase l do not use numbers or special characters as first character of the string Some words such as print, while four, and others are reserved identifiers, there are some considerations when you use functions, constants, classes and modules. However, I will go into detail in the next videos.
Now, we can ask ourselves, how could we change or reassign the value of the variable? Well, right and set the new value for it. That is equal to 10. Then press enter. When you do this, print the value of the variable to check it if it is changed its value. Python works with dynamic assignment.
Notice that in this case, the very Ah holds an integer. However, this could change to make a new assignment, Write hello world, and then press enter, then check if its value holds the string, hello, world. Even though you do, you don't need to write any declarative statement. To set the variable with a different data type. You have to be careful when you manipulate variables into different points of the program. Let's make another example.
Right, B equal five and press enter. Then write C equal three and press Enter. Now print the multiplication of B by see and press enter. You will notice that the Oh Come up the shell is the multiplication of B by C. Now write the underscore sign and press ENTER again, you will get the outcome of when you check a last time. So you can use the print function when you don't want to save the result on the operation. In the variable underscore.
There are different types of data in Python. This is Boolean, numeric sequence and set mapping. The last three data types contain the standard data structures like lists, tuples, dictionaries, strings, and sets. So in this tutorial, we are going to work with numeric type Python supports for Different numerical type, short or signed integers, long integers represented by decimal, octal, and hexadecimal floating point, real value and complex numbers. Keep in mind that all integers in Python three are long integers. Hence, there is no separate number type as long.
For example, when you set a variable to minus 10 that is an equal minus 10, which holds an integer. On the other hand, if you write b equal minus 10.0 B will hold a floating point value. However, if we compare these values, two times equal sign these and press enter. You will notice the result is true since they represent the same value. Now check if you assign minus 10 plus 0.0 G to C complex number and then you compare with a or b. What did you get?
You should get true again, even when the type of number is different, okay. Once you do that, change the value of b by nine, then divide a by b and press enter. The result is minus 1.11. Notice that the result is a floating point value, even though both value A and B integers. However, if you try to make this operation in Python two, the result will be minus two and an integer value. Since both are integers, this is a difference when you work with version two and version three.
On the other hand, if you divide B by A, the result will be minus 0.9. So what would be the result? If you do the same operation in version two, the answer is minus one, since it's an integer division, in other words, the result will be an integer without a fractional part. So keep that in mind when you work with version two or three. If you want to make an integer division in version three, you have to use the slash, notice that you will get back minus two as a result of the operation, which is the truncated result of the division. Besides, if you want to evaluate the remainder of the Division of 14 by three, you have to type in 14 percentage sign three.
In this case, the result would be to Python allows you to assign a single value to several variables simultaneously. That is right, x equal y equals that equal one, and then press enter. After that, print each variable you will notice that all hold the same value. Also, multiple variables can be assigned at the same line, which keeps your script compact. To make this write x comma y comma Zed equals one, comma four, comma seven, and press Enter. Now check if x is equal to one, y is equal to four, and Zed is equal to seven.
That's all for this tutorial. I hope you like it. And you want to watch more tutorials about Python and GIS