2 minutes read

Every data object (a variable or a constant) has a type that describes how to keep it in memory, which operations can be applied to this object and how to execute these operations.

A real-world analogy of types is given by biological species or any other abstract attribute shared among specific objects. For example, all dogs you've seen have the type dog, but each is an individual object. Thinking about a dog as a type, you can assume some operations are available. For example, a dog can bark.

In this topic, we will consider a few of the simplest data types commonly used in programming practice.

Strings

Whenever you want to use textual information in your program, you'll have to work with strings. In Python, the string type is called str. Strings are extremely common and useful. As discussed, string literals may be delimited using either single or double quotes.

  • Examples of strings in double quotes:

print("")               # an empty string
print("string")         # one word
print("Hello, world!")  # a sentence
  • Examples of strings in single quotes:

print('a')                   # a single character
print('1234')                # a sequence of digits
print('Bonjour, le monde!')  # a sentence

In a real program, a string can represent an email of a person or an organization.

print('[email protected]')  # print an email

As you can see, strings are very easy to use!

Numerical types

Numbers are essential for any programmer. You can hardly write any program without numbers, so let's discuss some basic numerical types:

  • int (signed integers). Called integers or ints, they are numbers (positive, negative, or zero) having no decimal point.

  • float (floating-point numbers). Called floats, they represent real numbers and have a decimal point.

You can start working with a number by just printing it out.

print(11)    # prints 11
print(11.0)  # prints 11.0

Even though 11 and 11.0 are the same number, the former is an integer, and the latter is a float. The simplest way to distinguish them is by the decimal point. Floats have a decimal point, and integers don't. Pay attention!

You can also use negative numbers and zeros:

print(0)      # prints 0
print(-5)     # prints -5
print(-1.03)  # prints -1.03

Integer numbers can count things in the real world, while floating-point numbers are a good choice for statistical and scientific calculations.

Printing types

We also have a way to demonstrate the types of different objects: use the type() function, which is part of Python.

print(type('hello'))  # <class 'str'>
print(type("world"))  # <class 'str'>

print(type(100))      # <class 'int'>
print(type(-50))      # <class 'int'>

print(type(3.14))     # <class 'float'>
print(type(-0.5))     # <class 'float'>

As you can see from the examples above, the type() function indicates the data type of a passed value after the word class.

Summary

We hope you now have some intuition about data types. You should remember the simplest types, str, int and float, and how to write their literals. In the following topics, we will learn the specific features of each type. If you need to know the type of an object, print it using the type() function.

5169 learners liked this piece of theory. 98 didn't like it. What about you?
Report a typo