learning python outloud

By Jen Richmond

July 4, 2024

When you are exploring a far off land and only know a tiny bit of the language they speak there, you ALWAYS carry a little dictionary with commonly used phrases translated from the language you speak into the other language. It is important to know how to ask someone where the toilets are while you are travelling!

I have just started learning Python with Posit Academy in the lead up to #positconf2024 and I am trying to approach in the same way I would if I was learning French. A dictionary that helps me link functions I know in R to new functions I am learning in Python could be handy.

Linking new concepts to old concepts is also a useful learning strategy. Research in Psychology tells us that memory is relational; the brain represents memories as networks of representations. If you can link something that you are trying to learn to something you already know, you are much more likely to remember that new thing into the long term.

Art credit: China Blue Art, Memory Network I

Figure 1: Art credit: China Blue Art, Memory Network I

Of course, there are probably a million R to Python dictionaries on the internet; why am I creating a new one for me?

That’s because research in Psychology has shown that generative learning and retrieval practice are more effective strategies for remembering things into the long term, than are learning strategies that involve passive review of materials that were created by someone else.

To create a dictionary of functions as I learn new things in Python, I need to retrieve the equivalent function in R from memory and actively evaluate the similarities and differences between the Python and R versions. That process of retrieving and using the concepts I already know, strengthens both my knowledge of R, and links my new Python understanding to it.

So I am starting a googlesheet to keep track of new things I am learning how to do in Python and their R equivalents. Maybe I can read that googlesheet in here using the googlesheets4 package and make it display in a searchable table using gt.

cheat_sheet %>%
  gt() %>%
  tab_header("My R vs Python cheatsheet") %>%
  opt_interactive(use_search = TRUE)
My R vs Python cheatsheet
how to
R
python
differences
assign variables
my_name <- Jenny
my_name = Jenny
NA
install packages
install.packages("tidyverse")
pip install numpy
NA
load packages
library(tidyverse)
import numpy
use import numpy as np if you want to use an alias to type less
use functions from a package
clean_names(data) OR janitor::clean_names(data)
numpy.sqrt(data) OR np.sqrt(data)
once you load a package in R with library(packagename), R just knows to use functions from that package. In python, you need to tell it what package each function comes from- requires namespace
use functions that are built in
round(x, digits = 3)
round(x, ndigits = 3)
no need to use namespace in python for functions that are built in (https://docs.python.org/3/library/functions.html)
get help
?name_of_package_function
help(name_of_package_function)
NA
random number between 0-1
runif(1)
random.random()
requires that you have loaded the random package with import random
random number between range
runif(1, min = 1, max = 100)
random.ranint(1,100)
NA
get frequencies
janitor::tabyl(data) %>% arrange(n)
pd.value_counts(data, ascending = True)
pd refers to pandas (will only work if you have said import pandas as pd, True works but TRUE does not
1–9 of 9 rows
Posted on:
July 4, 2024
Length:
7 minute read, 1477 words
See Also: