Tuesday, March 12, 2024

Python Data Types - Complete Guide

 



 

A data type is a classification that specifies what type of value a variable has and what types of operations can be performed on it. Here's a list of the main data types in Python


1. Numbers:

Integers (int): 

Whole numbers, both positive and negative, without decimals. Like 3, -5, 42.

Floating-point numbers (float):

 Numbers with decimal points or in exponential form, such as 3.14, -0.001, or 2e2

2.  Text:

Strings (str): 

Text or characters. Anything inside quotes is a string, like "Hello!", "1234", or even "True".

3. Boolean:

Booleans (bool):

 Represents truth or falsehood. Only two possible values: True or False. Great for making decisions in your code.

4. Sequences: 

Lists (list): 

A list of items in an order. You can change the items (mutable). For example, [1, 'apple', True].

Tuples (tuple): 

Like a list, but you can't change it after making it (immutable). Useful for fixed data. Example: (1, 'banana', False).

5. Mappings:

Dictionaries (dict): 

Stores pairs of items as keys and values. Like a real dictionary with words and their meanings. Example: {'name': 'John', 'age': 30}.

6. Sets:

Sets (set):

 A collection of unique items, without order. Great for when you need to ensure no duplicates. Example: {1, 2, 3, 4}.

7. None Type:

None (NoneType): 

Special type representing the absence of a value, or "nothing". It's like saying a variable is empty or has no value.