Python Data Types

  • Post author:
  • Post comments:0 Comments
  • Reading time:73 mins read

In Python, data types are used to classify one particular type of data, determining the values that you can assign to the type and the operations you can perform on it.

Python has several built-in data types, which can be grouped into the following categories:

  • Numeric: int, float, complex
  • Boolean: bool
  • Sequence: str, list, tuple, range
  • Set: set, frozenset
  • Mapping: dict

1) Numeric Data Types

int (integers)

An integer is a whole number, positive or negative, without a decimal point. You can use the int data type to store whole numbers ranging from -2147483648 to 2147483647.

Here are some examples of int values:


x = 42

y = -1234

z = 0

2) Float

float (floating-point numbers)

A float is a number with a decimal point. You can use the float data type to store fractional numbers, such as 3.14 or -9.81.

Here are some examples of float values:


x = 3.14

y = -9.81

z = 0.0

3) Complex Data Types

complex (complex numbers)

A complex number is a number with a real and imaginary part. You can use the complex data type to store complex numbers, such as 3 + 4j or -1.2 + 3.4j.

Here are some examples of complex values:


x = 3 + 4j

y = -1.2 + 3.4j

z = 0 + 0j

4) Boolean Data Type

The bool data type represents a boolean value, which can be either True or False.

Here are some examples of bool values:


x = True

y = False

5) Sequence Data Types

str (strings)

A string is a sequence of characters, such as “hello” or “goodbye”. You can use the str data type to store a string.

You can create a string by enclosing characters in single quotes (‘) or double quotes (“).

Here are some examples of str values:


x = 'hello'

y = "goodbye"

z = "I'm a string"

6) Lists

list (lists)

A list is an ordered collection of items. You can use the list data type to store a list of items.

You can create a list by enclosing a comma-separated sequence of items in square brackets ([]).

Here are some examples of list values:


x = [1, 2, 3]

y = ['apple', 'banana', 'cherry']

z = ['a', 1, 3.14, True]

7) Tuples

tuple (tuples)

A tuple is an immutable sequence of items. You can use the tuple data type to store a tuple of items. You can create a tuple by enclosing a comma-separated sequence of items in parentheses (()).


x = (1, 2, 3)

y = ('apple', 'banana', 'cherry')

z = ('a', 1, 3.14, True)

8) Ranges

range (ranges)

A range is an immutable sequence of numbers. You can use the range data type to store a range of values.

You can create a range by calling the range function with two or three arguments: the start value, the stop value, and (optionally) the step value.

Here are some examples of range values:


x = range(0, 3)      # [0, 1, 2]

y = range(2, 10, 2)  # [2, 4, 6, 8]

z = range(10, 2, -2) # [10, 8, 6, 4]

9) Set Data Types

set (sets)

A set is an unordered collection of unique items. You can use the set data type to store a set of items.

You can create a set by enclosing a comma-separated sequence of items in curly braces ({}).

Here are some examples of set values:


x = {1, 2, 3}

y = {'apple', 'banana', 'cherry'}

z = {'a', 1, 3.14, True}

10) Frozenset

frozenset (frozen sets)

A frozen set is an immutable set of unique items. You can use the frozenset data type to store a frozen set of items.

You can create a frozen set by calling the frozenset function with a set as an argument.

Here are some examples of frozenset values:


x = frozenset({1, 2, 3})

y = frozenset({'apple', 'banana', 'cherry'})

z = frozenset({'a', 1, 3.14, True})

11) Mapping Data Type

dict (dictionaries)

A dictionary is a collection of key-value pairs. You can use the dict data type to store a dictionary.

You can create a dictionary by enclosing a comma-separated sequence of key-value pairs in curly braces ({}).

Here are some examples of dict values:


x = {'a': 1, 'b': 2}

y = {'apple': 5, 'banana': 3, 'cherry': 7}

z = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
Publisher

Publisher

Publisher @ideasorblogs

Leave a Reply