Any respectable programming tutorial starts with a hello world example, so let’s get that out of the way!
If you haven’t opened the Python console yet, do it now: Plugins | Python console.
The Python console is an interactive command line interface. This means that you can type in a line of code and – once you press Enter – the code is executed.
In our hello world example, we use the print function to output the text “Hello world!” in the console. The corresponding code is:
print("Hello world!")
We are calling the print function with one parameter: the text (also known as string) value “Hello world!”.
When you type this command into the Python console input field, you’ll see that it shows text in different colors. This is called syntax highlighting, and the colors make it easier to make sure that what we’re typing is correct. In addition, when typing closing brackets, it highlights the corresponding opening bracket. This is very useful, since missing brackets are a common programming mistake.
If you have seen some Python code in the past, you might have seen print statements such as print “Hello world!” without brackets. This syntax is not valid in Python 3 anymore and would cause this error: SyntaxError: Missing parentheses in call to ‘print’
After pressing Enter, you will see that our code outputs Hello world! in the console:
These are the basics of using the Python console and how to execute a function with a string parameter.
That’s enough non-GISy basics for now, let’s get our hands dirty with some spatial data!
PyQGIS 101 is a work in progress. I’d appreciate any feedback, particularly from beginners!
I am going to follow your course here, However, as a beginner python wannabe I am looking for an explanation as to why I need to use programming, what is the advantage of using programming vs using the QGIS tools. So some context as to why would be useful.
Hi Bruce. There are tons of articles and videos on why one should learn programming. Therefore I’m not planning to elaborate on this topic here but rather spend my time on explaining PyQGIS.
This is awesome am so exited
Quick walk-through. Thanks ma’am.
Moving to the next lesson.
Great, let me know how it goes!
Dear Underdark
Thank you soooooo much…. It is very useful and help me to learn python scripting in QGIS. I am looking for the next posting and session.
Gruss aus East Java Indonesia
arieflh
Dear Anita,
I just wanted to thank you cordially for loading up this tutorial of PyQGIS!
While I know Python relatively well, I have trouble finding all the necessary functions and procedures I need for QGIS with sufficient explanation.
It is difficult to assemble all the bits and pieces until something really works out-
Your tutorial is a great support!
regards,
Ferdinand
I’m a beginner in QGIS and Python and i’m really enjoying your work! Greetings from Brazil (still breathing).
Hello Mam,
I have just started using Qgis. I am professor in college. I have just one problem with software that is i am not able to install any plugin in my Qgis software. Whenever i go to plugin and search for any plugin, nothing shows up. When i open settings there it shows missing python file. I have tried everywhere to find the solution on Google, YouTube but i got nothing. I hope you could help me.
Dear Mandeep, I strongly recommend to contact the QGIS user mailing list or the Telegram channel. When asking for support here, please include your operating system, the QGIS version you installed, and which installer you used. This information is vital to start finding out what may be wrong.