Skip to main content
Python Data Types With Examples

Python Data Types With Examples

A data type is a classification of data based on its type. Classification is required so that both we and the computer understand how the data will be used. Data types are present in all programming languages. So, what data types does the Python programming language support?

This time, we’ll go over different data types in the Python programming language, with examples.

Integer Data Type

The integer data type is a type of numeric data that is commonly used when dealing with integers. As an example, consider the numbers 1, 2, 3, and so on. This data type also distinguishes between positive and negative numbers (signed numbers). The following is an example of using the integer data type in Python:

a = 5
b = -5

print(a)
print(b)

Output:

5
-5

Float Data Type

The following data type in Python is float. Float, like integer, is a numeric data type. The float data type, on the other hand, can handle fractional or decimal fractions. A period is used as a decimal separator. Here’s an example of how to use the float data type:

a = 5.5
print(a)

Output:

5.5

String Data Type

The string data type is a text-handling data type. This data type is denoted by quotation marks at the start and end of the data. The data contained within the quotation marks, whether numbers or letters, will be treated as a string. An example of using the string data type is shown below:

name = "Sribuhost"
print(name)

Output:

Sribuhost

Boolean Data Type

The boolean data type has only two possible values true and false. This data type is typically used to represent a branch condition. Capital letters are used to denote true and false values (True and False). Here’s an example of how to use the boolean data type:

a = True

if a:
    print("Yes")
else:
    print("No")

Output:

Yes

Related post: Check Prime Number in Python With Flowchart


List Data Type

The list data type is a container for other data types. If the list data type is analogous to a cupboard. There are several shelves in the cupboard that represent various data types. The list data type is indicated by placing square brackets at the beginning and end of the data.

You can use index to access data on a specific rack. The index is the serial number of data in a list that starts at zero. The following is an example of how to use the list data type:

colors = ["Red", "Green", "Blue"]

print(colors)
print(colors[1])

Output:

["Red", "Green", "Blue"]
Green

Tuple Data Type

The tuple data type is the next one. Tuple data types are similar to list data types. The nature and marking of these two data types distinguishes them. The tuple data type is immutable. Parentheses start and end the tuple data type. The following is an example of how to use the tuple data type:

colors = ("Red", "Green", "Blue")

print(colors)
print(colors[2])

Output:

("Red", "Green", "Blue")
Blue

Dictionary Data Type

The dictionary data type is the following data type. This data type, like list data types, holds several other data types. The index and writing are what distinguishes these two data types. The dictionary data type is denoted by curly brackets at the start and end of the data. The index on this data type, also known as the key, is written manually. Here’s an example of how to use the dictionary data type:

student = {"name": "Smith", "age": 18}
print(student)
print(student["name"])

Output:

{"name": "Smith", "age": 18}
Smith

Set Data Type

A set data type is a data type in which no two members are the same. If two members have the same name, one of them will be deleted. Curly brackets can be used to store data with a set data type. This data type is typically used to locate intersections, joins, differences, and so on. Here’s an example of how to use the set data type:

a = {2, 7, 1, 1}
print(a)

Output:

{1, 2, 7}

Related post: Python Operators With Examples


Conclusion

That’s the data type found in the Python programming language. Aside from the data types mentioned above, the Python programming language has several others. It is documented in the Python documentation. If you have any questions, please leave them in the comments section.

Thank you. I hope it is useful to all of us.

Share this

Shafy Gunawan

I’m a web developer. I spend my whole day, practically every day, experimenting with HTML, CSS, JavaScript, and PHP; dabbling with Python programming language; and share the knowledge that I learned.

Leave a Reply

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