Tutorial on Built-in data types in Python explained

Python has 5 data types which I will be discussing in this tutorial.

1. Integer in Python

The integer data type represents a range of mathematical numbers. Integral data types may be of different sizes and may or may not be allowed to contain negative values. Integers are commonly represented in a computer as a group of binary digits (bits). The size of the grouping varies so the set of integer sizes available varies between different types of computers and different programming languages. In Python programming language, there is no limit on the range.

num = 5 
print(type(num))

#output 
<class 'int'>

I can use a type conversion to convert from another type to int using int().I will show a simple example here by converting a float to an int.

b = 4.9 
print(int(b))

#output 
4

Python interprets various other types of the numbers like hexadecimal, octal and binary as integers when used in certain way.

Prefix Type
0b or 0BBinary
0o or 0Ooctal
0x or 0X Hexadecimal
Other data types interpretation based on prefix

I have demonstrated this with example

print(0b1000)
print(0o25634)
print(0x7E)

#output 
8
11164
126

2. Float in Python

We write a float number as a decimal point representing integers and fraction.

b = 4.9 
print(type(b))

#output 
<class 'float'>

When you add an integer and a float is added together then the type of the sum will be a float.

# as per the prev values of a and b 
sum = a+b
print(type(sum))

#output 
<class 'float'>

We can convert any other data type to float by using float().
With the above example we know a has an integer value of 5. When I convert it to float as shown it gives 5.0 as output.

print(float(a))

3. Complex numbers in Python

A complex number is a number that can be expressed in the form a + bj, where a and b are real numbers, and ‘j’ represents the imaginary unit, satisfying the equation j2 = −1. Because no real number satisfies this equation, ‘j’ is called an imaginary number.

When you have an expression of the type a+bj, then it is considered as a complex type in Python as shown below.

c = 4+7j 
print(type(c))

#output 
<class 'complex'>

4. Boolean in Python

You can consider a boolean to be having either ‘True’ or ‘False’. I query the type and the class it belongs to is bool

print(type(True))
print(type(False))

#output
<class 'bool'>
<class 'bool'>

The type conversion from other types to bool is done using bool(). We know that a contains a value of 5 as per the example above and I make c as 0. if you convert these integers into bool then we get the following output.

c = 0 
print(bool(c))
print(bool(a))

#output 
False
True

5. Strings in Python

We will use strings a lot. String is a sequence of one or more characters. You can indicate a string by a pair of ‘ or “.

input = "hello python learner"
print(type(input))

#output 
<class 'str'>

You can use multi line strings using 3 double or single quotes.

input = """hello
This is a demo of 
Multi line string"""
print(input)

#output 
hello
This is a demo of 
Multi line string

At some point while programming, you may want to know the length of the string we can use len() to get the length of the string.

print(len(input))
#output 
42

Strings are a broader subject which will be later.

Go back to the lesson Overview.

Previous: Variables

Next: Loops