Introduction to Data Science

University of Redlands - DATA 101

Instr. Edgar Melendrez edgar_melendrez@redlands.edu

Class Website: edgar2020.github.io


Pre-Class Day 3


When I was taking my first computer science class in college, the class had a TA named Anthony who dropped a quote that has impacted the way I like to teach. That quote was

"The hardest part about learning something new, is not
knowing the vocabulary. Once you learn the vocabulary
the rest becomes easier"
— Anthony Hallack

Knowing the vocabulary will not only help you follow along when I am explaining something but it will also let you do a better job at communicate with others about the class. And remember the exam is collaborative so if you can use the right words in your explanation the other person is much more likely to understand what you mean.

GOALS:

  1. Learn some beginner Python vocabulary

1. Variables

Definition: A named container that stores a value so you can use it later.

Example:

age = 25         #age is a variable and it is ASSIGNED the value 25
name = "Maria"   #name is a variable and it is ASSIGNED the value "Maria"

How to identify it: Look for a word followed by an equals sign (=) with a value on the right. The word on the left is the variable.

The equal sign (=) is often called the assignment operator because it assigns a value to a variable.


2. Data Type

Definition: The kind of value a variable holds.

Common data types:

How to identify it: Numbers without quotes are int/float. Anything in " " or ' ' is a str. True/False (capitalized, no quotes) is a bool. Lists will include [].

More data types will be introduced as the semester advances.


3. Function

Definition: A reusable, named block of code that performs a task. You "call" it by writing its name followed by parentheses.

Example of functions you may have already seen:

"""
calling the function print, 
you are passing in the string "Hello, Sam" as an argument
"""
print("Hello, Sam")

"""
calling the function len, 
you are passing in the string "Hello, Sam" as an argument
"""
len(fruits)

print() and len() are built-in functions as in they already exist in Python, so you can just use them right away. (Later on, you'll learn you can also write your own custom functions using the def keyword. For now, focus on recognizing and using the ones Python already gives you.)

How to identify it: A word followed by parentheses ( ), e.g., functionname(...). Whatever's inside the parentheses are the arguments aka what you're "feeding" the function. A function will always have parentheses even if it has no arguments.


4. Method

Definition: A special kind of function that "belongs to" an object. You know it belongs to an object because it uses dot notation.

Example:

"""
Take this code block from Hw #2
I see two methods:
  .unique() and .groupby(...)
"""
countries = ['Turkey', 'United States', 'United Kingdom']
issues = list(DF['issue'].unique())
c_groups = DF.groupby(['country','issue'])
print(issues)

How to identify it: Written as object.method_name(), a dot connects the object to the method name, followed by parentheses.


5. Attribute

Definition: A piece of data (aka a variable) that belongs to an object.

Example:

df.shape        # returns (rows, columns)

How to identify it: Also uses dot notation (object.attribute) but has no parentheses.


6. Object

Definition: Anything in Python that holds data and has associated attributes/methods. Basically everything in Python is an object.


7. Library / Package / Module

Definition: Pre-written code that someone else created, which you can reuse instead of writing everything from scratch.

Example:

import pandas as pd
import numpy as np

How to identify it: Look for the import keyword at the top of a script.


8. DataFrame

Definition: A special data type that stores a table of data with rows and columns. The main structure used in pandas to store and analyze data. The python equivalent of an Excel spreadsheet.

Example:

import pandas as pd
df = pd.DataFrame({"name": ["Sam", "Ana"], "age": [22, 25]})

How to identify it: look for a variable that is assigned to pd.DataFrame(...), and displays as a table with row/column labels.


9. Index

Definition: The labels used to identify rows (or items in a list). Like row numbers, but they can also be custom labels. Usually an index will start counting from 0. So index[0] is actually the first row or item in a list.

Example:

df.index          # shows the row labels, e.g., RangeIndex(0, 1, 2...)
fruits[0]         # "0" is the index position of the first item in a list

10. Loop

Definition: A way to repeat an action multiple times, often over each item in a list.

Example:

for fruit in fruits:
    print(fruit)

How to identify it: Keywords for or while, followed by a colon : and an indented block underneath.


11. Conditional (if statement)

Definition: Code that only runs when a certain condition is true.

Example:

if age >= 18:
    print("Adult") #prints Adult only if variable age is greater or equal to 18
elif age >= 4:
    print("Child") #prints Child only if variable age is greater or equal to 4 
                   #and the previous conditions are False
else:
    print("Toddler") #only prints Toddler if all other conditions in the branch 
                     #are False

How to identify it: Keywords if, elif, else, each followed by a colon : and indented code.


12. Boolean Expression

Definition: A statement that evaluates to True or False, used to make decisions.

Example:

age >= 18        # evaluates to True or False

How to identify it: Comparison operators like == (equals), !=(not equals), >(greater than), <(less than), >=(greater than equals), <=(less than equals).


13. Comments

Definition: Text in your code that Python ignores, best used to leave notes for humans.

Example:

# This is a single line comment

"""
This is a 
multi line
comment
"""

How to identify it: Often a different color in code editors.


14. Errors

Definition: A mistake in how code is written. Three main types: