1 of 70

Data Types, Operators

Jake Shoudy

Aug 12, 2022

CSCI 110 - Lecture 2

2 of 70

Today’s Agenda

  • Recap
  • First “programming” exercise
  • Programming languages
  • Data types
  • Operators

3 of 70

Announcements

4 of 70

Attendance

  • Won’t take every day
  • Wednesdays attendance is recorded
  • Talk to me if I missed you

5 of 70

HW0

  • Sign up for Edstem! (link at the top of class website)
  • In the discussion tab make a new thread and add the “HW0” category
  • Answer the questions and include a picture of yourself!
  • Due next Wednesday (8/17) - no late days

6 of 70

Reminder: Quiz 0

Stop by my OH by next Friday (August 19th) to get 100% on quiz #0!

7 of 70

Pre-class Surveys

Two surveys posted on Ed

Please take them 🙏 🙏 🙏

8 of 70

Recap

9 of 70

Computer science vs programming

What is computer science?

The study of how computers/computing solves problems.

What is programming?

Writing instructions (code) to complete some task (ex: show an image on a screen, make a robot move its arm, create a website, navigate from Fisk Campus to Ryman theatre)

10 of 70

Base 2 (binary) -> Base 10

Each Byte can be decoded into a human-readable (base 10) number.

00110101

27=128

= (0 x 27) + (0 x 26) + (1 x 25) + (1 x 24) + (0 x 23) + (1 x 22) + (0 x 21) + (1 x 20)

26=64

25=32

24=16

23=8

22=4

21=2

20=1

= 0 + 0 + 32 + 16 + 0 + 4 + 0 + 1

= 53

11 of 70

Challenge: Base 10 -> Base 2

Each base 10 number can be converted into binary as well!

221

= 128 + 93

= 128 + 64 + 29

= 128 + 64 + 0 + 16 + 13

= 128 + 64 + 0 + 16 + 8 + 5

= 128 + 64 + 0 + 16 + 8 + 4 + 0 + 1

= (1x 27) + (1 x 26) + (0 x 25) + (1 x 24) + (1 x 23) + (1 x 22) + (0 x 21) + (1 x 20)

= 11011101

12 of 70

First “program”

13 of 70

Let’s make a PB&J

14 of 70

Let’s make a PB&J

Goal: Instruct me to make a PB&J sandwich

Using materials:

  • Loaf of bread
  • Jar of peanut butter
  • Jar of jelly
  • Plate
  • Knife

Get into groups and take five minutes to write a set of instructions for me to make a PB&J sandwich.

15 of 70

How does this relate to what we are doing in this course?

Computers are very literal. Instructions need to be very explicit.

Takeaway

16 of 70

Programming Languages

17 of 70

Software

Hardware

1100 1001

1101 0111

0010 1101

1001 1001

while cond != True:

instr = gen_inst()

cond = eval(instr)

return instr

Understands “Binary Code” Instructions

Understands Human instructions

Compiler

This Class

18 of 70

Languages

There are many different programming languages…

  • Python
  • Scratch
  • Java
  • C/C++
  • Javascript
  • Go
  • R
  • Swift

num_people = 5

Python Compiler

10110110 00111001 10011010 11111001 11000101 00000001

Machine Language (Binary)

Java Compiler

C++ Compiler

int num_people = 5

uint32_t num_people = 5

Programming Language (Python)

Programming Language (Java)

Programming Language (C++)

This Class

19 of 70

Why Python?

Perfect language to start coding in

  • Easy to read
  • Very little “syntax”
    • syntax: the rules of grammar in a programming language
  • Used extensively in the real world

20 of 70

21 of 70

Syntax

The rules for placement and ordering of words and symbols in a programming language.

→ like “grammar” in a human language

Concepts vs. Syntax��The concepts are the same across programming, but the syntax changes between programming languages.

22 of 70

Syntax vs Concepts

Apple?

¿Manzana?

concept

syntax

23 of 70

Types of learning in this class

Type 1

  • Concept you already know
  • New “syntax”

Type 2

New Concept and New “syntax”

Examples

  • Math operators (+, -, *, /)
  • Variables (y = mx + b)
  • Lists

Examples

  • Functions
  • Recursion
  • Dictionaries

24 of 70

Types of learning in this class

Keep this difference in mind as your learning.

Make sure you understand the concept on its own, and how to apply it (the “syntax”) in Python.

25 of 70

Data Types

26 of 70

Objective: Data Types

What are they?

Why are they important?

Examples

27 of 70

Learning Languages

Words

Phrases

Sentences

Values

Expressions

Statements

Data Types

Parts of speech

28 of 70

Data Types: What are they?

  • The categorization / classification of data values.
  • They indicate to the computer how the programmer intends to use the data.
  • Building blocks of computer science and programming.

29 of 70

Our first four data types

  1. integer
  2. float
  3. string
  4. boolean

30 of 70

Data Types: integer (int)

  • Whole numbers
  • Can be positive or negative
  • No decimal places

Commonly abbreviated as int

5

-40

0

13382

31 of 70

Data Types: float

  • Decimal numbers
  • Can be positive or negative

Comes from term floating point number

3.14159

2.0

-9.5

32 of 70

Recap: Numbers

integers or floats

integers are whole numbers, e.g. 3

floats are numbers with a decimal, e.g. 3.14

33 of 70

😴 Why does integer vs float matter?

Computers are faster at doing math with integers

Floats sometimes don’t make sense. Ex: can you be in 2.35th place in a race?

Ints sometimes don’t have enough information. Ex: student GPA

34 of 70

Practice: integer or float?

-980.55

-12345677890

10

17.0

float

int

int

float

35 of 70

Data Types: string (str)

  • Text (sequence of characters)
  • Surrounded by quotes (either single or double)
  • Always interpreted as string when in quotes, even if resembles a number

Commonly abbreviated as str

'abcdefg'

“Hello!”

'3.14'

36 of 70

Data Types: string (str)

Why double quotes and single quotes?

“Hello!” or 'Hello!'

‘My name’s Jake’

“My name’s Jake”

“Hello!’ or 'Hello!”

37 of 70

Data Types: boolean (bool)

  • True or False
  • Used to express a conditional value
    • E.g. whether you are tall enough to ride a roller coaster
    • E.g. whether two things are the same or not

Commonly abbreviated as bool

True

False

38 of 70

Practice: integer, float, string, or boolean?

"hi, mom"

“”

2.71828

1000000

0.0

true

str

float

int

str

float

not a value!

‘True’

False

str

bool

39 of 70

Data Types: Why are they important?

Do different things

Example: can do arithmetic (add, subtract, multiply, divide) on integers and floats, but not on strings and booleans

40 of 70

What did we learn? Data Types

What are they?

Classification for values

Why are they important?

How to use, change, combine each type

Examples

integer, float, string, boolean

41 of 70

Data Types: integer, float, string, boolean

Data Type Definition Value Examples

integer (int) number without a decimal point 5, -40, 0, 13382

float number with a decimal point 3.14, 2.0, -9.5

string (str) sequence of characters surrounded by quotes ‘abc’, “Hi!”, ‘3.14’

boolean (bool) True or False True, False

42 of 70

Data Operators

43 of 70

Learning Languages

Words

Phrases

Sentences

Values

Expressions

Statements

Groupings of values via operators

Groupings of words

44 of 70

Key Terms

operator: symbol performing specific computation on a set of values

expression: combination of operators and values evaluating to single value

45 of 70

Evaluating expressions

2 + 4

operator

values (operands)

expression

46 of 70

Arithmetic Operators

+ Add�- Subtract�* Multiply�/ Divide�** Exponent

47 of 70

Operator: Floor Division

// divides two numbers and rounds result down to next integer.

Evaluates to a float if one of the values is a float.

Examples:�5 // 2 evaluates to 2�5.0 // 2.0 evaluates to 2.0�42 // 10 evaluates to 4�9 // 9 evaluates to 1�9.0 // 9.0 evaluates to 1.0

48 of 70

Floor Division: Practice

23.5 // 7

3 // 3

23.5 / 7 = 3.36 -> 3.0

3 / 3 = 1 -> 1

9 // 5

9 / 5 = 1.8 -> 1

10 // 5.0

10 / 5.0 = 2.0 -> 2.0

-10 // 3

-10 / 3 = -3.3 -> -4

-14 // -4

-14 / -4 = 3.5 -> 3

49 of 70

Operator: Modulo (mod)

% computes remainder after division

Evaluated value always has the same sign as the denominator

For example:�5 % 2 evaluates to 1�5.0 % 2.0 evaluates to 1.0�24 % 10 evaluates to 4�9 % 9 evaluates to 0�9.0 % 9.0 evaluates to 0.0

50 of 70

Modulo: Practice

24 % 8

11.3 % 5

24 = 8 x 3 + 0 -> 0

11.3 = 5 x 2 + 1.3 -> 1.3

56 % 5

56 = 5 x 11 + 1 -> 1

1.4 % 0.3

1.4 = 0.3 x 4 + 0.2 -> 0.2

2 % 10

2 = 10 x 0 + 2 -> 2

-10 % 3

-10 = 3 x -4 + 2 -> 2

-8 % -3

-8 = -3 x 2 - 2 -> -2

14 % -5

14 = -5 x -3 - 1 -> -1

51 of 70

Arithmetic Operators

+ Add�- Subtract�* Multiply�/ Divide�** Exponent

// Floor Division

Round down to nearest whole number

% Modulo (mod)

Remainder

If any operand is float, evaluated value is float

If all operands are integers, evaluated value is integer

EXCEPT DIVIDE / - evaluated value is always float

52 of 70

Evaluate

2 * 5 - 2 ** 4 % 3 + 7

= 10 - 2 ** 1 + 7 = 8 ** 1 + 7 = 15 (?)

= 2 * 3 ** 4 % 10 = 6 ** 4 = 1296 (?)

= 2 * 5 - 16 % 3 + 7 = 2 * -11 % 10 =

2 * 9 = 18 (?)

53 of 70

Order of Operations

ParenthesesExponentsMultiplicationDivisionAdditionSubtraction

1

2

3

4

3) M and D have the same precedence, go left to right. % and // are included in “MD”

4) A and S have the same precedence, go left to right

54 of 70

Evaluate

2 * 5 - 2 ** 4 % 3 + 7

= 2 * 5 - 16 % 3 + 7

= 10 - 1 + 7

= 16

55 of 70

PEMDAS

2 + 1 * 5 - 6 / 2

2 + 5 - 3.0

4.0

8 / 4 * 5

2.0 * 5

10.0

3 + (1 + 4) * 2

2 * (5 - 1) ** 2 + 4

=

=

=

3 + 5 * 2

=

13

=

2 * 4 ** 2 + 4

=

36

=

=

56 of 70

Review: Data Types & Operators

Values have different data types - can be integers, floats, booleans, or strings

Values and operators make up expressions

Can use arithmetic operators on integers and floats

Computers evaluate expressions down to single value.

Evaluation of arithmetic expressions uses PEMDAS.

57 of 70

Questions?

58 of 70

Modulo Addendum

59 of 70

First, a couple of notes

  • Negative mods are confusing (I agree!)
  • There will likely be 1 question on quizzes/exams with a negative mod
  • Negative mods are *not* a super important computer science topic. We will not use them outside of these evaluating expressions exercises (we will use positive mods in this class). If you are struggling with them and you are struggling with other concepts, focus on other concepts.

60 of 70

Method 1: For Math lovers

(x % y) = x - (x // y) * y

Source (thanks to Philip Olapade)

61 of 70

Example (-8 % 3)

(-8 % 3) = -8 - (-8 // 3) * 3

= -8 - (-3) * 3

= -8 - -9

= 1

Source (thanks to Philip Olapade)

62 of 70

Example (-8 % -3)

(-8 % -3) = -8 - (-8 // -3) * -3

= -8 - (2) * -3

= -8 - -6

= -2

Source (thanks to Philip Olapade)

63 of 70

Method 2: For Visual Learners

  1. Enumerate possible answers
    1. If the denominator is positive the possible answers are: 0, 1, 2, …, (denominator - 1)
    2. If the denominator is negative the possible answers are: 0, -1, -2, …, (denominator + 1)
  2. Set the numerator as the “goal”
  3. Move in increments of the denominator and at each step check “can I add one of the possible answers to the current value to reach the goal?”
    • If yes that is your answer :)

64 of 70

Example (-8 % 3)

  • Possible answers are 0, 1, and 2

Goal

+ 1

65 of 70

Example (-8 % -3)

  • Possible answers are 0, -1, and -2

Goal

-2

66 of 70

Method 3: For Tricksters

Assuming you can correctly calculate the mod when the denominator is positive…

  • Calculate the mod assuming the denominator is positive
  • Subtract the absolute value of the denominator

67 of 70

Example (-8 % -3)

Pretend the denominator is positive:

(-8 % 3) = 1

Subtract the absolute value of the denominator:

1 - abs(-3) = -2

68 of 70

Example (15 % -7)

Pretend the denominator is positive:

(15 % 7) = 1

Subtract the absolute value of the denominator:

1 - abs(-7) = -6

69 of 70

Trickster Chart for -7

x % 7

0

1

2

3

4

5

6

x % -7

0

-6

-5

-4

-3

-2

-1

70 of 70

General Trickster Chart

x % y

0

1

2

3

y-1

x % -y

0

1-y

2-y

3-y

-1