Data, Variables, and Operators
CS 110
1
Announcements
A typical week…
Monday - Attend lecture, take a look at tutorial, and homework
Tuesday - Watch the pre-recorded lecture for Wednesday; peruse the tutorial
Wednesday - Come to the tutorial session, try your best to iron out all of your questions; see if you can do any of the HW
Thursday - by now, you should have a strong sense of how long it will take you to complete HW Exercise; if you need more time, apply for late penalty waiver
Friday - come to class, participate in MQ activities; finish up HW
Sat/Sun - take the weekend off
Resources available throughout the week: Canvas, Class, Office Hours, and edSTEM
Understanding the Words / Atoms of Programming
4
Python is made up of different kinds of “words”, which can be used to construct sentences and paragraphs (statements and code blocks). Some types of ‘words’ in Python include:
The first rule of programming!
Python will only do something you ask it to.
Data Representation
How we represent pieces of data in Python
2
two
2.0
Data Types
data come in different flavors we call data types
2
two
2.0
2
two
2.0
Data Types
integer (int)
2
two
2.0
Data Types
integer (int)
2
"two"
2.0
Data Types
string (str)
integer (int)
2
"two"
2.0
Data Types
string (str)
float (float)
Variables
Named containers that allow us to store and access data in Python.
Variables
variable_name = piece_of_data
15
Variables
Turn the following statement into a sentence:
�favorite_cookie = "oreo"�
16
Variables
Turn the following statement into a sentence:
�favorite_cookie = "oreo"�
17
Variables
Turn the following statement into a sentence:
�favorite_cookie = "oreo"�
Assign the text "oreo" to a container called favorite_cookie.
18
Variables
Turn the following statement into a sentence:
�favorite_cookie = "oreo"�
Assign the text "oreo" to a container called favorite_cookie.
Big Idea: Our goal for any line of Python we write is to be able to explain what it does in plain english.
19
Naming Variables
Example 1:
favorite_cookie = "oreo"
Example 2:
1st_cookie = "oreo"
20
Operators
Symbols that perform operations on inputs which we call operands.
Arithmetic Operators
�
22
+ | Addition | Adds values on either side of the operator |
- | Subtraction | Subtracts right hand operand from left hand operand |
* | Multiplication | Multiplies values on either side of the operator |
/ | Division | Divides left hand operand by right hand operand |
** | Exponent | Performs exponential (power) calculation on operators |
% | Modulus | Divides left hand operand by right hand operand; returns remainder |
// | Quotient | Divides left hand operand by right hand operand; returns quotient |
Example: Evaluate this expression (operator)
23
4 * 6
operand
operand
operator
Example: Evaluate this expression (operator)
24
4 * 6
4
operand
6
operand
*
operator
Example: Evaluate this expression (operator)
25
4 * 6
24
4
operand
6
operand
*
operator
Arithmetic Operators
�
26
+ | Addition | Adds values on either side of the operator |
- | Subtraction | Subtracts right hand operand from left hand operand |
* | Multiplication | Multiplies values on either side of the operator |
/ | Division | Divides left hand operand by right hand operand |
** | Exponent | Performs exponential (power) calculation on operators |
% | Modulus | Divides left hand operand by right hand operand; returns remainder |
// | Quotient | Divides left hand operand by right hand operand; returns quotient |
Operator Precedence
Python evaluates expressions as is done in mathematics. After precedence rules, expressions are evaluated left to right.
27
1 | () | Parenthesis |
2 | ** | Exponentiation |
3 | *, /, %, // | Multiplication, division, remainder, quotient |
4 | +, - | Addition, subtraction |
Order of Operations
16 - 2 * 5 // 3 + 1
�
28
1 | () |
2 | ** |
3 | *, /, %, // |
4 | +, - |
Order of Operations
16 - 2 * 5 // 3 + 1
�
29
1 | () |
2 | ** |
3 | *, /, %, // |
4 | +, - |
Order of Operations
16 - 2 * 5 // 3 + 1
�
30
1 | () |
2 | ** |
3 | *, /, %, // |
4 | +, - |
10
16 - 10 // 3 + 1
�
Order of Operations
31
1 | () |
2 | ** |
3 | *, /, %, // |
4 | +, - |
16 - 10 // 3 + 1
�
Order of Operations
32
1 | () |
2 | ** |
3 | *, /, %, // |
4 | +, - |
16 - 10 // 3 + 1
�
Order of Operations
33
1 | () |
2 | ** |
3 | *, /, %, // |
4 | +, - |
3
16 - 3 + 1
�
Order of Operations
34
1 | () |
2 | ** |
3 | *, /, %, // |
4 | +, - |
16 - 3 + 1
�
Order of Operations
35
1 | () |
2 | ** |
3 | *, /, %, // |
4 | +, - |
16 - 3 + 1
�
Order of Operations
36
1 | () |
2 | ** |
3 | *, /, %, // |
4 | +, - |
13
13 + 1
�
Order of Operations
37
1 | () |
2 | ** |
3 | *, /, %, // |
4 | +, - |
14
�
Order of Operations
38
1 | () |
2 | ** |
3 | *, /, %, // |
4 | +, - |
Examples
Example 02_arithmetic_operators.py
String Operators
�
40
+ | Addition | Adds (or concatenates) the left and right operands |
* | "Repetition" | Repeats the left operand a number of times specified by right operand |