Python Data Types

Python Data Types

Python Data Types define the nature of data that can be stored and manipulated in variables. Python has several built-in data types that are commonly used for different purposes. Understanding and correctly using these data types is crucial for writing effective and bug-free Python programs. Here are some commonly used Python data types:

Numeric Types

int: Represents integer values, such as 42 or -10.

x = 42
y = -10

float: Represents floating-point numbers with decimal places, such as 3.14 or -2.5.

pi = 3.14
temperature = -2.5

complex: Represents complex numbers with real and imaginary parts, such as 2 + 3j or -1.5 + 0.5j.

z = 2 + 3j
w = -1.5 + 0.5j

Sequence Types

str: Represents strings of characters, such as “Hello, World!” or ‘Python’.

message = "Hello, World!"
name = 'Python'

list: Represents ordered collections of items, enclosed in square brackets ([]), such as [1, 2, 3] or [‘apple’, ‘banana’, ‘orange’].

numbers = [1, 2, 3, 4, 5]
fruits = ['apple', 'banana', 'orange']

tuple: Represents ordered, immutable collections of items, enclosed in parentheses (()), such as (1, 2, 3) or (‘red’, ‘green’, ‘blue’).

coordinates = (10, 20)
colors = ('red', 'green', 'blue')

Mapping Type

dict: Represents key-value pairs, enclosed in curly braces ({}), such as {‘name’: ‘John’, ‘age’: 25}.

person = {'name': 'John', 'age': 25, 'city': 'New York'}

Set Types

set: Represents an unordered collection of unique elements, enclosed in curly braces ({}), such as {1, 2, 3} or {‘apple’, ‘banana’, ‘orange’}.

primes = {2, 3, 5, 7, 11}
vowels = {'a', 'e', 'i', 'o', 'u'}

frozenset: Represents an immutable set, enclosed in parentheses (()), such as frozenset([1, 2, 3]).

frozen_numbers = frozenset([1, 2, 3, 4, 5])

Boolean Type

bool: Represents the truth values True or False, used for logical operations and control flow.

is_valid = True
has_permission = False

None Type

None: Represents the absence of a value or a null value.

result = None

These data types provide different functionalities and operations. For example, you can perform arithmetic operations on numeric types, manipulate strings with string methods, access and modify elements in lists or tuples, and use dictionary keys to retrieve corresponding values.

Python also allows for type conversion or casting, which allows you to convert one data type to another as needed.

It’s important to understand the characteristics and behaviours of different data types to effectively utilize them in your programs and ensure data integrity and consistency.

Leave a Reply

Your email address will not be published. Required fields are marked *