Building Blocks

Husker POWER survey

  • Link sent out via Canvas/Email

The Husker Student POWER survey is currently open to first year and new transfer students.

The survey consists of 12 yes/no statements about academic, social, financial, and emotional well-being topics.

Staff and instructors with expertise use the survey responses to provide support and resources to students whose responses raise concerns.

Take a minute and complete the survey if you haven’t already!

Variables

  • Declare: set aside a space in memory for a variable

  • Assign: Set the value of a variable

In modern languages (R, Python, etc.) these two steps are usually done simultaneously

x <- 3 (R)
y = "hello world" (Python)

Naming Things

  • Variable names can’t
    • start with a number
    • start with a symbol (_ is ok in Python)
    • include . (Python)
    • be a reserved word
_x <- 3
Error: <text>:1:2: unexpected symbol
1: _x
     ^
3x <- 3
Error: <text>:1:2: unexpected symbol
1: 3x
     ^
if <- 3
Error: <text>:1:4: unexpected assignment
1: if <-
       ^

Naming Things

  • Variable names can’t
    • start with a number
    • start with a symbol (_ is ok in Python)
    • include . (Python)
    • be a reserved word
x.y = 3
Error: NameError: name 'x' is not defined
$y = 3
invalid syntax (<string>, line 1)
3y = 3
invalid decimal literal (<string>, line 1)
if = 3
invalid syntax (<string>, line 1)

Naming Things

When you create a variable:

  • Choose descriptive names
    • Match the equation/formula (if math)
    • May want to include units length_mm
  • Avoid single character names where possible
    c in particular is a bad name in R!
  • Balance length and clarity/information

Basic Variable Types

  • Integer - 2, 6, 109243
    • R calls these ‘integer’, Python calls them ‘int’
  • Floating point - 3.1415, 2.7, 9.0
    • R calls these ‘double’, Python calls them ‘float’ 1
  • String - “ABCDE”, “ ” , “Eddie is a Jack Russell Terrorist”
    • R calls these ‘character’, Python calls them ‘str’
  • Boolean - True, False
    • R calls these ‘logical’, Python calls them ‘bool’

Testing Types

Use a function to test if the type of y is xxx

R: is.xxx(y)

Python: isInstance(y, xxx)

is.logical(3.14)
[1] FALSE
is.character(3.14)
[1] FALSE
is.character("3.14")
[1] TRUE
is.integer(3.14)
[1] FALSE
is.numeric("pi")
[1] FALSE
is.numeric(pi)
[1] TRUE

Testing Types

Use a function to test if the type of y is xxx

R: is.xxx(y)

Python: isinstance(y, xxx)

pi = 3.14159
isinstance(3.14, bool)
False
isinstance(3.14, str)
False
isinstance("3.14", str)
True
isinstance(3.14, int)
False
isinstance("pi", float)
False
isinstance(pi, float)
True

Implicit Type Conversions

R and Python will both try to work with types that don’t match… to a point

v1 <- 1L + 2.0
v1
[1] 3
is.integer(v1)
[1] FALSE
is.double(v1)
[1] TRUE
v2 <- TRUE + 3L
v2
[1] 4
is.logical(v2)
[1] FALSE
is.integer(v2)
[1] TRUE
is.double(v2)
[1] FALSE
v3 <- "3" + 4
Error in "3" + 4: non-numeric argument to binary operator
v4 <- "3" + "4"
Error in "3" + "4": non-numeric argument to binary operator

Variables are converted to the least-complicated/least memory intensive compatible type.

Implicit Type Conversions

R and Python will both try to work with types that don’t match… to a point

v1 = 1 + 2.0
v1
3.0
isinstance(v1, int)
False
isinstance(v1, float)
True
v2 = True + 3
v2
4
isinstance(v2, bool)
False
isinstance(v2, int)
True
isinstance(v2, float)
False
v3 = "3" + 4
Error: TypeError: can only concatenate str (not "int") to str
v4 = "3" + "4"
v4
'34'

Python will concatenate (smush together) strings using +

Variables are converted to the least-complicated/least memory intensive compatible type.

Explicit Type Conversions

You can force things to be converted from one type to the other as well.

Python

int("3")
3
int(3.14)
3
int(3.84)
3
bool(3.0)
True

R

as.integer("3")
[1] 3
as.integer(3.14)
[1] 3
as.integer(3.84)
[1] 3
as.logical(3.0)
[1] TRUE

Functions

  • A function is a sequence of commands that performs a task

  • Functions (sometimes) take arguments and (sometimes) return values

  • We call a function by running code with the function name and values:

The `lm` function in R. The text 'lm(formula = y ~ x, data = mtcars)' is shown in blue, and each part of that sequence is annotated with additional information. A box around 'lm' is accompanied by text that says lm = linear model, function name. Boxes around formula and data have an annotation indicating that these are parameter names. Boxes around 'y ~ x' and 'mtcars' indicate that these are parameter values. The parentheses immediately after lm and at the end of the line are annotated with the information that function calls have parentheses around the arguments. The comma between 'y ~ x' and 'data' is annotated with the information that arguments are separated by commas.

Function anatomy

Functions

  • A function is a sequence of commands that performs a task

  • Functions (sometimes) take arguments and (sometimes) return values

  • We call a function by running code with the function name and any necessary arguments

The `lm` function in R. The text 'lm(formula = y ~ x, data = mtcars)' is shown in blue, with orange boxes around 'formula = y ~ x' and 'data = mtcars'. Orange text above the boxes reads 'arguments = values you pass in to the functions', and text below the boxes reads 'arguments may be named or unnamed'.

Function arguments

Software Libraries

  • We often group commonly used, related functions into packages (R) or modules (Python)

  • You install a package with install.packages("pkgname") (R) or pip install pkgname (Python)

  • Packages/modules must be loaded before they can be used.
    library(pkgname) (R)
    import pkgname (Python)

  • In Python, you access the functions in a package by pkgname.funcname, even after you’ve imported it

  • In R, after the library is loaded, you access a function using funcname

Python Package Functions

pkgname.funcname can make for wordy code

import pkgname as pn allows you to use pn.funcname instead
This creates an alias for the package

from pkgname import func1 func2 allows you to use func1() and func2() directly, without the package name
This directly imports ONLY the functions you need

It is considered bad practice to use from pkgname import *, which exposes all functions in the package directly.

  • This is bad because multiple packages could have the same function names inside
  • This very quickly leads to bugs