Day 14 Task: Python Data Types and Data Structures for DevOps

Day 14 Task: Python Data Types and Data Structures for DevOps

ยท

3 min read

Data Types:

In Python, data types are fundamental categories used to classify different types of data that variables can hold. Python is a dynamically-typed language, which means that you don't need to explicitly declare the data type of a variable. Instead, Python infers the data type based on the value assigned to the variable. Here are some of the common data types in Python:

  1. Numeric Types:

    • int: Integer data type for whole numbers. Example: x = 10.

    • float: Floating-point data type for numbers with decimal points. Example: y = 3.14.

    • complex: Complex number data type with a real and an imaginary part. Example: z = 2 + 3j.

  2. Text Type:

    • str: String data type for sequences of characters, enclosed in single or double quotes. Example: name = "John".
  3. Boolean Type:

    • bool: Boolean data type representing True or False values. Example: is_active = True.
  4. Sequence Types:

    • list: A mutable ordered collection that can hold elements of different data types. Example: my_list = [1, 2, "hello"].

    • tuple: An immutable ordered collection. Once created, its elements cannot be changed. Example: my_tuple = (10, 20, 30).

  5. Set Types:

    • set: An unordered collection of unique elements. Example: my_set = {1, 2, 3}.

    • frozenset: An immutable set. Once created, its elements cannot be changed. Example: frozen_set = frozenset({4, 5, 6}).

  6. Mapping Type:

    • dict: A collection of key-value pairs, where keys are unique. Example: my_dict = {"name": "Alice", "age": 25}.
  7. NoneType:

    • None: A special data type representing the absence of a value, similar to null in other languages. Example: my_variable = None.

Python Data Types Overview

Data Structures:

Data structures are fundamental constructs used to organize and store data in a way that facilitates efficient data manipulation and retrieval. They provide a way to represent and manage data effectively, making it easier to perform various operations and algorithms.

  1. ๐Ÿ“š Array:

    • ๐Ÿ“ Collection of elements stored in contiguous memory locations.

    • ๐Ÿ“Œ Elements accessed by index for quick retrieval.

    • โš–๏ธ Constant time access, but may require resizing for changes.

  2. ๐Ÿ”— Linked List:

    • ๐Ÿš„ Linear collection of nodes, each containing data and a reference to the next node.

    • โžก๏ธ Efficient insertions and deletions.

    • ๐Ÿ•ฐ๏ธ Linear time access to elements.

  3. ๐Ÿ“š Stack:

    • ๐Ÿ“ฅ Last-in, first-out (LIFO) data structure.

    • โžก๏ธ "Push" elements onto the top, "pop" elements from the top.

    • ๐Ÿ“ž Used for function calls, undo mechanisms.

  4. ๐Ÿšถ Queue:

    • ๐Ÿšถ First-in, first-out (FIFO) data structure.

    • โžก๏ธ "Enqueue" elements at the rear, "dequeue" elements from the front.

    • ๐Ÿ—“๏ธ Used for task scheduling, BFS algorithms.

Tasks:

  1. Give the Difference between List, Tuple and set.

    List:

    • A List is a mutable (modifiable) ordered collection of elements in Python.

    • It is defined using square brackets [] and elements are separated by commas.

    • Elements in a List can be changed, added, or removed after creation.

    • Lists allow duplicate elements, and their order is preserved.

Tuple:

  • A Tuple is an immutable (unchangeable) ordered collection of elements in Python.

  • It is defined using parentheses () and elements are separated by commas.

  • Once a Tuple is created, its elements cannot be modified, added, or removed.

  • Tuples allow duplicate elements, and their order is preserved.

Set:

  • A Set is a mutable (modifiable) unordered collection of unique elements in Python.

  • It is defined using curly braces {} or the set() function and elements are separated by commas.

  • Sets automatically remove duplicate elements, and their order is not guaranteed.

ย