Python and Arduino serial

I'm trying to send data to Arduino UNO using Python

the Arduino code

const int controlPin = 9; 

void setup(){
  pinMode(controlPin, OUTPUT);
  digitalWrite(controlPin, HIGH);
  Serial.begin(9600); 
}

void loop(){
if (Serial.available() > 0) {
    int inByte = Serial.read();
    
    switch (inByte) {
    case '0':    
     digitalWrite(controlPin, LOW);
      break;
    case '1':    
     digitalWrite(controlPin, HIGH);
      break;
}
}
}

this code is working when i open the Arduino ide and open the serial monitor and send 0 it turn off when i send 1 it turn on

im trying to do the same thing without using the ide i want to use python

i tried the following

import serial # if you have not already done so
ser = serial.Serial('/dev/ttyACM0', 9600)
ser.write('x')

anything i type in the x place it turn it on even if i typed 0 or 1 they both turn it on

any ideas ?

opening the serial port in Python will reset the Arduino
try this

import serial

ser = serial.Serial("COM31", 115200)

def main():
    ser.close()
    ser.open()
    while(True):
	print ser.inWaiting()
        print ser.read(1)

if __name__ == '__main__':
    main()

The Python demo that I posted in this Thread may be of interest.

...R