SomeQuiz

Course

Python Data Types

Co-Founder

Top Contributor

Nuwan Perera

@nuwanperera.me

Data types in Python represent the type of values that can be stored and manipulated in variables. Python is a dynamically typed language, which means you don't need to explicitly declare the data type of a variable. The interpreter determines the data type based on the value assigned to it. Here are some commonly used data types in Python:

  1. Numeric Types:
  • int: Represents integer values (e.g., 1, 100, -10).
  • float: Represents floating-point values with decimal places (e.g., 3.14, -2.5).
  1. Sequence Types:
  • str: Represents a string of characters (e.g., "hello", 'world').

  • list: Represents an ordered collection of items (e.g., [1, 2, 3]).

  • tuple: Represents an ordered, immutable collection of items (e.g., (1, 2, 3)).

  1. Mapping Type:
  • dict: Represents a collection of key-value pairs (e.g., {"name": "John", "age": 25}).
  1. Set Types:
  • set: Represents an unordered collection of unique items (e.g., 3).

  • frozenset: Represents an immutable set (e.g., frozenset(3)).

  1. Boolean Type:
  • bool: Represents the truth values True or False.
  1. None Type:
  • None: Represents the absence of a value or a null value.

Python Numeric Data Types

In Python, numeric data type is used to hold numeric values.

Integers, floating-point numbers and complex numbers fall under Python numbers category. They are defined as int, float and complex classes in Python.

int - holds signed integers of non-limited length.

float - holds floating decimal points and it's accurate up to 15 decimal places.

complex - holds complex numbers.

We can use the type() function to know which class a variable or a value belongs to.

SomeNum1 = 7
print(SomeNum1, "is type of", type(SomeNum1))
 
SomeNum2 = 9.0
print(SomeNum2, "is type of", type(SomeNum2))
 
SomeNum3 = 1 + 2S
print(SomeNum3, "is type of", type(SomeNum3))

Output:

7 is type of <class 'int'>
9.0 is type of <class 'float'>
(1+2j) is type of <class 'complex'>