DEV Community

Cover image for Data types in Python
Afraaz Ahmed
Afraaz Ahmed

Posted on

Data types in Python

Data types are one of the building blocks of python.
And You can do a lot of things with data types!

Fact: In python, all data types are implemented as an object.

A data type is like a specification of what kind of data we would like to store in memory and python has some built-in data types in these categories:

  • Text type: str
  • Numeric types: int, float, complex
  • Sequence types: list, tuple, range
  • Mapping type: dict
  • Set types: set, frozenset
  • Boolean type: bool
  • Binary types: bytes, bytearray, memoryview

Now, let's demistify all these data types by using type() function to display the data type of the variable.

Text type

str

  • str stands for string in python used for storing text in python.
  • Strings can be written either in single quotes or double qoutes in python, hence your choice.

Example:

Image description

Output:

Hello, world!
<class 'str'>
Enter fullscreen mode Exit fullscreen mode

Numeric types

int

  • int stands for integer used to store integers (positive and negative numbers). Example:

Image description

Output:

4
<class 'int'>
Enter fullscreen mode Exit fullscreen mode

float

  • float stands for floating-point numbers (decimal point numbers)

Example:

Image description

Output:

3.14
<class 'float'>
Enter fullscreen mode Exit fullscreen mode

complex

  • Complex numbers have a real and imaginary part, which are each a floating point number.
  • Complex numbers can be written in two forms:
  • real + (imag)j
  • complex(real, imag)

Example:

Image description

Output:

(5+10j)
<class 'complex'>
Enter fullscreen mode Exit fullscreen mode

Sequence types

list

  • A list is data type where you can store a collection of data
  • A list can also contain different data types
  • A list is ordered and changeable and allows duplicate members

Example:

Image description

Output:

['Captain America', 'Iron Man', 'Thor', 'Hulk', 'Black Widow', 'Hawkeye']
<class 'list'>
Enter fullscreen mode Exit fullscreen mode

tuple

  • A tuple is data type where you can store a collection of data
  • A tuple can also contain different data types
  • A tuple is ordered and unchangeable and allows duplicate members

Example:

Image description

Output:

('Captain America', 'Iron Man', 'Thor', 'Hulk', 'Black Widow', 'Hawkeye')
<class 'tuple'>
Enter fullscreen mode Exit fullscreen mode

range

  • The range type represents an immutable (unchangable) sequence of numbers
  • Commonly used for looping a specific number of times in for loops.

Example:

Image description

Output:

range(0, 10)
<class 'range'>
Enter fullscreen mode Exit fullscreen mode

Mapping type

dict

  • dict stands for dictionary in python
  • Dictionaries are used to store data values in key:value pairs
  • A dictionary is a collection which is unordered, changeable and does not allow duplicates

Example:

Image description

Output:

{'Learning': 'Programming', 'Language': 'Python', 'Day': 4}
<class 'dict'>
Enter fullscreen mode Exit fullscreen mode

Set types

set

  • A set is data type where you can store a collection of data
  • A set can also contain different data types
  • A set is unordered and unindexed and allows no duplicate members

Example:

Image description

Output:

{'Black Widow', 'Iron Man', 'Thor', 'Hawkeye', 'Hulk', 'Captain America'}
<class 'set'>
Enter fullscreen mode Exit fullscreen mode

frozenset

  • frozenset data type can be created by frozenset() function
  • The frozenset() function accepts an iterable and returns an unchangeable frozenset object (which is like a set object, only unchangeable)

Example:

Image description

Output:

frozenset({'cherry', 'banana', 'apple'})
<class 'frozenset'>
Enter fullscreen mode Exit fullscreen mode

Boolean type

bool

  • bool stands for boolean in python
  • Booleans represent one of two values: True or False

Example:

Image description

Output:

True
<class 'bool'>
False
<class 'bool'>
Enter fullscreen mode Exit fullscreen mode

Binary types

bytes

  • bytes data type can be created in two forms:
  • bytes() function
  • prefix 'b'

Example:

Image description

Output:

b'hello'
<class 'bytes'>
b'Hello'
<class 'bytes'>
Enter fullscreen mode Exit fullscreen mode

bytearray

  • bytearray() function returns a bytearray object
  • It can convert objects into bytearray objects

Example:

Image description

Output:

bytearray(b'\x00\x00\x00\x00')
<class 'bytearray'>
Enter fullscreen mode Exit fullscreen mode

memoryview

  • memoryview() function returns a memory view object from a specified object

Example:

Image description

Output:

<memory at 0x2b4f7a8a7408>
<class 'memoryview'>
Enter fullscreen mode Exit fullscreen mode

Note
As you might have observed earlier, some data types can be also implemented using their constructors.
This same technique can also be applied to every data type.
Example:

Image description

Output:

Hello, World!
4
3.14
(5+10j)
['Captain America', 'Iron Man', 'Thor', 'Hulk', 'Black Widow', 'Hawkeye']
('Captain America', 'Iron Man', 'Thor', 'Hulk', 'Black Widow', 'Hawkeye')
range(0, 10)
{'Learning': 'Programming', 'Language': 'Python', 'Day': 4}
{'apple', 'cherry', 'banana'}
frozenset({'banana', 'cherry', 'apple'})
True
False
b'\x00\x00\x00\x00'
bytearray(b'\x00\x00\x00\x00')
<memory at 0x2b8346a29408>
Enter fullscreen mode Exit fullscreen mode

Best Resources

Who Am I?

I’m Afraaz Ahmed, a Software Engineering Nerd who loves building Web Applications, now sharing my knowledge through Blogging during the busy time of my freelancing work life. Here’s the link to all of my socials categorized by platforms under one place: https://linktr.ee/afraazahmed

Thank you so much for reading my blog🙂.

Top comments (0)