Day 14: Python Data Types and Data Structures for DevOps

Day 14: Python Data Types and Data Structures for DevOps

#90daysofdevops

🔹What are Data Types?

  • Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data.

  • Since everything is an object in Python programming, data types are actually classes and variables are instance (object) of these classes.

  • Python has the following data types built-in by default: Numeric(Integer, complex, float), Sequential(string,lists, tuples), Boolean, Set, Dictionaries, etc.


🔹What are Data Structures?

Data Structures are a way of organizing data so that it can be accessed more efficiently depending upon the situation. Data Structures are fundamentals of any programming language around which a program is built. Python helps to learn the fundamental of these data structures in a simpler way as compared to other programming languages.

To check what is the data type of the variable used, we can simply write:

my_var=10
print(type(my_var))


🔸Task1:

👉What is lists?

Python Lists are just like the arrays, declared in other languages which is an ordered collection of data. It is very flexible as the items in a list do not need to be of the same type.

  • Lists are ordered and mutable.

  • They allow duplicate elements.

  • You can access, add, modify, or remove elements from a list.

  • Lists are represented by square brackets [] or by using the list() constructor.

my_list= [1,2,3,4,5,5]
print(my_list)
my_list.append(7)
print(my_list)


👉What is Tuple?

Python Tuple is a collection of Python objects much like a list but Tuples are immutable in nature i.e. the elements in the tuple cannot be added or removed once created. Just like a List, a Tuple can also contain elements of various types.

  • Tuples are ordered and immutable.

  • They allow duplicate elements.

  • Once a tuple is created, you cannot change its elements.

  • Tuples are commonly used for grouping related data.

  • Tuples are represented by parentheses () or by using the tuple() constructor.

my_tuple = (1, 2, 3, 3, 4, 5) 
print(my_tuple) # (1, 2, 3, 3, 4, 5)
# my_tuple[0] = 10 # This would raise a TypeError since tuples are immutable


👉What is Dictionary?

Python dictionary is like hash tables in any other language with the time complexity of O(1). It is an unordered collection of data values, used to store data values like a map, which, unlike other Data Types that hold only a single value as an element, Dictionary holds the key:value pair. Key-value is provided in the dictionary to make it more optimized.

👉What are Sets?

  • Sets are unordered and mutable.

  • They do not allow duplicate elements.

  • Sets are useful for mathematical set operations such as union, intersection, and difference.

  • You can add or remove elements from a set.

  • Sets are represented by curly braces {} or by using the set() constructor.

my_set = {1, 2, 3, 3, 4, 5} 
print(my_set) # {1, 2, 3, 4, 5}
my_set.add(6) 
print(my_set) # {1, 2, 3, 4, 5, 6}

1)Numeric data types:

int: x = 10
float: y = 3.14
complex: z = 2 + 3j

2) String data type:

str: name = “Vishesh”

3) Sequence types:

list: numbers = [1, 2, 3, 4, 5]
tuple: coordinates = (10, 20)
range: count = range(0, 10)

4) Binary types:

bytes: data = b’Hello’
bytearray: buffer = bytearray(5)
memoryview: view = memoryview(buffer)

5) Mapping data type:

dict: student = {“name”: “Alice”, “age”: 20}

6) Boolean type:

bool: is_true = True

7) Set data types:

set: fruits = {“apple”, “banana”, “orange”}
frozenset: vowels = frozenset({“a”, “e”, “i”, “o”, “u”})

🔸Task2:

1)Create below Dictionary and use Dictionary methods to print your favourite tool just by using the keys of the Dictionary.

fav_tools = {
    1: "Linux",
    2: "Git",
    3: "Docker",
    4: "Kubernetes",
    5: "Terraform",
    6: "Ansible",
    7: "Chef"
}

# Prompt the user to enter their favorite tool
favorite_tool_key = int(input("Enter the key of your favorite tool: "))

# Retrieve the favorite tool from the dictionary using the key
if favorite_tool_key in fav_tools:
    favorite_tool = fav_tools[favorite_tool_key]
    print(f"Your favorite tool is {favorite_tool}")
else:
    print("Tool not found in the dictionary.")

2) Create a List of cloud service providers
eg. cloud_providers = [“AWS”,”GCP”,”Azure”] .Write a program to add Digital Ocean to the list of cloud_providers and sort the list in alphabetical order.


Thanks for reading to the end; I hope you gained some knowledge.❤️🙌

Linkedln

Twitter