Python’s standard library is very extensive, offering a wide range of facilities. The library contains built-in modules (written in C) that provide access to system functionality such as file I/O that would otherwise be inaccessible to Python programmers, as well as modules written in Python that provide standardized solutions for many problems that occur in everyday programming
In this tutorial, we will learn everything which a beginner and a DevOps engineer should know in Python. We will cover the basic definition of python and some brilliant examples which will be enough to get you started with Python and for sure you will love it.
Table of content
- What is Python?
- Prerequisites
- Python Keywords
- Python Numbers
- Python Strings
- Python Tuple
- Python Lists
- Python Dictionary
- Python Sets
- Python variables
- Python Built-in functions
- Python Handling Exceptions
- Python Functions
- Python Searching
- Conclusion
What is Python?
Python is a high-level, oops-based, interactive, and general-purpose scripting programing language. Python is a language that is used as a backend as well as a frontend language. It focuses on objects over functions.
Python is also an interpreted language because it converts codes into machine-level code even before it runs. It works on a variety of protocols such as HTTPS, FTP , SMTP, and many more. The latest version is 3.9.2
which was released in December 2020. Python works very well with most of the such as atom, notepad ++, vim.
Python works on windows, Linux, and macOS systems, and many more. For Windows OS it can run a single command on windows terminal and for Linux & macOS it can easily run on the shell without needing to save the program every time.
Prerequisites
- Python doesn’t come installed on windows so make sure you have Python installed on windows machine. To see how to install python on windows click here.
- For macOS and Linux Python comes installed by default but could be the older version such as
python2
. To check python version run the command.


- In case if your system has Python2 or if you receive an error message “Python not found” then you need to run the following command to install python on macOS or Linux.
sudo apt install python3
Python Keywords
Python reserves certain words for special purposes known as keywords such as
continue
def
del
break
class
if
return
Python Data types
Whatever program you write in Python is data and this data contains some values which are also known as objects
. Each object has a type known as data types
. These data types are either mutable in nature that is modifiable or immutable in nature that is unmodifiable.
Python contains numerous built-in data types such as:
Python Numbers
Numbers are either integers or floating-point.
Decimal integer
: 1 , 2Binary integer
: 0b010101Octal integer
: 0o1Hexadecimal integer
: 0x1Floating point
: 0.0 , 2.e0
Boolean
Booleans are represented by either True or False
Python String
Python strings are a collection of characters surrounded by quotes ” “. There are different ways in which strings are declared such as:
- str() – In this method you decalre the characters or words or data inside the double quotes.

- Directly calling it in quotes – “Hello, this is method2 to display string”

- Using Format – format method was introduced in Python3 and uses curly brackets {} to replace the values.
- In below example you will notice that first curly bracket will be replaced by first value a and second will be replaced by b
- If you provide any numerical value inside the curly braces it considers it as
index
and then retrieve from the given values accordingly as shown in the second example below. - if you provide key value pair then values are substituted according to key as shown in the third example below.
'{} {}'.format('a','b')

'{0} {0}'.format('a','b')

'{a} {b}'.format(a='apple', b='ball')

- Using f string – f string are prepended with either f or F before the first quotation mark. The value is subsituted with the variables declared.
a=1 # Declaring a
f"a is {a}"

- Template strings – Template strings are designed to offer a simple string substitution mechanism. These built-in methods work for tasks where simple word substitutions are necessary.
from string import Template
new_value = Template("$a b c d") # a will be substituted here
x = new_value.substitute(a = "Automation")
y = new_value.substitute(a = "Automate")
print(x,y)

Some Tricky Examples of declaring string
Input String
print('This is my string 1') # Correct String
print("This is my string 2") # Correct String
# print('This is not a string ") # InCorrect String as you cannot used mixed quotes
# print("This is not a string') # InCorrect String as you cannot used mixed quotes
# Examples of Special characters inside the String such as quotes
# print('Hello's Everyone') # Incorrect Statement
print('Hello\'s Everyone') # Correct Statement after using escape (To insert characters that are illegal in a string, use an escape character. )
print("Hello's Everyone") # Correct Statement enclose within double quotes
print('Hello "shanky') # COrrect STatement
print('Hello "shanky"') # Correct STatment
# print("Hello "S"shanky") # Incorrect Statement
print("Hello ""shanky")
# No need to Escape if using triple quotes but proper use of triple quotes
print(''''This is not a string "''')
print('''Hello" how' are"" u " I am " f'ine'r''')
print('''''Hello" how' are"" u " I am " f'ine'r''')
print("""'''''Hello" how' are"" u " I am " f'ine'r""")
Output String
This is my string 1
This is my string 2
Hello's Everyone
Hello's Everyone
Hello "shanky
Hello "shanky"
Hello shanky
'This is not a string "
Hello" how' are"" u " I am " f'ine'r
''Hello" how' are"" u " I am " f'ine'r
'''''Hello" how' are"" u " I am " f'ine'r
Python Tuple
Tuples: Tuples are immutable
ordered sequence of items that cannot be modified. The items of a tuple are arbitrary objects and may be of different types and allow duplicate values. For Example
# 10,20,30,30 are fixed at respective index 0,1,2,3 positions
(10,20,30,30) or (3.14,5.14,6.14)
Python Lists
Lists: The list is a mutable
ordered sequence of items. The items of a list are arbitrary objects and may be of different types. List items are ordered, changeable, and allow duplicate values.
[2,3,"automate","2"]
Python Dictionaries
Dictionary are written as key:value
pair , where key is an expression giving the item’s key and value is an expression giving the item’s value. A dictionary is a collection which is ordered*, changeable and does not allow duplicates.
# Dictionary with three items where x,y and z are keys.
# where x,y and z have 42, 3.14 and 7 as the values.
{'x':42, 'y':3.14, 'z':7}
Python Sets
Sets: Set stores multiple items in a single variable. It contains unordered
and unindexed
data. Sets cannot have two items with the same value.
{"apple", "banana", "cherry"}
Data types | Mutable or Immutable |
String | Immutable (Cannot be modified) |
Tuples | Immutable (Cannot be modified) |
Integers | Immutable (Cannot be modified) |
List | Mutable (Can be modified) |
Sets | Mutable (Can be modified) |
Floating point | Immutable (Cannot be modified) |
Dictionaries | Mutable (Can be modified) |
Python variables
Variables are stored as a information it could be number , symbol , name etc. which are used to be referenced. Lets see some of the examples of Python variables.
- There are few points one must remember when using variables such as
- Variables cannot start with digits
- Spaces are not allowed in variables.
- Avoid using Python keywords
Example 1:
- In below example
var
is a variable and value ofvar
is this is a variable
var="this is a variable" # Defining the variable
print(var) # Printing the value of variable

Example 2:
- In below example we are declaring three variable.
first_word
andsecond_word
are storing the values- add_words is substituting the variables with values
first_word="hello"
second_word="devops"
add_words=f"{first_word}{second_word}"
print(add_words)

- If you wish to print words in different line then use
"\n"
as below
first_word="hello"
second_word="devops"
add_words=f"{first_word}\n{second_word}"
print(add_words)

Dictionary
In simple words these are key value pairs where keys can be number, string or custom object. Dictionary are represented in key value pairs separated by comma within curly braces.
map = {'key-1': 'value-1', 'key-2': 'value-2'}
- You can access the particular key using following way
map['key-1']

Lets see an example to access values using get()
method
my_dictionary = {'key-1': 'value-1', 'key-2': 'value-2'}
my_dictionary.get('key-1') # It will print value of key-1 which is value-1
print(my_dictionary.values()) # It will print values of each key
print(my_dictionary.keys()) # It will print keys of each value
my_dictionary.get('key-3') # It will not print anything as key-3 is missing

Lists
Lists are ordered collection of items. Lists are represented using square brackets containing ordered list of item.
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # Example of List
- We can add or remove items from the list using built in function such as
pop()
orinsert()
orappend()
and many more. Lets us see an example.
The contents of one list can be added to another using the extend
method:
list1 =['a', 'b', 'c', 'd']
print(list1) # Printing only List 1
list2 = ['e', 'f']
list2.extend(list1)
print(list2) # Printing List 2 and also 1

- Use insert() to add one new guest to the beginning of your list.
- Use insert() to add one new guest to the middle of your list.
- Use append() to add one new guest to the end of your list.
Python Built-in functions
There are various single line command which are already embedded in python library and those are known as built in functions. You invoke a function by typing the function name, followed by parentheses.
- To check the
Python version
on windows or Linux machine run the following command.
python3 --version


- To print the output of a program , use the
print
command.
print("Hello Devops")

- To generate a list of number through a
range
built-in function run the following command.
list(range(0,10))

Handling Exceptions
Exceptions are error which causes a program to stop if not handled properly. There are many built-in exceptions, such as IOError
, KeyError
, and ImportError
. Lets see a simple example below.
- Here we defined a list of characters and stored it in a
variable
devops - Now,
while true
indicated that till the ,condition is true it will execute the try block. .pop()
is built in method to remove each item one by one.- Now in our case as soon as all the characters are removed then
except
block catches theIndexError
and prints the message.
devops = ['d','e','v','o','p','s']
while True:
try:
devop = devops.pop()
print(devop)
except IndexError as e:
print("I think I did lot of pop ")
print(e)
break
Output:
s
p
o
v
e
d
I think I did lot of pop
pop from empty list
Python Functions
Earlier in this tutorial we have already seen that there are numerous built in function and some of them you used above. But you can define and create your own functions. Lets see the syntax of function.
def <FUNCTION NAME>(<PARAMETERS>):
<CODE BLOCK>
<FUNCTION NAME>(<ARGUMENTS>)
Lets look at some of the Python functions examples
EXAMPLE 1
- Here each argument use order of arguments to assign value which is also known as positional argument.
a
andb
variables are parameters which are required to run the function1
and2
are arguments which are used to pass the value to the function ( arguments are piece of information that’s passed from a function call to a function)
def my_function(a,b):
print(f" value of a is {a}")
print(f" value of b is {b}")
my_function(1, 2)

EXAMPLE 2:
- With keyword arguments, assign each argument a default value:
def my_function(a=3,b=4):
print(f" value of a is {a}")
print(f" value of b is {b}")
my_function()

EXAMPLE 3
Passing arbitrary number of arguments. When you are not sure about the number of parameters to be passed then we call it as arbitrary. Lets look at an example
- Find the Even in the string
mylist = []
def myfunc(*args): # args is to take any number of arguments together in myfunc
for item in args:
if int(item)%2 == 0:
mylist.append(item)
print(mylist)
myfunc(5,6,7,8,9)

EXAMPLE 4
- IF LOOP: Find the least among two numbers if both numbers are even else return greater among both the numbers
def two_of_less(a,b): # Defining the Function where a and b variables are parameters
if a%2==0 and b%2==0:
print(min(a,b)) # using built in function min()
if a%2==1 or b%2==1:
print(max(a,b)) # using built in function max()
two_of_less(2,4)

EXAMPLE 5
- Write a function takes a two-word string and returns True if both words begin with same letter
def check(a):
m = a.split()
if m[0][0] == m[1][0] :
print("Both the Words in the string starts with same letter")
else:
print("Both the Words in the string don't start with same letter")
check('devops Engineer')

Python Searching
The need to match patterns in strings comes up again and again. You could be looking for an identifier in a log file or checking user input for keywords or a myriad of other cases.
Regular expressions use a string of characters to define search patterns. The Python re
package offers regular expression operations similar to those found in Perl.
Lets look at example which will give you overall picture of in built functions which we can use with re
module.
- You can use the
re.search
function, which returns are.Match
object only if there is a match.
import re
import datetime
name_list = '''Ezra Sharma <esharma@automateinfra.com>,
...: Rostam Bat <rostam@automateinfra.com>,
...: Chris Taylor <ctaylor@automateinfra.com,
...: Bobbi Baio <bbaio@automateinfra.com'''
# Some commonly used ones are \w, which is equivalent to [a-zA-Z0-9_] and \d, which is equivalent to [0-9].
# You can use the + modifier to match for multiple characters:
print(re.search(r'Rostam', name_list))
print(re.search('[RB]obb[yi]', name_list))
print(re.search(r'Chr[a-z][a-z]', name_list))
print(re.search(r'[A-Za-z]+', name_list))
print(re.search(r'[A-Za-z]{5}', name_list))
print(re.search(r'[A-Za-z]{7}', name_list))
print(re.search(r'[A-Za-z]+@[a-z]+\.[a-z]+', name_list))
print(re.search(r'\w+', name_list))
print(re.search(r'\w+\@\w+\.\w+', name_list))
print(re.search(r'(\w+)\@(\w+)\.(\w+)', name_list))
OUTPUT
<re.Match object; span=(49, 55), match='Rostam'>
<re.Match object; span=(147, 152), match='Bobbi'>
<re.Match object; span=(98, 103), match='Chris'>
<re.Match object; span=(0, 4), match='Ezra'>
<re.Match object; span=(5, 10), match='Sharm'>
<re.Match object; span=(13, 20), match='esharma'>
<re.Match object; span=(13, 38), match='esharma@automateinfra.com'>
<re.Match object; span=(0, 4), match='Ezra'>
<re.Match object; span=(13, 38), match='esharma@automateinfra.com'>
<re.Match object; span=(13, 38), match='esharma@automateinfra.com'>
- Understanding the difference between high level and low level language.
- Interpreted v/s Compiled Language
- Introduction to Python
- How Python Works ?
- Python Interpreter
- Python Standard Library
- Python Implementations
- Python Installation
- Python Installation on Linux Machine
- Python Installation on Windows Machine
- Python Installation on MacOS
- Conclusion
Understanding the difference between High & Low-level Languages
High-Level Language: High-level language is easier to understand than is it is human readable. It is either compiled or interpreted. It consumes way more memory and is slow in execution. It is portable. It requires a compiler or interpreter for a translation.

Low-Level Language: Low-level Languages are machine-friendly that is machines can read the code but not humans. It consumes less memory and is fast to execute. It cannot be ported. It requires an assembler for translation.
Interpreted v/s Compiled Language
Compiled Language: Compiled language is first compiled and then expressed in the instruction of the target machine that is machine code. For example – C, C++, C# , COBOL

Interpreted Language: An interpreter is a computer program that directly executes instructions written in a programming or scripting language, without requiring them previously to have been compiled into a machine language program and these kinds of languages are known as interpreter languages. For example JavaScript, Perl, Python, BASIC

Introduction to Python
Python is a high-level language, which is used in designing, deploying, and testing at lots of places. It is consistently ranked among today’s most popular programming languages. It is also dynamic and object-oriented language but also works on procedural styles as well, and runs on all major hardware platforms. Python is an interpreted
language.
How does Python Works?
Bytecode, also termed p-code, is a form of instruction set designed for efficient execution by a software interpreter
- First step is to write a Python program such as test.py
- Then using
Python interpreter
program is in built compiled and gets converted into byte code that is test.pyc. - Python saves byte code like this as a startup speed optimization. The next time you run your program, Python will load the .pyc files and skip the compilation step, as long as you haven’t changed your source code since the byte code was last saved.
- Once your program has been compiled to byte code (or the byte code has been loaded from existing .pyc files), it is shipped off for execution to something generally known as the Python Virtual Machine
- Now byte code that is test.pyc is further converted into machine code using virtual machine such as
(10101010100010101010)
- Finally Program is executed and output is displayed.

Python Interpreter
Python includes both interpreter and compiler which is implicitly invoked.
- In case of Python version 2, the Python interpreter compiles the source file such as
file.py
and keep it in same directory with an extensionfile.pyc
- In case of Python version 3 : the Python interpreter compiles the source file such as
file.py
and keep it in subdirectory __pycache__ - Python does not save the compiled bytecode when you run the script directly; rather, Python recompiles the script each time you run it.
- Python saves bytecode files only for modules you import however running Python command with
-B
flag avoids saving compiled bytecode to disk. - You can also directly execute Python script in the Unix operating system if you add shebang inside your script.
#! /usr/bin/env python
Python Standard Library
Python standard library contains several well-designed Python modules for convenient reuse like representing data, processing text, processing data, interacting with operating systems and filesystems, and web programming. Python modules
are basically Python Programs like a file (abc.py) that are imported.
There are some extension modules
that allows Python code to access functionalities supplied by underlying OS or other software’s components such as GUI, database and network, XML parsing. You can also wrap existing C/C++ libraries into python extension modules.
Python Implementations
Python is more than a language, you can utilize the implementation of Python in many ways such as :
CPython:
CPython is an interpreter, compiler, set of built in and optional extension modules all coded in C language. Python code is converted into bytecode before interpreting it.
IronPython:
Python implementation for the Microsoft-designed Common Language Runtime (CLR), most commonly known as .NET, which is now open source and ported to Linux and macOS
PyPy:
PyPy is a fast and flexible implementation of Python, coded in a subset of Python itself, able to target several lower-level languages and virtual machines using advanced techniques such as type inferencing
Jython:
Python implementation for any Java Virtual Machine (JVM) compliant with Java 7 or better. With Jython, you can use all Java libraries and framework and it supports only v2 as of now.
IPython:
Enhances standard CPython to make it more powerful and convenient for interactive use. IPython extends the interpreter’s capabilities by allowing abbreviated function call syntax, using question mark to query an objects documentation etc.
Python Installation
Python Installation on Linux Machine
If you are working on the Latest platforms you will find Python already installed in the systems. At times Python is not installed but binaries are available in the system which you can install using RPM tool
or using APT
in Linux machines and for Windows use MSI
( Microsoft Installer ) .


Python Installation on Windows Machine
Python can be installed in Windows with a few steps, and to install Python steps can be found here.
Python Installation on macOS
Python V2 comes installed on macOS but you should install the latest Python version always. The popular third-party macOS open-source package manager Homebrew offers, among many other packages, excellent versions of Python, both v2 and v3
- To install Homebrew, open
Terminal
or your favorite OS X terminal emulator and run
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
- Add homebrew directory at the top of your
PATH
environment variable.
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
- Now install Python3 using the following commands.
brew install python3
- Verify the installation of Python using below command

Conclusion
In this tutorial you learnt everything which a beginner and a Devops engineer should know. This tutorial covered definition of python and some brilliant examples which will be enough to get you started with Python and for sure you will love it.
By Now, you are ready to build some exciting python programs. Hope you liked this tutorial and please share it with your friends.