Difference between revisions of "CSC231 Powers of 2 in Python"
(→Exercise: A Python Program that loops forever...) |
|||
(5 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
[[CSC231_Class_Page | Back to Weekly Schedule]] | [[CSC231_Class_Page | Back to Weekly Schedule]] | ||
---- | ---- | ||
− | + | --[[User:Thiebaut|D. Thiebaut]] 13:57, 8 October 2010 (UTC) | |
+ | ---- | ||
− | < | + | This Exercise is a companion to the [[CSC231 C++ Program and infinite loops |same exercise implemented in C++]]. |
+ | |||
+ | |||
+ | <br /> | ||
+ | =Exercise: Powers of 2 in Python...= | ||
+ | |||
+ | <br /> | ||
+ | * First do the exercise with C++ mentioned above. | ||
+ | * Then create the program below, and run it. It will wait you to press Enter at every loop, so that you can control the speed of the loop. | ||
+ | * What happens when you reach 2147483648, and press Enter a few times? How is that different from the C++ approach? Why? | ||
+ | <br /> | ||
+ | ==The Program== | ||
+ | <br /> | ||
+ | <source lang="python"> | ||
# mulby2.py | # mulby2.py | ||
# D. Thiebaut | # D. Thiebaut | ||
# csc231 | # csc231 | ||
# demonstrates how python changes type of | # demonstrates how python changes type of | ||
− | # variable as computation progresses. | + | # variable as computation progresses. Compare |
− | # how | + | # this to how a compiled language would react in this |
# situation (mulby2.cpp) | # situation (mulby2.cpp) | ||
x = 0x1 | x = 0x1 | ||
print "starting value of x = ", x | print "starting value of x = ", x | ||
− | curType = type( x ) | + | |
− | print "type( x ) = ", type( x ) | + | #curType = type( x ) |
+ | #print "type( x ) = ", type( x ) | ||
while ( True ): | while ( True ): | ||
Line 21: | Line 36: | ||
x = x * 2 | x = x * 2 | ||
print "x = ", x | print "x = ", x | ||
+ | raw_input( "> " ) | ||
continue | continue | ||
Line 30: | Line 46: | ||
break | break | ||
− | </ | + | </source> |
+ | |||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | [[Category:CSC231]][[Category:Python]] |
Latest revision as of 12:18, 13 November 2014
--D. Thiebaut 13:57, 8 October 2010 (UTC)
This Exercise is a companion to the same exercise implemented in C++.
Exercise: Powers of 2 in Python...
- First do the exercise with C++ mentioned above.
- Then create the program below, and run it. It will wait you to press Enter at every loop, so that you can control the speed of the loop.
- What happens when you reach 2147483648, and press Enter a few times? How is that different from the C++ approach? Why?
The Program
# mulby2.py
# D. Thiebaut
# csc231
# demonstrates how python changes type of
# variable as computation progresses. Compare
# this to how a compiled language would react in this
# situation (mulby2.cpp)
x = 0x1
print "starting value of x = ", x
#curType = type( x )
#print "type( x ) = ", type( x )
while ( True ):
lastx = x
x = x * 2
print "x = ", x
raw_input( "> " )
continue
if type( x )!= type( lastx ):
print "last x = ", lastx
print "type( last x ) = ", type( lastx )
print "x = ", x
print "type( x ) = ", type(x)
break