Basic Python: 4. Python Data Types
Tags: python, beginners, programming, tutorial
Published: 2023-08-30 14:34:43

Every value in Python has a data type, which determines what kind of data it represents and what operations can be performed on it. Some common data types in Python are:
### Table of Contents

1. **Numeric Data Types**
    - 1.1 Integer (`int`)
    - 1.2 Floating-Point (`float`)
    - 1.3 Complex (`complex`)
2. **Textual Data Types**
    - 2.1 String (`str`)
    - 2.2 String Operations
3. **Sequence Data Types**
    - 3.1 List (`list`)
    - 3.2 Tuple (`tuple`)
    - 3.3 Range (`range`)
4. **Mapping Data Type**
    - 4.1 Dictionary (`dict`)
5. **Set Data Type**
    - 5.1 Set (`set`)
    - 5.2 Frozenset (`frozenset`)
6. **Boolean Data Type**
   - 6.1 **bool**: Boolean Values
7. **Binary Types**
   - 7.1 **bytes**: Bytes
   - 7.2 **bytearray**: Byte Arrays
8. **NoneType**
   - 8.1 **None**: The None Object
9. **Type Conversion (Casting)**
10. **User-Defined Classes**
11. **Checking Data Types**

### 1. Numeric Data Types

#### 1.1 Integer (`int`)

Integers represent whole numbers without decimal points. They can be positive or negative.

```python
x = 5
y = -10
```

#### 1.2 Floating-Point (`float`)

Floating-point numbers are numbers with decimal points. They are used to represent real numbers.

```python
pi = 3.14159
temperature = -2.5
```

#### 1.3 Complex (`complex`)

Complex numbers consist of a real part and an imaginary part.

```python
z = 2 + 3j
```

### 2. Textual Data Types

#### 2.1 String (`str`)

Strings are sequences of characters enclosed in single or double quotes.

```python
name = "Akash"
message = 'Hello, world!'
```

#### 2.2 String Operations

Strings support various operations like concatenation, slicing, and formatting.

```python
greeting = "Hello"
name = "Akash"
full_greeting = greeting + ", " + name  # Concatenation
substring = full_greeting[7:12]  # Slicing
formatted_message = f"{greeting}, {name}"  # String formatting
```

### 3. Sequence Data Types

#### 3.1 List (`list`)

Lists are ordered collections that can contain elements of different types.

```python
fruits = ["apple", "banana", "orange"]
```

#### 3.2 Tuple (`tuple`)

Tuples are similar to lists but are immutable.

```python
coordinates = (3, 5)
```

#### 3.3 Range (`range`)

Ranges represent sequences of numbers.

```python
numbers = range(1, 6)  # Creates a range from 1 to 5
```

### 4. Mapping Data Type

#### 4.1 Dictionary (`dict`)

Dictionaries store key-value pairs and allow fast lookup.

```python
person = {
    "name": "Akash",
    "age": 23,
    "city": "Pune"
}
```

### 5. Set Data Type

#### 5.1 Set (`set`)

Sets are unordered collections of unique elements.

```python
colors = {"red", "green", "blue"}
```

#### 5.2 Frozenset (`frozenset`)

Frozensets are like sets but are immutable.

```python
immutable_colors = frozenset({"red", "green", "blue"})
```

### 6. Boolean Data Type

#### 6.1 bool: Boolean Values

Boolean values represent truth (True) or falsehood (False).

```python
is_valid = True
```

### 7. Binary Types

#### 7.1 bytes: Bytes

Bytes represent sequences of bytes (integers from 0 to 255).

```python
data = b"hello"
```

#### 7.2 bytearray: Byte Arrays

Byte arrays are mutable sequences of bytes.

```python
mutable_data = bytearray(data)
```

### 8. None Type

#### 8.1 None: The None Object

The None object represents the absence of a value.

```python
result = None
```

### 9. User-Defined Classes

You can define your own data types using classes.

```python
class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y
```

### 10. Type Conversion (Casting)

Python allows you to convert between different data types using type casting functions.

```python
x = "5"
y = int(x)  # Casting to integer
```

### 11. Checking Data Types

Python provides the `type()` function to determine the data type of a value. This function returns the class type of the argument. Here's how you can use it:

```python
print(type(age))          # Output: <class 'int'>
print(type(temperature))  # Output: <class 'float'>
print(type(name))         # Output: <class 'str'>
print(type(is_student))   # Output: <class 'bool'>
print(type(fruits))       # Output: <class 'list'>
print(type(coordinates))  # Output: <class 'tuple'>
print(type(person))       # Output: <class 'dict'>
print(type(unique_numbers))  # Output: <class 'set'>
```