Data Types in Python
In this tutorial, we are going to learn about the various data types that Python works upon.
Data Types – Description
- Boolean – Represents two values of logic and associated with conditional statements.
- Integer – Used to represent positive and negative whole numbers.
- Complex – Contains real and Imaginary parts (Of the form a+ib).
- Float – Real numbers with or without decimal parts.
- String – Sequence of characters. All Strings or characters are enclosed between single or double quotes.
Values and Representation
- Boolean – True and False
- Integer – set of all integers, Z. Ex: 1,2,3,…..
- Complex – Contains all complex Numbers. Ex: 5i
- Float – Floating point numbers. Ex: 1.29
- 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))
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
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