Built-in Data Types with Python

Built-in Data Types

Python provides several built-in data types that are essential for handling various kinds of data. These types are fundamental to programming in Python and include numeric types, sequence types, collection types, associative types, and special types.

Numeric Types

int (Integer)

  • Description: Represents whole numbers without any decimal point. Can be positive, negative, or zero.

Examples

a = 42
b = -7
  •  Supported Operations: Addition, subtraction, multiplication, division, modulo, exponentiation, and comparison operations.
  • Conversion: Convert other types to integers using the int() function. 
x = int(3.14)  # x = 3
y = int("123") # y = 123

float (Floating-point number)

  • Description: Represents real numbers with decimal points.

Examples

c = 3.14
d = -0.001
  •  Supported Operations: Addition, subtraction, multiplication, division, exponentiation, and comparison operations.
  • Conversion: Convert other types to floating-point numbers using the float() function. 
x = float(10)    # x = 10.0
y = float("2.5") # y = 2.5

 complex (Complex number)

  • Description: Represents complex numbers in the form a + bj, where a is the real part and b is the imaginary part.

Examples

e = 1 + 2j
f = -3 - 4j
  •  Supported Operations: Addition, subtraction, multiplication, division, and comparison for real and imaginary parts.
  • Conversion: Create complex numbers using the complex() function. 
x = complex(2, 3)  # x = 2 + 3j

 Sequence Types

str (String)

  • Description: Represents a sequence of characters. Strings are immutable in Python, meaning their values cannot be changed once created.

Examples

g = "Hello, World!"
h = 'Python'
  •  Supported Operations: Concatenation, repetition, slicing, and various methods for text manipulation (e.g., upper(), lower(), replace()).
  • Conversion: Convert other types to strings using the str() function. 
    x = str(123)     # x = '123'
    y = str(3.14)    # y = '3.14

list

  • Description: Represents an ordered, mutable collection of items that can be of different types.

Examples

i = [1, 2.5, "Python", True]
j = [3, [1, 2], "hello"]
  •  Supported Operations: Indexing, appending, removing, and modifying elements, and various methods like append(), remove(), extend().
  • Conversion: Convert other types to lists using the list() function. 
x = list("hello")  # x = ['h', 'e', 'l', 'l', 'o']

 tuple

Description: Represents an ordered, immutable collection of items. Tuples are often used to group related values. 

x = tuple([1, 2, 3])  # x = (1, 2, 3)

 Examples

k = (1, 2.5, "Python", True)
l = (3, (1, 2), "hello")
  •  Supported Operations: Indexing, but no modification (immutable). Methods include count() and index().
  • Conversion: Convert other types to tuples using the tuple() function.

range

Description: Represents an immutable sequence of numbers, typically used for iteration in loops.

Examples

m = range(5)     # represents the sequence [0, 1, 2, 3, 4]
n = range(2, 10) # represents the sequence [2, 3, 4, 5, 6, 7, 8, 9]
  •  Supported Operations: Indexing and conversion to lists or tuples for manipulation.

Collection Types

set

  • Description: Represents an unordered collection of unique elements. Useful for removing duplicates.

Examples

o = {1, 2, 3, 4}
p = {2, 4, 6, 8}
  •  Supported Operations: Union, intersection, difference, addition, and removal of elements. Methods include add(), remove(), discard(), union().
  • Conversion: Convert other types to sets using the set() function. 
x = set([1, 2, 2, 3])  # x = {1, 2, 3}

 frozenset

  • Description: Represents an immutable set. Similar to set, but cannot be modified after creation.

Examples

q = frozenset([1, 2, 3, 4])
  •  Supported Operations: Union, intersection, difference, but no modification. Methods include union(), intersection().

Conversion: Convert other types to frozensets using the frozenset() function. 

x = frozenset([1, 2, 2, 3])  # x = frozenset({1, 2, 3})

Associative Types

dict (Dictionary)

  • Description: Represents a collection of key-value pairs. Dictionaries are used to store data in a way that is easy to look up by key.

Examples

r = {"name": "Alice", "age": 30}
s = {1: "one", 2: "two", 3: "three"}
  • Supported Operations: Access by key, addition, deletion, and modification of key-value pairs. Methods include keys(), values(), items().
  • Conversion: Create dictionaries from key-value pairs. 
python
x = dict([(1, 'a'), (2, 'b')])  # x = {1: 'a', 2: 'b'}

 

2.5. Special Type

NoneType

  • Description: Represents the absence of a value or a null value. The only value of this type is None.

Examples

t = None

 Supported Operations: Comparison operations (is, is not), but no arithmetic or modification operations.

Summary

Python provides several built-in data types that are fundamental to data handling in your programs. These include:

  • Numeric Types: int, float, complex
  • Sequence Types: str, list, tuple, range
  • Collection Types: set, frozenset
  • Associative Type: dict
  • Special Type: NoneType

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *