Sitemap
Vipul Kaitke

Vipul Kaitke

Developer

Datatypes in Python

4 min readFeb 5, 2024

There are 7 types of datatypes present in Python:-

  1. Numeric Datatypes:- int, float, complex.
  2. String datatype:- str.
  3. Sequence datatype:- list, tuple, range.
  4. Binary datatype:- bytes, byte array, memory view.
  5. Mapping Datatype:- Dict.
  6. Boolean Datatype:- Bool.
  7. Set Datatype:- set.
  8. Numeric Datatypes:-> Python numeric datatype is used to hold numeric values like:

i. int: it holds signed integers of non-limited length

ii. long holds long integers.

iii. float holds floating precision numbers and its accurate upto 15 decimal places

iv. complex holds complex no's

Get Vipul Kaitke’s stories in your inbox

Join Medium for free to get updates from this writer.

In python we need not declare a datatype while declaring a variable like c or c++. We can simply just assign values in a variable but if we want to see what type of numerical values is it holding right now, we can use `type()`, like this.

Press enter or click to view image in full size

If we run the above code you will see o/p like the below image.

Press enter or click to view image in full size

2. String Datatype:-> the string in a sequence of characters Python supports Unicode characters. Generally, String are represented by either single or double-quotes.

Press enter or click to view image in full size

If we run the above code you will see o/p like the below image.

Press enter or click to view image in full size

3. list Datatype:-> The list is a versatile datatype exclusive in python. In a sense, it is the same as the array in C/C++. But the interesting link about the list in python is it can simultaneously hold different types of data. Formally list is an ordered sequence of some data written using square bracket ‘([])’ and ‘(,)’.

Press enter or click to view image in full size

If we run the above code you will see o/p like the below image.

Press enter or click to view image in full size

4. Tuple:-> The ‘Tuple’ is another datatype which is sequence of data similar to a list. But it is immutable. That means data in a tuplee is write- protected. Data is a tuple is written using parenthesis and commas.

Press enter or click to view image in full size

The O/P of this above Python datatype tuple examples code will be like the below image.

Press enter or click to view image in full size

5. Dict is an unordered sequence of data of keys-value pair form. It is similar to the hash type. Dictionaries are written within curly braces in the form “key: value”. It is very useful to retrive data in an optimized way among a large amount of data.

Press enter or click to view image in full size

The O/P of this above Python datatype tuple examples code will be like the below image.

Press enter or click to view image in full size

6. Bool:-> Datatype with one of the two built-in values true or false. Boolean objects that are equal to true or truthy, and these equal to false are falsie. However non-boolean objects can be evaluated in the boolean context as well and determined to be truth or false. It is denoted by the class bool.

Note: True or False with capital ‘T’ and ‘F’ are valid booleans otherwise python will through an error.

The O/P of this above Python datatype tuple examples code will be like the below image.

Press enter or click to view image in full size

7. Sets:-> A set in python Programming is an unordered collection datatype that is iterable, mutable and has no duplicate elements. Sets are represented by ‘{}’ Curly braces. The major advantages of using a set, as apposed to a list, is that is has a highly optimized method for checking whether a specific element is contained in the set. This based on a data structure known as a hash table. Since sets are unordered, we cannot access items using indexes as we do in lists.

The O/P of the above code set example code will be like the below image.

So that’s all for today. Try to write code in your own language don’t just copy-paste the code or the lines of the code. #HAPPY_CODING.

--

--