Lecture 3 Slides - Data Types, Operators, and Variables

1 of 40

Data, Variables, and Operators

CS 110

1

2 of 40

Announcements

  • Keep an eye out for Tutorial Team assignments tomorrow afternoon
    • Reminder: Tutorial 1 is on Wednesday!
    • Make sure to watch the pre-recorded lecture BEFORE coming to class on Wed. In order to get attendance credit you need BOTH watch the video AND attend.
  • Office hours schedule available via Canvas
  • Didn’t get a grade for MQ0 - check edSTEM! Had trouble with EX0? Keep an eye out for a Canvas message from me this afternoon.
  • Overview of this Week
  • Monday - Operators and Variables in Python
  • Wednesday - Functions in Python (Pre-Recorded - Out-of-Class MQ 1) + Tutorial 1
  • Friday - Writing our Own Functions + In-Class MQ 2
      • Ex 1 - Due at 11:59pm on Canvas

3 of 40

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

4 of 40

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:

  • pieces of data like numbers, words, images, etc.
  • operators are symbols that perform actions
  • variables are containers that can hold data
  • functions are blocks of code encased inside a function name
  • reserved words (special words in python)
  • modules

5 of 40

The first rule of programming!

Python will only do something you ask it to.

6 of 40

Data Representation

How we represent pieces of data in Python

7 of 40

8 of 40

2

two

2.0

9 of 40

Data Types

data come in different flavors we call data types

2

two

2.0

10 of 40

2

two

2.0

Data Types

11 of 40

integer (int)

2

two

2.0

Data Types

12 of 40

integer (int)

2

"two"

2.0

Data Types

string (str)

13 of 40

integer (int)

2

"two"

2.0

Data Types

string (str)

float (float)

14 of 40

Variables

Named containers that allow us to store and access data in Python.

15 of 40

Variables

  • Variables are named containers for storing and/or referencing data
  • You assign values to variables using the assignment operator (equal sign)

variable_name = piece_of_data

15

16 of 40

Variables

Turn the following statement into a sentence:

favorite_cookie = "oreo"

16

17 of 40

Variables

Turn the following statement into a sentence:

favorite_cookie = "oreo"

17

18 of 40

Variables

Turn the following statement into a sentence:

favorite_cookie = "oreo"

Assign the text "oreo" to a container called favorite_cookie.

18

19 of 40

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

20 of 40

Naming Variables

  • Variable names in Python can be any length and can consist of:
    • uppercase and lowercase letters (A-Z, a-z)
    • digits (0-9)
    • and the underscore character (_).
  • The first character of a variable name cannot be a digit.

Example 1:

favorite_cookie = "oreo"

Example 2:

1st_cookie = "oreo"

20

21 of 40

Operators

Symbols that perform operations on inputs which we call operands.

22 of 40

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

23 of 40

Example: Evaluate this expression (operator)

23

4 * 6

operand

operand

operator

24 of 40

Example: Evaluate this expression (operator)

24

4 * 6

4

operand

6

operand

*

operator

25 of 40

Example: Evaluate this expression (operator)

25

4 * 6

24

4

operand

6

operand

*

operator

26 of 40

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

27 of 40

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

28 of 40

Order of Operations

16 - 2 * 5 // 3 + 1

28

1

()

2

**

3

*, /, %, //

4

+, -

29 of 40

Order of Operations

16 - 2 * 5 // 3 + 1

29

1

()

2

**

3

*, /, %, //

4

+, -

30 of 40

Order of Operations

16 - 2 * 5 // 3 + 1

30

1

()

2

**

3

*, /, %, //

4

+, -

10

31 of 40

16 - 10 // 3 + 1

Order of Operations

31

1

()

2

**

3

*, /, %, //

4

+, -

32 of 40

16 - 10 // 3 + 1

Order of Operations

32

1

()

2

**

3

*, /, %, //

4

+, -

33 of 40

16 - 10 // 3 + 1

Order of Operations

33

1

()

2

**

3

*, /, %, //

4

+, -

3

34 of 40

16 - 3 + 1

Order of Operations

34

1

()

2

**

3

*, /, %, //

4

+, -

35 of 40

16 - 3 + 1

Order of Operations

35

1

()

2

**

3

*, /, %, //

4

+, -

36 of 40

16 - 3 + 1

Order of Operations

36

1

()

2

**

3

*, /, %, //

4

+, -

13

37 of 40

13 + 1

Order of Operations

37

1

()

2

**

3

*, /, %, //

4

+, -

38 of 40

14

Order of Operations

38

1

()

2

**

3

*, /, %, //

4

+, -

39 of 40

Examples

Example 02_arithmetic_operators.py

40 of 40

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