Tips and tricks#

You are now already well equipped to write more complex scripts and algorithms: you know (almost ;)) everything you need to know about the syntax, but you might be a little lost in all the possibilities that the Python language is now offering to you. Which tool should I use to do this and that? What was this function signature again? are very common questions, and it is normal to feel a bit overwhelmed.

This chapter will give you some brief tips to help you find answers to these type of questions.

Batteries included#

You may have read this expression (“batteries included”) during your research about the Python language. It is widely used to indicate that if you choose Python, it is not only for its syntax: it is for all the tools that come with it. And there are PLENTY. Spend a minute or two to scroll through the list of tools available in the standard library. The standard library is available per default with any Python installation. It is very robust and provides the core functionality of the language.

As you will see, there is not much for us scientists in this list though (no plotting library for example). This is why third party packages were developed. And there are so many of them that one can easily be overwhelmed.

Getting help#

In order to become a good programmer, nothing is more important than being able to find help, either online or from your friends and colleagues.

Online resources#

These days, getting help usually means asking AI, but there are also other options. For example, if you want to know how to use a specific function, you could just look up the details in the documentation instead (maybe think also about the environmental footprint of asking AI versus just opening the documentation on the web). Online tutorials can provide structured explanations that target learning rather than quickly looking up a solution and may guide you towards a deeper understanding. So try combining different tools.

  • The official Python documentation is definitely the best place to look for everything related to the standard library. It is also THE reference, and as such can sometimes be too detailed or complex.

  • Third-party packages typically come with good documentation as well and often also tutorials, for example, NumPy or pandas.

  • RealPython provides countless tutorials on different Python topics. Most of them are freely accessible.

  • Stack Overflow was probably the major resource for answering technical and other questions before AI. Their system of rewards for well formulated questions and correct answers clearly makes finding the right solution easier than in many other forums. You can still find the answer to many of your coding problems there if you just perform a quick web search.

  • When using a web search, use as few words as possible, but put in all the keywords that make your question unambiguous. Here is the example outcome of two google searches:

Ask questions the smart way#

If you want to ask a question on Stack Overflow or somewhere else (e.g. by sending an email to your teacher), please spend some time reading this resource or for a more Python-specific summary listen to this short tutorial (or read the transcript if you do not want to sign up). Believe me, this will help you to get better answers!

Getting help VS learning#

To be honest, without an internet connection, most of us are rather bad Python programmers. You are not expected to learn all the Python commands by heart. You are actually encouraged to use the internet to solve the weekly exercises.

However, there is a difference between “getting help to find a way how to realize a specific task” and “asking someone to do the homework for me”. The line between the two might be subtle, but you probably know when you cross it: at the moment you stopped using your brain and simply copy/pasted code from AI, you crossed it.

Debugging code#

It is impossible to write bug free code in one go (just accept it and get used to it). There will always be mistakes. Syntax errors and other obvious problems (e.g. NameError) will most often be picked up by your IDE’s linter. The other “easier ones” (e.g. TypeError) are also likely to be corrected after a first couple of test runs of your code. But then, sometimes, the error is not obvious: the code runs, but does not produce the expected output. What to do in these cases?

  • Use print statements (bad): with this method, you can inspect variables at run time by printing their values. It is a quick and dirty way to debug, and should not be used too often because it is error prone (indeed, by adding print statements you modify your code, might forget about them afterwards, and they are often not very useful). If you need more than 3 or 4 print statements to track the source of an issue, it is time for opening a debugger.

  • Use a debugger (good): a debugger allows you to interrupt a program’s execution and give you access to the program’s state at a predefined location (called a break point). It is far more useful than print, because it allows you to explore many variables at once. Positron comes with a debugger included.

  • Ask AI (good): AI can be a great resource to debug your code. Just make sure to thoroughly test the suggested code and make sure that you understand it (see below).

  • Go for a walk (very good): think about something else for a while and come back with a fresh, open mind.

  • Talk to your colleagues about your problem (very good): they might see the issue (or typo) from a different angle and detect the problem easily.

  • If you have no one around to talk to, talk to a duck (very good).

Coding with AI#

Artificial Intelligence, in particular large language models (LLM), has become a key resource for programming in recent years, which you can leverage to help you learn a programming language, to improve your programming skills, and for assistance with your everday programming tasks. AI-assisted coding covers nowadays the full range from asking an AI ChatBot a question to fully autonomous code design and development by the AI agent:

  • Chat-based coding: You, as the developer, put your question or task into an AI ChatBot, e.g., ChatGPT, Claude Chat, or Google Gemini, and copy and paste the code snippets from the answer into your code files. Chat agents are currently often accessible without a subscription. The University of Innsbruck also offers Academi AI to employees and students, where you can choose from a number of LLMs.

  • Agentic coding: In contrast to chat-based coding, the AI agent has access to your code (e.g., via your IDE or via the terminal). It plans the implementation when given a task, writes new code or edits files, executes the code, and observes the result. If the result does not meet the goal, it iterates through multiple cycles. While the code is largely written by the AI agent, the developer does still take full responsibility for testing and reviewing the code and making sure that it performs correctly even under less liquely scenarios, i.e., the developer needs to fully understand the code. Agents include Claude Code, Gemini CLI, and GitHub Copilot. These agents are typically not freely available, but require a paid subscription.

  • Vibe coding: In contrast to agentic coding, the AI agent works more or less fully autonomously. The developer does not review the code properly, but only performs some tests and lets the AI agent adjust the code if necessary. This raises of course some concerns regarding code quality and maintainability, security, and who is accountable for potential problems.

Some caveats#

  • Code quality: Always remember that the results are not necessarily correct even if they seem plausible. This means that you should not rely on AI generated code completely. Make sure that you test the code thoroughly and that you understand what it is doing. If you don’t, there is no guarantee that it does what it is supposed to do. Ultimately, you as the developer are responsible for the product. This means that despite the advances in AI, you still need to learn how to program. Also, while you may save time not having to write code yourself, you need to spend this time on testing and analyzing the code to ensure a high-quality product.

  • Data protection: Even if you run an AI agent on your computer (e.g., using the Claude Code extension in Positron), remember that the AI agents usually send code and information to external servers for processing.

  • Environmental footprint: You should also consider the environmental footprint of AI, which uses large amounts of energy and water, see e.g., this story from the UN environment programme.

Please also have a look at the just recently published guidelines on the use of AI of the Faculty of Geo- and Atmospheric Sciences. These are relevant for all your coursework and your thesis at the University of Innsbruck.