Image copyright EPA Image caption Mr Obama had supportive words for PM Alexis Tsipras on the Greek economy

Barack Obama has warned of a "rise in a crude sort of nationalism" following the Brexit and US presidential votes.

Speaking in Greece on his final foreign trip, he said: "We have to guard against... tribalism built around an 'us' or a 'them'."

He said the US was painfully aware of the danger of divisions "along lines of race or religion or ethnicity".

He also put the victory of Donald Trump down to "the view of the American people to just shake things up".

Mr Obama was speaking after talks with Greek PM Alexis Tsipras in the capital, Athens, as part of a final tour that also takes in Germany and Peru.

Nato reassurance

He said again that he had been surprised by Mr Trump's victory.

Mr Obama said part of the result was down to "natural desires for change if you've had an incumbent who's been there for eight years".

"At times of significant stress, people are going to be looking for something and they may opt for change even if they are not entirely confident what that change will bring."

He added: "The lesson I draw is that we have to deal with issues like inequality, economic dislocation, people's fears that their children will not do as well as they have."

Mr Obama said the UK's vote to leave the EU and the US vote showed that people generally were now "less certain of their national identities and place in the world" and that had produced populist movements both on the left and the right.

Image copyright Reuters Image caption Some US allies have expressed concern over Donald Trump's commitments going forward

He said a suspicion of globalisation and elites, wrapped up in issues of religious or ethnic or cultural identity was "a volatile mix".

He said: "Now we need to satisfy the people who are fearful, angry or concerned and that's going to be a pretty big test."

Mr Obama insisted his vision - where "the future will be decided by what we have in common, rather than what leads us into conflict" - was the right one.

He also said he was responsible for facilitating a good transition, presenting to Mr Trump his best ideas about how to move the country forward.

Mr Obama will be using the European leg of his trip to try to reassure leaders about future US commitment to its international alliances.

Analysis by James Reynolds, BBC News, Athens

For the first 30 minutes of their news conference, Mr Obama and Mr Tsipras managed to avoid talking about President-elect Trump. Given the drama of last week's election result, that surely sets some kind of record.

But the two leaders soon faced questions about Mr Trump's impact. Mr Obama chose his words carefully. He obeys two American traditions which come together on this trip: an outgoing president is normally gracious about his successor and a president on a foreign trip doesn't criticise his opponents back home.

Mr Tsipras - who himself came to power as the leader of a populist movement - said he knew very little about Mr Trump and hadn't read his book The Art of the Deal. The Greek PM then aligned himself with the position laid out by Mr Obama since the election result - that the United States is bound by a series of commitments to Europe and Nato that will endure.

But Greece will be worried. President Obama has advocated debt relief for this country. President-elect Trump may be unlikely to do the same.

Before leaving the US, Mr Obama said President-elect Trump had "expressed a great interest" in maintaining the US commitment to Nato, although during the campaign the Republican had said he might abandon a guarantee of protection for fellow Nato countries.

Article 5 of the Nato treaty commits allies to come to the aid of a member state under attack. Mr Trump's statements had alarmed the Baltic states, which fear Russian aggression.

Protest marches

Much of Mr Obama's news conference with Mr Tsipras concerned the Greek economy.

Greece's debt jargon explained

Greece's third debt bailout

Mr Tsipras praised Mr Obama for his support and for pursuing an economic policy in the US that focused on quantitative easing and job creation, rather than on the austerity measures favoured in Europe.

Image copyright AP Image caption Protests against Mr Obama's visit turned violent on Monday evening in Athens

Image copyright AFP Image caption Left-wing protesters threw petrol bombs at police officers

Greece agreed a third economic bailout package with international creditors in the middle of last year.

It was worth about €86bn ($92bn) but came with strict austerity measures. Greece says it needs a major debt restructuring programme to ease its economic malaise.

Mr Obama again signalled support for that, saying the Greek economy needed "space to return to growth" and that austerity could not be the only strategy.

Greek anarchist and left-wing groups later held protests in Athens "against the representative of imperialist powers".

Police used tear gas to disperse some 3,000 demonstrators who had tried to break through a cordon of police vehicles.

The protesters then pulled back to the Athens Polytechnic area and threw petrol bombs at officers.

At least four people were arrested.

Police have banned public gatherings in central Athens and near the city's international airport until after Mr Obama's departure on Wednesday.