The road to mastering the Python programming language is paved with objects. Objects are Python’s foundational building block. The entire Python programming language is used to create and control objects. This course provides a high-level glimpse into the basic mechanics of objects. With the goal of providing you with a vocabulary and mental model for understanding Python.
Upon completing this course you’ll be familiar with the anatomy and behaviors of objects.
This course was designed for first-time developers wanting to learn Python. Existing developers already familiar with the concept of an object may want to skip.
This is an introductory course and doesn’t require any prior programming knowledge.
The road to mastering Python is paved with objects. Let’s explore why objects are so important to Python in this lesson.
What do we currently know about objects? We know that:
- Objects are building blocks for modeling real world and abstract concepts.
- ...We model objects by defining meaningful attributes and behaviors of the concept we’re modeling.
We used the example of a cat previously, where we defined attributes for name, age, weight, and breed and we defined behaviors for play and meow.
Another thing we know about objects is that:
- Python objects are a collection of attributes.
- ...Attributes have two flavors, data and methods.
- Recall that objects have methods of doing things.
Let’s try and summarize those two points so that we can condense what we’ve learned. We’ll rephrase this to say:
- We know that objects are a data modeling strategy which defines data using attributes and behaviors using methods.
Using objects to model concepts is a very common theme in computing. If you can model a concept then you can interact with the resulting object in a more intuitive manner. For example, if I model a concept that you’re already familiar with and I show you the object, you’ll likely say: yeah, that makes sense.
Let’s try and illustrate that point. Imagine an object with an attribute named URL, and a method named browse. What does this look like to you? What expects a URL and can browse? If you’re thinking it’s a web browser then you’re correct!
Sure, this doesn’t describe a complete model of a browser. Though, hopefully it highlights the point. Which is, that modeling concepts with objects makes using these objects more intuitive for developers.
The reason we’re investing so much time talking about objects is that everything in Python is built with objects. Objects are a primary building block. Which means the better you understand Python objects, the better you’ll understand Python.
Let’s begin to understand how some of Python’s built-in types function by looking at the cat object used in a previous lesson.
Recall that the concept being modeled by an object is referred to as the object’s type. We haven’t revisited the term “type” in this lesson, so, let’s review it now. We know that objects are used to model concepts. The name of the concept being modeled is referred to as an object’s type. If we’re modeling a cat, then our object’s type is cat. If we’re modeling a list, then our object’s type is list. So, the concept we’re modeling is referred to as an object’s type.
We’re going to start using that term a lot from here on out whenever we talk about the different types of objects in Python. So when I say something such as: Python provides a lot of built-in types. What I’m actually saying is that Python has already modeled many of the common concepts used by developers.
Some concepts are so common in daily life that they’re essential to most programming languages. For example, concepts such as text and numbers are used in everyday life, all around the world. And we’ve actually seen these types used in our cat object.
We can see that our cat object has a few attributes that store text and numeric data. Though, what exactly are attributes? Attributes describe some aspect of the concept being modeled. In the case of a cat, we might have: name, age, breed, and weight. Attributes consist of two parts, a name and a value. The name describes an aspect of the concept we’re modeling. Since there are different breeds of cats, that makes ‘breeds’ an aspect for which we can model.
So, attribute names describe an aspect of the concept being modeled. Attribute values are objects that store data about the aspect. In our cat example the name attribute references the built-in Python object used for working with text.
Since a type is just the name of the concept modeled by an object, you might expect that Python’s built-in type for working with text is named text. However, the Python type used for working with text is actually called string. As in, text is just a string of characters.
In the name of brevity, Python actually just abbreviates this to str. I’m going to continue to use the full word string, rather than say s, t, r, or try and pronounce it as spelled.
The string type is built into the Python runtime. It’s modeled to be a sequence of characters with methods for interacting with those characters. What does that mean, a sequence of characters? By that, I’m talking about: text. Written text is just a sequence of characters.
There are actually rules about what constitutes a character. However, that’s a rabbit hole we’re not yet ready to explore. For now, just know that the list of available characters includes different written languages, symbols, and even emojis.
Let’s visualize the string type. Notice that our cat object has some attributes and some methods. The built-in string type can store a sequence of characters. In this case we’re storing the cat’s name.
The built-in string type has many methods for interacting with the characters. Here’s some of the built-in things it can do.
It can capitalize text. It can be made uppercase or lowercase. It can have whitespace characters such as spaces trimmed off the ends. It can be searched for the existence of other text. It can perform replacements. And this just scratches the surface.
Storing and working with text data is common. Making Python’s built-in string type a foundational building block. Our cat object only defines a few attributes and two of them are strings.
Let’s look at the other two types used by our cat object. Notice the age attribute which references the number 3. We use numbers often in daily life. Though, we may not think about the different types of numbers. For example if we’re filing taxes then we care about decimal places. Where if we’re counting chickens then we’d use whole numbers.
For our age attribute we’re using Python’s built-in integer type. Just as Python abbreviates string to str, integer is abbreviated as int. For consistency, I’ll refer to int as integer. The integer type stores whole numbers. Integer objects have methods which enable them to be added, subtracted, divided, and multiplied. They also include methods that enable them to be compared to other numbers. For example, we see if one number is greater, equal to, or less than another number.
The weight attribute of our cat object includes another type of number which is used when we need more precision. It’s called a float, and it models floating point numbers. The built-in float type has methods which enable them to be added, subtracted, divided and multiplied. As well as some other useful methods, to make working with floating point numbers easier.
Okay, with that brief introduction to some of Python’s built-in types, we should wrap up this lesson. Don’t worry about remembering everything that these types can do. At this stage in the learning process, I just want you to know that they exist.
Knowing that Python has built-in types that model text and numbers is enough for now. Recalling the names of these types will be helpful, but even that is something that will happen over time during this course.
Alright, let’s recap the points that we want to solidify starting with types. An object’s type defines the concept being modeled by the object. We used our Cat type to explore a few of Python’s built-in types. Python provides many built-in types that we can use as building blocks. The three types we reviewed are the string type, the integer type, and float type.
So, what did we learn about strings? We learned that the string type is used for working with sequences of characters. Which enables us to work with text. String objects have methods for behaviors such as making text capitalized; making it upper or lower case; performing find and replace; and many others.
What did we learn about numbers? We learned that there are different numeric types including integers and floating points.
We learned that the integer type enables us to work with whole numbers. Performing tasks such as addition and subtraction, etc.
We learned that the float type enables us to work with numbers that include a decimal place. Float types, similar to integer types can be used to add, subtract, divide and multiply, in addition to other operations.
Don’t worry if things don’t fully make sense now. The foundation we’re building on will be gradual. Different concepts will fall into place as we progress.
For now, the key takeaways should be that:
- Objects are building blocks for modeling real world or abstract concepts.
- The concept an object models is referred to as its type.
- Python provides a lot of commonly required types built into the runtime.
- ...Three of them include: string, integer, and float.
That’s going to wrap up this lesson. Thanks so much for watching and I’ll see you in another lesson!
Ben Lambert is a software engineer and was previously the lead author for DevOps and Microsoft Azure training content at Cloud Academy. His courses and learning paths covered Cloud Ecosystem technologies such as DC/OS, configuration management tools, and containers. As a software engineer, Ben’s experience includes building highly available web and mobile apps. When he’s not building software, he’s hiking, camping, or creating video games.