Skip to Content
Use Gemini for Code Conversion
shortcut

Use Gemini for Code Conversion

by Thomas Nield
October 2023
Intermediate
5 pages
4m
English
O'Reilly Media, Inc.
Content preview from Use Gemini for Code Conversion

Use Gemini for Code Conversion

Here is a useful task for which LLMs like Google Gemini can be of assistance. Let’s say you inherited several R scripts in your workplace and you want to convert them to Python. With a little bit of prompting, you can save yourself some time and get your R code turned into Python code.

Let’s start simple. I have the following basic R code to declare and print a vector. I want to turn it into Python code.

s <- c('apple','red',5,TRUE) 

print(s)

I can prompt Google Gemini to convert it to Python as follows (note that I am pasting the code into a triple-ticks (```) block):

Prompt: Can you convert this R code into Python code?

```s <- c('apple','red',5,TRUE) print(s)```

Gemini: Sure, here is the Python code that converts the R code s &lt;- c(‘apple’,‘red’,5,TRUE):

s = ['apple', 'red', 5, True]
print(s)

The Python code is equivalent to the R code in terms of functionality. It creates a list called s that contains the elements 'apple', 'red', 5, and True. Then, it prints the contents of the list s.

This is a great start, but let’s say I wanted to use pandas rather than a plain Python list. I can continue to prompt and ask for this change:

Prompt: Awesome, can you use pandas to do that task?

Gemini: Sure, here is the Python code that uses pandas to do the same task:

import pandas as pd

s = pd.Series(['apple', 'red', 5, True])
print(s)

The Python code first imports the pandas library. Then, ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Use Gemini for Shell Scripts

Use Gemini for Shell Scripts

Thomas Nield

Publisher Resources

ISBN: 9781098162450