File handling is an important part of any web application.
Python has several functions for creating, reading, updating, and deleting
files.
File Handling
The key function for working with files in Python is the open() function.
The open() function takes two parameters; filename, and mode.
There are four different methods (modes) for opening a file:
"r" - Read - Default value. Opens a file for reading, error if the file does not exist
"a" - Append - Opens a file for appending, creates the file if it does not exist
"w" - Write - Opens a file for writing, creates the file if it does not exist
"x" - Create - Creates the specified file, returns an error if the file exists
In addition you can specify if the file should be handled as binary or text mode
"t" - Text - Default value. Text mode
"b" - Binary - Binary mode (e.g. images)
Syntax
To open a file for reading it is enough to specify the name of the file:
f = open("demofile.txt")
The code above is the same as:
f = open("demofile.txt", "rt")
Because "r" for read, and "t" for text are the default values, you do not need to
specify them.
Note: Make sure the file exists, or else you will get an error.
Open a File on the Server
Assume we have the following file, located in the same folder as Python:
demofile.txt
Hello! Welcome to demofile.txt
This file is for testing purposes.
Good Luck!
To open the file, use the built-in open() function.
The open() function returns a file object, which has a read() method for reading
the content of the file:
f = open("demofile.txt", "r")
print(f.read())
If the file is located in a different location, you will have to specify the file path,
like this:
Open a file on a different location:
f = open("D:\\myfiles\welcome.txt", "r")
print(f.read())
Read Only Parts of the File
By default the read() method returns the whole text, but you can also specify
how many characters you want to return:
Example
Return the 5 first characters of the file:
f = open("demofile.txt", "r")
print(f.read(5))
Read Lines
You can return one line by using the readline() method:
Example
Read one line of the file:
f = open("demofile.txt", "r")
print(f.readline())
By calling readline() two times, you can read the two first lines:
Example
Read two lines of the file:
f = open("demofile.txt", "r")
print(f.readline())
print(f.readline())
By looping through the lines of the file, you can read the whole file, line by line:
Example
Loop through the file line by line:
f = open("demofile.txt", "r")
for x in f:
print(x)
Close Files
It is a good practice to always close the file when you are done with it.
Example
Close the file when you are finished with it:
f = open("demofile.txt", "r")
print(f.readline())
f.close()
Note: You should always close your files. In some cases, due to buffering,
changes made to a file may not show until you close the file
Local machine
file_path = 'C:/Users/Admin/Desktop/python.txt'
try:
with open(file_path, 'r') as file:
contents = file.read()
print(contents)
except FileNotFoundError:
print(f"Error: File not found at {file_path}")
except Exception as e:
print(f"An error occurred: {e}")
Google colab
Upload the file, copy the path and run
# prompt: to read a text file in desktop
from google.colab import drive
drive.mount('/content/drive')
file_path = '/content/drive/MyDrive/python.txt'
try:
with open(file_path, 'r') as file:
contents = file.read()
print(contents)
except FileNotFoundError:
print(f"Error: File not found at {file_path}")
except Exception as e:
print(f"An error occurred: {e}")
Notice that the open() function takes two input parameters: file path (or file
name if the file is in the current working directory) and the file access mode.
There are many modes for opening a file:
● open('path','r'): opens a file in read mode
● open('path',w'): opens or creates a text file in write mode
● open('path',a'): opens a file in append mode
● open('path','r+'): opens a file in both read and write mode
● open('path',w+'): opens a file in both read and write mode
● open('path',a+'): opens a file in both read and write mode
After opening the file with the read mode, you can also use the following
function to access or examine the Information stored in the file:
● .read(): This function reads the complete information from the file
unless a number is specified. Otherwise, it will read the first n bytes
from the text files.
● .readline(): This function reads the information from the file but not
more than one line of information unless a number is specified.
Otherwise, it will read the first n bytes from the text files. It is usually
used in loops
● .readlines() – This function reads the complete information in the file
and prints them as well in a list format
Example for uploading
import numpy as nm
import matplotlib.pyplot as mtp
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report
from google.colab import files
uploaded = files.upload()
import io
df = pd.read_excel(io.BytesIO(uploaded['des.xlsx']))
df.shape
Reading Excel Files
Pandas library also has a function, read_excel(), to read Excel files:
dataframe = pd.read_excel('path-to-file') # Full path to the txt file,
or simply the file name if the file is in
# your current working
directory
It is often the case where Excel file can contain multiple sheets. You can read
data from any sheet by providing its name in the sheet_name parameter in
the read_excel() function:
dataframe = pd.read_excel('path-to-file', sheet_name='sheet-name')
Sometimes, you might want to use one column of data for Analysis. To do
this, you can get the column data and convert it into a list of values.
print(dataframe['column-name'].tolist())
Modes of Opening a File
When opening a file in Python, you need to specify the mode in which you
want to open it. The most common modes are:
- r: Opens the file for reading (default mode).
- w: Opens the file for writing. If the file exists, its contents will be deleted. If
the file doesn't exist, it will be created.
- a: Opens the file for appending. If the file exists, new data will be appended
to the end of the file. If the file doesn't exist, it will be created.
- x: Opens the file for exclusive creation. If the file exists, the operation will
fail.
- b: Opens the file in binary mode.
- t: Opens the file in text mode (default).
- +: Opens the file for updating (reading and writing).
Reading a File
Here are a few ways to read a file in Python:
Method 1: Reading a File Line by Line
file = open('file.txt', 'r')
for line in file:
print(line.strip())
file.close()
Method 2: Reading a File into a List
file = open('file.txt', 'r')
lines = file.readlines()
file.close()
print(lines)
Method 3: Reading a File into a String
file = open('file.txt', 'r')
content = file.read()
file.close()
print(content)
Method 4: Using the with Statement
The with statement is a more Pythonic way to open and close files. It
automatically closes the file when you're done with it.
with open('file.txt', 'r') as file:
content = file.read()
print(content)
Note: Make sure to replace 'file.txt' with the actual path to your file.
Reading an excel file using Python using Pandas
In this method, We will first import the Pandas module then we will
use Pandas to read our excel file. You can read more operations
using the excel file using Pandas
# import pandas lib as pd
import pandas as pd
# read by default 1st sheet of an excel file
dataframe1 = pd.read_excel('C:\\Users\\Admin\\Desktop\\python1.xlsx')
print(dataframe1)
You can read an Excel file in Python using the openpyxl library,
which supports both .xlsx and .xls formats:
import openpyxl
# Load workbook
wb = openpyxl.load_workbook('C:\\Users\\Admin\\Desktop\\
python1.xlsx')
# Select active sheet (optional)
sheet = wb.active
# Access cell values
cell_value = sheet['A1'].value
print(cell_value)
# Close workbook when done
wb.close()
Replace ‘path/to/your/excel_file.xlsx’ with the actual path to
your Excel file.
How to open an Excel file from Python?
To open and manipulate Excel files, you can also use the xlrd
library, which supports .xls files (not .xlsx):
import xlrd
# Open Excel file
workbook = xlrd.open_workbook('path/to/your/excel_file.xls')
# Access sheet by index or name
sheet = workbook.sheet_by_index(0) # Access first sheet
# OR
# sheet = workbook.sheet_by_name('Sheet1') # Access sheet by
name
# Read cell value
cell_value = sheet.cell_value(0, 0) # Access cell A1
print(cell_value)
# Close workbook when done
workbook.close()
How to read an xlsx file in Python using Pandas?
Pandas provides a convenient way to read Excel files directly into a
DataFrame:
import pandas as pd
# Read Excel file into DataFrame
df = pd.read_excel('path/to/your/excel_file.xlsx')
# Display DataFrame
print(df.head())
Pandas’ read_excel() function can handle both .xls and .xlsx
formats.
How to read xlsx file in Python using CSV?
If you prefer working with CSV format data extracted from an Excel
file, you can convert an Excel file into CSV format using Pandas:
import pandas as pd
# Read Excel file into DataFrame
df = pd.read_excel('path/to/your/excel_file.xlsx')
# Export DataFrame to CSV
df.to_csv('output_file.csv', index=False)
This converts the Excel data into a CSV file named output_file.csv
in the current directory.
import openpyxl
# Load workbook
wb = openpyxl.load_workbook('C:\\Users\\Admin\\Desktop\\python2.xlsx')
# Select active sheet (optional)
sheet = wb.active
# Access cell values
cell_value = sheet['A3'].value
print(cell_value)
# Close workbook when done
wb.close()
rose
In [8]:
# import pandas lib as pd
import pandas as pd
# read by default 1st sheet of an excel file
dataframe1 = pd.read_excel('C:\\Users\\Admin\\Desktop\\python2.xlsx')
print(dataframe1)
flower colour
0 jasmine white
1 rose pink
2 marygold yellow
In [7]:
import pandas as pd
# Read Excel file into DataFrame
df = pd.read_excel('C:\\Users\\Admin\\Desktop\\python2.xlsx')
# Display DataFrame
print(df.head())
flower colour
0 jasmine white
1 rose pink
2 marygold yellow
In [15]:
import pandas as pd
# Read Excel file into DataFrame
df = pd.read_excel('C:\\Users\\Admin\\Desktop\\python2.xlsx')
# Export DataFrame to CSV
df.to_csv('C:\\Users\\Admin\\Desktop\\output_file.csv', index=False)
In [16]:
from openpyxl import Workbook
workbook = Workbook()
workbook.save(filename='C:\\Users\\Admin\\Desktop\\python4.xlsx')
In [23]:
# import openpyxl module
import openpyxl
wb = openpyxl.load_workbook('C:\\Users\\Admin\\Desktop\\python4.xlsx')
#sheet = wb.active
data = (
(1, 2, 3),
(4, 5, 6)
)
for row in data:
sheet.append(row)
wb.save('C:\\Users\\Admin\\Desktop\\python4.xlsx')
In [ ]:
In this example, a new blank Excel workbook is generated using the
openpyxl library’s Workbook() function, and it is saved as
“sample.xlsx” with the save() method. This code demonstrates the
fundamental steps for creating and saving an Excel file in Python.
from openpyxl import Workbook
workbook = Workbook()
workbook.save(filename="sample.xlsx")
Output:
After creating an empty file, let’s see how to add some data to it
using Python. To add data first we need to select the active sheet
and then using the cell() method we can select any particular cell by
passing the row and column number as its parameter. We can also
write using cell names. See the below example for a better
understanding.
Example:
In this example, the openpyxl module is used to create a new Excel
workbook and populate cells with values such as “Hello,” “World,”
“Welcome,” and “Everyone.” The workbook is then saved as
“sample.xlsx,” illustrating the process of writing data to specific
cells and saving the changes
# import openpyxl module
import openpyxl
wb = openpyxl.Workbook()
sheet = wb.active
c1 = sheet.cell(row=1, column=1)
# writing values to cells
c1.value = "Hello"
c2 = sheet.cell(row=1, column=2)
c2.value = "World"
c3 = sheet['A2']
c3.value = "Welcome"
# B2 means column = 2 & row = 2.
c4 = sheet['B2']
c4.value = "Everyone"
wb.save("sample.xlsx")
Output:
Refer to the below article to get detailed information about writing
to excel.
Append data in excel using Python
In the above example, you will see that every time you try to write
to a spreadsheet the existing data gets overwritten, and the file is
saved as a new file. This happens because the Workbook() method
always creates a new workbook file object. To write to an existing
workbook you must open the file with the load_workbook()
method. We will use the above-created workbook.
Example:
In this example, the openpyxl module is employed to load an
existing Excel workbook (“sample.xlsx”). The program accesses cell
‘A3’ in the active sheet, updates its value to “New Data,” and then
saves the modified workbook back to “sample.xlsx.”
# import openpyxl module
import openpyxl
wb = openpyxl.load_workbook("sample.xlsx")
sheet = wb.active
c = sheet['A3']
c.value = "New Data"
wb.save("sample.xlsx")
Output:
We can also use the append() method to append multiple data at
the end of the sheet.
Example:
In this example, the openpyxl module is utilized to load an existing
Excel workbook (“sample.xlsx”). A two-dimensional data structure
(tuple of tuples) is defined and iteratively appended to the active
sheet, effectively adding rows with values (1, 2, 3) and (4, 5, 6).
# import openpyxl module
import openpyxl
wb = openpyxl.load_workbook("sample.xlsx")
sheet = wb.active
data = (
(1, 2, 3),
(4, 5, 6)
)
for row in data:
sheet.append(row)
wb.save('sample.xlsx')
Output:
Arithmetic Operation on Spreadsheet
Arithmetic operations can be performed by typing the formula in a
particular cell of the spreadsheet. For example, if we want to find
the sum then =Sum() formula of the excel file is used.
Example:
In this example, the openpyxl module is used to create a new Excel
workbook and populate cells A1 to A5 with numeric values. Cell A7
is assigned a formula to calculate the sum of the values in A1 to A5.
# import openpyxl module
import openpyxl
wb = openpyxl.Workbook()
sheet = wb.active
# writing to the cell of an excel sheet
sheet['A1'] = 200
sheet['A2'] = 300
sheet['A3'] = 400
sheet['A4'] = 500
sheet['A5'] = 600
sheet['A7'] = '= SUM(A1:A5)'
# save the file
wb.save("sum.xlsx")
Output:
Refer to the below article to get detailed information about the
Arithmetic operations on Spreadsheet.
Adjusting Rows and Column
Worksheet objects have row_dimensions and column_dimensions
attributes that control row heights and column widths. A sheet’s
row_dimensions and column_dimensions are dictionary-like values;
row_dimensions contains RowDimension objects and
column_dimensions contains ColumnDimension objects. In
row_dimensions, one can access one of the objects using the
number of the row (in this case, 1 or 2). In column_dimensions, one
can access one of the objects using the letter of the column (in this
case, A or B).
Example:
In this example, the openpyxl module is used to create a new Excel
workbook and set values in specific cells. The content “hello” is
placed in cell A1, and “everyone” is placed in cell B2. Additionally,
the height of the first row is set to 70 units, and the width of column
B is set to 20 units.
# import openpyxl module
import openpyxl
wb = openpyxl.Workbook()
sheet = wb.active
# writing to the specified cell
sheet.cell(row=1, column=1).value = ' hello '
sheet.cell(row=2, column=2).value = ' everyone '
# set the height of the row
sheet.row_dimensions[1].height = 70
# set the width of the column
sheet.column_dimensions['B'].width = 20
# save the file
wb.save('sample.xlsx')
Output:
Merging Cells
A rectangular area of cells can be merged into a single cell with the
merge_cells() sheet method. The argument to merge_cells() is a
single string of the top-left and bottom-right cells of the rectangular
area to be merged.
Example:
In this example, the openpyxl module is employed to create a new
Excel workbook. The program merges cells A2 to D4, creating a
single cell spanning multiple columns and rows, and sets its value to
‘Twelve cells join together.’ Additionally, cells C6 and D6 are
merged, and the text ‘Two merge cells.’ is placed in the resulting
merged cell.
import openpyxl
wb = openpyxl.Workbook()
sheet = wb.active
sheet.merge_cells('A2:D4')
sheet.cell(row=2, column=1).value = 'Twelve cells join together.'
# merge cell C6 and D6
sheet.merge_cells('C6:D6')
sheet.cell(row=6, column=6).value = 'Two merge cells.'
wb.save('sample.xlsx’)
ASSIGNMENT 👍
1. Add two numbers
2. Maximum of two numbers
3. Factorial of a number
4. Find simple interest
5. Find compound interest
6. Check Armstrong Number
7. Find area of a circle
8. Print all Prime numbers in an Interval
9. Check whether a number is Prime or not
10. N-th Fibonacci number
11. Check if a given number is Fibonacci number?
12. Nth multiple of a number in Fibonacci Series
13. Print ASCII Value of a character
14. Sum of squares of first n natural numbers
15. Cube sum of first n natural numbers
Array Programs
16. Find sum of array
17. Find largest element in an array
18. Program for array rotation
19. Reversal algorithm for array rotation
20. Split the array and add the first part to the end
21. Find reminder of array multiplication divided by n
22. Check if given array is Monotonic
List Programs
23. Interchange first and last elements in a list
24. Swap two elements in a list
25. Ways to find length of list
26. Ways to check if element exists in list
27. Different ways to clear a list
28. Reversing a List
29. Find sum of elements in list
30. Multiply all numbers in the list
31. Find smallest number in a list
32. Find largest number in a list
33. Find second largest number in a list
34. Find N largest elements from a list
35. Even numbers in a list
36. Odd numbers in a List
37. Print all even numbers in a range
38. Print all odd numbers in a range
39. Print positive numbers in a list
40. Print negative numbers in a list
41. Print all positive numbers in a range
42. Print all negative numbers in a range
43. Remove multiple elements from a list
44. Remove empty List from List
45. Cloning or Copying a list
46. Count occurrences of an element in a list
47. Remove empty tuples from a list
48. Print duplicates from a list of integers
49. Find Cumulative sum of a list
50. Sum of number digits in List
51. Break a list into chunks of size N
52. Sort the values of first list using second list
Matrix Programs
53. Add two Matrices
54. Multiply two matrices
55. Matrix Product
56. Adding and Subtracting Matrices
57. Transpose a matrix in Single line
58. Matrix creation of n*n
59. Get Kth Column of Matrix
60. Vertical Concatenation in Matrix
String Programs
61. Check if a string is palindrome or not
62. Check whether the string is Symmetrical or Palindrome
63. Reverse words in a given String
64. Ways to remove i’th character from string
65. Check if a Substring is Present in a Given String
66. Words Frequency in String Shorthands
67. Convert Snake case to Pascal case
68. Find length of a string (4 ways)
69. Print even length words in a string
70. Accept the strings which contains all vowels
71. Count the Number of matching characters in a pair of string
72. Remove all duplicates from a given string
73. Least Frequent Character in String
74. Maximum frequency character in String
75. Check if a string contains any special character
76. Generating random strings until a given string is generated
77. Find words which are greater than given length k
78. Removing i-th character from a string
79. Split and join a string
80. Check if a given string is binary string or not
81. Find uncommon words from two Strings
82. Replace duplicate Occurrence in String
83. Replace multiple words with K
84. Permutation of a given string using inbuilt function
85. Check for URL in a String
86. Execute a String of Code
87. String slicing to rotate a string
88. String slicing to check if a string can become empty by
recursive deletion
89. Counter| Find all duplicate characters in string
90. Replace all occurrences of a substring in a string
Dictionary Programs
91. Extract Unique values dictionary values
92. Find the sum of all items in a dictionary
93. Ways to remove a key from dictionary
94. Ways to sort list of dictionaries by values Using itemgetter
95. Ways to sort list of dictionaries by values Using lambda
function
96. Merging two Dictionaries
97. Convert key-values list to flat dictionary
98. Insertion at the beginning in OrderedDict
99. Check order of character in string using OrderedDict( )
100. Dictionary and counter to find winner of election
101. Append Dictionary Keys and Values ( In order ) in
dictionary
102. Sort Python Dictionaries by Key or Value
103. Sort Dictionary key and values List
104. Handling missing keys in Python dictionaries
105. Dictionary with keys having multiple inputs
106. Print anagrams together using List and Dictionary
107. K’th Non-repeating Character using List
Comprehension and OrderedDict
108. Check if binary representations of two numbers are
anagram
109. Counter to find the size of largest subset of anagram
words
110. Remove all duplicates words from a given sentence
111. Dictionary to find mirror characters in a string
112. Counting the frequencies in a list using dictionary
113. Convert a list of Tuples into Dictionary
114. Counter and dictionary intersection example (Make a
string using deletion and rearrangement)
115. Dictionary, set and counter to check if frequencies
can become same
116. Scraping And Finding Ordered Words In A Dictionary
117. Possible Words using given characters
118. Keys associated with Values in Dictionary
Tuple Programs
119. Find the size of a Tuple
120. Maximum and Minimum K elements in Tuple
121. Create a list of tuples from given list having number
and its cube in each tuple
122. Adding Tuple to List and vice – versa
123. Closest Pair to Kth index element in Tuple
124. Join Tuples if similar initial element
125. Extract digits from Tuple list
126. All pair combinations of 2 tuples
127. Remove Tuples of Length K
128. Sort a list of tuples by second Item
129. Order Tuples using external List
130. Flatten tuple of List to tuple
131. Convert Nested Tuple to Custom Key Dictionary
Searching and Sorting Programs
132. Binary Search (Recursive and Iterative)
133. Linear Search
134. Insertion Sort
135. Recursive Insertion Sort
136. QuickSort
137. Iterative Quick Sort
138. Selection Sort
139. Bubble Sort
140. Merge Sort
141. Iterative Merge Sort
142. Heap Sort
143. Counting Sort
144. ShellSort
145. Topological Sorting
146. Radix Sort
147. Binary Insertion Sort
148. Bitonic Sort
149. Comb Sort
150. Pigeonhole Sort
151. Cocktail Sort
152. Gnome Sort
153. Odd-Even Sort / Brick Sort
154. BogoSort or Permutation Sort
155. Cycle Sort
156. Stooge Sort
Pattern Printing Programs
157. Print the pattern ‘G’
158. Print an Inverted Star Pattern
159. Print double sided stair-case pattern
160. Print with your own font
Date-Time Programs
161. Get Current Time
162. Get Current Date and Time
163. Find yesterday’s, today’s and tomorrow’s date
164. Convert time from 12 hour to 24 hour format
165. Find difference between current time and given time
166. Create a Lap Timer
167. Convert date string to timestamp
168. Convert timestamp string to datetime object?
169. Find number of times every day occurs in a Year
Python File Handling Programs
170. Read file word by word
171. Read character by character from a file
172. Get number of characters, words, spaces and lines in
a file
173. Count the Number of occurrences of a key-value pair
in a text file
174. Finding ‘n’ Character Words in a Text File
175. Obtain the line number in which given word is
present
176. Count number of lines in a text file
177. Remove lines starting with any prefix
178. Eliminate repeated lines from a file
179. Read List of Dictionaries from File
180. Append content of one text file to another
181. Copy odd lines of one file to other
182. Merge two files into a third file
183. Reverse a single line of a text file
184. Reverse the content of a file and store it in another
file
185. Reverse the Content of a File using Stack
__________________________________________________________
186. Reverse a linked list
187. Find largest prime factor of a number
188. Efficient program to print all prime factors of a given
number
189. Product of unique prime factors of a number
190. Find sum of odd factors of a number
191. Program for Coin Change
192. Program for Tower of Hanoi
193. Sieve of Eratosthenes
194. Check if binary representation is palindrome
195. Basic Euclidean algorithms
196. Extended Euclidean algorithms
197. Maximum height when coins are arranged in a
triangle
198. Find minimum sum of factors of number
199. Difference between sums of odd and even digits
200. Print Matrix in Z form
201. Smallest K digit number divisible by X
202. Print Number series without using any loop
203. Number of stopping station problem
204. Check if a triangle of positive area is possible with the
given angles
205. Find the most occurring character and its count
206. Find sum of even factors of a number
207. Check if all digits of a number divide it
208. Check whether a number has consecutive 0’s in the
given base or not
209. Number of solutions to Modular Equations
210. Legendre’s Conjecture
1. Add two numbers
2. Maximum of two numbers
3. Factorial of a number
4. Find simple interest
5. Find compound interest
6. Check Armstrong Number
7. Find area of a circle
8. Print all Prime numbers in an Interval
9. Check whether a number is Prime or not
10. N-th Fibonacci number
11. Check if a given number is Fibonacci number?
12. Nth multiple of a number in Fibonacci Series
13. Print ASCII Value of a character
14. Sum of squares of first n natural numbers
15. Cube sum of first n natural numbers
Array Programs
16. Find sum of array
17. Find largest element in an array
18. Program for array rotation
19. Reversal algorithm for array rotation
20. Split the array and add the first part to the end
21. Find reminder of array multiplication divided by n
22. Check if given array is Monotonic
List Programs
23. Interchange first and last elements in a list
24. Swap two elements in a list
25. Ways to find length of list
26. Ways to check if element exists in list
27. Different ways to clear a list
28. Reversing a List
29. Find sum of elements in list
30. Multiply all numbers in the list
31. Find smallest number in a list
32. Find largest number in a list
33. Find second largest number in a list
34. Find N largest elements from a list
35. Even numbers in a list
36. Odd numbers in a List
37. Print all even numbers in a range
38. Print all odd numbers in a range
39. Print positive numbers in a list
40. Print negative numbers in a list
41. Print all positive numbers in a range
42. Print all negative numbers in a range
43. Remove multiple elements from a list
44. Remove empty List from List
45. Cloning or Copying a list
46. Count occurrences of an element in a list
47. Remove empty tuples from a list
48. Print duplicates from a list of integers
49. Find Cumulative sum of a list
50. Sum of number digits in List
51. Break a list into chunks of size N
52. Sort the values of first list using second list
Matrix Programs
53. Add two Matrices
54. Multiply two matrices
55. Matrix Product
56. Adding and Subtracting Matrices
57. Transpose a matrix in Single line
58. Matrix creation of n*n
59. Get Kth Column of Matrix
60. Vertical Concatenation in Matrix
String Programs
61. Check if a string is palindrome or not
62. Check whether the string is Symmetrical or Palindrome
63. Reverse words in a given String
64. Ways to remove i’th character from string
65. Check if a Substring is Present in a Given String
66. Words Frequency in String Shorthands
67. Convert Snake case to Pascal case
68. Find length of a string (4 ways)
69. Print even length words in a string
70. Accept the strings which contains all vowels
71. Count the Number of matching characters in a pair of string
72. Remove all duplicates from a given string
73. Least Frequent Character in String
74. Maximum frequency character in String
75. Check if a string contains any special character
76. Generating random strings until a given string is generated
77. Find words which are greater than given length k
78. Removing i-th character from a string
79. Split and join a string
80. Check if a given string is binary string or not
81. Find uncommon words from two Strings
82. Replace duplicate Occurrence in String
83. Replace multiple words with K
84. Permutation of a given string using inbuilt function
85. Check for URL in a String
86. Execute a String of Code
87. String slicing to rotate a string
88. String slicing to check if a string can become empty by
recursive deletion
89. Counter| Find all duplicate characters in string
90. Replace all occurrences of a substring in a string
Dictionary Programs
91. Extract Unique values dictionary values
92. Find the sum of all items in a dictionary
93. Ways to remove a key from dictionary
94. Ways to sort list of dictionaries by values Using itemgetter
95. Ways to sort list of dictionaries by values Using lambda
function
96. Merging two Dictionaries
97. Convert key-values list to flat dictionary
98. Insertion at the beginning in OrderedDict
99. Check order of character in string using OrderedDict( )
100. Dictionary and counter to find winner of election
101. Append Dictionary Keys and Values ( In order ) in
dictionary
102. Sort Python Dictionaries by Key or Value
103. Sort Dictionary key and values List
104. Handling missing keys in Python dictionaries
105. Dictionary with keys having multiple inputs
106. Print anagrams together using List and Dictionary
107. K’th Non-repeating Character using List
Comprehension and OrderedDict
108. Check if binary representations of two numbers are
anagram
109. Counter to find the size of largest subset of anagram
words
110. Remove all duplicates words from a given sentence
111. Dictionary to find mirror characters in a string
112. Counting the frequencies in a list using dictionary
113. Convert a list of Tuples into Dictionary
114. Counter and dictionary intersection example (Make a
string using deletion and rearrangement)
115. Dictionary, set and counter to check if frequencies
can become same
116. Scraping And Finding Ordered Words In A Dictionary
117. Possible Words using given characters
118. Keys associated with Values in Dictionary
Tuple Programs
119. Find the size of a Tuple
120. Maximum and Minimum K elements in Tuple
121. Create a list of tuples from given list having number
and its cube in each tuple
122. Adding Tuple to List and vice – versa
123. Closest Pair to Kth index element in Tuple
124. Join Tuples if similar initial element
125. Extract digits from Tuple list
126. All pair combinations of 2 tuples
127. Remove Tuples of Length K
128. Sort a list of tuples by second Item
129. Order Tuples using external List
130. Flatten tuple of List to tuple
131. Convert Nested Tuple to Custom Key Dictionary
Searching and Sorting Programs
132. Binary Search (Recursive and Iterative)
133. Linear Search
134. Insertion Sort
135. Recursive Insertion Sort
136. QuickSort
137. Iterative Quick Sort
138. Selection Sort
139. Bubble Sort
140. Merge Sort
141. Iterative Merge Sort
142. Heap Sort
143. Counting Sort
144. ShellSort
145. Topological Sorting
146. Radix Sort
147. Binary Insertion Sort
148. Bitonic Sort
149. Comb Sort
150. Pigeonhole Sort
151. Cocktail Sort
152. Gnome Sort
153. Odd-Even Sort / Brick Sort
154. BogoSort or Permutation Sort
155. Cycle Sort
156. Stooge Sort
Pattern Printing Programs
157. Print the pattern ‘G’
158. Print an Inverted Star Pattern
159. Print double sided stair-case pattern
160. Print with your own font
Date-Time Programs
161. Get Current Time
162. Get Current Date and Time
163. Find yesterday’s, today’s and tomorrow’s date
164. Convert time from 12 hour to 24 hour format
165. Find difference between current time and given time
166. Create a Lap Timer
167. Convert date string to timestamp
168. Convert timestamp string to datetime object?
169. Find number of times every day occurs in a Year
Python File Handling Programs
170. Read file word by word
171. Read character by character from a file
172. Get number of characters, words, spaces and lines in
a file
173. Count the Number of occurrences of a key-value pair
in a text file
174. Finding ‘n’ Character Words in a Text File
175. Obtain the line number in which given word is
present
176. Count number of lines in a text file
177. Remove lines starting with any prefix
178. Eliminate repeated lines from a file
179. Read List of Dictionaries from File
180. Append content of one text file to another
181. Copy odd lines of one file to other
182. Merge two files into a third file
183. Reverse a single line of a text file
184. Reverse the content of a file and store it in another
file
185. Reverse the Content of a File using Stack
__________________________________________________________
186. Reverse a linked list
187. Find largest prime factor of a number
188. Efficient program to print all prime factors of a given
number
189. Product of unique prime factors of a number
190. Find sum of odd factors of a number
191. Program for Coin Change
192. Program for Tower of Hanoi
193. Sieve of Eratosthenes
194. Check if binary representation is palindrome
195. Basic Euclidean algorithms
196. Extended Euclidean algorithms
197. Maximum height when coins are arranged in a
triangle
198. Find minimum sum of factors of number
199. Difference between sums of odd and even digits
200. Print Matrix in Z form
201. Smallest K digit number divisible by X
202. Print Number series without using any loop
203. Number of stopping station problem
204. Check if a triangle of positive area is possible with the
given angles
205. Find the most occurring character and its count
206. Find sum of even factors of a number
207. Check if all digits of a number divide it
208. Check whether a number has consecutive 0’s in the
given base or not
209. Number of solutions to Modular Equations
210. Legendre’s Conjecture
Python in Data Science:
Because of its statistical analysis, data modeling, and readability,
Python is one of the best programming languages for extracting value
from this data.
Python is one of the best fits for data science for the following reasons–
● Built-in libraries to support a variety of data science tasks.
● Various development modules are available for use.
● Excellent memory management abilities.
● Algorithms for complex tasks processing
With the benefits listed above, Python can be used as a powerful tool
to handle and solve data science problems.
Python Data Science Libraries
1. NumPy
2. TensorFlow
3. SciPy
4. Pandas
5. Matplotlib
6. Keras
7. Seaborn
8. Beautiful Soup
9. PyTorch
10. Scrapy
NumPy
It is a free Python software library that allows you to perform numerical
computations on data in the form of large arrays and multi-dimensional
matrices. These multidimensional matrices are the main objects in
NumPy, where their dimensions are referred to as axes and the
number of axes is referred to as a rank. NumPy also includes a variety
of tools for working with these arrays, as well as high-level
mathematical functions for manipulating this data using linear algebra,
Fourier transforms, random number crunching, and so on. Adding,
slicing, multiplying, flattening, reshaping, and indexing arrays are
some of the basic array operations that NumPy can perform. Stacking
arrays, splitting them into sections, broadcasting arrays, and other
advanced functions are also available.
TensorFlow
TensorFlow is a high-performance numerical computation library with
approximately 35,000 comments and a vibrant community of
approximately 1,500 contributors. It is used in a variety of scientific
fields. TensorFlow is essentially a framework for defining and running
computations involving tensors, which are partially defined
computational objects that produce a value.
TensorFlow Features:
● improved visualization of computational graphs
● In neural machine learning, it reduces error by 50 to 60%.
● Parallel computing is used to run complex models.
● Google-backed seamless library management.
● Quicker updates and more frequent new releases to keep
you up to date on the latest features.
Applications:
● Image and speech recognition
● Text-based applications
● Analysis of Time-series
● Video recognition/detection
SciPy
The Python SciPy library is largely based on the NumPy library. It
performs the majority of the advanced computations related to data
modeling. The SciPy library enables us to perform statistical data
analysis, algebraic computations, algorithm optimization, and other
tasks.
We can even perform parallel computations on it using SciPy. It
includes functions for data science operations like regression,
probability, and so on.
In a nutshell, the SciPy module can easily handle all advanced
computations in statistics, modelling, and algebra.
Pandas
This is a free Python data analysis and manipulation software library. It
was developed as a community library project and was first made
available in 2008. Pandas offer a variety of high-performance and user-
friendly data structures and operations for manipulating data in the
form of numerical tables and time series. Pandas also include a
number of tools for reading and writing data between in-memory data
structures and various file formats.
In a nutshell, it is ideal for quick and easy data manipulation, data
aggregation, reading and writing data, and data visualization. Pandas
can also read data from files such as CSV, Excel, and others, or from a
SQL database, and generate a Python object known as a data frame. A
data frame is made up of rows and columns and can be used to
manipulate data using operations like join, merge, groupby,
concatenate, and so on.
Matplotlib
Matplotlib’s visualizations are both powerful and wonderful. It’s a
Python plotting library with over 26,000 comments on GitHub and a
thriving community of over 700 contributors. It’s widely used for data
visualization because of the graphs and plots it generates. It also
includes an object-oriented API for embedding those plots into
applications.
Matplotlib Features:
● It can be used as a MATLAB replacement and has the
advantage of being free and open source.
● Supports dozens of backends and output types, so you can
use it regardless of your operating system or output format
preferences.
● Pandas can be used as MATLAB API wrappers to drive
MATLAB like a cleaner.
● Low memory consumption and improved runtime
performance
Applications:
● Visualize the models’ 95 percent confidence intervals.
● Visualize data distribution to gain instant insights.
● Outlier detection with a scatter plot.
● Correlation analysis of variables.
Keras
Keras is a Python-based deep learning API that runs on top of the
TensorFlow machine learning platform. It was created with the goal of
allowing for quick experimentation. “Being able to go from idea to
result as quickly as possible is key to doing good research,” says
Keras.
Many people prefer Keras over TensorFlow because it provides a much
better “user experience.” Keras was developed in Python, making it
easier for Python developers to understand. It is an easy-to-use library
with a lot of power.
Seaborn
Seaborn is a Python library for data visualization that is based on
Matplotlib. Data scientists can use Seaborn to create a variety of
statistical models, such as heatmaps. Seaborn offers an impressive
array of data visualization options, including time-series visualization,
joint plots, violin diagrams, and many more. Seaborn uses semantic
mapping and statistical aggregation to generate informative plots with
deep insights.
Beautiful Soup
BeautifulSoup is a fantastic Python parsing module that supports web
scraping from HTML and XML documents.
BeautifulSoup identifies encodings and handles HTML documents
elegantly, even when they contain special characters. We can explore
a parsed document and discover what we need, making it quick and
easy to extract data from web pages.
PyTorch
PyTorch, is a Python-based scientific computing tool that makes use of
the power of graphics processing units, PyTorch is a popular deep
learning research platform that is designed to provide maximum
flexibility and speed.
● PyTorch is well-known for giving two of the most high-level
features
● tensor computations with significant GPU acceleration
support,
● construction of deep neural networks on a tape-based
autograd system.
Scrapy
Scrapy is a must-have Python module for anyone interested in data
scraping (extracting data from the screen). Scrapy allows you to
improve the screen-scraping and web crawling processes. Scrapy is
used by data scientists for data mining as well as automated testing.
Scrapy is an open-source framework that many IT professionals use
throughout the world to extract data from websites. Scrapy is
developed in Python and is extremely portable, running on Linux,
Windows, BSD, and Mac. Because of its great interactivity, many
skilled developers favour Python for data analysis and scraping.
Close a file after it has been opened:
f = open("demofile.txt", "r")
print(f.read())
f.close()
Definition and Usage
The close() method closes an open file.
You should always close your files, in some cases, due to buffering, changes
made to a file may not show until you close the file.
Syntax
file.close()
Parameter Values
No parameters
# code to delete entire data along with file
import os
# check if file exists
if os.path.exists("sample.txt"):
os.remove("sample.txt")
# Print the statement once
# the file is deleted
print("File deleted !")
else:
# Print if file is not present
print("File doesnot exist !")
# code to delete entire data
# but not the file, it is in
# open file
f = open("sample.txt", "r+")
# absolute file positioning
f.seek(0)
# to erase all data
f.truncate(
Python Exception Handling handles errors that occur during the
execution of a program. Exception handling allows to respond to the
error, instead of crashing the running program. It enables you to
catch and manage errors, making your code more robust and user-
friendly.
Example: Trying to divide a number by zero will cause an
exception.
# Example of an exception
n = 10
try:
res = n / 0 # This will raise a ZeroDivisionError
except ZeroDivisionError:
print("Can't be divided by zero!")
Output
Can't be divided by zero!
Explanation: In this example, dividing number by 0 raises a
ZeroDivisionError. The try block contains the code that might
cause an exception and the except block handles the exception,
printing an error message instead of stopping the program.
Difference Between Exception and Error
· Error: Errors are serious issues that a program should not
try to handle. They are usually problems in the code’s
logic or configuration and need to be fixed by the
programmer. Examples include syntax errors and
memory errors.
· Exception: Exceptions are less severe than errors and
can be handled by the program. They occur due to
situations like invalid input, missing files or network
issues.
Example:
# Syntax Error (Error)
print("Hello world" # Missing closing parenthesis
# ZeroDivisionError (Exception)
n = 10
res = n / 0
Explanation: A syntax error is a coding mistake that prevents the
code from running. In contrast, an exception like ZeroDivisionError
can be managed during the program’s execution using exception
handling.
Syntax and Usage
Exception handling in Python is done using the try, except, else and
finally blocks.
try:
# Code that might raise an exception
except SomeException:
# Code to handle the exception
else:
# Code to run if no exception occurs
finally:
# Code to run regardless of whether an exception occurs
try, except, else and finally Blocks
· try Block: try block lets us test a block of code for errors.
Python will “try” to execute the code in this block. If an
exception occurs, execution will immediately jump to the
except block.
· except Block: except block enables us to handle the
error or exception. If the code inside the try block throws
an error, Python jumps to the except block and executes
it. We can handle specific exceptions or use a general
except to catch all exceptions.
· else Block: else block is optional and if included, must
follow all except blocks. The else block runs only if no
exceptions are raised in the try block. This is useful for
code that should execute if the try block succeeds.
· finally Block: finally block always runs, regardless of
whether an exception occurred or not. It is typically used
for cleanup operations (closing files, releasing resources).
Example:
try:
n=0
res = 100 / n
except ZeroDivisionError:
print("You can't divide by zero!")
except ValueError:
print("Enter a valid number!")
else:
print("Result is", res)
finally:
print("Execution complete.")
You can't divide by zero!
Execution complete.
Common Exceptions in Python
Python has many built-in exceptions, each representing a specific
error condition. Some common ones include:
Exception Name Description
BaseException The base class for all built-in
exceptions.
Exception
The base class for all non-exit exceptions.
ArithmeticError Base class for all errors
related to arithmetic operations.
ZeroDivisionError
Raised when a division or modulo operation is
performed with zero as the divisor.
OverflowError
Raised when a numerical operation exceeds the
maximum limit of a data type.
FloatingPointError
Raised when a floating-point operation fails.
AssertionError
Raised when an assert statement fails.
AttributeError
Raised when an attribute reference or
assignment fails.
IndexError
Raised when a sequence subscript is out of
range.
KeyError
Raised when a dictionary key is not found.
MemoryError
Raised when an operation runs out of memory.
NameError
Raised when a local or global name is not found.
OSError
Raised when a system-related operation (like
file I/O) fails.
TypeError
Raised when an operation or function is applied
to an object of inappropriate type.
ValueError
Raised when a function receives an argument of
the right type but inappropriate value.
ImportError
Raised when an import statement has issues.
ModuleNotFoundError
Raised when a module cannot be found.
Python Catching Exceptions
When working with exceptions in Python, we can handle errors more
efficiently by specifying the types of exceptions we expect. This can
make code both safer and easier to debug.
Catching Specific Exceptions
Catching specific exceptions makes code to respond to different
exception types differently.
Example:
try:
x = int("str") # This will cause ValueError
#inverse
inv = 1 / x
except ValueError:
print("Not Valid!")
except ZeroDivisionError:
print("Zero has no inverse!")
Output
Not Valid!
Explanation:
· The ValueError is caught because the string “str” cannot
be converted to an integer.
· If x were 0 and conversion successful, the
ZeroDivisionError would be caught when attempting to
calculate its inverse.
Catching Multiple Exceptions
We can catch multiple exceptions in a single block if we need to
handle them in the same way or we can separate them if different
types of exceptions require different handling.
Example:
a = ["10", "twenty", 30] # Mixed list of integers and strings
try:
total = int(a[0]) + int(a[1]) # 'twenty' cannot be converted to int
except (ValueError, TypeError) as e:
print("Error", e)
except IndexError:
print("Index out of range.")
Output
Error invalid literal for int() with base 10: 'twenty'
Explanation:
· The ValueError is caught when trying to convert “twenty”
to an integer.
· TypeError might occur if the operation was incorrectly
applied to non-integer types, but it’s not triggered in this
specific setup.
· IndexError would be caught if an index outside the range
of the list was accessed, but in this scenario, it’s under
control.
Catch-All Handlers and Their Risks
Here’s a simple calculation that may fail due to various reasons.
Example:
try:
# Simulate risky calculation: incorrect type operation
res = "100" / 20
except ArithmeticError:
print("Arithmetic problem.")
except:
print("Something went wrong!")
Output
Something went wrong!
Explanation:
· An ArithmeticError (more specific like ZeroDivisionError)
might be caught if this were a number-to-number division
error. However, TypeError is actually triggered here due
to attempting to divide a string by a number.
· catch-all except: is used to catch the TypeError,
demonstrating the risk that the programmer might not
realize the actual cause of the error (type mismatch)
without more detailed error logging.
Raise an Exception
We raise an exception in Python using the raise keyword followed by
an instance of the exception class that we want to trigger. We can
choose from built-in exceptions or define our own custom
exceptions by inheriting from Python’s built-in Exception class.
Basic Syntax:
raise ExceptionType(“Error message”)
Example:
def set(age):
if age < 0:
raise ValueError("Age cannot be negative.")
print(f"Age set to {age}")
try:
set(-5)
except ValueError as e:
print(e)
Output
Age cannot be negative.
Explanation:
· The function set checks if the age is negative. If so, it
raises a ValueError with a message explaining the issue.
· This ensures that the age attribute cannot be set to an
invalid state, thus maintaining the integrity of the data.
Advantages of Exception Handling:
· Improved program reliability: By handling exceptions
properly, you can prevent your program from crashing or
producing incorrect results due to unexpected errors or
input.
· Simplified error handling: Exception handling allows
you to separate error handling code from the main
program logic, making it easier to read and maintain your
code.
· Cleaner code: With exception handling, you can avoid
using complex conditional statements to check for errors,
leading to cleaner and more readable code.
· Easier debugging: When an exception is raised, the
Python interpreter prints a traceback that shows the
exact location where the exception occurred, making it
easier to debug your code.
Disadvantages of Exception Handling:
· Performance overhead: Exception handling can be
slower than using conditional statements to check for
errors, as the interpreter has to perform additional work
to catch and handle the exception.
· Increased code complexity: Exception handling can
make your code more complex, especially if you have to
handle multiple types of exceptions or implement
complex error handling logic.
· Possible security risks: Improperly handled exceptions
can potentially reveal sensitive information or create
security vulnerabilities in your code, so it’s important to
handle exceptions carefully and avoid exposing too much
information about your program.