Tips for the Python beginner
Recently I started studying again and although I dabbled in Python before, it never really clicked. Books went from basics to write your own program much too quickly, so here are my tips for a beginner if I had to start all over again.
- Learn to recognize a function
To be able to read any piece of code, you will have to know what a function looks like. A function is always recognized by the name of the fuction followed directly by brackets, for example: print(“Hello world”). This little example will print “hello world” on your screen without the “”.
- Learn exactly what a variable does
To be able to explain a variable will help you in making it for you to read the code later on. A variable is a place in the memory (a box in the memory that you can fill, as my teacher explained) and given a name for you to make it easier to read what that specific variable does. For example: test = 1 (in this case the varabiable name is test and has been given the integer 1 to hold on to.
- Learn to read a program line per line as if it was python that was executing it
This is one of the most valuable skills my teacher imparted on me in the brief time that I was instructed by him, this and explaining a function and variable so clearly has helped me a lot in reading python code. You have to know to which line the code will jump back to when executing a while-loop for example, so that you know if you are going to have an infinite loop
- Test and test often
This is one I do all the freaking time. I will put in print-functions at critical points so I know what my program is doing when I am testing it. This is something that my fellow students don’t think about when writing code, but something I have been doing ever since I started programming. I will write a few lines of code and test if it works. I started programming in the time that debuggers weren’t that advanced yet, so it was better to test your code often, and that’s what I keep doing. Test and test often.
- Learn programming logic
This besides knowing how to read code and all the other basics, this is the most important one. This will help you not only in Python but in other programmign languages as well, as this is the very, very basic thing you have to understand before you can program. Programming has a certain logic to it. First you need to do a. then b. then c. When helping fellow students out I see that this is the part they really struggle with. Anybody can learn what functions are, and how to set a variable, but programming logic is something you only get with doing a lot or just getting it. When I get an assignment I read the assignment and then I start with the ending. Then I look over what the program has to ask of the user, thus the very start of the program. Then the thing in the middle so the program can work with the things the user has given and the end result is outputted correctly. When I don’t have a clear view of what my program is going to look like when it is outputting, I will not start writing my program.
- Think of your colleagues
When you program in a company or on an open-source project, you are not going to work on the program alone. So it’s best practice to write the code in such a way that it is clearly defined what a program does so your colleague doesn’t need a degree in cryptography to know what the code does. Now with the programs we have to write even complete newbies that have never programmed can read the code I have written and know what it does, even if they don’t know what a variable is or all the other intricacies of code.
- Better to write readable code than code in the least amount of lines
There are only a few cases where the least amount of code or characters is better. So it’s no use to make your code in the least lines possible if you can do it in a few lines more and make it more readable. Yes, there is a certain joy in coding in the least amount of lines possible, but it’s much more fun to write a program that just does what it does and is readable by anyone who has a look at your code. It’s not only fun for your colleagues down the line, but also good for yourself when you read your code again 6 months down to line to add something to it and not being able to figure out your own logic.
Every post is written first in scrivener 3, which you can get a 30 day free trial of here at literature and latte.