def average(a,b): #function to calculate average
return (a+b)/2
nums = raw_input("Enter three numbers: ") #input numbers from user
list1 = map(float, nums.split())
x=list1[0]
y=list1[1]
z=list1[2]
print "Average of %.1f and %.1f: %.2f" % (x,y,average(x,y)) #print average using function
print "Average of %.1f and %.1f: %.2f" % (y,z,average(y,z))
print "Average of %.1f and %.1f: %.2f" % (x,z,average(x,z))
def print_count(n): #function definition
print "T minus %d and counting" % n
for i in range (10,0,-1):
print_count(i) #print using function
def print_pun(): #function definition
print "To C, or not to C: that is the question."
print_pun() #function call
def is_prime(n): #function to check for prime number
if (n<=1):
return False
divisor=2
while(divisor*divisor<=n):
if(n%divisor==0):
return False
return True
n=int(raw_input("Enter a number: ")) #input number
if(is_prime(n)): #check if prime using function
print "Prime" #print result of check
else:
print "Not prime"
def main():
nums = raw_input("Enter three numbers: ") #input numbers from user
list1 = map(float, nums.split())
x=list1[0]
y=list1[1]
z=list1[2]
#function usage before definition
print "Average of %.1f and %.1f: %.2f" % (x,y,average(x,y)) #print average using function
print "Average of %.1f and %.1f: %.2f" % (y,z,average(y,z))
print "Average of %.1f and %.1f: %.2f" % (x,z,average(x,z))
if __name__=="__main__":
main()
def average(a,b): #function to calculate average
return (a+b)/2
x=3.0
print "Square: %d" % square(x) #gives error since function prototype doesnt exist before
def square(n):
return n*n
"""
def main():
x=3.0
print "Square: %d" % square(x)
#Now wouldn't give an error since writing the line below loads up all functions before starting main()
if __name__=="__main__":
main()
def square(n):
return n*n
"""
def store_zeroes(a,n):
for i in range(0,n):
a[i]=0
b=[None]*200
store_zeroes(b,100) #First 100 elements of b are now 100.
print b
N=10 #number of elements to sort
def split(a,low,high): #function to split list while sorting
part_element=a[low]
while(1):
while(low<high and part_element<=a[high]):
high=high-1
if(low>=high):
break
a[low+1]=a[high]
while(low<high and a[low]<=part_element):
low=low+1
if(low>=high):
break
a[high-1]=a[low]
a[high]=part_element
return high
def quicksort(a,low,high): #recursive function for quicksort
if(low>=high):
return
middle=split(a,low,high)
quicksort(a,low,(middle-1))
quicksort(a,(middle+1),high)
nums = raw_input("Enter %d numbers to be sorted: "%N) #input numbers to sort
a = map(int, nums.split())
a.sort()
quicksort(a,0,(N-1)) #call quicksort function
print "In sorted order: ", #print sorted result
for i in a:
print i,