Data Types in Python

In this tutorial, we are going to learn about the various data types that Python works upon.

Data Types – Description

  1. Boolean – Represents two values of logic and associated with conditional statements.
  2. Integer   – Used to represent positive and negative whole numbers.
  3. Complex – Contains real and Imaginary parts (Of the form a+ib).
  4.  Float       –  Real numbers with or without decimal parts.
  5.  String     –  Sequence of characters. All Strings or characters are enclosed between single or double quotes.

Values and Representation

  1. Boolean – True and False
  2. Integer – set of all integers, Z. Ex: 1,2,3,…..
  3. Complex – Contains all complex Numbers. Ex: 5i
  4.  Float – Floating point numbers. Ex: 1.29
  5. String – sequence of characters. Ex: “codespeedy”

Identifying Object Data Type

We can find the data type of a variable using the type() method.

a = "codespeedy"
b = 10
c = 120.83

print(type(a))
print(type(b))
print(type(c))

Output:

str
int
float

Verifying Object Data Type

We can verify the datatype of an object in Python using the ‘is’ operator.

type(a) is int
type(b) is float
type(c) is float

Output:

False
False
True

Changing Data Type of Object

We can explicitly convert data type of object in Python. We cannot convert a String to corresponding Integer value or Boolean value.

# type(c) is currently float.
# Let us convert to integer data type.

new_c = int(c)

You will notice that the float number was truncated, i.e the decimal part of the number was removed.

print(new_c)
#120 is the output.

Now we can use different data types based on the data to be stored in the particular variable.

Also read: Type conversion in Python

Leave a Reply

Your email address will not be published. Required fields are marked *