Tuples
Tuples are immutable
A tuple is a sequence of values. The values can be any type, and they are indexed by
integers, so in that respect tuples are a lot like lists. The important difference is that tuples
are immutable.
Syntactically, a tuple is a comma-separated list of values:
>>> t = 'a', 'b', 'c', 'd', 'e'
Although it is not necessary, it is common to enclose tuples in parentheses:
>>> t = ('a', 'b', 'c', 'd', 'e')
To create a tuple with a single element, you have to include a final comma:
>>> t1 = 'a',
>>> type(t1)
<class 'tuple'>
A value in parentheses is not a tuple:
>>> t2 = ('a')
>>> type(t2)
<class 'str'>
Another way to create a tuple is the built-in function tuple. With no argument, it creates
an empty tuple:
>>> t = tuple()
>>> t
()
Tuple assignment
One of the unique syntactic features of the Python language is the ability to have a tuple
on the left side of an assignment statement. This allows you to assign more than one
variable at a time when the left side is a sequence. In this example we have a two-element
list (which is a sequence) and assign the first and second elements of the sequence to the
variables x and y in a single statement.
>>> m = [ have, fun ]
>>> x, y = m
>>> x
have
>>> y
fun
Stylistically when we use a tuple on the left side of the assignment statement, we omit the
parentheses, but the following is an equally valid syntax:
>>> m = [ have, fun ]
>>> (x, y) = m
>>> x
have
>>> y
fun
>>
A particularly clever application of tuple assignment allows us to swap the values of two
variables in a single statement:
>>> a, b = b, a
Both sides of this statement are tuples, but the left side is a tuple of variables; the right
side is a tuple of expressions. Each value on the right side is assigned to its respective
variable on the left side. All the expressions on the right side are evaluated before any of
the assignments.
The number of variables on the left and the number of values on the right must be the
same:
>>> a, b = 1, 2, 3
ValueError: too many values to unpack
More generally, the right side can be any kind of sequence (string, list, or tuple).
For example, to split an email address into a user name and a domain, you could write:
>>> addr = monty@[Link]
>>> uname, domain = [Link](@)
The return value from split is a list with two elements; the first element is assigned to
uname, the second to domain.
>>> print(uname)
monty
>>> print(domain)
[Link]
Tuples as Return Values
A function can only return one value, but if the value is a tuple, it can return multiple
values in Python by separating them with commas, the returned values are stored as a
tuple.
>>>def student():
….. name = "Alice"
….. age = 20
….. grade = "A"
….. return name, age, grade
>>> result = student()
>>> print(result)
('Alice', 20, 'A')
Variable-Length Argument Tuples
In Python, variable-length argument tuples usually refer to using *args in a function
definition. It allows a function to accept any number of positional arguments, which are
collected into a tuple.
>>> def show_numbers(*args):
….. print(args)
>>> show_numbers(1, 2, 3)
(1, 2, 3)
Here args is a tuple, it can contain zero, one or many values
Why it's called variable-length: Because the number of arguments passed is not fixed:
show_numbers()
show_numbers(5)
show_numbers(10, 20, 30, 40)
All are valid in the above case
Lists and Tuples
Both lists and tuples are used to store collections of items in Python, but they differ in
important ways.
Lists: Mutable, you can change, add or remove items
Tuple: Immutable, once created items cannot be changed
Sample functions which returns data in the form of Tuples:
Zip Function
The zip() function is used to combine two or more iterables (like lists, tuples, strings)
element by element. It returns an iterator of tuples.
This example zips a string and a list:
>>> s = 'abc'
>>> t = [0, 1, 2]
>>> zip(s, t)
<zip object at 0x7f7d0a9e7c48>
The result is a zip object that knows how to iterate through the pairs. The most common
use of zip is in a for loop:
>>> for pair in zip(s, t):
... print(pair)
...
('a', 0)
('b', 1)
('c', 2)
A zip object is a kind of iterator, which is any object that iterates through a sequence.
Iterators are similar to lists in some ways, but unlike lists, you can’t use an index to select
an element from an iterator. If you want to use list operators and methods, you can use a
zip object to make a list:
>>> list(zip(s, t))
[('a', 0), ('b', 1), ('c', 2)]
The result is a list of tuples; in this example, each tuple contains a character from the
string and the corresponding element from the list.
Enumerate():
The enumerate() function is used to add an index number to each item in an iterable.
When used with tuples, it returns each element of the tuple along with its position.
>>> colors = ("red", "green", "blue")
>>> for index, value in enumerate(colors):
… print(index, value)
The result from enumerate is an enumerate object, which iterates a sequence of pairs;
each pair contains an index (starting from 0) and an element from the given sequence. In
this example, the output is
0 red
1 green
2 blue
Dictionaries and Tuples
Dictionaries have a method called items that returns a sequence of tuples, where each
tuple is a key-value pair:
>>> d = {'a':0, 'b':1, 'c':2}
>>> t = [Link]()
>>> t
dict_items([('c', 2), ('a', 0), ('b', 1)])
The result is a dict_items object, which is an iterator that iterates the key-value pairs.
You can use it in a for loop like this:
>>> for key, value in [Link]():
... print(key, value)
...
c2
a0
b1
As you should expect from a dictionary, the items are in no particular order
Going in the other direction, you can use a list of tuples to initialize a new dictionary:
>>> t = [('a', 0), ('c', 2), ('b', 1)]
>>> d = dict(t)
>>> d
{'a': 0, 'c': 2, 'b': 1}
Combining dict with zip yields a concise way to create a dictionary:
>>> d = dict(zip('abc', range(3)))
>>> d
{'a': 0, 'c': 2, 'b': 1}
The dictionary method update also takes a list of tuples and adds them, as key-value pairs,
to an existing dictionary. It is common to use tuples as keys in dictionaries (primarily
because you can’t use lists). For example, a telephone directory might map from last-
name, first-name pairs to telephone numbers. Assuming that we have defined last, first
and number, we could write:
directory[last, first] = number
The expression in brackets is a tuple. We could use tuple assignment to traverse this
dictionary:
for last, first in directory:
print(first, last, directory[last, first])
This loop traverses the keys in directory, which are tuples. It assigns the elements of each
tuple to last and first, then prints the name and corresponding telephone number. There
are two ways to represent tuples in a state diagram. The more detailed version shows the
indices and elements just as they appear in a list. For example, the tuple ('Cleese', 'John')
would appear as in Figure 12-1.
But in a larger diagram you might want to leave out the details. For example, a diagram
of the telephone directory might appear as in Figure 12-2. Here the tuples are shown
using Python syntax as a graphical shorthand.
Questions
1. Write a Python Program to Read student details (name, USN, marks) into a list of tuples, display
all records sorted based on USN and find the student with the highest marks.
2. For the given data = (10, 12, 12, 54, 33, 33, 344, 41),develop a python program to, find the
occurrence of each number in the tuple, extract all the odd numbers and store them in a new
tuple, also find the length of values in each item in the tuple and store them in the tuple in the
format (item,len)
3. For the given block names of employees residence in a tuple: block=("A", "B", "A", "C", "B",
"A","A","A"), develop a Python Program to Count the occurrence of each block , and find the block
where maimum employees reside, Store the block name and number of occurance in a file in the
given format " Ex: A:7, B:2, C:1"
4. A retail management system stores phone numbers by using a combination of first name and
last name as a unique identifier, ensuring that each employee can be accessed accurately even
when multiple people share the same first name.
Analyze the scenario and: Create a directory with at least two contacts using an appropriate key
type. Add a new contact and examine the update, retrieve a phone number using the full name,
Justify why tuples are preferred over lists as keys, considering their impact on program behavior.
5. Analyze the given output to determine the relationship between the tuple elements and their
respective positions. Using the concepts of lists and tuples, write a Python program that
reproduces the same output. Further, examine how built-in functions or iteration constructs are
used in creating the final list of tuples, and justify the logic and approach adopted to achieve the
required result.
Output:
('Chips', 0)
('Milk', 1)
('Clip', 2)
[('Chips', 0), ('Milk', 1), ('Clip', 2)]
CHAPTER 13
Case Study: Data Structure Selection
Random Numbers
Python generates pseudorandom numbers — sequences that look random but come from a
deterministic algorithm.
The random Module
• [Link]()
Returns a float in [0.0, 1.0) — includes 0.0, excludes 1.0.
• [Link](low, high)
Returns an integer between low and high, inclusive on both ends.
• [Link](sequence)
Picks a single element from a sequence at random.
The function random returns a random float between 0.0 and 1.0 (including 0.0 but not
1.0).
Each time you call random, you get the next number in a long series. To see a sample, run
this loop:
import random
for i in range(10):
x = [Link]()
print(x)
The function randint takes parameters low and high and returns an integer between low
and high (including both).
>>> [Link](5, 10)
Output: 5
>>> [Link](5, 10)
Output: 9
To choose an element from a sequence at random, you can use choice:
>>> t = [1, 2, 3]
>>> [Link](t)
Output: 3
>>> [Link](t)
Output: 2
Code Example — Print 10 Random Floats
import random
for i in range(10):
x = [Link]()
print(x)
13.3 Word Histogram
A histogram (implemented as a dictionary) maps each unique word to its frequency count.
This is the backbone of word frequency analysis.
Core Functions
• process_file(filename) : opens the file, iterates lines, delegates to process_line(),
returns the hist dict.
• process_line(line, hist) : cleans and splits the line, then updates the histogram.
Here is a program that reads a file and builds a histogram of the words in the file:
import string
def process_file(filename):
hist = dict()
fp = open(filename)
for line in fp:
process_line(line, hist)
return hist
def process_line(line, hist):
line = [Link]('-', ' ')
for word in [Link]():
word = [Link]([Link] + [Link])
word = [Link]()
hist[word] = [Link](word, 0) + 1
hist = process_file('[Link]')
This program reads [Link], which contains the text of Emma by Jane Austen.
process_file loops through the lines of the file, passing them one at a time to
process_line. The histogram hist is being used as an accumulator.
process_line uses the string method replace to replace hyphens with spaces before using
split to break the line into a list of strings. It traverses the list of words and uses strip and
lower to remove punctuation and convert to lower case. (It is a shorthand to say that
strings are “converted”; remember that strings are immutable, so methods like strip and
lower return new strings.)
Finally, process_line updates the histogram by creating a new item or incrementing an
existing one. To count the total number of words in the file, we can add up the frequencies
in the histogram:
def total_words(hist):
return sum([Link]())
13.4 Most Common Words
To rank words by frequency, build a list of (frequency, word) tuples and sort it in
descending order.
most_common(hist) Function
def most_common(hist):
t = []
for key, value in [Link]():
[Link]((value, key))
[Link](reverse=True)
return t
Frequency is placed first in the tuple so Python's built-in sort naturally orders by frequency
(highest first).
Printing Top 10 Words with Tab Alignment
t = most_common(hist)
for freq, word in t[:10]:
print(word, freq, sep='\t')
The keyword argument sep to tell print to use a tab character as a “separator”, rather than
a space, so the second column is lined up. Here are the results from Emma:
Top 10 Most Common Words in Emma
Word Frequency
to 5242
the 5205
and 4897
of 4295
i 3191
a 3130
it 2529
her 2483
was 2400
she 2364
13.5 Optional Parameters
Python functions can have optional parameters with default values, making them flexible
without requiring the caller to always supply every argument.
Syntax
def print_most_common(hist, num=10):
t = most_common(hist)
print('The most common words are:')
for freq, word in t[:num]:
print(word, freq, sep='\t')
The first parameter is required; the second is optional. The default value of num is 10. If you
only provide one argument:
print_most_common(hist)
num gets the default value. If you provide two arguments:
print_most_common(hist, 20)
num gets the value of the argument instead. In other words, the optional argument
overrides the default value. If a function has both required and optional parameters, all the
required parameters have to come first, followed by the optional ones.
• hist is required — must always be provided.
• num is optional — defaults to 10 if omitted.
• Calling print_most_common(hist) uses the default (top 10).
• Calling print_most_common(hist, 20) overrides the default (top 20).
13.6 Dictionary Subtraction
This section demonstrates set subtraction using dictionaries: finding all words in one set (the
book) that are absent from another set (a word list). Subtract takes dictionaries d1 and d2
and returns a new dictionary that contains all the keys from d1 that are not in d2. Since we
don’t really care about the values, we set them all to None.
def subtract(d1, d2):
res = dict()
for key in d1:
if key not in d2:
res[key] = None
return res
To find the words in the book that are not in [Link], we can use process_file to build a
histogram for [Link], and then subtract:
words = process_file('[Link]')
diff = subtract(hist, words)
print("Words in the book that aren't in the word list:")
for word in diff:
print(word, end=' ')
Here are some of the results from Emma:
• Names and possessives (e.g., jane's, woodhouses)
• Archaic/rare words (e.g., rencontre — a chance encounter)
• A few common words missing from the word list
13.7 Random Words
To choose a random word from the histogram, the simplest algorithm is to build a list with
multiple copies of each word, according to the observed frequency, and then choose from the
list:
Simple Approach — Build a Long List
def random_word(h):
t = []
for word, freq in [Link]():
[Link]([word] * freq)
return [Link](t)
• [word] * freq creates a list with freq copies of word.
• [Link]() appends all elements of a sequence (vs. append which adds one item).
• Works correctly but is memory-inefficient — the list is as large as the whole text.
• Also slow: the list is rebuilt on every call.
Efficient Approach — Cumulative Sum + Binary Search
A much more efficient algorithm:
1. Get the list of words with keys().
2. Build a cumulative sum list of word frequencies. The last element equals the total
word count n.
3. Pick a random integer from 1 to n.
4. Use bisection search to find which word the random number maps to.
13.8 Markov Analysis
Imagine you're reading a sentence and you cover up everything except the last two words.
Based on just those two words, you try to guess what comes next. That's essentially what
Markov analysis does — it predicts the next item based only on the current state, not the full
history. The probability of the next word depends only on the current prefix. This is called
the Markov property.
For example, the song Eric, the Half a Bee begins:
Half a bee, philosophically,
Must, ipso facto, half not be.
But half the bee has got to be
Vis a vis, its entity. D’you see?
But can a bee be said to be
Or not to be an entire bee
When half the bee is not a bee
Due to some ancient injury?
Markov analysis models the statistical relationships between words in a text. It captures the
probability that a given sequence of words (a prefix) will be followed by a particular word
(a suffix).
• Prefix — a sequence of consecutive words (e.g., 'half the' for prefix length 2).
• Suffix — the word that follows a prefix (e.g., 'bee').
• The Markov model is a dict mapping each prefix → list of possible suffixes.
For the prefix "half the", the suffix list is ["bee", "bee"]. The same suffix appears twice, which
reflects a repeated pattern in the poem itself. The phrase “half the bee” occurs multiple times,
making it a recurring and emphasized expression. In a Markov model, repeated suffixes
preserve frequency information: since bee always follows half the in the training text, the
model learns this phrase as highly fixed and strongly associated. This suggests that “half the
bee” is a stable structural unit in the poem and serves as a repeated thematic focus.
• half the" → ["bee", "bee"]: only one unique continuation (bee), repeated twice.
• "the bee" → ["has", "is"]: two different continuations.
This means "half the" is highly predictable because every occurrence leads to the same next
word. It has low flexibility and behaves almost like a set phrase. By contrast, "the bee" is
more flexible because it can continue in multiple ways (“has” or “is”). This makes it less
predictable and gives the text more variation at that point.
• It creates a direct relationship between a context (the prefix) and all words that can
follow it.
• Repeated suffixes naturally encode frequency information. For example, ["bee",
"bee"] means bee has a higher probability of being selected than if it appeared only
once.
• During generation, the algorithm can:
1. Look up the current prefix in the dictionary.
2. Randomly choose one suffix from its list.
3. Append the chosen word and shift the prefix window forward.
This representation therefore captures both possible continuations and their probabilities,
allowing generated text to mimic the patterns and style of the original poem.
Practice Programs
Write a python program to find most common word in the text file.
import string
def process_file(filename):
hist = dict()
fp = open(filename)
for line in fp:
process_line(line, hist)
return hist
def process_line(line, hist):
line = [Link]('-', ' ')
for word in [Link]():
word = [Link]([Link] + [Link])
word = [Link]()
hist[word] = [Link](word, 0) + 1
def most_common(hist):
t = []
for key, value in [Link]():
[Link]((value, key))
[Link](reverse=True)
return t
hist = process_file('[Link]')
t = most_common(hist)
print('The most common words are:')
for freq, word in t[:10]:
print(word, freq, sep='\t')
Write a python program to perform dictionary subtraction.
import string
def process_file(filename):
hist = dict()
fp = open(filename)
for line in fp:
process_line(line, hist)
return hist
def process_line(line, hist):
line = [Link]('-', ' ')
for word in [Link]():
word = [Link]([Link] + [Link])
word = [Link]()
hist[word] = [Link](word, 0) + 1
def subtract(d1, d2):
res = dict()
for key in d1:
if key not in d2:
res[key] = None
return res
d1 = process_file('[Link]')
d2 = process_file('[Link]')
diff = subtract(d1, d2)
print("Words in the book that aren't in the word list:")
for word in diff:
print(word, end=' ')
Write a python program using a dictionary to process the file by removing
punctuation and converting all words to lowercase for accurate counting.
import string
file = open("[Link]", "r")
hist = {}
for line in file:
for word in [Link]():
word = [Link]([Link] + [Link])
word = [Link]()
if word != "":
hist[word] = [Link](word, 0) + 1
[Link]()
print("Word Histogram:")
for word, count in [Link]():
print(word, ":", count)
Write a Python program to generate 10 random integers between 1 and 50, store
them in a list, and determine the maximum and minimum values.
import random
nums = []
for i in range(10):
n = [Link](1, 50)
[Link](n)
print("Random numbers:", nums)
print("Maximum:", max(nums))
print("Minimum:", min(nums))
Write a python program to read 3 files from the user and find the total number of
characters in each file and print the output in a new file.
f1=open('[Link]','w')
for i in range(3):
name=input("Enter the file name")
f=open(name)
data=[Link]()
print("The total number of characters in each file is ", len(data))
[Link](data+'\n')
[Link]()
[Link]()
Files
Introduction
This chapter explains how Python programs store data permanently using files and
databases. Earlier programs were temporary because the data disappeared once the
program ended. Such programs are called transient programs. Some programs continue
running for a long time and save their data permanently so that the information is not lost
even after restarting the system. These are called persistent programs. Examples include
operating systems and web servers. Python provides different ways to store permanent
data such as text files and databases.
Reading and Writing Files
File handling is one of the most important concepts in Python programming. Files are
used to store information permanently so that it can be accessed later whenever
required. Python supports different operations on files such as opening, reading, writing,
appending, and closing files.
Text files store data in the form of characters and are commonly used for storing notes,
reports, source code, and other textual information. Python makes file handling simple
through built-in functions and methods.
A text file is a sequence of characters stored permanently in devices such as hard disks,
pen drives, or CDs. To write data into a file, Python uses the open() function with write
mode 'w'.
>>> fout = open('[Link]', 'w')
If the file already exists, the old content is removed and the file starts fresh. If the file does
not exist, Python creates a new file.
Data can be written into the file using the write() method.
>>> line1 = "This here's the wattle,"
>>> [Link](line1)
24
The value 24 indicates the number of characters written into the file. More data can be
added using another write() statement.
>>> line2 = "the emblem of our land."
>>> [Link](line2)
24
After writing data, the file should be closed using close().
>>> [Link]()
Closing the file saves all changes properly and releases system resources.
Format Operator
While storing data in files, different types of values such as integers, floating-point
numbers, and strings may need to be combined together. The format operator helps in
creating formatted strings by inserting values into a sentence in a readable form.
Formatting improves the appearance of output and makes programs easier to
understand. It is widely used while displaying reports, printing messages, and storing
structured information in files.
The write() method accepts only strings. Therefore, other data types should first be
converted into strings using str().
>>> x = 52
>>> [Link](str(x))
Python also provides the format operator % to insert values into strings. When used with
strings, % acts as a formatting operator.
>>> camels = 42
>>> '%d' % camels
'42'
Here %d is used for integers. Values can also be inserted into sentences.
>>> 'I have spotted %d camels.' % camels
'I have spotted 42 camels.'
Different format specifiers can be used together.
>>> 'In %d years I have spotted %g %s.' % (3, 0.1, 'camels')
'In 3 years I have spotted 0.1 camels.'
%d is used for integers, %g for floating-point numbers, and %s for strings.
If the number of values does not match the format specifiers or the wrong data type is
used, Python generates errors.
>>> '%d %d %d' % (1, 2)
TypeError: not enough arguments for format string
>>> '%d' % 'dollars'
TypeError: %d format: a number is required, not str
Filenames and Paths
A computer system organizes files using directories or folders. Every file has a location,
and Python uses paths to identify these locations. Understanding filenames and paths is
essential while working with files because programs must know where to read or store
data.
Python provides several functions through the os module to work with files, directories,
and paths. These functions help programmers access files easily and manage directory
structures effectively.
Files are stored inside directories or folders. Every running program has a current working
directory which acts as the default location for most file operations. The os module
provides functions to work with files and directories.
>>> import os
>>> cwd = [Link]()
>>> cwd
'/home/dinsdale'
The function [Link]() returns the current working directory. A path specifies the
location of a file or directory. A filename like [Link] is called a relative path because it
depends on the current directory. A complete location beginning with / is called an
absolute path.
>>> [Link]('[Link]')
'/home/dinsdale/[Link]'
Python also provides functions to check files and directories.
>>> [Link]('[Link]')
True
>>> [Link]('[Link]')
False
>>> [Link]('/home/dinsdale')
True
The contents of a directory can be listed using [Link]().
>>> [Link](cwd)
['music', 'photos', '[Link]']
The following recursive program walks through directories and prints file names.
def walk(dirname):
for name in [Link](dirname):
path = [Link](dirname, name)
if [Link](path):
print(path)
else:
walk(path)
Here [Link]() combines directory names and filenames to create a complete path.
Catching Exceptions
While performing file operations, many unexpected situations may occur. Files may not
exist, users may not have permission to access them, or invalid operations may be
attempted. Such situations generate runtime errors called exceptions.
Python provides exception handling mechanisms to prevent programs from terminating
abruptly. Using exception handling makes programs more reliable and user friendly
because errors can be managed gracefully.
Errors may occur while reading or writing files. For example, trying to open a file that does
not exist produces an error.
>>> fin = open('bad_file')
IOError: [Errno 2] No such file or directory: 'bad_file'
Trying to access a file without permission also produces an error.
>>> fout = open('/etc/passwd', 'w')
PermissionError: [Errno 13] Permission denied: '/etc/passwd'
Opening a directory as a file generates another type of error.
>>> fin = open('/home')
IsADirectoryError: [Errno 21] Is a directory: '/home'
Python handles such errors using try and except blocks.
try:
fin = open('bad_file')
except:
print('Something went wrong.')
The try block contains risky statements, while the except block executes when an error
occurs. This method is called exception handling.
Databases
Databases are used to store and organize large amounts of information permanently.
Unlike normal files, databases allow data to be managed efficiently using keys and
values. Databases are widely used in applications such as banking systems, websites,
libraries, and student management systems.
Python provides modules to create simple databases easily. The dbm module allows
programmers to store and retrieve data similarly to dictionary operations.
A database is a file organized to store data permanently. The dbm module is used to
create and manage databases in Python.
>>> import dbm
>>> db = [Link]('captions', 'c')
The mode 'c' creates the database if it does not already exist. Database objects work
similarly to dictionaries.
>>> db['[Link]'] = 'Photo of John Cleese.'
Data can be retrieved using keys.
>>> db['[Link]']
b'Photo of John Cleese.'
The letter b indicates that the value is stored as bytes. Existing values can also be
updated.
>>> db['[Link]'] = 'Photo of John Cleese doing a silly walk.'
>>> db['[Link]']
b'Photo of John Cleese doing a silly walk.'
A database can be traversed using a loop.
for key in db:
print(key, db[key])
After completing the work, the database should be closed.
>>> [Link]()
Pickling
Sometimes programs need to store complex Python objects such as lists, tuples, or
dictionaries. Databases generally store only strings or bytes, so direct storage of Python
objects becomes difficult.
The pickle module solves this problem by converting Python objects into byte streams
that can be stored in files or databases. Later, these byte streams can be converted back
into their original objects whenever needed.
The pickle module converts Python objects into strings that can be stored in files or
databases. The function [Link]() converts an object into a byte string.
>>> import pickle
>>> t = [1, 2, 3]
>>> [Link](t)
b'\x80\x03]q\x00(K\x01K\x02K\x03e.'
The function [Link]() converts the byte string back into the original object.
>>> t1 = [1, 2, 3]
>>> s = [Link](t1)
>>> t2 = [Link](s)
>>> t2
[1, 2, 3]
Although the new object contains the same values, it is not the same object in memory.
>>> t1 == t2
True
>>> t1 is t2
False
Pipes
Operating systems provide command-line utilities to perform different tasks. Python
programs can interact with these system commands using pipes. Pipes establish
communication between Python and the operating system.
Using pipes, Python can execute external commands and capture their output. This
feature is useful in system administration, automation tasks, and utility programs.
A pipe allows Python programs to run system commands. The [Link]() function is used
for this purpose.
>>> cmd = 'ls -l'
>>> fp = [Link](cmd)
The output of the command can be read using read().
>>> res = [Link]()
After execution, the pipe is closed.
>>> stat = [Link]()
>>> print(stat)
None
The value None indicates successful execution.
The following example shows the use of the md5sum command.
>>> filename = '[Link]'
>>> cmd = 'md5sum ' + filename
>>> fp = [Link](cmd)
>>> res = [Link]()
>>> stat = [Link]()
>>> print(res)
1e0033f0ed0656636de0d75144ba32e0 [Link]
>>> print(stat)
None
Writing Modules
As programs become larger, organizing code into separate files becomes necessary.
Python allows programmers to store reusable functions and statements in modules. A
module is simply a Python file containing definitions and functions.
Modules improve code reusability, reduce duplication, and make programs easier to
maintain. Once a module is created, it can be imported and used in multiple programs.
Any Python file containing code can be imported as a module.
def linecount(filename):
count = 0
for line in open(filename):
count += 1
return count
print(linecount('[Link]'))
This program counts the number of lines in a file.
The module can be imported using:
>>> import wc
7
After importing, functions inside the module can be accessed.
>>> wc
<module 'wc' from '[Link]'>
>>> [Link]('[Link]')
7
Programs intended to work as modules generally use the following statement.
if __name__ == '__main__':
print(linecount('[Link]'))
If the program is executed directly, __name__ becomes '__main__'. If imported as a
module, the test code does not run.
read() Function
The read() function is used to read the contents of a file. It reads the entire file as a single
string. If the file contains multiple lines, all the lines are read together including spaces
and newline characters.
Syntax: [Link]()
readlines() Function
The readlines() function is used to read all lines of a file and store them as elements of a
list. Each line of the file becomes one element in the list.
Syntax: [Link]()
Programs on Files
Develop a Program to count the number of characters in the file except 'Space' and
extract all the digits, store as a list of tuples with their index values
file = open("[Link]", "r")
text = [Link]()
[Link]()
count = 0
for ch in text:
if ch != ' ':
count += 1
print("Number of characters excluding spaces:", count)
l = []
for i in range(len(text)):
if text[i].isdigit():
[Link]((text[i], i))
print("Digits with index positions:")
print(l)
Write a program to: i) Count the number of words in the file ii) Read from two
different files and store the content in a single file
i) f = open("[Link]", "r")
data = [Link]()
words = [Link]()
count = len(words)
print("Number of words in the file:", count)
[Link]()
ii) f1 = open("[Link]", "r")
f2 = open("[Link]", "r")
data1 = [Link]()
data2 = [Link]()
f3 = open("[Link]", "w")
[Link](data1)
[Link](data2)
print("Contents copied successfully")
[Link]()
[Link]()
[Link]()
Write a program to Count the number of unique digits in the file
f = open("[Link]", "r")
data = [Link]()
unique = []
for ch in data:
if [Link]():
if ch not in unique:
[Link](ch)
print("Unique digits are:", unique)
print("Number of unique digits:", len(unique))
[Link]()
Develop a program to extract the special characters from the file and find the
occurrence of each unique character
f = open("[Link]", "r")
data = [Link]()
special = {}
for i in data:
if i in "*$.^&()!@#%":
if i in special:
special[i] = special[i] + 1
else:
special[i] = 1
print("Special characters and their occurrences:")
print(special)
[Link]()
Write a program to read the contents of the file and create a new file which consists
of only email IDs
f = open("[Link]", "r")
data = [Link]()
words = [Link]()
f1 = open("[Link]", "w")
for i in words:
if "@" in i and "." in i:
[Link](i + "\n")
print("Email IDs copied successfully")
[Link]()
[Link]()
Develop a program to count the number of lines in the files with more than 10
characters
c=0
f = open('[Link]')
for i in f:
if len(i) > 10:
c=c+1
print("Total number of lines with more than 10 characters :", c)
[Link]()
Write a program to extract all strings from the file with length greater than 3 and
store them in a file separated by ‘,’
f=open('[Link]')
f1=open('[Link]','w')
for i in f:
words=[Link]()
for w in words:
if len(w)>3:
[Link](w+',')
[Link]()
[Link]()
Consider a text file with the names and marks of students, develop a program to
store the name and marks in two different lists; Zip the name and marks to
generate a list of tuples and print the list of tuples in reverse order
name=[]
marks=[]
f=open('[Link]').read()
data=[Link](' ')
for i in data:
if [Link]():
[Link](i)
else:
[Link](i)
print("The list of name",name)
print("The list of marks",marks)
data=list(zip(name,marks))
print(sorted(data,reverse=True))
Write a program to read 5 names and age from the user, write to a file in the format
'Name:Age', and in separate lines.
f=open('[Link]','w')
for i in range(5):
name=input("Enter the name: ")
age=input("Enter the age: ")
[Link](name+":"+age+'\n')
[Link]()
Write a program to count the number of vowels and consonants from a given file.
file = open("[Link]", "r")
text = [Link]().lower()
[Link]()
vowels = 0
consonants = 0
for ch in text:
if ch in "aeiou":
vowels += 1
elif [Link]():
consonants += 1
print("Number of vowels:", vowels)
print("Number of consonants:", consonants)
Write a program to read the file and write only odd line content to a new file.
infile = open("[Link]", "r")
lines = [Link]()
[Link]()
odd_lines = lines[::2]
outfile = open("[Link]", "w")
[Link](odd_lines)
[Link]()
(OR)
f = open("[Link]", "r")
lines = [Link]()
[Link]()
f1 = open("[Link]", "w")
for i in range(len(lines)):
if i % 2 == 0:
[Link](lines[i])
[Link]()
Write a program to read the file and write only even line content to a new file.
infile = open("[Link]", "r")
lines = [Link]()
[Link]()
odd_lines = lines[1::2]
outfile = open("[Link]", "w")
[Link](odd_lines)
[Link]()
(OR)
f = open("[Link]", "r")
lines = [Link]()
[Link]()
f1 = open("[Link]", "w")
for i in range(len(lines)):
if i % 2 != 0:
[Link](lines[i])
[Link]()