I was flying a Cessna 172 doing some pattern work. On one touch and go sequence I was told by the tower to make a 360 turn for spacing. As I completed the turn and ready to reentered the downwind leg, I lifted the left wing to check for traffic. To my surprise, I made eye contact with a passenger in a Piper Cherokee. Needless to say we both avoided the midair collision or I wouldn't be here today. Turned out the other pilot entered the traffic pattern without informing anyone.