Python Data Types Overview

In Python, common data types include the following:

  1. Integer (int): Used to represent whole numbers, such as 1, 2, 3, etc.
  2. Floating-point number (float): Used to represent real numbers, such as 1.0, 2.5, 3.14, etc.
  3. Boolean (bool): Has only two possible values, True and False, used to represent logical truth or falsity.
  4. String (str): Used to represent a sequence of characters, which can include letters, numbers, punctuation marks, etc.
  5. List (list): Used to represent an ordered collection of data that can contain elements of any type, for example [1, 2, 'hello', True].
  6. Tuple (tuple): Similar to a list, used to represent an ordered collection of data; however, tuples are immutable, meaning their elements cannot be modified, for example (1, 2, 'hello').
  7. Set (set): Used to represent a collection of unique items; set operations such as intersection, union, and difference can be performed, for example {1, 2, 3}.
  8. Dictionary (dict): Used to represent a collection of key-value pairs, where both keys and values can be of any type, for example {'name': 'Tom', 'age': 20}.
  9. None: Represents an empty value or a missing value; commonly used to initialize variables or return function results.

In addition to these basic data types, Python also supports several advanced data types, such as:

  1. Numpy array (numpy.ndarray): Used to represent multi-dimensional arrays; commonly used for numerical and scientific computations.
  2. Pandas DataFrame (pandas.DataFrame): Used to represent two-dimensional tabular data; allows for data filtering, manipulation, and analysis.
  3. Custom class (class): Used to represent complex data structures; allows custom attributes and methods to describe the structure.
  4. Other data types: Python supports other data types as well, such as frozensets, byte strings (bytes), and byte arrays (bytearray).

Getting Data Types

type()

a = '230'
b = 230
print(type(a)) # dtype str
print(type(b)) # dtype int

isinstance()

a = '123'
b = 123
print(isinstance(a, str)) # True
print(isinstance(a, int)) # False
print(isinstance(b, str)) # False
print(isinstance(b, int)) # True

Detailed Explanation of Basic Data Types

String

String Definition

Example:

def print_strings():
    print("www.supremepole.com")  # Double quotes
    website = 'www.supremepole.com'  # Single quotes
    print(website)
    print("$")  # Single character
    empty = ""
    print(empty)  # Empty string

Output:

www.supremepole.com
www.supremepole.com
$

String Length

Example:

def length_of_a_string():
    random_string = "www.supremepole.com"  # 19 characters
    print(len(random_string))

Output:

19

Accessing Characters in a String

Example:

def accessing_character():
    website = "www.supremepole.com"
    first = website[0]  # Get the first character of the string
    print(first)
    dot = website[3]  # Get the first period
    print(dot)
    last = website[len(website) - 1]
    print(last)
    website = "www.supremepole.com"
    print(website[-1])  # Get the last character
    print(website[-5])  # Get the fifth character from the end

Output:

w
.
m
m
e

String Slicing and Reversing

Example:

def string_slicing():
    # Slice the string
    my_string = "This is MY string!"
    print(my_string[0:4])  # From the start till before the 4th index
    print(my_string[1:7])
    print(my_string[8:len(my_string)])  # From the 8th index till the end
    # Step size
    my_string = "This is MY string!"
    print(my_string[0:7])  # Step size is 1
    print(my_string[0:7:2])  # Step size is 2
    print(my_string[0:7:5])  # Step size is 5
    # Reverse the string
    my_string = "This is MY string!"
    print(my_string[13:2:-1])  # Reverse the string, step size is 1
    print(my_string[17:0:-2])  # Reverse the string, step size is 2
    # Partial slices
    my_string = "This is MY string!"
    print(my_string[:8])  # All characters before 'M'
    print(my_string[8:])  # All characters after 'M'
    print(my_string[:])  # All characters
    print(my_string[::-1])  # Reverse the string, step size is 1

Output:

This
his is
MY string!
This is
Ti s
Ti
rts YM si s
!nrsY ish
This is 
MY string!
This is MY string!
!gnirts YM si sihT

Numeric Types

There are two data types for numeric values in Python: integers and floating-point numbers.

Integer

Example:

def print_integers():
    print(10)  # Positive number
    print(-3000)  # Negative number
    num = 123456789  # Assign integer to variable
    print(num)
    num = -16000  # Reassign the variable
    print(num)

Run Result:

10
-3000
123456789
-16000

Floating-point Number

Example:

def print_floating():
    print(1.00000000005)  # Positive floating-point number
    print(-85.6701)  # Negative floating-point number
    flt_pt = 1.23456789
    print(flt_pt)

Run Result:

1.00000000005
-85.6701
1.23456789

Boolean Type

There are two types of boolean values in Python: True and False.
Example:

def print_booleans():
    print(True)
    f_bool = False
    print(f_bool)

Run Result:

True
False