Getting Started:
IDEs, Operations,
Basic Types of Data

Enrico Toffalini

Where to run Python: terminal / bash

Where to run Python: Spyder (full IDE)

Where to run Python: RStudio (full IDE)

Where to run Python: Google Colab

Where to run Python? Other options…

  • Visual Studio (VS) Code: Full general-purpose, language agnostic code editor, maybe the most used environment for Python today; by Microsoft, but free
  • PyCharm: powerful IDE dedicated to Python; paid professional licence, but also free version available
  • Jupyter Notebook: ideal for reports and markdown; very similar to Colab but run locally; enhanced IDE-like version: JupyterLab; free
  • Thonny: super-simple interface IDE; free

Let’s Test the Environment

Let’s run a few commands in your IDE or Colab to see if it works…

IDEs = ["Spyder", "PyCharm", "RStudio"]
IDEs.append("VS Code") 
print(IDEs) 

Let’s try to install and use a package:

!pip install numpy # if not already installed

import numpy as np # import and alias before using

N = 20
x = np.random.normal(0, 1, size=N)
y = -0.5 + x*0.3 + np.random.normal(0, 0.8, size=N)

print(y.round(3))

(The whole thing of installing, loading packages, and using functions is much like R, as previously seen)

Use basic operations

Python as calculator: basic operators

Operator What it does Example Result
+ Addition 5.4 + 6.1 11.5
- Subtraction 9 - 4.3 4.7
* Multiplication 7 * 1.4 9.8
/ Division 9 / 12 0.75
// Floor division 13 // 4 3
% Modulus 13 % 4 1
** Exponentiation 15 ** 2 225

(Math constants like pi might require modules, e.g., math.pi)

Use basic operations

Python as calculator: functions

Unlike R, many math functions require importing a module: import math

Function What it does Example Result
abs(x) Absolute value abs(4.3 - 9.8) 5.5
round(x) Round to nearest integer round(1.7384) 2
round(x, n) Round to n digits round(1.7384, 2) 1.74
math.sqrt(x) Square root math.sqrt(176.4) 13.28157
math.exp(x) Exponential function (\(e^x\)) math.exp(2.2) 9.02501
math.log(x) Natural logarithm, base \(e\) math.log(9.025013) 2.2
math.log(x, b) Logarithm, base \(b\) math.log(10, 2) 3.32193

Relational operators

age = 20
Operator What it does Example Result
== Equal to age == 18 False
!= Not equal to age != 18 True
> Greater than age > 18 True
< Less than age < 18 False
>= Greater than or equal to age >= 18 True
<= Less than or equal to age <= 18 False

Logical operators

basic logical operators are and, or, not, just natural language words!

age = 20
Operator What it does Example Result
and and age > 25 and age < 60 False
or or age < 25 or age > 60 True
not not not age < 18 True

Logical operators

however, when dealing with arrays (more on them later!) you need to use elementwise operators, as basic ones will not work!

ages = np.array([20, 18, 5, 52, 46, 29, 39]) # more on this later!
Operator Description Example
& elementwise “and (ages > 18) & (ages < 60)
| elementwise “or (ages < 18) | (ages > 60)
~ elementwise “not ~(ages < 18)

These elementwise operators are much like “&”, “|”, and “!” in R

Basic types of data

numeric and logical

So far, we have already encountered at least two types of data:

  • Numeric: e.g., 20, 11.5, 13.28157;

  • Logical / Boolean (i.e., True, False).

Like in R, numeric data could actually be float (“floating-point”) that is with decimals like 11.5, and int (“integer”) like 20

Unlike R, there is not need to specify “L” for integers

Basic types of data

strings (text values)

Another very important type of data is string

This is much like R. Python uses strings to represent text, that must be enclosed in quotes (' ', or " "), like this:

myName = "Enrico"

And you may even perform operations with strings like:

myName == "Bob" # is my name equal to Bob?
False
myName != "Bob" # is my name NOT equal to Bob?
True
myName > "Bob" # is my name larger than Bob? (yes, alphabetically!)
True

Basic types of data

Lists and Tuples: ordered collections

Lists are ordered, mutable series of elements that can be of mixed types

myList = [10, 3.14, "Enrico", True, [8, 7.2], 11]

Tuples are exactly the same, just immutable (cannot update their values!)

myTup = (10, 3.14, "Enrico", True, [8, 7.2], 11)

Both can be indexed by numeric position using [ ]:

myTup[0] # first element
10
myTup[-2] # second last element 
[8, 7.2]

Basic types of data

Dictionaries: unordered collection of key-value pairs

student = {  "name": "Jim",
             "age": 25,
             "courses": ["BasicsR", "BasicPy", "Simulations"]
          }

Cannot be indexed by numeric position, but by key:

student["name"]
'Jim'
student["courses"]
['BasicsR', 'BasicPy', 'Simulations']
student["age"] ** 2
625
student["courses"][1] # but you can index a list inside a dict
'BasicPy'

Basic types of data

inspect variables types

Python has the built-in type() function to inspect the type of any object:

myName = "Enrico"
isProf = True
coursesTaught = ["GenPsy","Olfact","BasicsR","BasicsPy"]
nCourses = len(coursesTaught)

# see objects types
type(myName)
<class 'str'>
type(isProf)
<class 'bool'>
type(coursesTaught)
<class 'list'>
type(nCourses)
<class 'int'>

Basic types of data

inspect variables types

You may also inquire data type directly with functions is.*:

isinstance(isProf, bool)
True
isinstance(coursesTaught, bool)
False
isinstance(nCourses, int)
True
isinstance(nCourses, float)
False
isinstance(myName, str)
True

Python has no built-in missing data type like NA in R. None can be used but is not a type and operations may incur errors. Libraries like numpy and pandas have their own np.nan and pd.NA missing value markers that are more like NA in R.