Chapter 7: Basic Types

Example sum2.c, Page 131

In [1]:
def main():
    sum=0
    print "This program sums a series of integers."
    n=int(raw_input("Enter integers (0 to terminate): ")) #input the integers to operate on
    while(n!=0):
        sum=sum+n #calculating sum till 0 encountered
        n=input()
    print "The sum is: %d" % sum #printing sum
if  __name__=='__main__':
    main()
This program sums a series of integers.
Enter integers (0 to terminate): 8
23
71
5
0
The sum is: 107

Example length.c, Page 142

In [5]:
def main():
    str=raw_input("Enter a message: ") #input string
    length=len(str) #calculate length
    print "Your message was %d character(s) long" % length #display length
if  __name__=='__main__':
    main()
Enter a message: Brevity is the soul of wit.
Your message was 27 character(s) long
In [ ]: