Type Casting, Variables
Jake Shoudy
Aug 15, 2022
CSCI 110 - Lecture 3
Today’s Agenda
|
|
| |
| |
| |
| |
Announcements
Reminder: HW0
|
|
| |
| |
| |
| |
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!
Pre-class Surveys
Two surveys posted on Ed
Please take them 🙏 🙏 🙏
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.
Lab Tomorrow
Bring laptop!
Recap
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
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
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
Review: Expressions and Operators
Values and operators make up expressions
Computers evaluate expressions down to a single value
4 ** 2
operator
operands (values)
expression
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
Review: 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
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
=
=
=
=
String operators
String Operators
+ Add
* Multiply
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
=
=
Fixing TypeError
‘b’ * 2 + ‘8’
=
‘bb’ + ‘8’
=
‘bb8’
‘b’ * 2 + 8
=
‘bb’ + 8
=
TypeError: can only concatenate str (not "int") to str
Built-in functions
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.
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
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:
Fixing TypeError V2
‘b’ * 2 + str(8)
=
‘bb’ + ‘8’
=
‘bb8’
‘b’ * 2 + 8
=
‘bb’ + 8
=
TypeError: can only concatenate str (not "int") to str
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
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'
Variables
2, 37208, 6789998212
What are these numbers?
siblings_count = 2
fisk_zip = 37208
burna_boy_phone = 6789998212
Objective: Variables
What are they?
Why are they important?
Examples
Variables
Why?
Variable Naming
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
Practice: Variable Naming
my_num
x - y
num_1
_my_variable
1st_num
a*b
✅
❌
❌
❌
✅
✅
Good naming is important!
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?
Variable
Name Value
Variable Assignment Syntax
a_descriptive_variable_name = value
Example:
>>> age = 55
not checking for equivalency�is an ASSIGNMENT
Variable declaration
keshav = 18
variable name
keshav
value
assignment statement
“assigns the value”
Variable declaration
jaden = 18
variable name
jaden
value
assignment statement
“assigns the value”
Variable declaration
imani = 18
variable name
imani
value
assignment statement
“assigns the value”
Access Statement
print(keshav + 3)
keshav
access statement
“accesses the value”
Access Statement
print(jaden + 3)
jaden
access statement
“accesses the value”
Access Statement
print(imani + 3)
imani
access statement
“accesses the value”
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’
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
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
Questions?