Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering Python Scripting for System Administrators

You're reading from   Mastering Python Scripting for System Administrators Write scripts and automate them for real-world administration tasks using Python

Arrow left icon
Product type Paperback
Published in Jan 2019
Publisher Packt
ISBN-13 9781789133226
Length 318 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Ganesh Sanjiv Naik Ganesh Sanjiv Naik
Author Profile Icon Ganesh Sanjiv Naik
Ganesh Sanjiv Naik
Arrow right icon
View More author details
Toc

Table of Contents (21) Chapters Close

Preface 1. Python Scripting Overview FREE CHAPTER 2. Debugging and Profiling Python Scripts 3. Unit Testing - Introduction to the Unit Testing Framework 4. Automating Regular Administrative Activities 5. Handling Files, Directories, and Data 6. File Archiving, Encrypting, and Decrypting 7. Text Processing and Regular Expressions 8. Documentation and Reporting 9. Working with Various Files 10. Basic Networking - Socket Programming 11. Handling Emails Using Python Scripting 12. Remote Monitoring of Hosts Over Telnet and SSH 13. Building Graphical User Interfaces 14. Working with Apache and Other Log Files 15. SOAP and REST API Communication 16. Web Scraping - Extracting Useful Data from Websites 17. Statistics Gathering and Reporting 18. MySQL and SQLite Database Administrations 19. Assessments 20. Other Books You May Enjoy

Loops

To handle all of the looping requirements in your script, Python supports two loops:

  • for loop
  • while loop

Now, we are going to learn about for loop and while loop.

for loop

for loop iterates over each item of the sequence or any other iterable object and it will execute the statements in the for block each time. Refer to the following syntax:

for i in sequence:
for loop body

Here, i is the variable that takes the value of the item inside the sequence on each iteration. This loop continues until we reach the last item in the sequence. This is illustrated in the following diagram:

Refer to the following example:

numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]
sum = 0
for i in numbers:
sum = sum + i
print("The sum is", sum)

Output:
The sum is 6
The sum is 11
The sum is 14
The sum is 22
The sum is 26
The sum is 28
The sum is 33
The sum is 37
The sum is 48

The range() function

The Python range() function will generate a sequence of numbers. For example, range(10) will generate numbers from 0 to 9 (10 numbers).

We can also define the start, stop, and step size as parameters and range() will be as follows:

range(start, stop, step size).
Step size defaults to 1 if not provided.
For loop example using range() function:

Let's take a look at an example:

for i in range(5):
print("The number is", i)

Output:
The number is 0
The number is 1
The number is 2
The number is 3
The number is 4

while loop

while is a looping statement that will iterate over a block of code until the entered test expression is true. We use this loop when we don't know how many times the iterations will go on. Refer to the following syntax:

while test_expression:
while body statements

In the while loop, first we will check the test expression. The while block will get executed only if the test expression is true. After one iteration, the expression will be checked again and this process continues until test_expression evaluates to false. This is illustrated in the following diagram:

The following is an example of the while loop:

a = 10
sum = 0
i = 1
while i <= a:
sum = sum + i
i = i + 1
print("The sum is", sum)


Output:
The sum is 1
The sum is 3
The sum is 6
The sum is 10
The sum is 15
The sum is 21
The sum is 28
The sum is 36
The sum is 45
The sum is 55
You have been reading a chapter from
Mastering Python Scripting for System Administrators
Published in: Jan 2019
Publisher: Packt
ISBN-13: 9781789133226
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime