Tucson ( or occasionally locally ) is a city in and the county seat of Pima County, Arizona, United States, and home to the University of Arizona. The 2010 United States Census puts the city's population at 520,116, while the 2013 estimated population of the entire Tucson metropolitan statistical area (MSA) was 996,544. The Tucson MSA forms part of the larger Tucson-Nogales combined statistical area (CSA), with a total population of 980,263 as of the 2010 Census. Tucson is the second-largest populated city in Arizona behind Phoenix, which both anchor the Arizona Sun Corridor. The city is  located southeast of Phoenix and north of the U.S.-Mexico border. Tucson is the 33rd largest city and the 59th largest metropolitan area in the United States. Roughly 150 Tucson companies are involved in the design and manufacture of optics and optoelectronics systems, earning Tucson the nickname Optics Valley.
Major incorporated suburbs of Tucson include Oro Valley and Marana northwest of the city, Sahuarita south of the city, and South Tucson in an enclave south of downtown. Communities in the vicinity of Tucson (some within or overlapping the city limits) include Casas Adobes, Catalina Foothills, Flowing Wells, Tanque Verde, Tortolita, and Vail. Towns outside the Tucson metro area include Benson to the southeast, Catalina and Oracle to the north, and Green Valley to the south.
Tucson was probably first visited by Paleo-Indians, known to have been in southern Arizona by about 12,000 years ago. Recent archaeological excavations near the Santa Cruz River have located a village site dating from 2100 BC. The floodplain of the Santa Cruz River was extensively farmed during the Early Agricultural period, circa 1200 BC to AD 150. These people constructed irrigation canals and grew corn, beans, and other crops while gathering wild plants and hunting animals. The Early Ceramic period occupation of Tucson saw the first extensive use of pottery vessels for cooking and storage. The groups designated by archaeologists as the Hohokam lived in the area from AD 600 to 1450 and are known for their vast irrigation canal systems as well as their red-on-brown pottery.
Jesuit missionary Eusebio Francisco Kino visited the Santa Cruz River valley in 1692, and founded the Mission San Xavier del Bac in 1700 about upstream from the site of the settlement of Tucson. A separate Convento settlement was founded downstream along the then flowing Santa Cruz River, near the base of what is now "A" mountain. Hugo O'Conor, the founding father of the city of Tucson, Arizona authorized the construction of a military fort in that location, Presidio San Agustín del Tucsón, on August 20, 1775 (near the present downtown Pima County Courthouse). During the Spanish period of the presidio, attacks such as the Second Battle of Tucson were repeatedly mounted by Apaches. Eventually the town came to be called "Tucson" and became a part of Mexico after Mexico gained independence from Spain in 1821. Tucson was captured by the Mormon Battalion during the Mexican-American War, but later returned to Mexican control. Tucson was not included in the Mexican Cession – it was following the Gadsden Purchase in 1853 that Tucson became a part of the United States of America, although the American military did not formally take over control of the community until March 1856. In 1857 Tucson became a stage station on the San Antonio-San Diego Mail Line and in 1858 became 3rd division headquarters of the Butterfield Overland Mail until the line shut down in March 1861. The Overland Mail Corporation attempted to continue operations, however following the Bascom Affair, devastating Apache attacks on the stations and coaches ended operations in August 1861.
From August 1861 to mid-1862, Tucson was the western capital of the Confederate Arizona Territory, the eastern capital being Mesilla. In 1862 the California Column drove the Confederate forces out of Arizona. Tucson and all of what is now Arizona was part of New Mexico Territory until 1863, when it became part of the new Arizona Territory. From 1867 to 1877, Tucson was the capital of the Arizona Territory. Southern Arizona was legally bought from Mexico in the Gadsden Purchase on June 8, 1854. Tucson was incorporated in 1877, making it the oldest incorporated city in Arizona.
From 1877 to 1878, the Tucson area suffered from a rash of stagecoach robberies. Most notable, however, were the two robberies committed by masked road-agent William Whitney Brazelton. Brazelton held up two stages in the summer of 1878 near Point of Mountain Station approximately seventeen miles northwest of Tucson. John Clum, of Tombstone, Arizona fame was one of the passengers and Brazelton would eventually be tracked down and killed on Monday August 19, 1878, in a mesquite bosque along the Santa Cruz River south of Tucson by Pima County Sheriff Charles A. Shibell and his citizen's posse. Brazelton had been suspected of highway robbery not only in the Tucson area, but also in the Prescott region and Silver City, New Mexico area as well. Brazelton's crimes prompted John J. Valentine, Sr. of Wells, Fargo & Co. to send special agent and future Pima County sheriff Bob Paul to investigate. Fort Lowell, then east of Tucson, was established to help protect settlers from Apache attacks. In 1882, Frank Stilwell was implicated in the murder of Morgan Earp by Cowboy Pete Spence's wife, Marietta, at the coroner's inquest on Morgan Earp's shooting. The coroner's jury concluded that Spence, Stilwell, Frederick Bode, and Florentino "Indian Charlie" Cruz were the prime suspects in the assassination of Morgan Earp.
Deputy U.S. Marshal Wyatt Earp gathered a few friends he could trust and accompanied Virgil Earp and his family as they traveled to Benson for a train ride to California. They found Stilwell lying in wait for Virgil in the Tucson train station and killed him on the train tracks. After killing Stilwell, Wyatt deputized others and rode on a vendetta, killing three more Cowboys over the next few days before leaving the state.
By 1900, 7,531 people lived in the city. The population increased gradually to 13,913 in 1910. At about this time, the U.S. Veterans Administration had begun construction on the present Veterans Hospital. Many veterans who had been gassed in World War I and were in need of respiratory therapy began coming to Tucson after the war, due to the clean dry air. Over the following years the city continued to grow, with the population increasing to 20,292 in 1920 and 36,818 in 1940. In 2006 the population of Pima County, in which Tucson is located, passed one million while the City of Tucson's population was 535,000.
In 1912, when Arizona statehood became reality, the total number of different flags that had flown over Tucson now numbered five: American, Spanish, Mexican, Confederate, and the State of Arizona.