Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
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

Tuples

Python's tuple data structure is immutable, meaning we cannot change the elements of the tuples. Basically, a tuple is a sequence of values that are separated by commas and are enclosed in parentheses ( ). Like lists, tuples are an ordered sequence of elements:

>>> t1 = 'h', 'e', 'l', 'l', 'o'

Tuples are enclosed in parentheses ( ):

>>> t1 = ('h', 'e', 'l', 'l', 'o')

You can also create a tuple with a single element. You just have to put a final comma in the tuple:

>>> t1 = 'h',
>>> type(t1)
<type 'tuple'>

A value in parentheses is not a tuple:

>>> t1 = ('a')
>>> type(t1)
<type 'str'>

We can create an empty tuple using the tuple() function:

>>> t1 = tuple()
>>> print (t1)
()

If the argument is a sequence (string, list, or tuple), the result is a tuple with the elements of the sequence:

>>> t = tuple('mumbai')
>>> print t
('m', 'u', 'm', 'b', 'a', 'i')

Tuples have values between parentheses ( ) separated by commas:

>>> t = ('a', 'b', 'c', 'd', 'e')
>>> print t[0]
'a'

The slice operator selects a range of elements.

>>> print t[1:3]
('b', 'c')

Accessing values in tuples

To access values in a tuple, use the square brackets for slicing along with the index or indices to obtain the value available at that index or indices, as shown in the following example:

#!/usr/bin/python3
cities = ('Mumbai', 'Bangalore', 'Chennai', 'Pune')
numbers = (1, 2, 3, 4, 5, 6, 7)
print (cities[3])
print (numbers[1:6])

Output:
Pune
(2, 3, 4, 5)

Updating tuples

Tuple updating is not possible in Python, as tuples are immutable. But you can create a new tuple with an existing tuple, as shown in the following example:

#!/usr/bin/python3
cities = ('Mumbai', 'Bangalore', 'Chennai', 'Pune')
numbers = (1,2,3,4,5,6,7)
tuple1 = cities + numbers
print(tuple1)

Output:
('Mumbai', 'Bangalore', 'Chennai', 'Pune', 1, 2, 3, 4, 5, 6, 7)

Deleting tuple elements

We cannot remove individual tuple elements. So, to remove an entire tuple explicitly, use the del statement. Refer to the following example:

#!/usr/bin/python3
cities = ('Mumbai', 'Bangalore', 'Chennai', 'Pune')
print ("Before deleting: ", cities)
del cities
print ("After deleting: ", cities)

Output:
Before deleting: ('Mumbai', 'Bangalore', 'Chennai', 'Pune')
Traceback (most recent call last):
File "01.py", line 5, in <module>
print ("After deleting: ", cities)
NameError: name 'cities' is not defined

Basic tuple operations

Like lists, there are five basic tuple operations:

  • Concatenation
  • Repetition
  • Length
  • Membership
  • Iteration

Description

Expression

Results

Iteration

for x in (45,58,99) : print (x,end = ' ')

45 58 99

Repetition

('Python') * 3

('python', 'python', 'python')

Length

len(45, 58, 99, 65)

4

Concatenation

(30, 50, 60) + ('Hello', 75, 66)

(30,50,60,'Hello',75,66)

Membership

45 in (45,58,99,65)

True

Indexing, slicing, and matrices

Tuple indices work the same way as list indices. Values can be accessed using index. If you try to read or write an element that does not exist, you get IndexError. If an index has a negative value, it counts backward from the end of the list.

Now, we will create a tuple named cities and perform some index operations:

cities = ('Mumbai', 'Bangalore', 'Chennai', 'Pune')

Description

Expression

Results

Index starts at zero

cities[2]

'Chennai'

Slicing: getting sections

cities[1:]

('Bangalore', 'Chennai', 'Pune')

Negative: count from the right

cities[-3]

'Bangalore'

max() and min()

Using the max() and min() functions, we can find the highest and lowest values from the tuple. These functions allow us to find out information about quantitative data. Let's look at an example:

>>> numbers = (50, 80,98, 110.5, 75, 150.58)
>>> print(max(numbers))
150.58
>>>

Using max(), we will get the highest value in our tuple. Similarly, we can use the min() function:

>>> numbers = (50, 80,98, 110.5, 75, 150.58)
>>> print(min(numbers))
50
>>>

So, here we are getting the minimum value.

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
Banner background image