- 2021
- Aug
- 27
Learning a new programming language. Python Version.
So, you have learned another high or low-level programming language, and now you want to learn Python because it’s the trend (and will be for a lot of time) or because it’s sexy, or just because you want to learn it to solve some specific problem you might have.
Switching to Python might be a herculean task or an easy one, it will be up to you, you can make this experience a nightmare or a dream. If you want the former, I’ve listed some tips I wish someone shared with me when I switched over to python.
1- Be sure to configure your editor to a minimum indentation of 4 spaces.
Python doesn’t use brackets or any other symbols to define blocks of code, it uses indentation and it relies on it for everything. The recommended indentation is 4 spaces. Also, this helps to force you to maintain the code clean and readable, because of the line limit we’ll discuss later.
2- You can have multi-line statements using the line continuation character. ()
Having multi-line statements makes your code easier to read and follow. For example.
a = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9
3- Keep your line length to 79 characters and make sure your docstrings and comments lines are below 72 characters.
Backslashes or the continuation character () will help a lot to keep the line length under the 79 limit. This will help you reduce the complexity of your code, if something goes beyond this limit, you should refactor it as soon as possible.
with open('/path/to/some/file/you/want/to/read') as file_1, open('/path/to/some/file/being/written', 'w') as file_2: file_2.write(file_1.read())
4- Use Pyflakes and PyLint
PyFlakes is a static code checker, or as the authors call it “ A simple program which checks Python source files for errors”. Early bug check means early bug destruction.
PyLint on the other hand is a real-time code analysis tool, so you’ll get immediate feedback while typing. You get corrections for interfaces, modules, coding standards, etc.
5- Keep your code simple.
Reduce complexity whenever you can. For example, you have a piece of code that looks like this:
def some_handler(vars): for item in vars: if item != false: with open(os.path.join(root, item), 'r') as fin: try: file = fin.read() if "AWSTemplateFormatVersion" in file: data = json.dumps(file) except ValueError as e: raise SystemExit(e) return data
You can reduce complexity by redefining it like, yes the code is larger, but it’s a bit easier to read and understand. Also if you need to process the file somewhere else, you just have to isolate the function and use it whenever you need it.
def process_file(file):
return_data = ""; with open(os.path.join(root, item), 'r') as fin: try: file = fin.read() if "AWSTemplateFormatVersion" in file: return_data += json.dumps(file) except ValueError as e: raise SystemExit(e) return return_data
def some_handler(vars): for item in vars: if item != false: data += process_file(item) return data
6- The Zen of Python
Read and learn The Zen of Python, everything line of code should follow those principles, especially having clean and beautiful code, using namespaces often, and try to find the simplest solution.
The next set of tips are about learning any new technology or language.
7- Practice, practice, get some rest, and practice a bit more.
Practice every day, practice as much as you can, build small tools, games, when you think you are done, get some rest, and start over. Python may seem like an easy language, but it has some pitfalls that we will fall into, but with time and practice, we will learn how to avoid those.
8- Don’t try to be an expert, but look forward to it.
Try and learn and master everything you can about the language, but don’t make this an obsession, mastering a language take years, and sometimes the language dies before you master it.
Look, read, and learn the standard library, the most used third-party libraries, how big projects using the language, read other people code (look below), etc.
You can never understand everything. But, you should push yourself to understand the system.
- Ryan Dahl (Creator of Node JS)
9- Read other people’s code, but don’t copy-paste it.
Reading other people’s code is the best learning exercise you can do when not coding yourself. Try to understand what the code does, write down the basic algorithm pseudocode, try to replicate it without looking into the original code, correct errors, solve the problem yourself, and try to make a copy of the original functionality without using any of the original author’s code.
10- Teach.
As simple as that, teach, if you learned something teach it to someone, your coworkers, your peers, your friends, anyone. Use the ELI5 (Explain like I am 5) definition, because “If you can’t explain it in simple words, you haven’t learned it well”.
11- Ask questions.
If you are blocked, google, google again, ask in stack overflow, ask in a forum, as a coworker, ask a peer, ask anyone. Don’t be afraid to ask, but, elaborate on the correct questions, be specific with your doubts and comments, you’ll get better answers if you know exactly what you are asking for.
12- Have fun.
You are not in a race, build things you love, stop, take a look at your old code, refactor it, build weird things, break things, go wild, enjoy the trip while learning.
Try to find people with the same interests, work with them, find learning partners, participate in communities, attend talks, attend lectures, increase your network.
Best of luck