 -- President Barack Obama today said his empathy for the grievances expressed by black people about how they are treated by police and fellow citizens is rooted in his experiences as an African-American man.

At an ABC town hall on race relations in America titled "The President and the People: A National Conversation," a person in the audience — in light of the national debate over whether law enforcement officers unfairly target black people — asked Obama if he was ever pulled over by police.

Obama said he has been pulled over, though he was careful not to say it was due to racism.

"I will say that the overwhelming majority of the time, I deserved to be pulled over 'cause I was going too fast, and the police officers were courteous, and I, and I got a ticket, which I deserved," he said. "There have been a couple of times in my life where that was not the case."

But the president made clear that at times in his life he did feel treated differently simply because he is black.

"What is true for me is true for a lot of African-American men — is there's a greater presumption of dangerousness that arises from the social and cultural perceptions that have been fed to folks for a long time," he said. "And I think it is not as bad as it used to be, but it's still there, and there's a history to that."

He recalled being a boy growing up in Hawaii with his white grandparents and encountering a neighbor at the elevator.

"I still remember when I was 10 years old walking into the elevator, and there was a woman who I thought knew me, and as soon as I walked on — and she lived on my grandparent's floor — when I walked on, she got off," Obama said. "And I was puzzled. I said, 'Do you want to come up?' And she said no. And then I went up, and then I saw the elevator go back down, and I just kind of peeked out the peephole, and I could see she came right back up but was just worried about riding the elevator with me."

He said sometimes he encountered that sense of dangerousness just going about daily life.

"And then over time you start learning as you're crossing the street, suddenly the locks start going on doors," he said.

The comments from Obama, the nation's first black president, come as the country is reeling from a string of shootings that have raised racial tensions.

Last week two black men were shot dead by white police officers — one in Baton Rouge, Louisiana, and another outside St. Paul, Minnesota — in cases that sparked outrage over concerns that the officers greatly overreacted to the threats posed in each case.

Then during a protest in Dallas, five police officers were shot and killed by a man who authorities say was upset about the recent police shootings.

At the town hall, Obama appealed for a greater sense of understanding and for people to not make assumptions about one another based on race or ethnicity.

"So the question, I think, for all of us is, 'How do we try to lessen those barriers and those misunderstandings?" And some of it involves us being very conscious of our assumptions, because white folks and Latino folks also carry some assumptions," he said. "You may see a police officer who's doing everything right, and you automatically assume the worst rather than the best in him. And we have to guard against that as well. And that has to be reflected in how we talk about these issues going forward."