Day-15 of #90DaysOfDevOps

Day-15 of #90DaysOfDevOps

Basics of Python for DevOps Engineers

Welcome to Day 15 of my #90DaysOfDevOps journey! Today, we dive into Python basics, covering its installation across various operating systems and a detailed look at its built-in data types.


What is Python?

Python is an open-source, general-purpose, high-level, and object-oriented programming language created by Guido van Rossum. Its simplicity and versatility make it an ideal choice for DevOps tasks such as automation, configuration management, and infrastructure as code.

Python’s vast ecosystem of libraries and frameworks, like Django, TensorFlow, Flask, Pandas, and Keras, makes it indispensable in modern development and operations.


Task 1: Installing Python and Checking the Version

Let’s explore how to install Python and verify its installation on various operating systems.

1. Windows

  • Step 1: Download Python from the official website.

  • Step 2: Run the installer and check the box "Add Python to PATH" before proceeding with the installation.

  • Step 3: Open Command Prompt and type:

      python --version
    

Example Output:

C:\Users\Amitabh> python --version  
Python 3.11.0


2. Ubuntu/Linux

  • Step 1: Open the terminal.

  • Step 2: Update the package list:

      sudo apt update
    
  • Step 3: Install Python:

      sudo apt install python3
    
  • Step 4: Verify the installation:

      python3 --version
    

Example Output:

amitabh@ubuntu:~$ python3 --version  
Python 3.8.10


3. macOS

  • Step 1: Use the built-in Python or install it using Homebrew:

      brew install python
    
  • Step 2: Verify the installation:

      python3 --version
    

Example Output:

MacBook-Pro:~ amitabh$ python3 --version  
Python 3.9.7

Task 2: Understanding Python Data Types

Python provides a wide variety of built-in data types, enabling flexibility and simplicity in programming.

1. Numeric Types

  • Integer (int): Whole numbers.

      age = 25  
      print(type(age))  # Output: <class 'int'>
    
  • Float (float): Numbers with a decimal point.

      pi = 3.14  
      print(type(pi))  # Output: <class 'float'>
    
  • Complex (complex): Numbers with real and imaginary parts.

      complex_num = 3 + 5j  
      print(type(complex_num))  # Output: <class 'complex'>
    

2. Sequence Types

  • String (str): A sequence of characters.

      name = "Amitabh"  
      print(type(name))  # Output: <class 'str'>
    
  • List (list): Ordered and mutable collection.

      fruits = ["apple", "banana", "cherry"]  
      print(type(fruits))  # Output: <class 'list'>
    
  • Tuple (tuple): Ordered but immutable collection.

      coordinates = (10, 20, 30)  
      print(type(coordinates))  # Output: <class 'tuple'>
    

3. Mapping Type

  • Dictionary (dict): A collection of key-value pairs.

      student = {"name": "Amitabh", "age": 21}  
      print(type(student))  # Output: <class 'dict'>
    

4. Set Types

  • Set (set): Unordered collection of unique items.

      colors = {"red", "blue", "green"}  
      print(type(colors))  # Output: <class 'set'>
    
  • Frozen Set (frozenset): Immutable version of a set.

      immut_colors = frozenset(["red", "blue", "green"])  
      print(type(immut_colors))  # Output: <class 'frozenset'>
    

5. Boolean Type

  • Boolean (bool): Represents True or False.

      is_student = True  
      print(type(is_student))  # Output: <class 'bool'>
    

6. None Type

  • NoneType (None): Represents the absence of a value.

      data = None  
      print(type(data))  # Output: <class 'NoneType'>
    

Summary Table of Data Types

Data TypeExample
intx = 10
floatpi = 3.14
complexz = 1 + 2j
strname = "Amitabh"
listcolors = ["red", "blue"]
tupletup = (1, 2)
dictstudent = {"key": "value"}
setunique = {1, 2, 3}
boolis_ready = True

Thank you for reading! Python’s simplicity and versatility make it a powerful tool for DevOps engineers, especially in scripting and automation tasks.