Objects in Python | strings, integers, and floats

Previously we learned how to call functions in order to get their output. For example, we looked at the print function:

In the example above, the print function got as argument a string object. But you could also pass an integer object to print:

Or a float object:

So, "Program started!", 120, and 120.35 are three different types of objects, respectively a string object type, an integer object type, and a float object type. Here are some more examples of:

👉string objects: "Welcome", "Submit", "yellow"

👉integer objects: 12, 101, 6

👉float objects 12.441, 177.1, 100.22

So, Python, like any other programming language tries to abstract the real world into a computer program. Do you want your program to greet users with a welcome message? Then, you need to use a string object. Do you want to assign a label to a button of your Python mobile app? Again, you use a string to represent that label. Do you want to add a color in your program drop down list? Again, you need to use a string to represent that color. Python knows you are using a string when you wrap the text inside double or single quotes. 

Likewise, when we deal with whole numbers, we need to use integer objects. If you were to make a program for an egg farm, you would need to use integers to represent the number of eggs. If you want to represent the price of the eggs, then you are dealing with decimal numbers. That means you would need to use a float. 

So, what do we mean when we say "have to use a float" or "have to use a string"? Well, it just means you have to follow the Python syntax rules to create a float which is by simply writing a number followed by a dot followed by the decimal part after the dot. For strings, you have to use double quotes or single quotes around the text. Those rules are part of the syntax. Python knows you are creating a string object when you use quotes and it treats that object as a string. It will not try to do math operations on that object. So, everything has to be explicit following the proper syntax rules.

Strings, integers, floats, and lists are the four more used object types in Python. Let us learn about lists in the next lecture.

Next Lecture
Recommended Course

Python Mega Course: Learn Python in 60 Days, Build 20 Apps
Learn Python on Udemy completely in 60 days or less by building 20 real-world applications from web development to data science.