Learning Python as an R Programmer (part 1)
While I have used Python, I would like to grow stronger in its use as I have primarily been a SAS and R programmer in my career (with SQL throughout). This post communicates some of my learning.
The goal should be, learn Python in 10 years.
-- Guido Van Rossum, original Python designer, quoting a comedic blog poster whose name eluded him during a November, 2022 Lex Fridman podcast/video
Using Python Within R Studio
The reticulate R package makes practicing the use of Python from within R Studio quite convenient. I found that I had to install the development package of reticulate via the following:
remotes::install_github("rstudio/reticulate")
One can then use the following in R Studio:
reticulate::repl_python()
I have had the most success installing Python packages using py_install.
And once imported, I have the pandas-datareader functions available.
Coursera's "Crash Course on Python" by Google
Python - Cannot Change String Object by Index
Google’s excellent Crash Course on Python (Coursera), taught by Site Reliability Engineer Christine Rafla, featured the following in Week 4’s “Creating New Strings” lesson. One cannot change particular characters in a string object by index, unlike in R. Python springs “TypeError: ‘str’ object does not support item assignment”.
R – Can Change String Object by Index
On the other hand, we can assign a letter by index using R’s substr() function. substr(message, 3, 3) = "l" makes the third character of the message object a lower case L.
Immutable Objects - R and Python
I read some of the famous Hadley Wickham’s 2010 “Mutable Objects in R” to help me understand the computer programming paradigm of mutability. Wickham’s abstract communicated his creation of a package, mutatr, to support mutable objects, implying that R uses immutable objects normally. Other sources also supported that R generally uses immutable objects, like Python.
Code that uses immutable objects tends to be easier to reason about because effects are local. This is a big advantage: it is easier to understand how code works […] The disadvantage of immutability is its lack of modularity: since we can not modify objects directly, each function needs to return a copy of all objects that it modifies.
-- Hadley Wickham in Mutable Objects in R
R quietly copies the object into RAM, and then changes the copy, when functions like substr() are used so it only appears to be using mutable objects. One can use tracemem() to see that the object name now refers to a different location in memory (RAM).
By the way, as Crash Course on Python instructs, we can use a Python technique called string slicing to create a new object with the same name but a different string value.
Strings and IN operators - R and Python
Finally, referencing another example from Crash Course on Python, both R and Python have IN operators. Both use a lower-case in and only R surrounds those letters with percentage marks (%).
My blog
More of this content, including a use of ChatGPT, can be found in my blog posts from which I sampled to produce this LinkedIn post:
- https://rickpackblog.wordpress.com/2022/12/11/learning-python-as-an-r-user-repl_python-from-the-reticulate-package/
- https://rickpackblog.wordpress.com/2022/12/25/learning-python-as-an-r-user-strings-and-immutable-objects-and-chatgpt-win-1/