Difference between revisions of "CSC111 Block and Loop Exercises"
(→Python Version 3) |
|||
(3 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | Predict the output of the following python statements | ||
+ | =Python Version 3= | ||
+ | |||
+ | <source lang="python"> | ||
+ | |||
+ | # ----------------------------------------- | ||
+ | # program1.py | ||
+ | |||
+ | def main(): | ||
+ | a = 3 | ||
+ | b = 5 | ||
+ | print( "a+b=", a+b ) | ||
+ | |||
+ | main() | ||
+ | |||
+ | # ----------------------------------------- | ||
+ | # program2.py | ||
+ | |||
+ | print( "hello!" ) | ||
+ | def main(): | ||
+ | a, b = 3, 5 | ||
+ | print( "a+b=", a+b ) | ||
+ | |||
+ | main() | ||
+ | |||
+ | # ----------------------------------------- | ||
+ | # program3.py | ||
+ | |||
+ | print( "hello!" ) | ||
+ | def main(): | ||
+ | a, b = 3, 5 | ||
+ | print( "a+b=", a+b ) | ||
+ | print( "bye!" | ||
+ | |||
+ | main() | ||
+ | |||
+ | # ----------------------------------------- | ||
+ | # program4.py | ||
+ | |||
+ | print( "hello!" ) | ||
+ | def main(): | ||
+ | a, b = 3, 5 | ||
+ | print "a+b=", a+b | ||
+ | print "bye!" | ||
+ | |||
+ | # ----------------------------------------- | ||
+ | # program5.py | ||
+ | |||
+ | a = 7 | ||
+ | |||
+ | def main(): | ||
+ | a, b = 3, 5 | ||
+ | print( a , '+', b, '=', a+b ) | ||
+ | |||
+ | print( a ) | ||
+ | |||
+ | main() | ||
+ | |||
+ | |||
+ | # ----------------------------------------- | ||
+ | # program6.py | ||
+ | |||
+ | a = 7 | ||
+ | |||
+ | def main(): | ||
+ | a, b = 3, 5 | ||
+ | print( a , '+', b, '=', a+b ) | ||
+ | |||
+ | print( a ) | ||
+ | |||
+ | main() | ||
+ | |||
+ | print( a ) | ||
+ | |||
+ | |||
+ | # ----------------------------------------- | ||
+ | # program7.py | ||
+ | |||
+ | def main(): | ||
+ | for name in [ 'Alex', 'Kate', 'John', 'Monique' ]: | ||
+ | print( "hello", end=", " ) | ||
+ | print( name ) | ||
+ | |||
+ | main() | ||
+ | |||
+ | |||
+ | # ----------------------------------------- | ||
+ | # program8.py | ||
+ | |||
+ | def main(): | ||
+ | for name in [ 'cat', 'dog', 1, 3.14159 ]: | ||
+ | print( "item is", name ) | ||
+ | |||
+ | main() | ||
+ | |||
+ | |||
+ | # ----------------------------------------- | ||
+ | # program9.py | ||
+ | |||
+ | def main(): | ||
+ | sum = 0 | ||
+ | for i in [ 2, 10, 3, 4 ]: | ||
+ | sum = sum + i | ||
+ | print( "sum = ", sum ) | ||
+ | |||
+ | main() | ||
+ | |||
+ | |||
+ | </source> | ||
+ | |||
+ | =Python Version 2= | ||
+ | |||
<source lang="python"> | <source lang="python"> | ||
− | |||
# ----------------------------------------- | # ----------------------------------------- |
Latest revision as of 08:43, 20 September 2011
Predict the output of the following python statements
Python Version 3
# -----------------------------------------
# program1.py
def main():
a = 3
b = 5
print( "a+b=", a+b )
main()
# -----------------------------------------
# program2.py
print( "hello!" )
def main():
a, b = 3, 5
print( "a+b=", a+b )
main()
# -----------------------------------------
# program3.py
print( "hello!" )
def main():
a, b = 3, 5
print( "a+b=", a+b )
print( "bye!"
main()
# -----------------------------------------
# program4.py
print( "hello!" )
def main():
a, b = 3, 5
print "a+b=", a+b
print "bye!"
# -----------------------------------------
# program5.py
a = 7
def main():
a, b = 3, 5
print( a , '+', b, '=', a+b )
print( a )
main()
# -----------------------------------------
# program6.py
a = 7
def main():
a, b = 3, 5
print( a , '+', b, '=', a+b )
print( a )
main()
print( a )
# -----------------------------------------
# program7.py
def main():
for name in [ 'Alex', 'Kate', 'John', 'Monique' ]:
print( "hello", end=", " )
print( name )
main()
# -----------------------------------------
# program8.py
def main():
for name in [ 'cat', 'dog', 1, 3.14159 ]:
print( "item is", name )
main()
# -----------------------------------------
# program9.py
def main():
sum = 0
for i in [ 2, 10, 3, 4 ]:
sum = sum + i
print( "sum = ", sum )
main()
Python Version 2
# -----------------------------------------
# program1.py
def main():
a = 3
b = 5
print "a+b=", a+b
main()
# -----------------------------------------
# program2.py
print "hello!"
def main():
a, b = 3, 5
print "a+b=", a+b
main()
# -----------------------------------------
# program3.py
print "hello!"
def main():
a, b = 3, 5
print "a+b=", a+b
print "bye!"
main()
# -----------------------------------------
# program4.py
print "hello!"
def main():
a, b = 3, 5
print "a+b=", a+b
print "bye!"
# -----------------------------------------
# program5.py
a = 7
def main():
a, b = 3, 5
print "a+b=", a+b
print a
main()
# -----------------------------------------
# program6.py
def main():
for name in [ 'Alex', 'Kate', 'John', 'Monique' ]:
print "hello",
print name
main()
# -----------------------------------------
# program7.py
def main():
for name in [ 'cat', 'dog', 1, 3.14159 ]:
print "item is", name
main()
# Questions:
# =========
# - What happens if we add lists together?
#
# - What about the range() function?
#
# - Where can I find more information about range()?