import string
import sys
print "Enter a character: ",
ch = raw_input()
if ch.isalnum():
if ch.isdigit():
print "You entered the digit ", ch
elif ch.islower():
print "You entered a lower case ", ch.upper()
elif ch.isupper():
print "You entered a upper case ", ch.lower()
elif ch in string.punctuation:
print "You entered a punctuation character ", ch
else:
print "You entered ", ch, "but I don't know what it is!"
print "Enter a string of less than 100 characters: ",
text = raw_input()
print "Enter the string sought: ",
sought = raw_input()
sought = sought.lower()
count = 0
sought_len = len(sought)
for i in range(len(text)):
substr = text[i:sought_len+i]
if substr == sought:
count += 1
print "First string entered: ", text
print "Second string entered: ", sought
print "The second string was found in first %d times" %count
cx = 1.0 + 3.0j
cy = 1.0 - 4.0j
sum = cx + cy
diff = cx - cy
product = cx * cy
quotient = cy/cy
conjugate = cx.conjugate()
print "Complex numbers are supported"
print "Working with complex numbers: "
print "Starting values: cx = %.2f%+.2fi cy = %.2f%+.2fi" %(cx.real, cx.imag, cy.real, cy.imag)
print "The sum cx + cy = %.2f%+.2fi" %(sum.real, sum.imag)
print "The difference cx - cy = %.2f%+.2fi" %(diff.real, diff.imag)
print "The product cx * cy = %.2f%+.2fi" %(product.real, product.imag)
print "The quotient cx / cy = %.2f%+.2fi" %(quotient.real, quotient.imag)
print "The conjugate of cx = %.2f%+.2fi" %(conjugate.real, conjugate.imag)
from threading import Thread
def get_data(pdata):
pd = pdata
print "The get_data thread received: data.a=%d and data.b=%d" %(pd[0], pd[1])
pd[0] *= 3
print "The get_data thread makes it: data.a=%d and data.b=%d " %(pd[0], pd[1])
def process_data(pdata):
pd = pdata
print "The process_data thread received: data.a=%d and data.b=%d "%(pd[0], pd[1])
mydata = [123, 345]
print "Before starting the get_data thread: mydata.a=%d and mydata. b=%d" %(mydata[0], mydata[1])
print "get_data thread started."
t1 = Thread(target=get_data, args=(mydata,))
t1.start()
Thread.join(t1)
print "process_data thread started."
t2 = Thread(target=process_data, args=(mydata,))
t2.start()
Thread.join(t2)
print "After both threads finish executing: mydata.a=%d and mydata. b=%d" %(mydata[0], mydata[1])
from threading import Thread
import math
import time
thread_count = 5
thread_id = []
task = 0
def execute_task():
global task
task += 1
print "Task %d started." %task
time.sleep(1)
for i in range(1000000):
x = math.sqrt(3.1415926)
print "Task %d finished" %task
for i in range(thread_count):
thread_id.append(Thread(target=execute_task))
thread_id[i].start()
for j in range(thread_count):
Thread.join(thread_id[j])