President’s Day Python script

What a better way to celebrate the great leaders of our country by writing a President’s Day program and learn a little python along the way. A fun fact first, President’s Day is actually meant to celebrate Washington and Lincoln’s birthday, but for a lot of people it’s interpreted as being a celebration of all US Presidents.

I’d also like to thank my man Namuol, for being the number one hit on google when you search presidents.csv, and having this awesome csv file in his repo that’s exactly what we need to get started. No need to reinvent the wheel here, so let’s begin by downloading the CSV file in this link. Hit the Download ZIP button to get the CSV file downloaded on your machine:

Extract the file presidents.csv from the ZIP file, and place it in the same directory where you want to build this awesome python script in. Open your favorite text editor, begin a new file, save As, and call it something cool, like “presidentsUS.py”. Consider the following script:

import csv
with open('presidents.csv') as csvFile:
reader = csv.reader(csvFile)
rowIndex = 0
for row in reader:
if rowIndex == 0:
pass # Don't use the first row, since it has all the column headers
else:
print(row[1] + " was President number " + \
row[0] + ", served from " + \
row[3] + " to " + row[4] + ", " + \
"was from " + row[8] + ", and was in the " + \
row[5] + " party.")
rowIndex += 1

Your output should look as follows:

George Washington was President number 1,  served from 30/04/1789 to 4/03/1797, was from Virginia, and was in the Independent  party.
John Adams was President number 2, served from 4/03/1797 to 4/03/1801, was from Massachusetts, and was in the Federalist party.
Thomas Jefferson was President number 3, served from 4/03/1801 to 4/03/1809, was from Virginia, and was in the Democratic-Republican party.
James Madison was President number 4, served from 4/03/1809 to 4/03/1817, was from Virginia, and was in the Democratic-Republican party.
James Monroe was President number 5, served from 4/03/1817 to 4/03/1825, was from Virginia, and was in the Democratic-Republican party.
John Quincy Adams was President number 6, served from 4/03/1825 to 4/03/1829, was from Massachusetts, and was in the Democratic-Republican/National Republican party.
Andrew Jackson was President number 7, served from 4/03/1829 to 4/03/1837, was from Tennessee, and was in the Democratic party.
Martin Van Buren was President number 8, served from 4/03/1837 to 4/03/1841, was from New York, and was in the Democratic party.
William Henry Harrison was President number 9, served from 4/03/1841 to 4/04/1841, was from Ohio, and was in the Whig party.
John Tyler was President number 10, served from 4/04/1841 to 4/03/1845, was from Virginia, and was in the Whig party.
James K. Polk was President number 11, served from 4/03/1845 to 4/03/1849, was from Tennessee, and was in the Democratic party.
Zachary Taylor was President number 12, served from 4/03/1849 to 9/07/1850, was from Louisiana, and was in the Whig party.
Millard Fillmore was President number 13, served from 9/07/1850 to 4/03/1853, was from New York, and was in the Whig party.
Franklin Pierce was President number 14, served from 4/03/1853 to 4/03/1857, was from New Hampshire, and was in the Democratic party.
James Buchanan was President number 15, served from 4/03/1857 to 4/03/1861, was from Pennsylvania, and was in the Democratic party.
Abraham Lincoln was President number 16, served from 4/03/1861 to 15/04/1865, was from Illinois, and was in the Republican/National Union party.
Andrew Johnson was President number 17, served from 15/04/1865 to 4/03/1869, was from Tennessee, and was in the Democratic/National Union party.
Ulysses S. Grant was President number 18, served from 4/03/1869 to 4/03/1877, was from Ohio, and was in the Republican party.
Rutherford B. Hayes was President number 19, served from 4/03/1877 to 4/03/1881, was from Ohio, and was in the Republican party.
James A. Garfield was President number 20, served from 4/03/1881 to 19/09/1881, was from Ohio, and was in the Republican party.
Chester A. Arthur was President number 21, served from 19/09/1881 to 4/03/1885, was from New York, and was in the Republican party.
Grover Cleveland was President number 22, served from 4/03/1885 to 4/03/1889, was from New York, and was in the Democratic party.
Benjamin Harrison was President number 23, served from 4/03/1889 to 4/03/1893, was from Indiana, and was in the Republican party.
Grover Cleveland (2nd term) was President number 24, served from 4/03/1893 to 4/03/1897, was from New York, and was in the Democratic party.
William McKinley was President number 25, served from 4/03/1897 to 14/9/1901, was from Ohio, and was in the Republican party.
Theodore Roosevelt was President number 26, served from 14/9/1901 to 4/3/1909, was from New York, and was in the Republican party.
William Howard Taft was President number 27, served from 4/3/1909 to 4/03/1913, was from Ohio, and was in the Republican party.
Woodrow Wilson was President number 28, served from 4/03/1913 to 4/03/1921, was from New Jersey, and was in the Democratic party.
Warren G. Harding was President number 29, served from 4/03/1921 to 2/8/1923, was from Ohio, and was in the Republican party.
Calvin Coolidge was President number 30, served from 2/8/1923 to 4/03/1929, was from Massachusetts, and was in the Republican party.
Herbert Hoover was President number 31, served from 4/03/1929 to 4/03/1933, was from Iowa, and was in the Republican party.
Franklin D. Roosevelt was President number 32, served from 4/03/1933 to 12/4/1945, was from New York, and was in the Democratic party.
Harry S. Truman was President number 33, served from 12/4/1945 to 20/01/1953, was from Missouri, and was in the Democratic party.
Dwight D. Eisenhower was President number 34, served from 20/01/1953 to 20/01/1961, was from Texas, and was in the Republican party.
John F. Kennedy was President number 35, served from 20/01/1961 to 22/11/1963, was from Massachusetts, and was in the Democratic party.
Lyndon B. Johnson was President number 36, served from 22/11/1963 to 20/1/1969, was from Texas, and was in the Democratic party.
Richard Nixon was President number 37, served from 20/1/1969 to 9/8/1974, was from California, and was in the Republican party.
Gerald Ford was President number 38, served from 9/8/1974 to 20/01/1977, was from Michigan, and was in the Republican party.
Jimmy Carter was President number 39, served from 20/01/1977 to 20/01/1981, was from Georgia, and was in the Democratic party.
Ronald Reagan was President number 40, served from 20/01/1981 to 20/01/1989, was from California, and was in the Republican party.
George H. W. Bush was President number 41, served from 20/01/1989 to 20/01/1993, was from Texas, and was in the Republican party.
Bill Clinton was President number 42, served from 20/01/1993 to 20/01/2001, was from Arkansas, and was in the Democratic party.
George W. Bush was President number 43, served from 20/01/2001 to 20/01/2009, was from Texas, and was in the Republican party.
Barack Obama was President number 44, served from 20/01/2009 to Incumbent , was from Illinois, and was in the Democratic party.

2018 Lenovo Flex 5 15 2-IN-1 Laptop: 15.6″ IPS Touchscreen Full HD (1920×1080), Intel Quad Core i7-8550U, 512GB SSD, 16GB DDR4, NVIDIA 940MX, Backlit Keys, Windows 10 – Black (Certified Refurbished) – Price $778.00

Neat! You’re probably wondering why this doesn’t include our current president, right? This CSV file was created years ago before Donald Trump became our 45th President. If you want to extend this exercise, you could use the CSV write functionality in this CSV python module to add a president to this file.

csvFile  = open('presidents.csv', "a") writer = csv.writer(ofile)

for row in reader:
writer.writerow(["", "", "", "", "", "", "", "", ""])

csvFile.close()

Happy President’s Day!

Fire 7 Tablet with Alexa, 7″ Display, 8 GB, Black – with Special Offers – Price $39.99



Intel NUC 7 Mainstream Mini PC (NUC7i5BNHXF) – Core i5, 4GB RAM, 16GB Optane Memory, 1TB HDD – Price $529.00

Leave a comment