What are Python Objects?
First rule of Python. Everything is an object. But what is an object? Let’s take a few examples:
- A number
5
is an object - A word
"susan"
is an object
What are Python Types?
Every object has a type. There are numerous built-in types in Python. We will some of the basic types here to get familiar with the language. Before continuing, please download and install Python 2.7 from the official website.
Everything that is preceded with >>>
assumes that the statement is being run inside Python shell.
This is an assignment statement since 5
is being assigned to a
. a
is known as a variable and it holds a value 5
in it.
type()
is built-in function that returns ‘type’ of the object passed in it
Also, we can directly pass value to type()
to know that value’s type.
So a
or 5
is of type int
i.e. integer type.
Let’s try a decimal number.
Decimal numbers are called float
type.
Let’s try and pass a word to type()
What’s that? Python raises error when we make mistakes. A word is surrounded by either "
or '
(quotes).
Such words are of type str
i.e. string. Read more about Python strings.
Let’s take a look at one more type before we wrap up this post.
This is called Boolean type. They are either True
or False
. This type is widely used in programming.
There are many more types in Python, we will see them one at a time in future posts. Some important ones are: List, Dictionary, Tuple.
Next post will discuss some arithmetic and logical operations](/posts/arithmetic-and-logical-operators-in-python) we can perform on various data-types.
Note: This is a part of what I learned in an online Open Course Ware offered by MIT on edX. Its for my personal reference & also for those who would like to revisit the course.