President Barack Obama has called on Americans to defend their democracy in his farewell speech in Chicago.
"By almost every measure, America is a better, stronger place" than it was eight years ago when he took office, he told thousands of supporters.
But he warned "democracy is threatened whenever we take it for granted".
He implored Americans of all backgrounds to consider things from each other's point of view, saying "we have to pay attention and listen".
The country's first black president, now 55, was first elected in 2008 on a message of hope and change.
His successor, Donald Trump, has vowed to undo some of Mr Obama's signature policies. He will be sworn into office on 20 January.
Raucous chants of "four more years" from the crowd were brushed aside by the president. "I can't do that," he said with a smile. US presidents are limited to two terms by the constitution.
Mr Obama said "bubbles" on social media and elsewhere in society threatened to undermine democracyExit playerMedia captionMr Obama said "bubbles" on social media and elsewhere in society threatened to undermine democracy
"No, no, no, no no," he said, when the crowd booed the prospect of Mr Trump replacing him.
Striking an upbeat tone, Mr Obama said that the peaceful transfer of power between presidents was a "hallmark" of American democracy.
But, he outlined three threats to American democracy - economic inequality, racial divisions and the retreat of different segments of society into "bubbles", where opinions are not based on "some common baseline of facts".
"If you're tired of arguing with strangers on the internet, try to talk with one in real life," he said to laughter and applause.
In his closing remarks he said he had one final request for Americans as president: "I am asking you to believe. Not in my ability to bring about change - but in yours."
Media captionFive times Barack Obama went viralImage copyrightREUTERSImage captionPresident Obama said he had "gained a brother" in Vice-President Joe Biden
Returning to Chicago, where he first declared victory in 2008, Mr Obama delivered a mostly positive message to Americans after a divisive election campaign which saw Mr Trump defeat Democrat Hillary Clinton.
Mr Obama said that young Americans - including those who worked on his campaigns, and who believe "in a fair, just, inclusive America" - left him feeling "even more optimistic about this country than I was when we started".
In choosing Chicago, Mr Obama had earlier said he wanted to return to "where it all started" for him and First Lady Michelle Obama, instead of delivering the speech from the White House.
'An elegantly worded warning', by Nick Bryant, BBC New York correspondent
"Yes we can. Yes we did." A bookend to the speech he delivered on the night he won the presidency in Grant Park, Chicago, Barack Obama's farewell address contained much of the same hope and optimism that were hallmarks of his candidacy while at the same time outlining his legacy.
But it was also an elegantly worded warning: about the country's broken politics, its naked partisanship, its stark economic inequalities, its social and racial dislocation.
Though he did not mention the President-elect by name, much of it obviously read like a rebuttal to Donald Trump's campaign.
Obama called for respect for the science of climate change and drew one of his biggest applause lines when he noted: "I reject discrimination against Muslim Americans." The line "democracy can buckle when we give in to fear," could easily be interpreted as being aimed at Donald Trump.
This speech highlighted a stark difference between the two men: Obama's preference for delivering reflective and historically literate orations, and Trump's penchant for expressing himself in Tweets.
Mr Obama said that it was in Chicago as a young man, "still trying to figure out who I was, still searching for purpose in my life", that he "witnessed the power of faith and dignity of working people in the face of struggle and loss".
"This is where I learned that change only happens when ordinary people get involved and they get engaged and they come together to demand it," he said. "After eight years as your president I still believe that."
Some 18,000 people attended the farewell address at McCormick Place, the largest convention centre in North America and the venue for Mr Obama's speech after he defeated Mitt Romney in the 2012 election.
The tickets were given out free, but were selling online for more than $1,000 (£820) each hours ahead of the speech.
As he leaves the Oval Office, President Obama is viewed favourably by 57% of Americans, according to an Associated Press-NORC Center for Public Affairs Research poll, a similar level to Bill Clinton when he left office.