How to Master Python Built in Data Types with Examples

This video showcases the common data types used in Python programming. With specific code examples, we explain the definitions and uses of strings, integers, floats, complex numbers, lists, tuples, dictionaries, sets, booleans, and binary data types. The content is presented in an easy-to-understand manner, helping you quickly grasp these fundamental data types, making it an ideal tutorial to boost your Python programming knowledge.

1. Video.

2. Python Source Code.

def example_string_type():
    x = "Python Programming"
    print(f"String type: {type(x)} -> {x}")

    y = str("Simple is better than complex")
    print(f"String type: {type(y)} -> {y}")

def example_number_type():
    x_int = 42
    x_int = int(42)

    x_float = 3.14
    x_float = float(3.14)

    x_complex = 2 + 3j
    x_complex = complex(2 + 3j)

    print(f"Number type int: {type(x_int)} -> {x_int}")
    print(f"Number type float: {type(x_float)} -> {x_float}")
    print(f"Number type complex: {type(x_complex)} -> {x_complex}")

def example_sequence_type():
    x_list = ["dog", "cat", "mouse"]
    x_list = list(("dog", "cat", "mouse"))

    x_tuple = ("dog", "cat", "mouse")
    x_tuple = tuple(("dog", "cat", "mouse"))

    x_range = range(10)
    
    print(f"Sequence type list: {type(x_list)} -> {x_list}")
    print(f"Sequence type tuple: {type(x_tuple)} -> {x_tuple}")
    print(f"Sequence type range: {type(x_range)} -> {list(x_range)}")

def example_mapping_type():
    x_dict = {"brand": "Ford", "model": "Mustang", "year": 1964}
    print(f"Mapping type dict: {type(x_dict)} -> {x_dict}")

def example_set_type():
    x_set = {"apple", "orange", "pear"}
    x_frozenset = frozenset({"apple", "orange", "pear"})
    print(f"Set type set: {type(x_set)} -> {x_set}")
    print(f"Set type frozenset: {type(x_frozenset)} -> {x_frozenset}")

def example_boolean_type():
    x_bool = False
    print(f"Boolean type bool: {type(x_bool)} -> {x_bool}")

def example_binary_type():
    x_bytes = b"World"
    x_bytearray = bytearray(10)
    x_memoryview = memoryview(bytes(10))
    print(f"Binary type bytes: {type(x_bytes)} -> {x_bytes}")
    print(f"Binary type bytearray: {type(x_bytearray)} -> {x_bytearray}")
    print(f"Binary type memoryview: {type(x_memoryview)} -> {x_memoryview}")

def main():
    example_string_type()
    example_number_type()
    example_sequence_type()
    example_mapping_type()
    example_set_type()
    example_boolean_type()
    example_binary_type()

if __name__ == "__main__":
    main()

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.