KRIGOLSON TEACHING
  • NEUROSCIENCE
    • NEUROSCIENCE 100 >
      • NEURO 100 INTRODUCTION
      • NEURO 101 ADVANCED
      • NEURO 102 AGING
      • NEURO 103 MEMORY
      • NEURO 104 DECISION MAKING
      • NEURO 105 LEARNING
      • Research Statistics
    • NRSC 500B / MEDS 470
  • Kinesiology
    • EPHE 245
    • EPHE 357
  • STATISTICS
    • BIOMEDICAL STATISTICS
    • MULTIVARIATE STATISTICS >
      • MULTIPLE REGRESSION
    • RESOURCES
    • R TIPS
  • MATLAB
    • THE BASICS >
      • Hello World
      • BASIC MATHEMATICS
      • VARIABLES
      • Matrices
      • Writing Scripts
      • PATHS AND DIRECTORIES
      • USER INPUT
      • FOR LOOPS
      • WHILE LOOPS
      • IF STATEMENTS
      • RANDOM NUMBERS
    • STATISTICS >
      • LOADING DATA
      • DESCRIPTIVE STATISTICS
      • MAKING FUNCTIONS
      • BAR GRAPHS
      • LINE GRAPHS
      • TTESTS
    • EXPERIMENTS: THE BASICS >
      • DRAWING A CIRCLE
      • DRAWING MULTIPLE OBJECTS
      • DRAWING TEXT
      • DRAWING AN IMAGE
      • PLAYING A TONE
      • KEYBOARD INPUT
      • BUILDING A TRIAL
      • BUILDING TRIALS
      • NESTED LOOPS
      • RIGHT OR WRONG
      • SAVING DATA
    • EXPERIMENTS: ADVANCED >
      • STROOP
      • N BACK
      • Oddball
      • Animation
      • VIDEO
    • EEG and ERP Analysis >
      • ERP Analysis
      • FFT Analysis
      • Wavelet Analysis
  • Directed Studies
    • Advanced Topics in Motor Control A
    • Advanced Topics in Motor Control B
    • An Introduction to EEG
    • Advanced EEG and ERP Methods
    • Neural Correlates of Human Reward Processing
    • Independent Research Project
  • RESOURCES
    • EXCEL
    • HOW TO READ A RESEARCH PAPER
    • HOW TO WRITE A RESEARCH PAPER
  • Workshops
    • Iowa State EEG Workshop 2018
  • Python
    • The Basics >
      • Setting Up Python
      • Hello, world!
      • Basic Math & Using Import
      • Variables
      • Matrices
      • Scripts
      • User Input
      • For Loops

Basic Math & Using Import​

When programming, whether it is for statistical analysis of for experimental design we will be doing a lot of mathematical operations. For example, in the console type:
2 + 2

Python will compute the answer for you and output it onto the screen.
Picture
Python has 7 basic arithmetic operators:

4 + 2 = 6
Addition

4 - 2 = 2
Subtraction

4 * 2 = 8
Multiplication

4 / 2 = 2
Division

4 ** 2 = 16
Exponent

4 % 2 = 0
Modulus (computes the remainder in a division)

4 // 2  = 2
Floor Division (computes a division, then drops anything after the decimal)

​These are useful, but what if we want to do some more sophisticated math, like logarithms and trigonometry?

Modules

A module in Python is a file that has a specific functionality. In our case, we are looking for more robust math operations. Python comes with a module called math which provides exactly what we need. To get access to the operations inside math, we have to include it in our environment.

Type import math into the console to bring in the math module. Now, we can use anything provided by math in our programming!

Try out a few of these functions from the math module by typing the following into the console:

math.exp(4)
Computes e to the power of 4.

math.log(4)
Computes the natural logarithm of 4.

math.sqrt(4)
Computes the square root of 4.
Picture
The math module is full of useful mathematical operators. The documentation provides a list of all of them here: https://docs.python.org/3.8/library/math.html

We will be using modules all the time to make our programming lives easier. They allow us to use the power of Python without writing all of the code from scratch.

Let's move on to the next tutorial to learn how to use variables in Python.
  • NEUROSCIENCE
    • NEUROSCIENCE 100 >
      • NEURO 100 INTRODUCTION
      • NEURO 101 ADVANCED
      • NEURO 102 AGING
      • NEURO 103 MEMORY
      • NEURO 104 DECISION MAKING
      • NEURO 105 LEARNING
      • Research Statistics
    • NRSC 500B / MEDS 470
  • Kinesiology
    • EPHE 245
    • EPHE 357
  • STATISTICS
    • BIOMEDICAL STATISTICS
    • MULTIVARIATE STATISTICS >
      • MULTIPLE REGRESSION
    • RESOURCES
    • R TIPS
  • MATLAB
    • THE BASICS >
      • Hello World
      • BASIC MATHEMATICS
      • VARIABLES
      • Matrices
      • Writing Scripts
      • PATHS AND DIRECTORIES
      • USER INPUT
      • FOR LOOPS
      • WHILE LOOPS
      • IF STATEMENTS
      • RANDOM NUMBERS
    • STATISTICS >
      • LOADING DATA
      • DESCRIPTIVE STATISTICS
      • MAKING FUNCTIONS
      • BAR GRAPHS
      • LINE GRAPHS
      • TTESTS
    • EXPERIMENTS: THE BASICS >
      • DRAWING A CIRCLE
      • DRAWING MULTIPLE OBJECTS
      • DRAWING TEXT
      • DRAWING AN IMAGE
      • PLAYING A TONE
      • KEYBOARD INPUT
      • BUILDING A TRIAL
      • BUILDING TRIALS
      • NESTED LOOPS
      • RIGHT OR WRONG
      • SAVING DATA
    • EXPERIMENTS: ADVANCED >
      • STROOP
      • N BACK
      • Oddball
      • Animation
      • VIDEO
    • EEG and ERP Analysis >
      • ERP Analysis
      • FFT Analysis
      • Wavelet Analysis
  • Directed Studies
    • Advanced Topics in Motor Control A
    • Advanced Topics in Motor Control B
    • An Introduction to EEG
    • Advanced EEG and ERP Methods
    • Neural Correlates of Human Reward Processing
    • Independent Research Project
  • RESOURCES
    • EXCEL
    • HOW TO READ A RESEARCH PAPER
    • HOW TO WRITE A RESEARCH PAPER
  • Workshops
    • Iowa State EEG Workshop 2018
  • Python
    • The Basics >
      • Setting Up Python
      • Hello, world!
      • Basic Math & Using Import
      • Variables
      • Matrices
      • Scripts
      • User Input
      • For Loops