Chapter 4: Expressions

Example upc.c, Page Number-57

In [1]:
def main():
    #input from user
    d=int(raw_input("Enter the first (single) digit: ")) 
    firstfive= raw_input("Enter first group of five digits")
    list1=list(firstfive) 
    i1=list1[0]
    i2=list1[1]
    i3=list1[2]
    i4=list1[3]
    i5=list1[4]
    secondfive= raw_input("Enter second group of five digits")
    list2=list(firstfive) 
    j1=list2[0]
    j2=list2[1]
    j3=list2[2]
    j4=list2[3]
    j5=list2[4]
    first_sum=d+int(i2)+int(i4)+int(j1)+int(j3)+int(j5)
    second_sum=int(i1)+int(i3)+int(i5)+int(j2)+int(j4)
    total=3*first_sum+second_sum
    s=9-((total - 1) % 10)
    print "Check digit: %d" % s #print result
    
if  __name__=='__main__':
    main()
Enter the first (single) digit: 0
Enter first group of five digits13800
Enter second group of five digits15713
Check digit: 2

Example on Page number-59

In [2]:
#variable declaration
i = 1
#k = l + (j=i)
j=i
k=1+j
print "%d %d %d" % (i,j,k)  #prints "1 1 2" 
1 1 2

Example on Page number-61

In [5]:
i=1 #variable declaration
i+=1
print "i is %d" % i #++i
print "i is %d" % i #i

i = 1
j=i+1
print "i is %d" % i #i++
print "i is %d" % j #i
i is 2
i is 2
i is 1
i is 2

Example on Page number-62

In [6]:
i=1 #variable declaration
i-=1
print "i is %d" % i #--i
print "i is %d" % i #i

i = 1
j=i-1
print "i is %d" % i #i--
print "i is %d" % j #i
i is 0
i is 0
i is 1
i is 0
In [ ]: