Python for DevOps
Week 1: Python Fundamentals
About the trainer
● Education
○ HCM University of Science
● Experience
○ Database Reliability Engineer (DRE) at Axon
○ DevOps Engineer for a Germany payment-
solution company.
○ Formerly working as Software Engineer at
Teko. Tu Doan
● Certifications:
Python – a Programming Language
● Introduction: Why is Python?
● Python Fundamentals
● Python Shell
● Package management
Roadmap
Why Programming Language so important ?
What is Python
● Python is a Programming Language.
● Scripting like Bash or Javascript.
● Don’t have to compile.
● Support by almost system.
Why is Python
● Easy to learn.
● Lightweight and Reliable
● Fastest growing programming language
● Large community
● Flexibility
Python in DevOps
● DevOps require a lot of Automation, Python is fit for it.
● Interact with Cloud computing.
● Ready-to-use API and Library.
Compared to Bash
Bash Script is most famous shell CLI on Linux.
PYTHON BASH
Highly efficient programming language Command-line interpreter (CLI)
1
General-purpose programming Not a programming language
2 Object-oriented programming (OOP) Replacement for the original Bourne shell
3 Easy, simple and powerful language Tough to write and not powerful as python.
4 Designed for web and app development. Found on Linux distributions and macOS.
It supports OOP and allow users to easily and neatly
5 Bash does not support OOP and it only understands text
break problems.
6 Require third-party programs to be installed Does not require third-party apps/programs to be installed
7 Better to use when script is larger than 100 lOC. For smaller script Bash is good.
Installation
● Windows:
○ https://www.python.org/downloads/release/python-3107/
● Linux:
○ sudo apt-get update
○ sudo apt-get install python 3.10
● MacOS:
○ brew install python
● Check for version
○ python --version
First code
Operators
● Basic math operations in Python is all
built-in.
Comment
Variables
● Variables can be any types of String, integer, array, map, …
● Naming is popular in 2 cases
○ CamelCase: capital first letter of each word
○ snake_case: underscore between words
Variables (continue)
● Naming rules:
○ Cannot use hyphens (-), spaces, and special characters ($,’,”,…)
○ Cannot start with number, e.g “1var”
● Assign value of a variable by using =
Function
● Print() help you to output the variable
or a string.
● A Function call with ()
● Variables passed into the function are
put between ( and ).
Define a Function
● Keyword “def” to define a
function.
● “sum” is the function name.
● ”a” and “b” is the input variable
● Keyword “return” to return the
output of a function.
● The body of the function
definition will ident with a tab.
String and Integer
● Cannot add string and int.
● String and int can be multiply.
String and Integer (cont.)
● That’s why we need to convert between
string and integer.
● Using function int() and str().
Interactive read input
● Using input() function.
● Output of this function can assigned to variable.
Exercise
● Write a program that:
○ Input: name and year of birth.
○ Output: a string that “I am … and I am ... years old.”
● Example:
Conditional Statement
● “if/else/elif” is the keyword when you want to make decision in
code.
● The block of code after ”if” will run when the statement is
True.
● The block of code after “else” will run when statement is False.
● Operators in statement:
○ ==, >, <, !=
○ ! (not)
○ and, or, in
○ Only variables
Truth table
Loop
● Looping is technique when doing a block of code multiple
times.
● 2 kinds of loop is “while” and “for”.
● Use “while” when a block of code repeats as long as the
condition meets.
● Use “for” to iterate through a sequence.
For loop
● Use “continue” keyword when
skipping the rest of code block.
While loop
● The while will run until the
condition is false.
● Or whenever we use “break”
keyword.
● It is very important to find a way
for the “while” loop to be end.