Thursday, February 24, 2011

Feb 24

When working with the digit program. I began to think, what if I add more digit space; would it still give me the number of digits in the number. Testing the program:
def num_digits(n):
        count = 0
        while n:
                 count = count + 1
                 n = n /  10
        return count
I then plugged in 9,999,999,999,999,999,999,999. After i enter this number into the program, it correctly came out to have 22 digits.
With working with abbreviation of the numbers i found it really easy to understand that += mean + what ever number. The abbreviation also works with all the other symbols in mathematical expressions such as * / and -. Lets me show you how all the expressions will look. Starting with that count = 0
count = 0
count += 1
count = 1

count = 0
count -= 1
count = -1

count = 0
count += 2
count = 2
count  /= 2
count = 1

count = 0
count *= 1
count = 0

count = 3
count %= 2
count = 1
These are all the expressions you can use.

2 comments:

  1. This was also being worked on Feb. 23

    ReplyDelete
  2. Excellent post! Yes, the augmented assignment statements (that's what they are called -- see http://docs.python.org/release/2.4.3/ref/augassign.html) are great. They are clean and readable.

    I like the way you demonstrated your understanding of how they work with a running example.

    ReplyDelete