944,068 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 19193
  • Python RSS
Sep 19th, 2005
0

Comparing Python and C, Part 1, Integer Variables

Expand Post »
Intro

When you declare an integer variable in C, the variable is assigned a fixed memory location with enough space to hold the type integer. When you then initialize the variable with a value, the value is put into that space.

Take a look at the C code sample below:

  1. #include <stdio.h>
  2.  
  3. int main(void)
  4. {
  5. int a, b, c;
  6. // once you declare the variable, it is assigned a fixed memory location
  7. printf("a at %d b at %d c at %d\n", &a, &b, &c); // // a at 1245060 b at 1245056 c at 1245052
  8.  
  9. // initializes a, b and c to integer 3
  10. a = b = c = 3;
  11.  
  12. printf("a = %d b = %d c = %d\n", a, b, c); // a = 3 b = 3 c = 3
  13. printf("a at %d b at %d c at %d\n", &a, &b, &c); // a at 1245060 b at 1245056 c at 1245052
  14.  
  15. a = 4;
  16. printf("a = %d b = %d c = %d\n", a, b, c); // a = 4 b = 3 c = 3
  17. // 'a' has changed, but it's address has not
  18. printf("a at %d b at %d c at %d\n", &a, &b, &c); // a at 1245060 b at 1245056 c at 1245052
  19.  
  20. a++; // same as a = a + 1; or a += 1;
  21. printf("a = %d\n", a); // a = 5
  22.  
  23. // the time honored way to swap two variables using a temporary variable
  24. a = 77;
  25. b = 99;
  26. printf("before swap: a = %d b = %d\n", a, b);
  27. c = b; // here c is the temporary variable
  28. b = a;
  29. a = c;
  30. printf("after swap: a = %d b = %d\n", a, b);
  31.  
  32. // exceeding the integer limits ...
  33. c = 2147483647 + 0;
  34. printf("2147483647 + 0 = %d\n", c); // 2147483647
  35. c = 2147483647 + 1;
  36. // erroneous results
  37. printf("2147483647 + 1 = %d\n", c); // -2147483648 ?
  38. c = 2147483647 * 3;
  39. printf("2147483647 * 3 = %d\n", c); // 2147483645 ?
  40.  
  41. getchar(); // console wait
  42. return 0;
  43. }

A different approach

Python uses a different approach. When a variable is initialized with an integer value, that value becomes an integer object, and the variable points to it (references the object). Notice that the Python code and the C code have a lot of similarities, but the variable declarations are missing:

python Syntax (Toggle Plain Text)
  1. # in Python you don't have to declare the variables
  2. # Python gets the needed information during the initialization of the variables
  3. # a, b, c all point to integer object 3
  4. a = b = c = 3
  5.  
  6. print "a = %d b = %d c = %d" % (a, b, c) # a = 3 b = 3 c = 3
  7. print "a at %d b at %d c at %d" % (id(a), id(b), id(c)) # a at 9721064 b at 9721064 c at 9721064
  8.  
  9. a = 4
  10. print "a = %d b = %d c = %d" % (a, b, c) # a = 4 b = 3 c = 3
  11. # address pointed to by 'a' has changed, the object there is 4
  12. print "a at %d b at %d c at %d" % (id(a), id(b), id(c)) # a at 9721052 b at 9721064 c at 9721064
  13.  
  14. # a++ would increment the address and doesn't make much sense
  15. # this construct is not allowed in Python
  16. # however a = a + 1 or a += 1 increments the integer object
  17. a += 1
  18. print "a = %d" % a # a = 5
  19.  
  20. # swap two variables
  21. a = 77
  22. b = 99
  23. print "before swap: a = %d b = %d" % (a, b)
  24. # swap, no temporary variable is needed
  25. a, b = b, a
  26. print "after swap: a = %d b = %d" % (a, b)
  27.  
  28. # exceeding the 32bit integer object
  29. c = 2147483647 + 0
  30. print "2147483647 + 0 = %d type = %s" % (c, type(c)) # 2147483647 type = <type 'int'>
  31. c = 2147483647 + 1
  32. # no erroneous result
  33. # simply switches type 32bit int to memory limited long
  34. print "2147483647 + 1 = %d type = %s" % (c, type(c)) # 2147483648 type = <type 'long'>
  35. c = 2147483647 * 3
  36. print "2147483647 * 3 = %d type = %s" % (c, type(c)) # 6442450941 type = <type 'long'>
  37.  
  38. print
  39.  
  40. # a little extra information Python can easily give you ...
  41. print "show a dictionary representing the current global symbol table:"
  42. print globals()
  43.  
  44. raw_input() # console wait

Note: Your memory locations might differ.

Number length

As you look at the result of the C code, and figure out the difference between the memory locations if int a and int b, you come to the conclusion that they are 4 bytes apart. In fact, an integer in C takes up 4 bytes or 32 bits. The maximum value of a C integer can then be 2^32 = 4,294,967,296 - 1 (-1 since we start with zero for the unsigned integer). If we want to include negative values, then we have to declare a signed (signed is default) integer with values from -2,147,483,648 to + 2,147,483,647. If you limit yourself to a number 9 digits long, you are pretty safe! Sometimes you see long instead of int, they are the same thing. Long is there to distinguish from short int, limited to 16 bits.

Conclusion

Python removes this confusion, there is only the integer object. Does it have any limits? Very early versions of Python had a limit that was later removed. The limits now are set by the amount of memory you have in your computer. If you want to create an astronomical integer 5,000 digits long, go ahead. Typing it or reading it will be the only problem! How does Python do all of this? It automatically manages the integer object, which is initially set to 32 bits for speed. If it exceeds 32 bits, then Python increases its size as needed up to the RAM limit.
Last edited by vegaseat; Aug 12th, 2009 at 5:19 pm. Reason: Formatting
Similar Threads
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Python as a first language
Next Thread in Python Forum Timeline: Python and the JPEG Image File, Part 1, The Header





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC