Python Data Types


I hope you guys are doing well and having fun while coding. Today we are going to learn Python data types. There are mainly six data types in Python. Today we will see them in detail.

  1. Numeric Types
  2. Sequence Types
  3. Mapping Type
  4. Set Types
  5. Boolean Type
  6. Binary Types

Numeric Types:

Numeric data type includes integer, floating points and complex type of numbers. In Python, we have int, float, and complex for that. We can use the type function to check the type of any variable.

a = 9
print("Type of a is ",type(a))

b = 9.9
print("Type of b is ",type(b))

c = 9+3j
print("Type of c is ",type(c))

Output,

Type of a is <class 'int'> 
Type of b is <class 'float'> 
Type of c is <class 'complex'>

Sequence Types:

Sequence Types include list, tuples, and range. They are used to store multiple items in a single variable. The difference between lists and tuples is lists are changeable and tuples are unchangeable. To know more about the Python list and its methods go check out my blogs Play with Python list – Part 1 and Play with Python list – Part 2.

a = ['a', 'b', 'b']
print("Type of a is ",type(a))

b = ('a', 'b', 'c')
print("Type of b is ",type(b))

c = range(6)
print(c)
print("Type of c is ",type(c))

Output,

Type of a is <class 'list'> 
Type of b is <class 'tuple'> 
range(0, 6) 
Type of c is <class 'range'>

Mapping Type:

Mapping Type includes dictionary type of data in Python it called ‘dict’. We usually uese dictionary store key avd value type of data, just like list dictiory can also stored multiple items and is changeble but in list we can store duplicate items whereas in dictionary we can not store duplicate items.

x = {"first_name" : "Chandni", "last_name" : "Soni"}
print(x)
print("Type of a is ",type(x))

Output,

{'first_name': 'Chandni', 'last_name': 'Soni'}
Type of a is  <class 'dict'>

Set Types:

Set Types include set and frozenset. Set are used to store multple items, they are unchangable, unordered and they do not allow duplicate values. Frosenset functi2on makes the list freeze which means you can not change that.

example of set,

x = {"a", "b", "c"}
print(x)
print("Type of a is ",type(x))

Output,

{'b', 'a', 'c'} 
Type of a is <class 'set'>

exaple of frozenset,

x = frozenset({"a", "b", "c"})
print(x)
print("Type of a is ",type(x))

Output,

frozenset({'c', 'a', 'b'}) 
Type of a is <class 'frozenset'>

Boolean Type:

Boolean Type includes boolean data, in Python, it is called ‘bool’ type. It can be either ‘True’ or ‘False’.

x = True
print(x)
print("Type of a is ",type(x))

y= False
print(y)
print("Type of a is ",type(y))

Output,

True
Type of a is  <class 'bool'>
False
Type of a is  <class 'bool'>

Binary Types:

Binary Types include bytes, bytearray, and memoryview types. Bytes and Bytearray both store bytes which are between 0 to 255, the main difference between them is bytes are immutable whereas bytearray is mutable. Memoryview is used to store large objects and memoryview is also used to wrap the bytearray.

a = b'Hi'
print(a)
print("Type of a is ",type(a))

b = bytearray(9)
print(b)
print("Type of b is ",type(b))

c = memoryview(bytes(9))
print(c)
print("Type of c is ",type(c))

Output,

b'Hi' 
Type of a is <class 'bytes'> bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00') 
Type of b is <class 'bytearray'> 
<memory at 0x7f324d344e20> 
Type of c is <class 'memoryview'>

Thank you for reading 🙂

Happy coding 🙂

Comments are closed.

Create a website or blog at WordPress.com

Up ↑

Design a site like this with WordPress.com
Get started