Data Type

Understanding Data Types in Python

In Python, data types are special keywords that tell the interpreter how we are going to use the data. That means certain types of values can do certain things. Python comes with a set of built-in data types that can be used to construct more complex data structures.

Given below are the most commonly used Python data types:

  • Numeric
  • Sequence Type
  • Boolean
  • Set
  • Dictionary

Numeric

In Python, numeric data type represent the data which has numeric value. There are three distinct numeric types: integers, floating point numbers, and complex numbers.

1x = 10    # integer
2y = -15.6 # floating point
3z = 4j    # complex number

Sequence Type

In Python, sequence type includes String, List, Tuple. They are ordered collections of similar or different data types.

1str = "Codebay"  # String
2list = [1, 2.2, 'python', 4j]  # List
3tuple = (10, 'welcome', 20.3)  # Tuple

Boolean

In Python, boolean datatype is also a data type that has one of the two built-in values, True or False. Often used in conditional expressions, and anywhere else you need to distinguish between truth and falsehood.

1bool1 = True  # Bool set to True
2bool2 = False  # Bool set to False

Set

In Python, Set is an unordered collection data type that is iterable, mutable and has no duplicate elements. Python set class represents the mathematical notion of a set.

1set1 = {1, 2, 3, 4, 5}  # An example of set

Dictionary

A dictionary is a set of key:value pairs. All keys in a dictionary must be unique. In a dictionary, keys are like an index, that is used to access its respective value.

1dict = {'name': 'Codebay', 'year': 2022}  # An example of dictionary

By understanding Python data types, you can write more flexible and optimized code. It is crucial to select the most suitable data type for a particular program to solve problems efficiently.

Did you know?

Python is dynamically typed, which means the same variable name can be used to hold different data types. But, choosing the correct type, can optimize the performance of your program.