Learning python as an R user
‘Kaylea is very much R’
Introduction
As an avid listener to some tech podcasts, particularly ones that are run or feature people that I know, (pydataMCR and How AI Built this - find them on your favourite podcast player) I heard in one episode Liam (the host of How AI Built this) say “Kaylea is very much as R person … but she is learning python”. This is true, and in fact I feel like I’ve made quite good progress recently in using python that I thought it would be useful to share some of my tips and tricks for what has helped me.
Note that I have about 8 years experience in using R and previous to that I used Matlab for a couple of years so I am picking up python as an experienced coder which makes it easier to pick up a new language than learning code from scratch.
There’s a lot of debate on what is better. R or Python. Obviously I’m in the R camp, but that’s because I’m statistically trained and R (in my opinion) is the best tool for statistics. I think it’s worth knowing both, it helps with collaborating and helps keep you flexible for working on a range of projects. I also think it’s worth knowing at least one of the languages really really well. A lot of skills when learning to code can be transferred between languages.
I started my Python journey trying various courses online. I went to Pydata Manchester’s code evening once when I was determined to stick to a course and learn python. We had a python group at work for a short while as well. Mainly due to time restrictions I found that none of these methods really worked for me. In this blog I’m going to share what has worked for me in learning python. Many of these tips can be applied to other coding languages too.
Use python exclusively for a project
I found it difficult to find time outwith work commitments to learn python so instead I made python part of my daily work. I decided when I started a new project that I would use python for it as much as possible. There was a little bit of slack at the start of the project so the time was right and any analysis I required quickly I reverted back to R.
The solution I was implenting in python was something I was familiar with so didn’t require me to also learn some super complex algorithm at the same time. Using python on a work project forced me to use it so I had no excuse that I didn’t have the time.
If you don’t have the luxury of the time to work on a work project using python then find a personal project to work on - one that is interesting to you which will keep you motivated to persue it. Work with the goal of completing the project then learning python will be an added bonus.
Find a python buddy
Part of what made it easier to use python for a work project was that for a couple of months I had another colleague working on the project with me. She was a python user so it made sense for us to work in the same language. Having someone else work on the same project using python meant I could have a look at how they were structuring and running their code. It also gave me someone I could ask lots of questions and because she knew the project and what I was trying to do she could give me advice specific to the project.
If you don’t have a work buddy then find a local meetup and see if anyone is interested in working on a project with you. Meetups are starting to move online so find one that has a slack channel which means you don’t have the excuse of not being able to attend meetups.
Choose an IDE that you like
This might sound superficial but I’ve recently found VSCode and have set my themeing up to be a really nice purple colour and it’s really lovely to use. (Don’t @ me with your dark modes). When I first started learning python I was using Jupyter notebooks. I really dislike notebooks. I don’t mind them for writing documents to pass on to someone else when collaborating but for day to day code scribbles or for putting models into production I find notebooks a real hinderance, I think it’s running things in code chunks which frustrates me.
I found a way in Jupyter notebooks to run .py files in a console instead and that worked ok. The transition to VScode however has been a game changer. I’m still trying to figure out what all it can do but the short cut to put in function documentation is really useful. I also like that I can run python code from different scripts into one interactive python window which I couldn’t get on jupyter.
I learnt R before R studio and the day someone showed me RStudio changed my life forever. I think finding an IDE that you really enjoy using is key as it means you’ll look forward to coding in it.
Stack overflow and Google are your friend
A lot of my search history at the moment is “what is the python equivalent of the R function…”. I think learning one programming language once you already know another is a bit like learning a foreign language. I know what I want to do I just need the translation. Most of the time someone else has already asked the same question so it’s not a huge time burden.
If I’m trying to code something a bit more complicated I’ll revert back to R and try code up what I’m trying to do there first and then translate it into python. This also acts as a good way to check for errors in my python code that have crept due to lack of experience (I’ve been caught out quite a lot recently with drop_index()
).
Practise, Practise, Pratise
I think the key to learning anything is to practise. I’ve got R packages that I published a few years ago that have some C code what I had written and I’m a collaborator on a julia package. Neither julia or C are languages that I could code in today without a few weeks to refresh and that’s because I’m not using them regularly. Now that I use python more day to day I’m finding I understand how to use it more.
I’m still no expert when it comes to python, that’s going to take a few year, but I’m going to be continuing to use it day to day alongside R (as they both have their uses)!