Basics of Python for DevOps Engineers

Basics of Python for DevOps Engineers

  1. What is Python?

  • Python is a dynamically typed, general purpose programming language that supports an object-oriented programming approach as well as a functional programming approach.

  • Python is an interpreted and a high-level programming language.

  • It was created by Guido Van Rossum in 1989.


  1. Features of Python?

  • Python is simple and easy to understand.

  • It is Interpreted and platform-independent which makes debugging very easy.

  • Python is an open-source programming language.

  • Python provides very big library support. Some of the popular libraries include NumPy, Tensorflow, Selenium, OpenCV, etc.

  • It is possible to integrate other programming languages within python.


  1. What is Python used for?

  • Python is used in Data Visualization to create plots and graphical representations.

  • Python helps in Data Analytics to analyze and understand raw data for insights and trends.

  • It is used in AI and Machine Learning to simulate human behavior and to learn from past data without hard coding.

  • It is used to create web applications.

  • It can be used to handle databases.

  • It is used in business and accounting to perform complex mathematical operations along with quantitative and qualitative analysis.


  1. How to Install Python?

Installation on Windows

  • Download installer-

Follow this line python.

Verify python installed or not?

> python --version


Installation on Linux

linux$ sudo apt install python3.6
Python 3.6.14

To take a look on python interpreter,

# On both Windows and Linux

linux$ python


  1. Data types in Python

  • Python 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, Python data types are classes and variables are instances (objects) of these classes.

Text Type:str
Numeric Types:int, float, complex
Sequence Types:list, tuple, range
Mapping Type:dict
Set Types:set, frozenset
Boolean Type:bool
Binary Types:bytes, bytearray, memoryview
None Type:NoneType
ExampleData Type
x = "Hello World"str
x = 20int
x = 20.5float
x = 1jcomplex
x = ["apple", "banana", "cherry"]list
x = ("apple", "banana", "cherry")tuple
x = range(6)range
x = {"name" : "John", "age" : 36}dict
x = {"apple", "banana", "cherry"}set
x = frozenset({"apple", "banana", "cherry"})frozenset
x = Truebool
x = b"Hello"bytes
x = bytearray(5)bytearray
x = memoryview(bytes(5))memoryview
x = NoneNoneType
  • Every value has a datatype, and variables can hold values. Python is a powerfully composed language; consequently, we don't have to characterize the sort of variable while announcing it. The interpreter binds the value implicitly to its type.

    1. a = 5
  • We did not specify the type of the variable a, which has the value five from an integer. The Python interpreter will automatically interpret the variable as an integer.

#!/usr/bin/python2.7

name="John" # Int
age=23 # String
height=5.7 # Float
male=True # Boolean
city=["Pune", "Mumbai", "Bangalore"] # List
river=("Ganga", "Alaknanda", "Bhagirathi") # Tuple
numbers={1,2,3,4,5,9,8,7,6,6,2,1,3} # Set
directions={1:"North", 2:"South", 3: "East", 4:"West"} # Dictionary
limit=range(6) # range till 5
empty=None # Empty


print(name)
print(age)
print(height)
print(male)
print(city)
print(river)
print(numbers)
print(directions)
print(limit)
print(empty)
# Output

python$ ./demo.py
John
23
5.7
True
['Pune', 'Mumbai', 'Bangalore']
('Ganga', 'Alaknanda', 'Bhagirathi')
set([1, 2, 3, 4, 5, 6, 7, 8, 9])
{1: 'North', 2: 'South', 3: 'East', 4: 'West'}
[0, 1, 2, 3, 4, 5]
None

To see type of variable?

name="Sweety" # Declare a variable which holds string value
print(type(name)) # trying to print type variable

# Output
<type 'str'>


  1. List vs Tuple vs Set vs Dictionary

ListTupleSetDictionary
A list is a non-homogeneous data structure that stores the elements in columns of a single row or multiple rows.A Tuple is also a non-homogeneous data structure that stores elements in columns of a single row or multiple rows.The set data structure is also a non-homogeneous data structure but stores the elements in a single row.A dictionary is also a non-homogeneous data structure that stores key-value pairs.
The list can be represented by [ ]Tuple can be represented by ( )The set can be represented by { }The dictionary can be represented by { }
The list allows duplicate elementsTuple allows duplicate elementsThe Set will not allow duplicate elementsThe dictionary doesn’t allow duplicate keys.
The list can use nested among allTuple can use nested among allThe set can use nested among allThe dictionary can use nested among all
Example: [1, 2, 3, 4, 5]Example: (1, 2, 3, 4, 5)Example: {1, 2, 3, 4, 5}Example: {1: “a”, 2: “b”, 3: “c”, 4: “d”, 5: “e”}
A list can be created using the list() functionTuple can be created using the tuple() function.A set can be created using the set() functionA dictionary can be created using the dict() function.
A list is mutable i.e we can make any changes in the list.A tuple is immutable i.e we can not make any changes in the tuple.A set is mutable i.e we can make any changes in the set, its elements are not duplicated.A dictionary is mutable, its Keys are not duplicated.
List is orderedTuple is orderedSet is unorderedDictionary is ordered (Python 3.7 and above)
Creating an empty listCreating an empty TupleCreating a setCreating an empty dictionary

Connect Me

LinkedIn | GitHub

Follow me for more amazing content :)