Think Python AND R and not just PYTHON OR R: From NULL to String types


Continuing the saga of types in python and R, in this short post i will talk briefly about the NULL object (or NoneType in python) and how python and R handle the String type. I am using python 2.7.13 and R 3.3.3, both 64-bit on Ubuntu 16.04 and I am also using the free book called A Hands-On Introduction to Using Python in the Atmospheric and Oceanic Sciences by prof. Johnny Lin as a guide.

The NULL/None type

The NoneType in python and the NULL object in R are basically used to signify the absence of a value in many situations. It is often returned by expressions and functions whose value is undefined.  Because variables are dynamically typed, objects with value NULL can be changed by replacement operators and will be coerced to the type of the right-hand side. This object is also good to “safely” initialize a parameter, making sure to set the variable to a real value later.
For example, to initialize a variable to NULL (or None in python), and if later on the code tries to do an operation with the variable before the variable has been reassigned to a non-Null Type (or non-NoneType in python) variable, the interpreter will give an error.

In addition because NULL (None) is a special type (object) it has a relational operator to test if an object is NULL (or None). Using R:

a = NULL
a == NULL
is.null(a)
## logical(0)
## [1] TRUE

And using pyhton:

a = None
print a == None
print a is None
## True
## True

In python it is possible to use the common relational operator == but it is not recommended. One should use is instead of ==. More information about why here and an example here.

String variables

In python or R string variables (character vectors) are created by setting text in either paired single or double quotes.
Python uses the operator (+) to join strings together:

a = "Hello"
b = "World"
print a 
print b 
print a + b 
## Hello
## World
## HelloWorld

However R does not use the same operator. There are diffrent forms to concatenate strings in R and one of the simplest way is to use the functions paste() (by default includes a space caracther between the strings but this can be easily changed) or paste0().

paste(a,b)
paste0(a,b)
## [1] "Hello World"
## [1] "HelloWorld"

If you have any question, suggestion or opinion about this post please feel free to write a comment below.

Advertisements

One Comment

Add yours →

  1. Reblogged this on Stack of Post.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: