This question is about Python not Arduino... Before asking here please ask GOOGLE
Anyway...
This is the answer Arduino and Python
Python code:
## import the serial library
import serial
## Boolean variable that will represent
## whether or not the arduino is connected
connected = False
## open the serial port that your ardiono
## is connected to.
ser = serial.Serial("COM11", 9600)
## loop until the arduino tells us it is ready
while not connected:
serin = ser.read()
connected = True
## Tell the arduino to blink!
ser.write("1")
## Wait until the arduino tells us it
## is finished blinking
while ser.read() == '1':
ser.read()
## close the port and end the program
ser.close()
Arduino code:
// Open a serial connection and flash LED when input is received
void setup(){
// Open serial connection.
Serial.begin(9600);
pinMode(13, OUTPUT);
Serial.write('1');
}
void loop(){
if(Serial.available() > 0){ // if data present, blink
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
Serial.write('0');
}
}