<class 'int'>
Introduction to Python
Python
is a high level, general purpose programming language 1
Popular programming language due to its easy syntax, large community, and extensive set of libraries
Used data science, machine learning, web development, application development, data visualization, and more
integers
, booleans
, floating points
, and strings
.Data Type | Example | |
---|---|---|
Number | Integer | x = 4 |
Long integer | x = 15L | |
Floating point | x = 3.142 | |
Boolean | x = True | |
Text | Character | x = ‘c’ |
String | x = "hello" or x = ‘hello’ |
Python | R | |
---|---|---|
Overview | High level, general purpose programming language | Language for statistical computing and graphics |
Advantages | -Production ready (e.g. deploying a model into a website or application) -Superior natural language processing libraries |
-Superior time series analysis and statistical libraries -CRAN has a more comprehensive screening process |
Disadvantages | -Less intuitve/clean visualizations -Less alternative packages |
-Less readability -Lacks robust image analysis libraries |
There are functional and syntax differences between R and Python.
For example, setting a variable in Python uses = while R uses ->
“The reticulate package provides a comprehensive set of tools for interoperability between Python and R”3
Core functions include:
Calling Python from R in a variety of ways: R Markdown, sourcing Python scripts
, importing Python modules
, and using Python interactively
Translating between R and Python objects (between R and Pandas data frames, or between R matrices and NumPy arrays)
Let’s load the recirculate package first
Once reticulate is imported, it is as easy at setting the chunk to use python with {python}.
Note: the variables created in your Python environment
will not be contained in your R environment
.
To get around this, we can pass the variable from one environment
to another.
Likewise with R:
Basic variable manipulation is not the only Python feature available. More advanced Python can be leveraged with the ability to import Python libraries
.
'/mnt/rstor/CSE_MSE_RXF131/cradle-members/casf/eib14/git/23-bootcamp-data-science-social-impact/topics'
script
[1] "/mnt/rstor/CSE_MSE_RXF131/cradle-members/casf/eib14/git/23-bootcamp-data-science-social-impact/topics"
These libraries
can be leveraged to do classic Python manipulations.
Python
: A high-level programming language known for its readability, versatility, and extensive library support, making it popular for web development, data science, scientific computing, and many other applications.
integer
: A whole number that can be either positive, negative, or zero (e.g., -3, 0, 42). It does not have any fractional parts.
boolean
: A data type that has only two possible values: True or False. It represents the logical concepts of true and false.
floating point
: A number that has both an integer and a fractional part, separated by a decimal point (e.g., 3.14, -0.001). Floating point numbers are used in Python and R to represent real numbers.
string
: A sequence of characters enclosed in either single (’ ’) or double (” “) quotes. Strings can include letters, numbers, symbols, and whitespace (e.g.,”Hello, World!“).
operator
: A symbol or keyword that performs a specific operation on one or more operands. Examples include + (addition), - (subtraction), * (multiplication), and == (equality comparison).
function
: A block of reusable code designed to perform a specific task. Functions can take in parameters (input values) and return a result.
index
: The position of an item in a sequence, such as a string, list, or array. In both Python and R, indexing starts at 0 for the first element.
environment
: In the context of programming, it refers to a space where variables, functions, and other objects reside and can be accessed or modified. In R, environments are especially important and are used to manage the scope of variables and functions.
script
: A file containing a sequence of instructions written in a programming language. When run, the instructions are executed in order. In Python, scripts typically have the .py extension, while in R they might have the .R or .Rscript extension.
library
: A collection of pre-written code, functions, or routines that can be used to perform specific tasks or operations in a program. Libraries help in reducing the amount of code a developer needs to write by providing standardized solutions. In Python, libraries are often referred to as “modules” and are imported using the import statement. In R, libraries are packages of functions and data sets, and they can be loaded into the session using the library() function.
What is Python? Executive Summary. Python.org.
R vs Python: R or Python? - Reasons behind this Cloud War. Analytics Vidhya