Data Types, Operators
Jake Shoudy
Aug 12, 2022
CSCI 110 - Lecture 2
Today’s Agenda
|
|
| |
| |
| |
| |
Announcements
Attendance
|
|
| |
| |
| |
| |
HW0
|
|
| |
| |
| |
| |
Reminder: Quiz 0
Stop by my OH by next Friday (August 19th) to get 100% on quiz #0!
Pre-class Surveys
Two surveys posted on Ed
Please take them 🙏 🙏 🙏
Recap
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) |
| |
| |
| |
| |
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
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
First “program”
Let’s make a PB&J
Let’s make a PB&J
| Goal: Instruct me to make a PB&J sandwich Using materials:
Get into groups and take five minutes to write a set of instructions for me to make a PB&J sandwich. |
| |
| |
| |
| |
How does this relate to what we are doing in this course?
Computers are very literal. Instructions need to be very explicit.
Takeaway
Programming Languages
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
Languages
| There are many different programming languages…
|
| |
| |
| |
| |
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
Why Python?
| Perfect language to start coding in
|
| |
| |
| |
| |
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.
Syntax vs Concepts
Apple?
¿Manzana?
concept
syntax
Types of learning in this class
Type 1
Type 2
New Concept and New “syntax”
Examples
Examples
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. |
| |
| |
| |
| |
Data Types
Objective: Data Types
What are they?
Why are they important?
Examples
Learning Languages
Words
Phrases
Sentences
Values
Expressions
Statements
Data Types
Parts of speech
Data Types: What are they?
Our first four data types
Data Types: integer (int)
Commonly abbreviated as int
5
-40
0
13382
Data Types: float
Comes from term floating point number
3.14159
2.0
-9.5
Recap: Numbers
integers or floats
integers are whole numbers, e.g. 3
floats are numbers with a decimal, e.g. 3.14
😴 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
Practice: integer or float?
-980.55
-12345677890
10
17.0
float
int
int
float
Data Types: string (str)
Commonly abbreviated as str
'abcdefg'
“Hello!”
'3.14'
Data Types: string (str)
Why double quotes and single quotes?
“Hello!” or 'Hello!'
‘My name’s Jake’
“My name’s Jake”
“Hello!’ or 'Hello!”
Data Types: boolean (bool)
Commonly abbreviated as bool
True
False
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
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
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
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
Data Operators
Learning Languages
Words
Phrases
Sentences
Values
Expressions
Statements
Groupings of values via operators
Groupings of words
Key Terms
operator: symbol performing specific computation on a set of values
expression: combination of operators and values evaluating to single value
Evaluating expressions
2 + 4
operator
values (operands)
expression
Arithmetic Operators
+ Add�- Subtract�* Multiply�/ Divide�** Exponent
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
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
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
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
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
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 (?)
Order of Operations
Parentheses�Exponents�Multiplication�Division�Addition�Subtraction
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
Evaluate
2 * 5 - 2 ** 4 % 3 + 7
= 2 * 5 - 16 % 3 + 7
= 10 - 1 + 7
= 16
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
=
=
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.
Questions?
Modulo Addendum
First, a couple of notes
Method 1: For Math lovers
(x % y) = x - (x // y) * y
Source (thanks to Philip Olapade)
Example (-8 % 3)
(-8 % 3) = -8 - (-8 // 3) * 3
= -8 - (-3) * 3
= -8 - -9
= 1
Source (thanks to Philip Olapade)
Example (-8 % -3)
(-8 % -3) = -8 - (-8 // -3) * -3
= -8 - (2) * -3
= -8 - -6
= -2
Source (thanks to Philip Olapade)
Method 2: For Visual Learners
Example (-8 % 3)
Goal
+ 1
Example (-8 % -3)
Goal
-2
Method 3: For Tricksters
Assuming you can correctly calculate the mod when the denominator is positive…
Example (-8 % -3)
Pretend the denominator is positive:
(-8 % 3) = 1
Subtract the absolute value of the denominator:
1 - abs(-3) = -2
Example (15 % -7)
Pretend the denominator is positive:
(15 % 7) = 1
Subtract the absolute value of the denominator:
1 - abs(-7) = -6
Trickster Chart for -7
x % 7 | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
x % -7 | 0 | -6 | -5 | -4 | -3 | -2 | -1 |
General Trickster Chart
x % y | 0 | 1 | 2 | 3 | … | y-1 |
x % -y | 0 | 1-y | 2-y | 3-y | … | -1 |