Week5 Lecture1
Week5 Lecture1
LeBron.right(90)
LeBron.forward(200)
LeBron.left(90)
LeBron.forward(100)
turtle.done()
Everything is an Object!
§ Python keeps track of every value,
variable, function, etc. as an object
§ There is a function that you can
call to confirm:
>>> isinstance(4, int) >>> isinstance(4, object)
True True
>>> isinstance(“Hello”, str) >>> isinstance(‘Hello”, object)
True True
Everything is an Object!
object_name.method_name(arguments)
LeBron.right(90)
LeBron.forward(200)
LeBron.left(90)
LeBron.forward(100)
turtle.done()
Let’s revisit our friend LeBron…
import turtle
LeBron = turtle.Turtle()
LeBron.begin_fill()
LeBron.color(‘red’)
LeBron.right(90)
LeBron.forward(200)
LeBron.left(90)
LeBron.forward(100)
LeBron.end_fill()
turtle.done()
Let’s Code!
§ Let’s take a look at how this works in
Python!
§ id function to get object’s memory address
§ isinstance function to determine object type Open your
§ Turtle LeBron drawing shapes!
notebook
Click Link:
1. String
Comparisons
RECAP: Input and Output
§ Python has a built-in Input/Output functions:
§ print – for displaying text to the user
§ input – for reading text from the user
Click Link:
2. Escape Sequences
String Operators
§ There are certain mathematical operators that can be applied on
strings
§ The * and + obey standard precedence rules (i.e. * before +)
§ All other mathematical operators and operands result in a TypeError
Click Link:
3. String Operators
Working with Strings
§ The string (str) type was briefly introduced in
previous weeks
§ Let’s take our string knowledge to the next level!
§ escape sequences
§ str operations
§ type conversion
§ str indexing and slicing
§ str methods
Type Conversion
§ The built-in function str takes any value and
returns a string representation of that value
§ Like our built-in functions int and float that
can take a string and attempt to return a number
representation of the string
>>> str(4) >>> int('12345') >>> float(‘-43.2’)
‘4’ 12345 -43.2
Click Link:
4. Type Conversions
Breakout Coding Session!
§ Ask the user how many times they would like to see the string
”knock knock knock… Penny” repeated. Then, print it!
§ Can you customize the name?
Open Python
(Wing or Jupyter)