In 1819 Panama was granted independence from Spain, but the country was indecisive on their direction until newspapers encouraged independence thoughts and soon the country decided to forgo opportunities to join federations to their north and south and move forward as an independent entity, officially creating such in 1921.
Almost immediately after independence, Panama joined then rejoined countries to their south, including Venezuela and New Granada (modern day Colombia) as one country. This union eventually failed though and in 1841 Panama again joined Colombia to form New Granada.
In 1846 the United States joined forces with New Granada (which included Panama), giving the U.S. the rights to build a railroad across the isthmus, which was completed in 1855. This treaty also gave the U.S. the right to intercede militarily to restore order if needed. The U.S. enacted this clause multiple times as they regularly put down riots in the region.
The late 1800s consisted of arguments between Panama and Colombia as power shifted from regional to central as Panama was controlled locally by a small number of wealthy families of almost entirely Spanish descent. Also in the late 1800s the French were given the rights to build a canal across Panama. This attempt was a colossal disaster due to various diseases being spread among the workers and nearly impossible geological formations that slowed progress.
In 1902 the United States took on the task of building the canal, however their conditions of control over the canal fell upon deaf ears in Colombia, leading to hostilities between Panama and Colombia, eventually leading to Panamanian independence from Colombia-controlled New Granada in 1903 (although Colombia didn't officially recognize this until 1921). With independence, Panama gave the United States the rights to build and control the canal (on December 31, 1999 the canal was handed over to Panama).
The canal was built from 1904 to 1914 and during this time many improvements were brought to the region, including healthcare improvements, fighting yellow fever and malaria, plus improved roads, sewage projects, communication, and transportation, all of which were needed to complete the building of the canal.
Throughout the early 1900s Panama was dominated on every level by economic growth and prosperity as an enormous amount of trade moved through the canal and Panama. However, from a political level freedoms were restricted as a small group of the country's wealthy held power throughout this time. This monopoly on power was protested numerous times, most notably in the 1960s. These protests encouraged Panamanians to demand more control and profits from the canal and in 1977 Panama and the United States signed a treaty transferring the canal and all U.S. military bases in Panama over to the Panamanian government by the end of 1999. However, this treaty also gave the U.S. the right to militarily intervene in the country.
With political struggles in the 1980s though came worsening relations with the U.S. This was magnified by the U.S.'s war on drugs, which involved U.S. interjection into the region and even the conviction of some Panamanian citizens. These arguments escalated until 1989 when the U.S. entered Panama and ended violence, but threatened Panama's sovereignty in about a week.
Since the U.S. invasion, Panama has altered political leadership as direction and priorities were argued and regularly altered. However, relations with the U.S. have substantially improved as the two countries are now great allies and the handover of the Panama Canal to Panama was a success.