There is a huge connection between the mind and the body.
Some people may have experienced this while doing Yoga, or perhaps just dry workouts in general. However in Swimming it is almost immediately apparent, at least when you are looking. The more upset your mind gets, then the more upset your body gets.
Why + Philosophy:
This is one of the more philosophical heavy lessons, as it really takes that "why" to another level. You have to realize why your body is responding in a certain way, then be proactive rather than reactive.
In order to do that physical action, the first step is to take the mental action.
Generally most people have to wrap their head around why it is happening. What they did in the swimming, being under the water, or something of the sort.
How + Physics:
Now most of the time you can just blow out more air, and that can temporarily solve the panic. However 99% of lessons in swimming, in my experience, have been to overcome panic.
Panic isn't just the "oh my I'm gonna die" type of thing, but sometimes as simple as "I need to breathe... NOW". Rather than LATER.
We have to physically control our body, and delay the "gratification" of breathing sooner. It isn't hard to do physically, but it is mentally.
What + Psychology:
The brain is a lot more primitive than you might think. We have all this cognition, but we are wired from eons ago. Meaning our sole goal in parts of the brain is survival.
We have to keep going, and sometimes it means fighting off a lion. Or swimming/walking for longer than we think we can in order to escape.
However when learning to swim it isn't that serious, we aren't really that much in danger. Oftentimes there are lifeguards, or at least a parent around, or your instructor etc. Never try to do this by yourself!
The point is that there is a safety net right next to us, but our brains don't process that. We think if we are taking too much water in, then we stop what we are doing. Yet the action we were doing would get us to the wall, to safety.
Conclusion
Your brain has control over the body, but the body has control over the brain as well. If your heartrate gets too out of control, your lungs are quite strong enough, or maybe if you get a bit disoriented from being under water.
There can be a lot of factors that play into the panic starting.
The panic could be as simple as 1%, all the way to full on freaking out. We want to avoid that at all costs, even the 1%, as it slips down the slope. Stay calm, stay focused, and remember what you are doing IN THE MOMENT.
This is advice for people to level up their swimming, or perhaps get started in the first place. While you swim you should make sure you are doing so in a public facility with a lifeguard on duty for safety.