Lung
The lung is the essential respiration organ in air-breathing vertebrates, the most primitive being the lungfish. Its principal function is to transport oxygen from the atmosphere into the bloodstream, and to release carbon dioxide from the bloodstream into the atmosphere. This exchange of gases is accomplished in the mosaic of specialized cells that form millions of tiny, exceptionally thin-walled air sacs called alveoli. The lungs also have non respiratory functions.
Medical terms related to the lung often begin with pulmo-, from the Latin pulmonarius ("of the lungs"), or with pneumo- (from Greek  "lung")

# Respiratory function
Energy production from aerobic respiration requires oxygen and glucose and produces carbon dioxide as a waste product, creating a need for an efficient means of oxygen delivery to cells and excretion of carbon dioxide from cells. In small organisms, such as single-celled bacteria, this process of gas exchange can take place entirely by simple diffusion. In larger organisms, this is not possible; only a small proportion of cells are close enough to the surface for oxygen from the atmosphere to enter them through diffusion. Two major adaptations made it possible for organisms to attain great multicellularity: an efficient circulatory system that conveyed gases to and from the deepest tissues in the body, and a large, internalized respiratory system that centralized the task of obtaining oxygen from the atmosphere and bringing it into the body, whence it could rapidly be distributed to all the circulatory system.
In air-breathing vertebrates, respiration occurs in a series of steps. Air is brought into the animal via the airways  in reptiles, birds and mammals this often consists of the nose; the pharynx; the larynx; the trachea (also called the windpipe); the bronchi and bronchioles; and the terminal branches of the respiratory tree. The lungs of mammals are a rich lattice of alveoli, which provide an enormous surface area for gas exchange. A network of fine capillaries allows transport of blood over the surface of alveoli. Oxygen from the air inside the alveoli diffuses into the bloodstream, and carbon dioxide diffuses from the blood to the alveoli, both across thin alveolar membranes.
The drawing and expulsion of air is driven by muscular action; in early tetrapods, air was driven into the lungs by the pharyngeal muscles, whereas in reptiles, birds and mammals a more complicated musculoskeletal system is used. In the mammal, a large muscle, the diaphragm (in addition to the internal intercostal muscles), drive ventilation by periodically altering the intra-thoracic volume and pressure; by increasing volume and thus decreasing pressure, air flows into the airways down a pressure gradient, and by reducing volume and increasing pressure, the reverse occurs. During normal breathing, expiration is passive and no muscles are contracted (the diaphragm relaxes).
Another name for this inspiration and expulsion of air is ventilation.  Vital capacity is the maximum volume of air that a person can exhale after maximum inhalation.  A person's vital capacity can be measured by a spirometer (spirometry). In combination with other physiological measurements, the vital capacity can help make a diagnosis of underlying lung disease.

# Non respiratory functions
In addition to respiratory functions such as gas exchange and regulation of hydrogen ion concentration, the lungs also:
- influence the concentration of biologically active substances and drugs used in medicine in arterial blood
- filter out small blood clots formed in veins
- serve as a physical layer of soft, shock-absorbent protection for the heart, which the lungs flank and nearly enclose.
- filter out gas micro-bubbles occurring in the venous blood stream during SCUBA diving decompression.

# Mammalian lungs
The lungs of mammals have a spongy texture and are honeycombed with epithelium having a much larger surface area in total than the outer surface area of the lung itself. The lungs of humans are typical of this type of lung.
Breathing is largely driven by the muscular diaphragm at the bottom of the thorax. Contraction of the diaphragm pulls the bottom of the cavity in which the lung is enclosed downward. Air enters through the oral and nasal cavities; it flows through the larynx and into the trachea, which branches out into bronchi. Relaxation of the diaphragm has the opposite effect, passively recoiling during normal breathing.  During exercise, the diaphragm contracts, forcing the air out more quickly and forcefully. The rib cage itself is also able to expand and contract to some degree, through the action of other respiratory and accessory respiratory muscles. As a result, air is sucked into or expelled out of the lungs, always moving down its pressure gradient. This type of lung is known as a bellows lung as it resembles a blacksmith's bellows.

# Anatomy
In humans, the trachea divides into the two main bronchi that enter the roots of the lungs. The bronchi continue to divide within the lung, and after multiple divisions, give rise to bronchioles.  The bronchial tree continues branching until it reaches the level of terminal bronchioles, which lead to alveolar sacks.  Alveolar sacs are made up of clusters of alveoli, like individual grapes within a bunch.  The individual alveoli are tightly wrapped in blood vessels, and it is here that gas exchange actually occurs.  Deoxygenated blood from the heart is pumped through the pulmonary artery to the lungs, where oxygen diffuses into blood and is exchanged for carbon dioxide in the hemoglobin of the erythrocytes.  The oxygen-rich blood returns to the heart via the pulmonary veins to be pumped back into systemic circulation.
Human lungs are located in two cavities on either side of the heart.  Though similar in appearance, the two are not identical.  Both are separated into lobes, with three lobes on the right and two on the left.  The lobes are further divided into lobules, hexagonal divisions of the lungs that are the smallest subdivision visible to the naked eye.  The connective tissue that divides lobules is often blackened in smokers and city dwellers.  The medial border of the right lung is nearly vertical, while the left lung contains a cardiac notch.  The cardiac notch is a concave impression molded to accommodate the shape of the heart.  
Lungs are to a certain extent 'overbuilt' and have a tremendous reserve volume as compared to the oxygen exchange requirements when at rest.  This is the reason that individuals can smoke for years without having a noticeable decrease in lung function while still or moving slowly; in situations like these only a small portion of the lungs are actually perfused with blood for gas exchange.  As oxygen requirements increase due to exercise, a greater volume of the lungs is perfused, allowing the body to match its CO2/O2 exchange requirements.
The environment of the lung is very moist, which makes it hospitable for bacteria. Many respiratory illnesses are the result of bacterial or viral infection of the lungs.

# Avian lungs
Avian lungs do not have alveoli, as mammalian lungs do, but instead contain millions of tiny passages known as para-bronchi, connected at both ends by the dorsobronchi and ventrobronchi. Air flows through the honeycombed walls of the para-bronchi and into air capillaries, where oxygen and carbon dioxide are traded with cross-flowing blood capillaries by diffusion, a process of crosscurrent exchange.
Avian lungs contain two sets of air sacs, one towards the front, and a second towards the back.  Upon inspiration, air travels backwards into the rear (caudal) sac, and a small portion travels forward past the para-bronchi and oxygenating the blood into the cranial air sac.  On expiration, deoxygenated air held in the cranial air sack is exhaled, and the still-oxygenated air stored in the caudal sack moves over the parabronchi and is exhaled, with some remaining in the cranial sac.  The complex system of air sacs ensures that the airflow through the avian lung always travels in the same direction - posterior to anterior. This is in contrast to the mammalian system, in which the direction of airflow in the lung is tidal, reversing between inhalation and exhalation. By utilizing a unidirectional flow of air, avian lungs are able to extract a greater concentration of oxygen from inhaled air. Birds are thus equipped to fly at altitudes at which mammals would succumb to hypoxia, and this also allows them to sustain a higher metabolic rate than an equivalent weight mammal.  Because of the complexity of the system, misunderstanding is common and it is incorrectly believed that that it takes two breathing cycles for air to pass entirely through a bird's respiratory system.  A bird's lungs do not store air in either of the sacs between respiration cycles, air moves continuously from the posterior to anterior air sacs throughout respiration. This type of lung construction is called circulatory lungs as distinct from the bellows lung possessed by most other animals.

# Reptilian lungs
Reptilian lungs are typically ventilated by a combination of expansion and contraction of the ribs via axial muscles and buccal pumping. Crocodilians also rely on the hepatic piston method, in which the liver is pulled back by a muscle anchored to the pubic bone (part of the pelvis), which in turn pulls the bottom of the lungs backward, expanding them.

# Amphibian lungs
The lungs of most frogs and other amphibians are simple balloon-like structures, with gas exchange limited to the outer surface area of the lung. This is not a very efficient arrangement, but amphibians have low metabolic demands and also frequently supplement their oxygen supply by diffusion across the moist outer skin of their bodies. Unlike mammals, which use a breathing system driven by negative pressure, amphibians employ positive pressure. Note that the majority of salamander species are lung-less salamanders and conduct respiration through their skin and the tissues lining their mouth.

# Invertebrate lungs
Some invertebrates have "lungs" that serve a similar respiratory purpose, but are not evolutionarily related to, vertebrate lungs. Some arachnids have structures called "book lungs" used for atmospheric gas exchange. The Coconut crab uses structures called branchiostegal lungs to breathe air and indeed will drown in water, hence it breathes on land and holds its breath underwater. The Pulmonata are an order of snails and slugs that have developed "lungs".

# Origins
The lungs of today's terrestrial vertebrates and the gas bladders of today's fish have evolved from simple sacs (outpocketings) of the esophagus that allowed the organism to gulp air under oxygen-poor conditions. Thus the lungs of vertebrates are homologous to the gas bladders of fish (but not to their gills). This is reflected by the fact that the lungs of a fetus also develop from an outpocketing of the esophagus and in the case of gas bladders, this connection to the gut continues to exist as the pneumatic duct in more "primitive" teleosts, and is lost in the higher orders. (This is an instance of correlation between ontogeny and phylogeny.) There are currently no known animals which have both a gas bladder and lungs.