Unit 6 · Class is in Session · Lesson 46 of 57
Everything Is an Object
Everything is an object
You've been using something this whole time without knowing its name.
name = "amer"
print(name.upper())
crew = ["Karl", "Amer"]
crew.append("Zain")
That . thing. name.upper(), crew.append(), roles.items(). What's actually going on there?
In Python, every value is an object. An object is a bundle of two things:
- data: the stuff it holds (the letters in a string, the things in a list)
- methods: things it can do (
.upper(),.append())
The . means "reach inside this object and use one of its methods." name.upper() is "hey name, give me an uppercase version of yourself."
What kind of object is it?
Every object has a type, and the type() function tells you what it is:
print(type("amer"))
print(type(["Karl", "Amer"]))
<class 'str'>
<class 'list'>
See the word class? A class is the blueprint for a kind of object. str is the blueprint for strings, list is the blueprint for lists. Every string you've ever made was built from the str blueprint, which is why they all have .upper().
Here's the cool part: you can make your own classes. That's what this unit is about.
Run this and see what types these are.
Python exercise
Write the program in an editor next to the lesson, run it, and have it checked automatically. Create a free account to do this part.
Question
Answer it in the lesson and it is checked as soon as you send it. Create a free account to do this part.
Do this lesson for real
With a free account you get the terminals, challenges and checks that go with the lessons in Intro to Python, and your progress is saved as you go. CyberAcad is a 501(c)(3) nonprofit, so there is nothing to pay.