1 of 50

Type Casting, Variables

Jake Shoudy

Aug 15, 2022

CSCI 110 - Lecture 3

2 of 50

Today’s Agenda

  • Recap
  • String operators
  • Built-in functions
  • Variables

3 of 50

Announcements

4 of 50

Reminder: HW0

  • Post on EdStem!
  • Due next Wednesday (8/17) - no late days

5 of 50

Reminder: Quiz 0

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

Quiz 1 will be this Friday in class! It will be the first 15 minutes of class. Please be on time!

6 of 50

Pre-class Surveys

Two surveys posted on Ed

Please take them 🙏 🙏 🙏

7 of 50

CS Emailing List

Posted in Ed.

If you are planning to be a Computer Science or Data Science minor or major fill out this form. Dr. Qian will be using it to create an email list that will be used for sending out information about important events, scholarships, internships, etc.

8 of 50

Lab Tomorrow

Bring laptop!

9 of 50

Recap

10 of 50

Review: Data Types

What are they?

They are classifications for values

Why are they important?

They define how we use, change, combine data

Examples

integer, float, string, boolean

11 of 50

Review: 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

12 of 50

Review: integer, float, string, or boolean?

“2.0”

false

2.0

‘’

‘hi”

name

str

float

str

not a value!

not a value!

not a value!

True

-1334

bool

int

13 of 50

Review: Expressions and Operators

Values and operators make up expressions

Computers evaluate expressions down to a single value

4 ** 2

operator

operands (values)

expression

14 of 50

Review: Arithmetic Operators on Numbers

+ 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

15 of 50

Review: 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

16 of 50

Review: Arithmetic Expressions

2 ** 3 * 3 / 6 + 2

8 * 3 / 6 + 2

24 / 6 + 2

2.5 * 3 - 18 % (7 - 3)

2.5 * 3 - 18 % 4

7.5 - 2

6.0

5.5

10 // (-8 / 2)

=

=

=

10 // -4.0

=

-3.0

=

=

=

=

17 of 50

String operators

18 of 50

String Operators

+ Add

  • Concatenate (combine) two strings
  • Must be used on two strings. Otherwise, TypeError

* Multiply

  • Repeat a string
  • Must be used with one string and one int

19 of 50

String Expressions

“Hi” + “mom”

“Himom”

“lol” + “ha” * 3

“lol” + “hahaha”

“lolhahaha”

(‘lol’ + “ ha”) * 3

‘b’ * 2 + 8

=

=

“lol ha” * 3

=

“lol halol halol ha”

=

‘bb’ + 8

=

TypeError: can only concatenate str (not "int") to str

=

=

20 of 50

Fixing TypeError

‘b’ * 2 + ‘8’

=

‘bb’ + ‘8’

=

‘bb8’

‘b’ * 2 + 8

=

‘bb’ + 8

=

TypeError: can only concatenate str (not "int") to str

21 of 50

Built-in functions

22 of 50

Functions

function: performs specific action on a set of values

Takes inputs

Performs task on inputs

Optionally, gives output

Inputs and outputs of a function are values.

23 of 50

Functions Syntax

myFunction(input)

myFunction(input)

myFunction(input)

myFunction(input)

Function Name

Open and close parentheses mark the start and end

Input values go in between the parentheses. Multiple values are separated by commas

24 of 50

Casting functions

Casting is how we convert between different types.

You cast a variable or value of one type to another type by using the function of the destination type:

  • int() to convert something to an integer
  • str() to convert something to a string
  • float() to convert something to a float
  • bool() to convert something to a boolean

25 of 50

Fixing TypeError V2

‘b’ * 2 + str(8)

=

‘bb’ + ‘8’

=

‘bb8’

‘b’ * 2 + 8

=

‘bb’ + 8

=

TypeError: can only concatenate str (not "int") to str

26 of 50

Built-in functions

int() converts input value to an integer

float() converts input value to a float

str() converts input value to a string

bool() converts input value to a boolean

type() determines data type of input value

print() displays input value in console

27 of 50

Review: Practice Expressions

0.0 + 1

int('5' + '1') + 3

str(4.9) * 5

‘cat’ * -3

type(‘3.1’ + “4”)

‘cat’ * -3.0

float(4)

int(-4.8)

bool(0)

float(‘banana’)

1.0

54

“4.94.94.94.94.9”

“ ”

str

TypeError

4.0

-4

False

ValueError: could not convert string to float: 'banana'

28 of 50

Variables

29 of 50

2, 37208, 6789998212

What are these numbers?

30 of 50

siblings_count = 2

fisk_zip = 37208

burna_boy_phone = 6789998212

31 of 50

Objective: Variables

What are they?

Why are they important?

Examples

32 of 50

Variables

  • Give name to value
  • Use variables wherever would use value
  • Have only one value at any moment in time
    • But can change value!

33 of 50

Why?

  • Variables allow us to use the same number multiple times in program, then change it
  • Without them, it’s not clear what a number means
  • Makes it easier to read and understand code

34 of 50

Variable Naming

  • Letters a-z, upper / lowercase, numbers 0-9, underscore (_)
  • Can't begin with number
  • Cannot contain operator
  • Case sensitive
  • No spaces, separate words with underscore
  • No reserved words (technically works but will break code)

35 of 50

Reserved Words

False class return is finally

None if for lambda continue

True def from while nonlocal

and del global not with

as elif try or yield

assert else import pass

break except in raise

Words used for specific purposes in Python

36 of 50

Practice: Variable Naming

my_num

x - y

num_1

_my_variable

1st_num

a*b

37 of 50

Good naming is important!

38 of 50

x1q3z9ocd = 35.0

x1q3z9afd = 12.50

x1q3p9afd = x1q3z9ocd * x1q3z9afd

print(x1q3p9afd)

hours = 35.0

rate = 12.50

pay = hours * rate

print(pay)

a = 35.0

b = 12.50

c = a * b

print(c)

What is this code doing?

39 of 50

Variable

Name Value

40 of 50

Variable Assignment Syntax

a_descriptive_variable_name = value

Example:

>>> age = 55

not checking for equivalency�is an ASSIGNMENT

41 of 50

Variable declaration

keshav = 18

variable name

keshav

value

assignment statement

“assigns the value”

42 of 50

Variable declaration

jaden = 18

variable name

jaden

value

assignment statement

“assigns the value”

43 of 50

Variable declaration

imani = 18

variable name

imani

value

assignment statement

“assigns the value”

44 of 50

Access Statement

print(keshav + 3)

keshav

access statement

“accesses the value”

45 of 50

Access Statement

print(jaden + 3)

jaden

access statement

“accesses the value”

46 of 50

Access Statement

print(imani + 3)

imani

access statement

“accesses the value”

47 of 50

What's in my box?

imani = 18

next_num = 9

next_num = 'goodbye'

rachel = 'purple' + 'green'

Next_num = 'hello' + next_num

imani = imani + 1.2

word = rachel

imani = 19.2

next_num = ‘goodbye’

rachel = ‘purplegreen'

Next_num = ‘hellogoodbye’

word = ‘purplegreen’

48 of 50

What's in my box?

first_num = 10

second_num = first_num * 2

third_num = first_num

first_num = first_num + 2

first_num = 12

second_num = 20

third_num = 10

49 of 50

What did we learn? Variables

What are they?

Give name to value and variable gets assigned: [variable name] = [value]

First time variable is assigned: declare variable

Then access variable value with variable name

Why are they important?

Know what a value is representing

Reuse and change value throughout program

50 of 50

Questions?