Python Programming 19-06-2023
Python Programming 19-06-2023
Python Programming
Namya Press
India: 213, Vardan House, 7/28 Ansari Road Darya Ganj - Delhi India 110002
USA: 10685-B Hazelhurst Dr. # 31013 Houston, Texas USA - 77043
Email: namyapress@gmail.com
Website: https://namyapress.com
This book is being sold on the condition that it cannot be used commercially or in
any other form without the prior written permission of the publisher. This book
cannot be republished or sold or rented. And it cannot be operated among readers
in book-binding or any other form. All these conditions will also apply to the buyer
of the book. All rights of copyright are reserved in this context.
This book has been published with all efforts taken in making the material error-
free after the consent of the author. However, the author and the publisher do not
assume and hereby disclaim any liability of any part for loss, damage, or disruption
caused by error or omissions.
Preface
Matter Required
vi | Python Programming
Contents
Preface
xi
1. Python Basics 1
1.1 Entering Expressions into the Interactive Shell 2
1.2 The Integer Data Type 3
1.3 Floating-Point Data Type 4
1.4 The String Data Type 4
1.5 String Concatenation and Replication 5
1.6 Storing Values in Variables 5
1.7 First Program 6
1.8 Dissecting Your Program 6
Exercise 7
2. Flow Control 11
2.1 Boolean Values 11
2.2 Comparison Operators 14
2.3 Boolean Operators 17
2.4 Combining Boolean and Comparison Operators 19
2.5 Elements of Flow Control 21
2.6 Program Execution 22
2.7 Flow Control Statements 23
2.8 Importing Modules 27
2.9 Ending a Program Early with sys.exit() 29
Exercise 30
viii | Python Programming
3. Functions 33
3.1 def Statements with Parameters 35
3.2 Return Values and return Statements 36
3.3 Keyword Arguments and print() 37
3.4 Local and Global Scope 38
3.5 The global Statement 39
3.6 Exception Handling 40
Exercise 41
4. Lists 46
4.1 The List Data Type 46
4.2 Working with Lists 47
4.3 Augmented Assignment Operators 49
4.4 Methods 49
Exercise 53
5. Dictionaries and Structuring Data 58
5.1 The Dictionary Data Type 58
5.2 Pretty Printing 60
5.3 Using Data Structures to Model Real-World Things 61
Exercise 64
6. Strings 68
6.1 Creating Strings 68
6.2 Useful String Methods 70
Exercise 73
7. Reading and Writing Files 78
7.1 Files and File Paths 78
7.2 The os.path Module 79
7.3 The File Reading/Writing Process 80
7.4 Saving Variables with the shelve Module 83
7.5 Saving Variables with the pprint.pformat() Function 84
Exercise 86
Contents | ix
8. Organizing Files 91
8.1 The shutil Module 91
8.2 Walking a Directory Tree 93
8.3 Compressing Files with the zipfile Module 94
Exercise 95
9. Web Scraping 100
9.1 Project: MAPIT.PY with the web browser Module 100
9.2 Introduction to the Web Browser Module 101
9.3 Building MAPIT.PY 104
9.4 Running MAPIT.PY 105
Exercise 110
1
Python Basics
Numeric Types:
•• int: Represents integer numbers, e.g., 5, -10, 100.
•• float: Represents floating-point numbers (decimals), e.g.,
3.14, -2.5, 0.75.
•• complex: Represents complex numbers with real and
imaginary parts, e.g., 2 + 3j, -1.5 - 2j.
Sequence Types:
•• str: Represents a string of characters, e.g., “Hello, World!”,
‘Python’.
•• list: Represents an ordered collection of items, enclosed in
square brackets [], e.g., [1, 2, 3], [‘apple’, ‘banana’, ‘orange’].
•• tuple: Represents an ordered collection of items (immutable),
enclosed in parentheses (), e.g., (1, 2, 3), (‘a’, ‘b’, ‘c’).
Python Basics | 3
Mapping Type:
•• dict: Represents a collection of key-value pairs, enclosed in
curly braces {}, e.g., {‘name’: ‘John’, ‘age’: 25}.
Set Types:
•• set: Represents an unordered collection of unique elements,
enclosed in curly braces {}, e.g., {1, 2, 3}, {‘apple’, ‘banana’,
‘orange’}.
•• frozenset: Represents an immutable version of a set.
Boolean Type:
•• bool: Represents a Boolean value, either True or False.
None Type:
•• None: Represents the absence of a value or null.
These data types can be used to store and manipulate different
kinds of data in Python. It’s important to understand the data type
of a variable or value, as it affects the operations you can perform
on it and how it behaves in your program.
EXERCISE
c) “Hello World!”
d) Error Answer:
Answer: c) “Hello World!”
10. Which statement is used to execute a block of code repeatedly
as long as a certain condition is met?
a) if statement
b) for loop
c) while loop
d) else statement
Answer: c) while loop
2
Flow Control
x = True
y = False
In this example, x is assigned the boolean value True, and y is
assigned the boolean value False.
Let’s explore each comparison operator and see how they work
with different data types.
Flow Control | 15
Equal to (==)
The equal to operator == checks if the values on both sides are
equal. Here’s an example:
x=5
y=5
result = x == y
print(result) # Output: True
In this case, x == y evaluates to True because both x and y have
the same value, which is 5.
Not equal to (!=)
The not equal to operator != checks if the values on both sides
are not equal. Let’s see an example:
x=5
y = 10
result = x != y
print(result) # Output: True
In this example, x != y evaluates to True because x is 5 and y is
10, which are not equal.
Less than (<) and Greater than (>)
The less than operator < checks if the value on the left side is
less than the value on the right side. Similarly, the greater than
operator > checks if the value on the left side is greater than the
value on the right side. Let’s look at some examples:
x=5
y = 10
result1 = x < y
result2 = x > y
print(result1) # Output: True
print(result2) # Output: False
In this case, x < y evaluates to True because 5 is indeed less
than 10. On the other hand, x > y evaluates to False since 5 is not
greater than 10.
16 | Python Programming
The and operator returns True if both operands are True, and
False otherwise. It can be used with boolean values or expressions.
Let’s see some examples:
Using Boolean Values
x = True
y = False
result = x and y
print(result) # Output: False
In this case, x and y evaluates to False because y is False.
Using Boolean Expressions
x=5
18 | Python Programming
y = 10
result = x > 0 and y < 20
print(result) # Output: True
In this example, x > 0 and y < 20 evaluates to True because
both conditions are true.
z=7
result = x < y and y > z
print(result) # Output: True
In this case, x < y and y > z are both true, so x < y and y > z
evaluates to True.
else:
print(“x is non-positive”)
In this example, the if-else statement checks if the condition
x > 0 is true. If it is true, the code block print(“x is positive”) is
executed. Otherwise, the code block print(“x is non-positive”) is
executed. The output will be “x is positive”.
if-elif-else statement:
Syntax:
if condition1:
# Code block executed if condition1 is true
elif condition2:
# Code block executed if condition1 is false and condition2 is
true
else:
# Code block executed if all conditions are false
Example:
x=5
if x > 0:
print(“x is positive”)
elif x < 0:
print(“x is negative”)
else:
print(“x is zero”)
In this example, the if-elif-else statement checks multiple
conditions. If x > 0 is true, the code block print(“x is positive”) is
executed. If it is false and x < 0 is true, the code block print(“x is
negative”) is executed. If both conditions are false, the code block
print(“x is zero”) is executed. The output will be “x is positive”.
Looping Statements:
for loop:
Syntax:
for item in iterable:
Flow Control | 25
Example:
try: x = 10 / 0
except ZeroDivisionError:
print(“Division by zero is not allowed”)
In this example, the try block attempts to perform a division
by zero operation, which raises a ZeroDivisionError exception.
The except block catches the exception and executes the code
block print(“Division by zero is not allowed”). The output will be
“Division by zero is not allowed”.
finally statement:
Syntax:
try:
# Code block where exceptions might occur except
ExceptionType
: # Code block executed if an exception of type ExceptionType
occurs
finally:
# Code block always executed, regardless of whether an exception
occurred or not
Example:
try: x = 10 / 0
except ZeroDivisionError:
print(“Division by zero is not allowed”)
finally:
print(“Finally block executed”)
In this example, similar to the previous example, the try block
raises a ZeroDivisionError exception. The except block catches
the exception and executes the code block print(“Division by zero
is not allowed”). Finally, the finally block is always executed,
regardless of whether an exception occurred or not. The output will
be:
•• Division by zero is not allowed
•• Finally block executed
Flow Control | 27
EXERCISE
Output:
None
In this code, we define a function called perform_task that
does not have a return statement. When we call this function and
assign its result to the variable result, we get the value None. This
indicates that the function does not return anything.
We can also explicitly return None from a function:
def greet(name):
if name:
return “Hello, “ + name + “!”
else:
return None
result = greet(“”)
print(result)
Output:
None
In this example, if the name parameter is an empty string, the
function returns None to indicate that no greeting can be formed.
print(global_var) # Output: 20
In the above example, we define a global variable global_var with
an initial value of 10. Inside the modify_global function, we use
the global statement to indicate that we want to modify the global
variable global_var. After calling the modify_global function, we
can see that the value of global_var has changed to 20.
EXERCISE
Lists can be iterated using loops. We can use a for loop to iterate
over each element in a list:
for fruit in fruits:
print(fruit)
This will output each fruit on a new line:
grape
banana
orange
4.4 METHODS
In this section, we will explore various methods available for
lists. Methods are built-in functions that can be applied directly to
list objects.
fruits.append(‘pear’)
print(fruits)
Output: [‘grape’, ‘banana’, ‘orange’, ‘pear’]
The extend() method is used to append multiple elements from
an iterable (e.g., another list) to the end of the original list:
fruits.extend([‘kiwi’, ‘mango’])
print(fruits)
Output: [‘grape’, ‘banana’, ‘orange’, ‘pear’, ‘kiwi’, ‘mango’]
Syntax:
list_name.insert(index, value)
Example:
fruits = [‘apple’, ‘banana’, ‘orange’]
fruits.insert(1, ‘grape’)
print(fruits)
Output: [‘apple’, ‘grape’, ‘banana’, ‘orange’]
In this example, the insert() method is used to insert the element
‘grape’ at index 1 in the fruits list. As a result, the list is modified to
[‘apple’, ‘grape’, ‘banana’, ‘orange’].
2. The index() Method:
The index() method is used to find the index of the first occurrence
of a specified element within a list. It takes one argument: the value
of the element to be searched.
Syntax:
list_name.index(value)
Example:
fruits = [‘apple’, ‘banana’, ‘orange’]
index = fruits.index(‘banana’)
print(index)
Output: 1
In this example, the index() method is used to find the index
of the element ‘banana’ in the fruits list. The returned value, 1,
represents the index position of ‘banana’ within the list.
3. The sort() Method:
The sort() method is used to sort the elements of a list in
ascending order. This method modifies the original list and does
not return a new sorted list.
Syntax:
list_name.sort()
Example:
numbers = [5, 2, 8, 1, 3]
52 | Python Programming
numbers.sort()
print(numbers)
Output: [1, 2, 3, 5, 8]
In this example, the sort() method is applied to the numbers list,
which contains unsorted integers. After calling the sort() method,
the list is sorted in ascending order: [1, 2, 3, 5, 8].
4. The reverse() Method:
The reverse() method is used to reverse the order of elements in
a list. This method modifies the original list and does not return a
new reversed list.
Syntax:
list_name.reverse()
Example:
fruits = [‘apple’, ‘banana’, ‘orange’]
fruits.reverse()
print(fruits)
Output: [‘orange’, ‘banana’, ‘apple’]
In this example, the reverse() method is applied to the fruits
list. After calling the reverse() method, the order of elements in the
list is reversed: [‘orange’, ‘banana’, ‘apple’].Top of Form
Lists | 53
EXERCISE
‘name’: ‘John’,
‘age’: 30,
‘city’: ‘New York’
}
In this example, the dictionary ‘person’ has three key-value
pairs: ‘name’: ‘John’, ‘age’: 30, and ‘city’: ‘New York’. You can access
the values using the respective keys:
print(person[‘name’])
Output: John
print(person[‘age’])
Output: 30
print(person[‘city’])
Output: New York
‘grape’: 1,
‘orange’: 5,
‘watermelon’: 2}
The ‘pprint’ module also provides other functions, such as
‘pformat()’ that returns the formatted string instead of printing it
directly.
student1 = {
‘name’: ‘John’,
‘subjects’: {
‘Maths’: {
‘teacher’: ‘Mr. Smith’,
‘grade’: ‘A’
},
‘English’: {
‘teacher’: ‘Ms. Johnson’,
‘grade’: ‘B’
}
}
}
Dictionaries and Structuring Data | 63
student2 = {
‘name’: ‘Alice’,
‘subjects’: {
‘Maths’: {
‘teacher’: ‘Mr. Johnson’,
‘grade’: ‘B’
},
‘Science’: {
‘teacher’: ‘Mrs. Adams’,
‘grade’: ‘A’
}
}
}
In this example, each student is represented by a dictionary
containing the ‘name’ and ‘subjects’ as key-value pairs. The
‘subjects’ key further maps to another dictionary, representing the
student’s subjects and their respective details.
EXERCISE
first_name = “John”
last_name = “Doe”
full_name = first_name + “ “ + last_name
print(full_name)
Output: John Doe
In this example, we concatenate the first name, a space character,
and the last name to create a full name string. The resulting string
is then printed.
lower():
text = “Hello, World!”
lowercase_text = text.lower()
print(lowercase_text)
Output: hello, world!
upper():
The upper() method converts all characters in a string to
uppercase. Here’s an example:
text = “Hello, World!”
uppercase_text = text.upper()
print(uppercase_text)
Output: HELLO, WORLD!
capitalize():
The capitalize() method converts the first character of a string
to uppercase and the rest to lowercase. Here’s an example:
text = “hello, world!”
capitalized_text = text.capitalize()
print(capitalized_text)
Output: Hello, world!
index():
The index() method works similarly to find(), but if the substring
is not found, it raises a ValueError instead of returning -1. Here’s
an example:
text = “Hello, World!”
index = text.index(“World”)
print(index)
Output: 7
count():
The count() method returns the number of occurrences of a
substring within a string. Here’s an example:
text = “Hello, World!”
occurrences = text.count(“l”)
print(occurrences)
Output: 3
EXERCISE
print(“Path is a directory.”)
else:
print(“Path is not a directory.”)
os.path.isfile(path):
This function checks if the path corresponds to a regular file.
import os
file_path = ‘data/file.txt’
if os.path.isfile(file_path):
print(“Path is a file.”)
else:
print(“Path is not a file.”)
To open a file, you use the open() function, which takes the file
path and a mode parameter as arguments. The mode determines
whether the file should be opened for reading, writing, or both. The
most commonly used modes are:
‘r’: Read mode (default). Opens the file for reading.
‘w’: Write mode. Opens the file for writing. Creates a new file if it
doesn’t exist, and truncates (empties) the file if it exists.
‘a’: Append mode. Opens the file for writing, but appends data to
the end instead of truncating it.
‘x’: Exclusive creation mode. Creates a new file but raises an
error if it already exists.
file_path = ‘data/file.txt’
# Open the file in write mode
file = open(file_path, ‘w’)
Reading and Writing Files | 81
Once the file is open for reading, you can read its contents. The
read() method is used to read the entire contents of the file, while
readline() reads one line at a time, and readlines() reads all lines
and returns them as a list.
file_path = ‘data/file.txt’
file_path = ‘data/file.txt’
print(name)
Output: John Doe
print(age)
Output: 30
print(favorite_fruits)
Output: [‘apple’, ‘banana’, ‘orange’]
EXERCISE
To copy a file using the shutil module, we can use the copy()
function. This function takes two arguments: the source file path
92 | Python Programming
and the destination folder path. It creates a new copy of the file in
the destination folder, while preserving the original file.
import shutil
source_file = ‘/path/to/source/file.txt’
destination_folder = ‘/path/to/destination/’
shutil.copy(source_file, destination_folder)
By utilizing the copy() function, you can easily duplicate files for
backup purposes or to process them separately without modifying
the original source.
EXERCISE
c. shutil module
d. zipfile module
Answer: c
2. What operation can be performed using the shutil module?
a. Copying files
b. Deleting files and directories
c. Moving files and directories
d. All of the above
Answer: d
3. What function from the shutil module is used to copy a file?
a. shutil.copy()
b. shutil.move()
c. shutil.delete()
d. shutil.compress()
Answer: a
4. What does walking through a directory tree mean?
a. Traversing all the directories and files within a directory
b. Renaming all the files in a directory
c. Compressing all the files in a directory
d. Deleting all the directories in a directory tree
Answer: a
5. Which module in Python provides the os.walk() function?
a. os module
b. shutil module
c. zipfile module
d. sys module
Answer: a
6. What does the os.walk() function return?
a. List of files in a directory
b. List of subdirectories in a directory
Organizing Files | 97
So, let’s dive into the world of web scraping and unleash the
power of extracting valuable information from the vast expanse of
the internet!
web driver acts as a bridge between your Python code and the
browser. The supported web drivers can be downloaded from the
Selenium website (https://www.selenium.dev/documentation/en/
webdriver/driver_requirements/).
Basic Usage: Once you have installed Selenium and downloaded
the web driver, you can start using the web browser module in
Python. The following code snippet demonstrates how to open a web
page using the web browser module:
from selenium import webdriver
# Create an instance of the web driver
driver = webdriver.Chrome(‘/path/to/chromedriver’)
# Open a web page driver.get(‘https://www.example.com’)
# Close the browser driver.quit()
In this example, we import the webdriver module from Selenium
and create an instance of the web driver for the Chrome browser. We
specify the path to the downloaded Chrome web driver executable.
Then, we use the get() method to open the specified web page (in this
case, ‘https://www.example.com’). Finally, we close the browser
using the quit() method.
Interacting with Web Elements: The web browser module allows
you to interact with various web elements such as buttons, forms,
input fields, and links. You can locate elements on a web page using
different methods such as by ID, by class name, by tag name, by
CSS selector, or by XPath. Once you have located an element, you
can perform actions like clicking, typing text, submitting forms, or
extracting information.
# Locate and interact with a button
button = driver.find_element_by_id(‘button-id’) button.click()
# Locate and interact with an input field
input_field = driver.find_element_by_name(‘input-name’)
input_field.send_keys(‘Hello, World!’)
# Locate and interact with a link
link = driver.find_element_by_link_text(‘Click Here’)
link.click()
Web Scraping | 103
To use the web browser module, we need to install it. Open the
terminal and run the following command:
pip install webbrowser
Now, we need to handle the user’s input and pass it to the get_
coordinates() function. Here’s the code:
def main():
if len(sys.argv) > 1:
address = ‘ ‘.join(sys.argv[1:])
else:
address = input(“Enter an address: “)
# Get the coordinates for the address
lat, lng = get_coordinates(address)
# Print the coordinates
print(f”Latitude: {lat}\nLongitude: {lng}”)
if __name__ == ‘__main__’:
main()
When prompted, enter the address for which you want to retrieve
the coordinates. The application will then open the corresponding
location on Google Maps in your web browser and display the
latitude and longitude coordinates in the terminal.
Section 1: Understanding the requests Module
The requests module is a popular Python library that simplifies
the process of making HTTP requests. It allows us to send GET and
POST requests, handle cookies, headers, sessions, and much more.
Before diving into downloading files, let’s understand the basics of
the requests module.
Subsection 1.1: Installation and Importing
To get started, we need to install the requests module. Open your
terminal or command prompt and execute the following command:
$ pip install requests
Once installed, import the module into your Python script using
the following line of code:
import requests
Subsection 1.2: Making GET Requests
The requests module provides a simple interface to make GET
requests. Let’s see an example:
import requests
response = requests.get(‘https://api.example.com/data’)
print(response.text)
In the above example, we import the requests module and use
the get function to send a GET request to the specified URL. The
response object contains the server’s response, and we can access
the response content using the text attribute.
Section 2: Downloading Files from the Web
Now that we have a basic understanding of the requests module,
let’s explore how to download files from the web using Python. We
will focus on downloading image files in this section, but the same
concepts apply to other file types as well.
Web Scraping | 107
title = soup.title
title.string = “New Title”
paragraph = soup.find(‘p’)
paragraph.string = “Modified paragraph text”
# Save the modified HTML to a new file
with open(‘modified.html’, ‘w’) as file:
file.write(str(soup))
In this example, we open an HTML file (‘example.html’) and read
its content. Then, we create a BeautifulSoup object and modify
specific elements within the HTML structure. In this case, we change
the title and the text content of a paragraph element. Finally, we
save the modified HTML to a new file (‘modified.html’).
•• Other Libraries for Handling HTML: Besides BeautifulSoup,
there are other Python libraries that can assist in handling
HTML files. Some notable ones include:
•• lxml: A powerful library for processing XML and HTML files.
It provides a fast and efficient way to parse and manipulate
HTML structures.
•• html5lib: A pure-Python library that parses HTML according
to the HTML5 specification. It is generally slower than lxml
or BeautifulSoup but handles malformed HTML better.
Depending on your requirements, you can choose the library
that best suits your needs for working with HTML files in Python.
Working with HTML files in Python enables you to extract
data, manipulate the structure, and perform various tasks
programmatically. Libraries like BeautifulSoup, lxml, and html5lib
provide the necessary tools and functionalities to handle HTML files
effectively, making it easier to automate tasks, scrape data from
websites, or generate HTML content dynamically.
110 | Python Programming
EXERCISE
c) <http>
d) <data>
Answer: b) <div>
6. What is the purpose of the BeautifulSoup library in web
scraping?
a) It is used to download files from the web.
b) It is used to parse HTML and XML documents.
c) It provides a web browser interface for scraping data.
d) It is used to save downloaded files to the hard drive.
Answer: b) It is used to parse HTML and XML documents.
7. Which attribute is commonly used in HTML tags to uniquely
identify elements for web scraping?
a) class
b) id
c) name
d) href
Answer: b) id
8. Which method is commonly used in the requests module to
make HTTP GET requests?
a) get()
b) fetch()
c) retrieve()
d) request()
Answer: a) get()
9. Which of the following is NOT a common challenge faced in
web scraping?
a) Captchas
b) Rate limiting
c) HTML validation errors
d) Broken links
Answer: c) HTML validation errors
Web Scraping | 113